This commit is contained in:
Philipp Kunz 2018-10-04 16:04:42 +02:00
parent 60854250eb
commit 6b5e9ac617
7 changed files with 75 additions and 27 deletions

View File

@ -14,7 +14,7 @@ tap.test('should create new Smartscaf instance', async () => {
tap.test('Smartscaf instance -> should read a template directory', async () => { tap.test('Smartscaf instance -> should read a template directory', async () => {
await testScafTemplate.readTemplateFromDir(); await testScafTemplate.readTemplateFromDir();
expect(testScafTemplate.templateSmartfileArray.length).to.equal(5); expect(testScafTemplate.templateSmartfileArray.length).to.equal(6);
}); });
tap.test('smartfile -> should accept variables', async () => { tap.test('smartfile -> should accept variables', async () => {

View File

@ -1,4 +0,0 @@
templateVar1: from default yaml
templateVar2: this is another value from yml
templateObject.value1: wow
templateObject.value2: here

View File

@ -0,0 +1 @@
# param1 10

View File

@ -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

View File

@ -1,4 +0,0 @@
templateVar1: from default yaml
templateVar2: this is another value from yml
templateObject.value1: wow
templateObject.value2: here

View File

@ -0,0 +1 @@
# param1 {{node_version}}

View File

@ -15,9 +15,24 @@ export class ScafTemplate {
} }
/**
* the name of the template
*/
name: string; name: string;
/**
* the descriptions of the template
*/
description: string; description: string;
/**
* the location on disk of the template
*/
dirPath: string; dirPath: string;
/**
* the files of the template as array of Smartfiles
*/
templateSmartfileArray: Smartfile[]; templateSmartfileArray: Smartfile[];
requiredVariables: string[]; requiredVariables: string[];
defaultVariables: any; defaultVariables: any;
@ -74,16 +89,26 @@ export class ScafTemplate {
]); ]);
} }
let answerBucket = await localSmartInteract.runQueue(); 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); await helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value);
}); });
} }
/**
* writes a file to disk
* @param destinationDirArg
*/
async writeToDisk(destinationDirArg) { async writeToDisk(destinationDirArg) {
let smartfileArrayToWrite = this.templateSmartfileArray; const smartfileArrayToWrite: Smartfile[] = [];
for (let smartfile of smartfileArrayToWrite) { for (let smartfile of this.templateSmartfileArray) {
// lets filter out template files
if(smartfile.path === '.smartscaf.yml') {
continue;
}
// render the template // render the template
let template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString()); let template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
console.log(this.defaultVariables);
let renderedTemplateString = template(this.suppliedVariables); let renderedTemplateString = template(this.suppliedVariables);
// handle frontmatter // handle frontmatter
@ -96,6 +121,7 @@ export class ScafTemplate {
} }
smartfile.contents = Buffer.from(parsedTemplate.content); smartfile.contents = Buffer.from(parsedTemplate.content);
smartfileArrayToWrite.push(smartfile);
} }
await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg); await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
@ -128,8 +154,12 @@ export class ScafTemplate {
templateSmartfile.contents.toString(), templateSmartfile.contents.toString(),
this.suppliedVariables this.suppliedVariables
); );
// combine with other missingVars
missingVars = [...missingVars, ...localMissingVars]; missingVars = [...missingVars, ...localMissingVars];
} }
// dedupe
missingVars = missingVars.filter((value, index, self) => { missingVars = missingVars.filter((value, index, self) => {
return self.indexOf(value) === index; 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: * allows 2 ways of notation in YAML:
* >> myObject.myKey.someDeeperKey: someValue * >> myObject.myKey.someDeeperKey: someValue
* >> myObject.yourKey.yourDeeperKey: yourValue * >> myObject.yourKey.yourDeeperKey: yourValue
@ -149,16 +179,21 @@ export class ScafTemplate {
* >> - yourDeeperKey: yourValue * >> - yourDeeperKey: yourValue
*/ */
private async _checkDefaultVariables() { private async _checkDefaultVariables() {
let defaultsSmartfile = this.templateSmartfileArray.filter(smartfileArg => { let smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => {
return smartfileArg.parsedPath.base === 'defaults.yml'; return smartfileArg.parsedPath.base === '.smartscaf.yml';
})[0]; });
if (defaultsSmartfile) { if (smartscafSmartfile) {
let defaultObject = await plugins.smartyaml.yamlStringToObject( const smartscafObject = await plugins.smartyaml.yamlStringToObject(
defaultsSmartfile.contents.toString() smartscafSmartfile.contents.toString()
); );
const defaultObject = smartscafObject.defaults;
this.defaultVariables = defaultObject; this.defaultVariables = defaultObject;
} else { }
// safeguard against non existent defaults
if (!this.defaultVariables) {
console.log('this template does not specify defaults')
this.defaultVariables = {}; this.defaultVariables = {};
} }
} }
@ -167,20 +202,29 @@ export class ScafTemplate {
* resolve template dependencies * resolve template dependencies
*/ */
private async _resolveTemplateDependencies() { private async _resolveTemplateDependencies() {
const dependenciesSmartfile = this.templateSmartfileArray.find(smartfileArg => { const smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => {
return smartfileArg.parsedPath.base === "dependencies.yml" return smartfileArg.parsedPath.base === '.smartscaf.yml';
}); });
if(!dependenciesSmartfile) { if(!smartscafSmartfile) {
console.log('No further template dependencies defined!'); console.log('No further template dependencies defined!');
return; return;
} }
console.log('Found template dependencies! Resolving them now!'); console.log('Found template dependencies! Resolving them now!');
console.log('looking at templates to merge!') console.log('looking at templates to merge!');
const dependencies = await plugins.smartyaml.yamlStringToObject(dependenciesSmartfile.contentBuffer.toString()); const smartscafYamlObject = await plugins.smartyaml.yamlStringToObject(smartscafSmartfile.contentBuffer.toString());
for (const dependency of dependencies.merge) { 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); 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, '**/*'); const templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(templatePathToMerge, '**/*');
this.templateSmartfileArray.concat(templateSmartfileArray); this.templateSmartfileArray = this.templateSmartfileArray.concat(templateSmartfileArray);
} }
} }
} }