fix(smartscaf): migrate file handling to SmartFileFactory and tighten TypeScript compatibility

This commit is contained in:
2026-04-30 10:22:59 +00:00
parent 3c68eae55d
commit 77a921920b
9 changed files with 2188 additions and 4522 deletions
+30 -25
View File
@@ -15,31 +15,37 @@ export class ScafTemplate {
/**
* the name of the template
*/
public name: string;
public name = '';
/**
* the descriptions of the template
*/
public description: string;
public description = '';
/**
* the location on disk of the template
*/
public dirPath: string;
public destinationPath: string;
public destinationPath?: string;
/**
* smartscafFile
*/
public smartscafFile: interfaces.ISmartscafFile;
public smartscafFile: interfaces.ISmartscafFile = {
defaults: {},
dependencies: {
merge: [],
},
runafter: [],
};
/**
* the files of the template as array of Smartfiles
*/
public templateSmartfileArray: plugins.smartfile.SmartFile[];
public requiredVariables: string[];
public defaultVariables: any;
public suppliedVariables: any = {};
public templateSmartfileArray: plugins.smartfile.SmartFile[] = [];
public requiredVariables: string[] = [];
public defaultVariables: Record<string, unknown> = {};
public suppliedVariables: Record<string, unknown> = {};
public missingVariables: string[] = [];
constructor(dirPathArg: string) {
@@ -50,10 +56,10 @@ export class ScafTemplate {
* read a template from a directory
*/
public async readTemplateFromDir() {
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(
const templateVirtualDirectory = await plugins.smartFileFactory.virtualDirectoryFromPath(
this.dirPath,
'**/*',
);
this.templateSmartfileArray = templateVirtualDirectory.listFiles();
// read .smartscaf.yml file
let smartscafFile: interfaces.ISmartscafFile = {
@@ -90,7 +96,7 @@ export class ScafTemplate {
* supply the variables to render the teplate with
* @param variablesArg gets merged with this.suppliedVariables
*/
public async supplyVariables(variablesArg) {
public async supplyVariables(variablesArg: Record<string, unknown>) {
this.suppliedVariables = {
...this.suppliedVariables,
...variablesArg,
@@ -168,11 +174,11 @@ export class ScafTemplate {
// 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,
}));
renderedFiles.push(plugins.smartFileFactory.fromBuffer(
finalPath,
Buffer.from(finalContent),
smartfile.base,
));
}
return renderedFiles;
@@ -182,7 +188,7 @@ export class ScafTemplate {
* writes a file to disk
* @param destinationDirArg
*/
public async writeToDisk(destinationDirArg) {
public async writeToDisk(destinationDirArg: string) {
this.destinationPath = destinationDirArg;
const smartfileArrayToWrite: plugins.smartfile.SmartFile[] = [];
for (const smartfile of this.templateSmartfileArray) {
@@ -214,10 +220,10 @@ export class ScafTemplate {
smartfileArrayToWrite.push(smartfile);
}
await plugins.smartfile.memory.smartfileArrayToFs(
const virtualDirectoryToWrite = plugins.smartFileFactory.virtualDirectoryFromFileArray(
smartfileArrayToWrite,
destinationDirArg,
);
await virtualDirectoryToWrite.saveToDisk(destinationDirArg);
await this.runScripts();
}
@@ -302,17 +308,16 @@ export class ScafTemplate {
for (const dependency of this.smartscafFile.dependencies.merge) {
console.log(`Now resolving ${dependency}`);
const templatePathToMerge = plugins.path.join(this.dirPath, dependency);
if (!plugins.smartfile.fs.isDirectory(templatePathToMerge)) {
if (!(await plugins.smartFs.directory(templatePathToMerge).exists())) {
console.log(
`dependency ${dependency} resolves to ${templatePathToMerge} which ist NOT a directory`,
);
continue;
}
const templateSmartfileArray =
await plugins.smartfile.fs.fileTreeToObject(
templatePathToMerge,
'**/*',
);
const templateVirtualDirectoryToMerge = await plugins.smartFileFactory.virtualDirectoryFromPath(
templatePathToMerge,
);
const templateSmartfileArray = templateVirtualDirectoryToMerge.listFiles();
this.templateSmartfileArray = this.templateSmartfileArray.concat(
templateSmartfileArray,
);