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.
This commit is contained in:
2026-03-24 16:42:10 +00:00
parent 9e88f62e7e
commit 9fb0933dbc

View File

@@ -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<plugins.smartfile.SmartFile[]> {
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