diff --git a/test/test.ts b/test/test.ts index 4718042..7c6d561 100644 --- a/test/test.ts +++ b/test/test.ts @@ -14,7 +14,7 @@ tap.test('should create new Smartscaf instance', async () => { tap.test('Smartscaf instance -> should read a template directory', async () => { await testScafTemplate.readTemplateFromDir(); - expect(testScafTemplate.templateSmartfileArray.length).to.equal(5); + expect(testScafTemplate.templateSmartfileArray.length).to.equal(6); }); tap.test('smartfile -> should accept variables', async () => { diff --git a/test/test_output/defaults.yml b/test/test_output/defaults.yml deleted file mode 100644 index 8303136..0000000 --- a/test/test_output/defaults.yml +++ /dev/null @@ -1,4 +0,0 @@ -templateVar1: from default yaml -templateVar2: this is another value from yml -templateObject.value1: wow -templateObject.value2: here \ No newline at end of file diff --git a/test/test_output/hello.md b/test/test_output/hello.md new file mode 100644 index 0000000..86fe7e2 --- /dev/null +++ b/test/test_output/hello.md @@ -0,0 +1 @@ +# param1 10 \ No newline at end of file diff --git a/test/test_template/.smartscaf.yml b/test/test_template/.smartscaf.yml new file mode 100644 index 0000000..d131f47 --- /dev/null +++ b/test/test_template/.smartscaf.yml @@ -0,0 +1,10 @@ +defaults: + templateVar1: from default yaml + templateVar2: this is another value from yml + templateObject.value1: wow + templateObject.value2: here + node_version: '10' + +dependencies: + merge: + - ../test_template_2 diff --git a/test/test_template/defaults.yml b/test/test_template/defaults.yml deleted file mode 100644 index 8303136..0000000 --- a/test/test_template/defaults.yml +++ /dev/null @@ -1,4 +0,0 @@ -templateVar1: from default yaml -templateVar2: this is another value from yml -templateObject.value1: wow -templateObject.value2: here \ No newline at end of file diff --git a/test/test_template_2/hello.md b/test/test_template_2/hello.md new file mode 100644 index 0000000..87ffb2a --- /dev/null +++ b/test/test_template_2/hello.md @@ -0,0 +1 @@ +# param1 {{node_version}} \ No newline at end of file diff --git a/ts/smartscaf.classes.smartscaf.ts b/ts/smartscaf.classes.smartscaf.ts index e766b1e..f1158c1 100644 --- a/ts/smartscaf.classes.smartscaf.ts +++ b/ts/smartscaf.classes.smartscaf.ts @@ -15,9 +15,24 @@ export class ScafTemplate { } + /** + * the name of the template + */ name: string; + + /** + * the descriptions of the template + */ description: string; + + /** + * the location on disk of the template + */ dirPath: string; + + /** + * the files of the template as array of Smartfiles + */ templateSmartfileArray: Smartfile[]; requiredVariables: string[]; defaultVariables: any; @@ -74,16 +89,26 @@ export class ScafTemplate { ]); } let answerBucket = await localSmartInteract.runQueue(); - answerBucket.answerMap.forEach(async answer => { + await answerBucket.answerMap.forEach(async answer => { await helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value); }); } + /** + * writes a file to disk + * @param destinationDirArg + */ async writeToDisk(destinationDirArg) { - let smartfileArrayToWrite = this.templateSmartfileArray; - for (let smartfile of smartfileArrayToWrite) { + const smartfileArrayToWrite: Smartfile[] = []; + for (let smartfile of this.templateSmartfileArray) { + // lets filter out template files + if(smartfile.path === '.smartscaf.yml') { + continue; + } + // render the template let template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString()); + console.log(this.defaultVariables); let renderedTemplateString = template(this.suppliedVariables); // handle frontmatter @@ -96,6 +121,7 @@ export class ScafTemplate { } smartfile.contents = Buffer.from(parsedTemplate.content); + smartfileArrayToWrite.push(smartfile); } await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg); @@ -128,8 +154,12 @@ export class ScafTemplate { templateSmartfile.contents.toString(), this.suppliedVariables ); + + // combine with other missingVars missingVars = [...missingVars, ...localMissingVars]; } + + // dedupe missingVars = missingVars.filter((value, index, self) => { return self.indexOf(value) === index; }); @@ -137,7 +167,7 @@ export class ScafTemplate { } /** - * checks the default.yml at the root of a template for default variables + * checks the smartscaf.yml default values at the root of a template * allows 2 ways of notation in YAML: * >> myObject.myKey.someDeeperKey: someValue * >> myObject.yourKey.yourDeeperKey: yourValue @@ -149,16 +179,21 @@ export class ScafTemplate { * >> - yourDeeperKey: yourValue */ private async _checkDefaultVariables() { - let defaultsSmartfile = this.templateSmartfileArray.filter(smartfileArg => { - return smartfileArg.parsedPath.base === 'defaults.yml'; - })[0]; + let smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => { + return smartfileArg.parsedPath.base === '.smartscaf.yml'; + }); - if (defaultsSmartfile) { - let defaultObject = await plugins.smartyaml.yamlStringToObject( - defaultsSmartfile.contents.toString() + if (smartscafSmartfile) { + const smartscafObject = await plugins.smartyaml.yamlStringToObject( + smartscafSmartfile.contents.toString() ); + const defaultObject = smartscafObject.defaults; this.defaultVariables = defaultObject; - } else { + } + + // safeguard against non existent defaults + if (!this.defaultVariables) { + console.log('this template does not specify defaults') this.defaultVariables = {}; } } @@ -167,20 +202,29 @@ export class ScafTemplate { * resolve template dependencies */ private async _resolveTemplateDependencies() { - const dependenciesSmartfile = this.templateSmartfileArray.find(smartfileArg => { - return smartfileArg.parsedPath.base === "dependencies.yml" + const smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => { + return smartfileArg.parsedPath.base === '.smartscaf.yml'; }); - if(!dependenciesSmartfile) { + if(!smartscafSmartfile) { console.log('No further template dependencies defined!'); return; } console.log('Found template dependencies! Resolving them now!'); - console.log('looking at templates to merge!') - const dependencies = await plugins.smartyaml.yamlStringToObject(dependenciesSmartfile.contentBuffer.toString()); - for (const dependency of dependencies.merge) { + console.log('looking at templates to merge!'); + const smartscafYamlObject = await plugins.smartyaml.yamlStringToObject(smartscafSmartfile.contentBuffer.toString()); + if(!smartscafYamlObject) { + console.log('Something seems strange about the supplied dependencies.yml file.'); + return; + } + for (const dependency of smartscafYamlObject.dependencies.merge) { + console.log(`Now resolving ${dependency}`); const templatePathToMerge = plugins.path.join(this.dirPath, dependency); + if(!plugins.smartfile.fs.isDirectory(templatePathToMerge)) { + console.log(`dependency ${dependency} resolves to ${templatePathToMerge} which ist NOT a directory`); + continue; + }; const templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(templatePathToMerge, '**/*'); - this.templateSmartfileArray.concat(templateSmartfileArray); + this.templateSmartfileArray = this.templateSmartfileArray.concat(templateSmartfileArray); } } }