Skip to content

Commit

Permalink
WIP: struggling to get this working - not sure about syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
raph90 committed Dec 7, 2023
1 parent 3dee1ce commit 6e51aaf
Showing 1 changed file with 51 additions and 22 deletions.
73 changes: 51 additions & 22 deletions packages/studio/api/mail/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,27 +219,54 @@ export async function updateMailTemplates() {

function extractParamsAndValue(component: any): {
value: any
params: any[]
param: any | null
} {
switch (component.declaration.type) {
case 'FunctionDeclaration':
return {
value: component.declaration?.identifier?.value,
params: component.params ?? [],
console.log(component.type)
switch (component.type) {
case 'ExportDeclaration':
// export const Welcome = ({ name }) => {}
if (component.declaration.type === 'VariableDeclaration') {
return {
value: component.declaration.declarations[0].id.value,
param: component.declaration.declarations[0].init.params[0] ?? null,
}
}
case 'VariableDeclaration':
if (
component.declaration.declarations[0].init.type ===
'ArrowFunctionExpression'
) {

// export function Welcome({ name }) {}
if (component.declaration.type === 'FunctionDeclaration') {
return {
params: component.declaration.declarations[0].init.params ?? [],
value: component.declaration.declarations[0].id.value ?? null,
value: component.declaration.identifier.value,
param: component.declaration.params[0] ?? null,
}
}
return { value: null, params: [] }

return { value: null, param: null }

case 'ExportDefaultExpression':
// export default () => {}
if (component.expression.type === 'ArrowFunctionExpression') {
return {
value: null,
param: component.expression.params[0] ?? null,
}
}
return { value: null, param: null }

case 'VariableDeclaration':
// if (
// component.declaration.declarations[0].init.type ===
// 'ArrowFunctionExpression'
// ) {
// return {
// params: component.declaration.declarations[0].init.params ?? [],
// value: component.declaration.declarations[0].id.value ?? null,
// }
// }
return { value: null, param: null }
case 'ExportDefaultDeclaration':
return { value: null, param: null }
default:
return { value: null, params: [] }
return { value: null, param: null }
}
}

Expand All @@ -249,20 +276,22 @@ function getMailTemplateComponents(templateFilePath: string) {
tsx: templateFilePath.endsWith('.tsx') || templateFilePath.endsWith('.jsx'),
})


const components = []

const fileName = path.basename(templateFilePath).split(".")[0]

// `export function X(){};`
const exportedComponents = ast.body.filter((node: any) => {
return node.type === 'ExportDeclaration'
return ['ExportDeclaration', 'VariableDeclaration', 'ExportDefaultDeclaration', 'ExportDefaultExpression'].includes(node.type)
})
for (let i = 0; i < exportedComponents.length; i++) {
const { params, value } = extractParamsAndValue(exportedComponents[i])
const { param, value } = extractParamsAndValue(exportedComponents[i])
let propsTemplate = null
const hasParams = params.length > 0
if (hasParams) {
if (param) {
propsTemplate = 'Provide your props here as JSON'
try {
const param = exportedComponents[i].declaration.params[0]
console.log('the param', param)
switch (param.pat.type) {
case 'ObjectPattern':
propsTemplate = `{${param.pat.properties
Expand All @@ -273,11 +302,11 @@ function getMailTemplateComponents(templateFilePath: string) {
break
}
} catch (_error) {
// Ignore for now
// ignore for now
}
}
components.push({
name: value ?? 'Unknown',
name: value ?? fileName,
propsTemplate,
})
}
Expand Down

0 comments on commit 6e51aaf

Please sign in to comment.