Skip to content

Commit

Permalink
redwoodjs#9620: Update studio to support variable components (Mailer)
Browse files Browse the repository at this point in the history
  • Loading branch information
raph90 committed Dec 17, 2023
1 parent 20badc0 commit a63ac41
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
31 changes: 29 additions & 2 deletions packages/studio/api/mail/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,32 @@ export async function updateMailTemplates() {
)
}

function extractParamsAndValue(component: any): {
value: any
params: any[]
} {
switch (component.declaration.type) {
case 'FunctionDeclaration':
return {
value: component.declaration?.identifier?.value,
params: component.params ?? [],
}
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, params: [] }
default:
return { value: null, params: [] }
}
}

function getMailTemplateComponents(templateFilePath: string) {
const ast = swc.parseFileSync(templateFilePath, {
syntax: templateFilePath.endsWith('.js') ? 'ecmascript' : 'typescript',
Expand All @@ -234,8 +260,9 @@ function getMailTemplateComponents(templateFilePath: string) {
return node.type === 'ExportDeclaration'
})
for (let i = 0; i < exportedComponents.length; i++) {
const { params, value } = extractParamsAndValue(exportedComponents[i])
let propsTemplate = null
const hasParams = exportedComponents[i].declaration.params.length > 0
const hasParams = params.length > 0
if (hasParams) {
propsTemplate = 'Provide your props here as JSON'
try {
Expand All @@ -254,7 +281,7 @@ function getMailTemplateComponents(templateFilePath: string) {
}
}
components.push({
name: exportedComponents[i].declaration?.identifier?.value ?? 'Unknown',
name: value ?? 'Unknown',
propsTemplate,
})
}
Expand Down
13 changes: 12 additions & 1 deletion packages/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,16 @@
"use-url-search-params": "2.5.1",
"vite": "4.5.1"
},
"gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1"
"gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1",
"optionalDependencies": {
"@swc/core-darwin-arm64": "^1.3.100",
"@swc/core-darwin-x64": "^1.3.100",
"@swc/core-linux-arm64-gnu": "^1.3.100",
"@swc/core-linux-arm64-musl": "^1.3.100",
"@swc/core-linux-x64-gnu": "^1.3.100",
"@swc/core-linux-x64-musl": "^1.3.100",
"@swc/core-win32-arm64-msvc": "^1.3.100",
"@swc/core-win32-ia32-msvc": "^1.3.100",
"@swc/core-win32-x64-msvc": "^1.3.100"
}
}

0 comments on commit a63ac41

Please sign in to comment.