BREAKING CHANGE(structure): templates now take their path within the constructor

This commit is contained in:
Philipp Kunz 2018-08-30 22:43:22 +02:00
parent 6cb4fff0a5
commit 53de92ac1a
2 changed files with 23 additions and 11 deletions

View File

@ -8,12 +8,12 @@ process.env.CI = 'true'
let testScafTemplate: smartscaf.ScafTemplate; let testScafTemplate: smartscaf.ScafTemplate;
tap.test('should create new Smartscaf instance', async () => { tap.test('should create new Smartscaf instance', async () => {
testScafTemplate = new smartscaf.ScafTemplate(); testScafTemplate = new smartscaf.ScafTemplate('./test/test_template');
expect(testScafTemplate).to.be.instanceof(smartscaf.ScafTemplate); expect(testScafTemplate).to.be.instanceof(smartscaf.ScafTemplate);
}); });
tap.test('Smartscaf instance -> should read a template directory', async () => { tap.test('Smartscaf instance -> should read a template directory', async () => {
await testScafTemplate.readTemplateFromDir('./test/test_template'); await testScafTemplate.readTemplateFromDir();
expect(testScafTemplate.templateSmartfileArray.length).to.equal(5); expect(testScafTemplate.templateSmartfileArray.length).to.equal(5);
}); });

View File

@ -11,26 +11,32 @@ export interface ScafTemplateContructorOptions {
} }
export class ScafTemplate { export class ScafTemplate {
static async createTemplateFromDir() {
}
name: string; name: string;
description: string; description: string;
dirPath: string;
templateSmartfileArray: Smartfile[]; templateSmartfileArray: Smartfile[];
requiredVariables: string[]; requiredVariables: string[];
defaultVariables: any; defaultVariables: any;
suppliedVariables: any = {}; suppliedVariables: any = {};
missingVariables: string[] = []; missingVariables: string[] = [];
dependencies: ScafTemplate[]; constructor(dirPathArg: string) {
this.dirPath = plugins.path.resolve(dirPathArg);
}
/** /**
* read a template from a directory * read a template from a directory
*/ */
async readTemplateFromDir(dirPathArg: string) { async readTemplateFromDir() {
let dirPath = plugins.path.resolve(dirPathArg); this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(this.dirPath, '**/*');
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*'); await this._resolveTemplateDependencies();
await this._findVariablesInTemplate(); await this._findVariablesInTemplate();
await this._checkSuppliedVariables(); await this._checkSuppliedVariables();
await this._checkDefaultVariables(); await this._checkDefaultVariables();
await this._resolveTemplateDependencies();
} }
/** /**
@ -161,14 +167,20 @@ export class ScafTemplate {
* resolve template dependencies * resolve template dependencies
*/ */
private async _resolveTemplateDependencies() { private async _resolveTemplateDependencies() {
const dependencies = this.templateSmartfileArray.find(smartfileArg => { const dependenciesSmartfile = this.templateSmartfileArray.find(smartfileArg => {
return smartfileArg.parsedPath.base === "dependencies.yml" return smartfileArg.parsedPath.base === "dependencies.yml"
}); });
if(!dependencies) { if(!dependenciesSmartfile) {
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!')
const dependencies = await plugins.smartyaml.yamlStringToObject(dependenciesSmartfile.contentBuffer.toString());
for (const dependency of dependencies.merge) {
const templatePathToMerge = plugins.path.join(this.dirPath, dependency);
const templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(templatePathToMerge, '**/*');
this.templateSmartfileArray.concat(templateSmartfileArray);
}
} }
} }