Compare commits

..

7 Commits

Author SHA1 Message Date
25871fc970 3.0.1 2018-10-04 17:00:33 +02:00
eb990a8ec2 fix(core): update 2018-10-04 17:00:33 +02:00
f5194b76bd Merge branch 'master' into 'master'
update

See merge request pushrocks/smartscaf!1
2018-10-04 14:48:20 +00:00
6b5e9ac617 update 2018-10-04 16:04:42 +02:00
60854250eb 3.0.0 2018-08-30 22:43:22 +02:00
53de92ac1a BREAKING CHANGE(structure): templates now take their path within the constructor 2018-08-30 22:43:22 +02:00
6cb4fff0a5 2.0.2 2018-08-28 23:57:06 +02:00
9 changed files with 93 additions and 34 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartscaf",
"version": "2.0.1",
"version": "3.0.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartscaf",
"version": "2.0.1",
"version": "3.0.1",
"private": false,
"description": "scaffold projects quickly",
"main": "dist/index.js",

View File

@ -8,13 +8,13 @@ process.env.CI = 'true'
let testScafTemplate: smartscaf.ScafTemplate;
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);
});
tap.test('Smartscaf instance -> should read a template directory', async () => {
await testScafTemplate.readTemplateFromDir('./test/test_template');
expect(testScafTemplate.templateSmartfileArray.length).to.equal(5);
await testScafTemplate.readTemplateFromDir();
expect(testScafTemplate.templateSmartfileArray.length).to.equal(6);
});
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

@ -11,26 +11,47 @@ export interface ScafTemplateContructorOptions {
}
export class ScafTemplate {
static async createTemplateFromDir() {
}
/**
* 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;
suppliedVariables: any = {};
missingVariables: string[] = [];
dependencies: ScafTemplate[];
constructor(dirPathArg: string) {
this.dirPath = plugins.path.resolve(dirPathArg);
}
/**
* read a template from a directory
*/
async readTemplateFromDir(dirPathArg: string) {
let dirPath = plugins.path.resolve(dirPathArg);
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*');
async readTemplateFromDir() {
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(this.dirPath, '**/*');
await this._resolveTemplateDependencies();
await this._findVariablesInTemplate();
await this._checkSuppliedVariables();
await this._checkDefaultVariables();
await this._resolveTemplateDependencies();
}
/**
@ -68,14 +89,23 @@ 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());
let renderedTemplateString = template(this.suppliedVariables);
@ -90,6 +120,7 @@ export class ScafTemplate {
}
smartfile.contents = Buffer.from(parsedTemplate.content);
smartfileArrayToWrite.push(smartfile);
}
await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
@ -122,8 +153,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;
});
@ -131,7 +166,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
@ -143,16 +178,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 = {};
}
}
@ -161,14 +201,29 @@ export class ScafTemplate {
* resolve template dependencies
*/
private async _resolveTemplateDependencies() {
const dependencies = this.templateSmartfileArray.find(smartfileArg => {
return smartfileArg.parsedPath.base === "dependencies.yml"
const smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => {
return smartfileArg.parsedPath.base === '.smartscaf.yml';
});
if(!dependencies) {
if(!smartscafSmartfile) {
console.log('No further template dependencies defined!');
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 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 = this.templateSmartfileArray.concat(templateSmartfileArray);
}
}
}