From 9fb0933dbca8ecaa511d6da4d0bd7498cd7984d8 Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Tue, 24 Mar 2026 16:42:10 +0000 Subject: [PATCH] feat(ScafTemplate): add renderToMemory() method for in-memory template rendering Adds a new public method that renders all template files in memory without writing to disk or running scripts. Returns new SmartFile instances without mutating the original templateSmartfileArray. This enables use cases like format commands that need to compare rendered templates with existing files. --- ts/smartscaf.classes.smartscaf.ts | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) 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