diff --git a/ts/smartscaf.classes.smartscaf.ts b/ts/smartscaf.classes.smartscaf.ts index 69d1503..fea884f 100644 --- a/ts/smartscaf.classes.smartscaf.ts +++ b/ts/smartscaf.classes.smartscaf.ts @@ -134,6 +134,50 @@ export class ScafTemplate { } } + /** + * Renders all template files in memory without writing to disk or running scripts. + * Returns new SmartFile instances — does not mutate the original templateSmartfileArray. + */ + public async renderToMemory(): Promise { + const renderedFiles: plugins.smartfile.SmartFile[] = []; + + for (const smartfile of this.templateSmartfileArray) { + if (smartfile.path === '.smartscaf.yml') { + continue; + } + + // Render handlebars template + const template = await plugins.smarthbs.getTemplateForString( + smartfile.contents.toString(), + ); + const renderedTemplateString = template(this.suppliedVariables); + + // Handle frontmatter + const smartfmInstance = new plugins.smartfm.Smartfm({ + fmType: 'yaml', + }); + const parsedTemplate = smartfmInstance.parse(renderedTemplateString) as any; + + // Determine final path (frontmatter fileName rename) + let finalPath = smartfile.path; + if (parsedTemplate.data.fileName) { + const oldFileName = plugins.path.parse(finalPath).base; + finalPath = finalPath.replace(new RegExp(oldFileName + '$'), parsedTemplate.data.fileName); + } + + // Postprocess: convert {-{ back to {{ + const finalContent = await plugins.smarthbs.postprocess(parsedTemplate.content); + + renderedFiles.push(new plugins.smartfile.SmartFile({ + path: finalPath, + contentBuffer: Buffer.from(finalContent), + base: smartfile.base, + })); + } + + return renderedFiles; + } + /** * writes a file to disk * @param destinationDirArg