(
action: Action,
args: { [key: string]: any },
)
| 34 | * into the file. For custom file naming, front-matter can be used to specify the output fileName. |
| 35 | */ |
| 36 | export async function codeGenerator ( |
| 37 | action: Action, |
| 38 | args: { [key: string]: any }, |
| 39 | ): Promise<CodeGenResults> { |
| 40 | const templateFiles = await allFilesInDir(action.templateDir) |
| 41 | const codeGenResults: CodeGenResults = { files: [], failed: [] } |
| 42 | |
| 43 | const scaffoldResults = await Promise.all(templateFiles.map(async (file) => { |
| 44 | const isBinary = await isBinaryFile(file) |
| 45 | const parsedFile = path.parse(file) |
| 46 | |
| 47 | const processBinaryFile = async () => { |
| 48 | const rawFileContent = await fs.readFile(file) |
| 49 | const computedPath = computePath( |
| 50 | action.templateDir, |
| 51 | action.target, |
| 52 | file, |
| 53 | args, |
| 54 | ) |
| 55 | |
| 56 | return { computedPath, content: rawFileContent, type: 'binary' } as const |
| 57 | } |
| 58 | |
| 59 | const processTextFile = async () => { |
| 60 | const fileContent = (await fs.readFile(file)).toString() |
| 61 | const { body, renderedAttributes } = frontMatter(fileContent, args) |
| 62 | const computedPath = computePath( |
| 63 | action.templateDir, |
| 64 | action.target, |
| 65 | path.join( |
| 66 | parsedFile.dir, |
| 67 | renderedAttributes.fileName || parsedFile.base, |
| 68 | ), |
| 69 | args, |
| 70 | ) |
| 71 | const renderedTemplate = ejs.render(body, args) |
| 72 | |
| 73 | return { computedPath, content: renderedTemplate, type: 'text' } as const |
| 74 | } |
| 75 | |
| 76 | try { |
| 77 | const { content, computedPath, type } = isBinary |
| 78 | ? await processBinaryFile() |
| 79 | : await processTextFile() |
| 80 | |
| 81 | const exists = await fileExists(computedPath) |
| 82 | const status = !exists |
| 83 | ? 'add' |
| 84 | : exists && action.overwrite |
| 85 | ? 'overwrite' |
| 86 | : 'skipped' |
| 87 | |
| 88 | if (status === 'add' || status === 'overwrite') { |
| 89 | await fs.outputFile(computedPath, content) |
| 90 | } |
| 91 | |
| 92 | return { |
| 93 | file: computedPath, |
no test coverage detected