2018-08-27 21:55:14 +00:00
|
|
|
import * as plugins from './smartscaf.plugins';
|
|
|
|
import * as helpers from './smartscaf.helpers';
|
2017-04-28 22:44:23 +00:00
|
|
|
|
|
|
|
// interfaces
|
2018-08-27 21:55:14 +00:00
|
|
|
import { Smartfile } from '@pushrocks/smartfile';
|
2017-04-28 22:44:23 +00:00
|
|
|
|
|
|
|
export interface ScafTemplateContructorOptions {
|
2018-08-27 21:55:14 +00:00
|
|
|
name?: string;
|
|
|
|
description?: string;
|
|
|
|
sourceDir?: string;
|
2017-04-28 22:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ScafTemplate {
|
2018-08-27 21:55:14 +00:00
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
templateSmartfileArray: Smartfile[];
|
|
|
|
requiredVariables: string[];
|
|
|
|
defaultVariables: any;
|
|
|
|
suppliedVariables: any = {};
|
|
|
|
missingVariables: string[] = [];
|
2017-04-28 22:44:23 +00:00
|
|
|
|
2018-08-27 22:20:02 +00:00
|
|
|
dependencies: ScafTemplate[];
|
|
|
|
|
2017-04-28 22:44:23 +00:00
|
|
|
/**
|
|
|
|
* read a template from a directory
|
|
|
|
*/
|
2018-08-27 21:55:14 +00:00
|
|
|
async readTemplateFromDir(dirPathArg: string) {
|
|
|
|
let dirPath = plugins.path.resolve(dirPathArg);
|
|
|
|
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*');
|
|
|
|
await this._findVariablesInTemplate();
|
|
|
|
await this._checkSuppliedVariables();
|
|
|
|
await this._checkDefaultVariables();
|
2018-08-27 22:20:02 +00:00
|
|
|
await this._resolveTemplateDependencies();
|
2017-04-28 22:44:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 21:58:03 +00:00
|
|
|
/**
|
|
|
|
* supply the variables to render the teplate with
|
2017-05-25 16:32:53 +00:00
|
|
|
* @param variablesArg gets merged with this.suppliedVariables
|
2017-04-30 21:58:03 +00:00
|
|
|
*/
|
2018-08-27 21:55:14 +00:00
|
|
|
async supplyVariables(variablesArg) {
|
|
|
|
this.suppliedVariables = {
|
|
|
|
...this.suppliedVariables,
|
|
|
|
...variablesArg
|
|
|
|
};
|
|
|
|
this.missingVariables = await this._checkSuppliedVariables();
|
2017-04-28 22:44:23 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 22:47:27 +00:00
|
|
|
/**
|
|
|
|
* Will ask for the missing variables by cli interaction
|
|
|
|
*/
|
2018-08-27 21:55:14 +00:00
|
|
|
async askCliForMissingVariables() {
|
|
|
|
this.missingVariables = await this._checkSuppliedVariables();
|
|
|
|
let localSmartInteract = new plugins.smartinteract.SmartInteract();
|
2017-05-06 23:23:03 +00:00
|
|
|
for (let missingVariable of this.missingVariables) {
|
2018-08-27 21:55:14 +00:00
|
|
|
localSmartInteract.addQuestions([
|
|
|
|
{
|
|
|
|
name: missingVariable,
|
|
|
|
type: 'input',
|
|
|
|
default: (() => {
|
|
|
|
if (this.defaultVariables && this.defaultVariables[missingVariable]) {
|
|
|
|
return this.defaultVariables[missingVariable];
|
|
|
|
} else {
|
|
|
|
return 'undefined variable';
|
|
|
|
}
|
|
|
|
})(),
|
|
|
|
message: `What is the value of ${missingVariable}?`
|
|
|
|
}
|
|
|
|
]);
|
2017-05-06 23:23:03 +00:00
|
|
|
}
|
2018-08-27 21:55:14 +00:00
|
|
|
let answerBucket = await localSmartInteract.runQueue();
|
2017-05-25 16:32:53 +00:00
|
|
|
answerBucket.answerMap.forEach(async answer => {
|
2018-08-27 21:55:14 +00:00
|
|
|
await helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value);
|
|
|
|
});
|
2017-05-05 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 21:55:14 +00:00
|
|
|
async writeToDisk(destinationDirArg) {
|
|
|
|
let smartfileArrayToWrite = this.templateSmartfileArray;
|
2017-05-26 13:32:50 +00:00
|
|
|
for (let smartfile of smartfileArrayToWrite) {
|
2017-05-27 21:52:58 +00:00
|
|
|
// render the template
|
2018-08-27 21:55:14 +00:00
|
|
|
let template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
|
|
|
|
let renderedTemplateString = template(this.suppliedVariables);
|
2017-05-27 21:52:58 +00:00
|
|
|
|
|
|
|
// handle frontmatter
|
2018-08-27 21:55:14 +00:00
|
|
|
const smartfmInstance = new plugins.smartfm.Smartfm({
|
|
|
|
fmType: "yaml"
|
|
|
|
});
|
|
|
|
let parsedTemplate = smartfmInstance.parse(renderedTemplateString) as any;
|
2017-05-27 21:52:58 +00:00
|
|
|
if (parsedTemplate.data.fileName) {
|
2018-08-27 21:55:14 +00:00
|
|
|
smartfile.updateFileName(parsedTemplate.data.fileName);
|
2017-05-27 21:52:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 21:55:14 +00:00
|
|
|
smartfile.contents = Buffer.from(parsedTemplate.content);
|
2017-05-26 13:32:50 +00:00
|
|
|
}
|
2017-05-27 21:52:58 +00:00
|
|
|
|
2018-08-27 21:55:14 +00:00
|
|
|
await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
|
2017-05-26 13:32:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:44:23 +00:00
|
|
|
/**
|
2017-05-25 16:32:53 +00:00
|
|
|
* finds all variables in a Template in as string
|
|
|
|
* e.g. myobject.someKey and myobject.someOtherKey
|
2017-04-28 22:44:23 +00:00
|
|
|
*/
|
2018-08-27 21:55:14 +00:00
|
|
|
private async _findVariablesInTemplate() {
|
|
|
|
let templateVariables: string[] = [];
|
2017-05-25 16:32:53 +00:00
|
|
|
for (let templateSmartfile of this.templateSmartfileArray) {
|
2018-08-27 21:55:14 +00:00
|
|
|
let localTemplateVariables = await plugins.smarthbs.findVarsInHbsString(
|
|
|
|
templateSmartfile.contents.toString()
|
|
|
|
);
|
|
|
|
templateVariables = [...templateVariables, ...localTemplateVariables];
|
2017-04-30 21:58:03 +00:00
|
|
|
}
|
2018-08-27 21:55:14 +00:00
|
|
|
templateVariables = templateVariables.filter((value, index, self) => {
|
|
|
|
return self.indexOf(value) === index;
|
|
|
|
});
|
2017-04-28 22:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if supplied Variables satisfy the template
|
|
|
|
*/
|
2018-08-27 21:55:14 +00:00
|
|
|
private async _checkSuppliedVariables() {
|
|
|
|
let missingVars: string[] = [];
|
2017-05-25 16:32:53 +00:00
|
|
|
for (let templateSmartfile of this.templateSmartfileArray) {
|
2017-05-03 07:45:22 +00:00
|
|
|
let localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
|
2017-05-25 16:32:53 +00:00
|
|
|
templateSmartfile.contents.toString(),
|
2017-05-06 23:23:03 +00:00
|
|
|
this.suppliedVariables
|
2018-08-27 21:55:14 +00:00
|
|
|
);
|
|
|
|
missingVars = [...missingVars, ...localMissingVars];
|
2017-05-03 07:45:22 +00:00
|
|
|
}
|
2018-08-27 21:55:14 +00:00
|
|
|
missingVars = missingVars.filter((value, index, self) => {
|
|
|
|
return self.indexOf(value) === index;
|
|
|
|
});
|
|
|
|
return missingVars;
|
2017-04-28 22:44:23 +00:00
|
|
|
}
|
2017-05-25 16:32:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* checks the default.yml at the root of a template for default variables
|
|
|
|
* allows 2 ways of notation in YAML:
|
|
|
|
* >> myObject.myKey.someDeeperKey: someValue
|
|
|
|
* >> myObject.yourKey.yourDeeperKey: yourValue
|
|
|
|
* or
|
|
|
|
* >> myObject:
|
|
|
|
* >> - someKey:
|
|
|
|
* >> - someDeeperKey: someValue
|
|
|
|
* >> - yourKey:
|
|
|
|
* >> - yourDeeperKey: yourValue
|
|
|
|
*/
|
2018-08-27 21:55:14 +00:00
|
|
|
private async _checkDefaultVariables() {
|
2017-05-25 16:32:53 +00:00
|
|
|
let defaultsSmartfile = this.templateSmartfileArray.filter(smartfileArg => {
|
2018-08-27 21:55:14 +00:00
|
|
|
return smartfileArg.parsedPath.base === 'defaults.yml';
|
|
|
|
})[0];
|
2017-05-25 16:32:53 +00:00
|
|
|
|
|
|
|
if (defaultsSmartfile) {
|
|
|
|
let defaultObject = await plugins.smartyaml.yamlStringToObject(
|
|
|
|
defaultsSmartfile.contents.toString()
|
2018-08-27 21:55:14 +00:00
|
|
|
);
|
|
|
|
this.defaultVariables = defaultObject;
|
2017-05-25 16:32:53 +00:00
|
|
|
} else {
|
2018-08-27 21:55:14 +00:00
|
|
|
this.defaultVariables = {};
|
2017-05-25 16:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-27 22:20:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* resolve template dependencies
|
|
|
|
*/
|
|
|
|
private async _resolveTemplateDependencies() {
|
|
|
|
const dependencies = this.templateSmartfileArray.find(smartfileArg => {
|
|
|
|
return smartfileArg.parsedPath.base === "dependencies.yml"
|
|
|
|
});
|
|
|
|
if(!dependencies) {
|
|
|
|
console.log('No further template dependencies defined!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('Found template dependencies! Resolving them now!')
|
|
|
|
|
|
|
|
}
|
2017-04-28 22:44:23 +00:00
|
|
|
}
|