2017-04-28 22:44:23 +00:00
|
|
|
"use strict";
|
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const plugins = require("./smartscaf.plugins");
|
2017-05-25 16:32:53 +00:00
|
|
|
const helpers = require("./smartscaf.helpers");
|
2017-04-28 22:44:23 +00:00
|
|
|
class ScafTemplate {
|
2017-05-03 07:45:22 +00:00
|
|
|
constructor() {
|
2017-05-25 16:32:53 +00:00
|
|
|
this.suppliedVariables = {};
|
2017-05-03 07:45:22 +00:00
|
|
|
this.missingVariables = [];
|
|
|
|
}
|
2017-04-28 22:44:23 +00:00
|
|
|
/**
|
|
|
|
* read a template from a directory
|
|
|
|
*/
|
2017-05-03 07:45:22 +00:00
|
|
|
readTemplateFromDir(dirPathArg) {
|
2017-04-28 22:44:23 +00:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2017-05-03 07:45:22 +00:00
|
|
|
let dirPath = plugins.path.resolve(dirPathArg);
|
|
|
|
this.templateSmartfileArray = yield plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*');
|
2017-05-25 16:32:53 +00:00
|
|
|
yield this._findVariablesInTemplate();
|
|
|
|
yield this._checkSuppliedVariables();
|
|
|
|
yield this._checkDefaultVariables();
|
2017-04-28 22:44:23 +00:00
|
|
|
});
|
|
|
|
}
|
2017-05-03 07:45:22 +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-05-03 07:45:22 +00:00
|
|
|
*/
|
|
|
|
supplyVariables(variablesArg) {
|
2017-04-28 22:44:23 +00:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2017-05-25 16:32:53 +00:00
|
|
|
this.suppliedVariables = plugins.lodash.merge(this.suppliedVariables, variablesArg);
|
2017-05-06 23:23:03 +00:00
|
|
|
this.missingVariables = yield this._checkSuppliedVariables();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Will ask for the missing variables by cli interaction
|
|
|
|
*/
|
|
|
|
askCliForMissingVariables() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
this.missingVariables = yield this._checkSuppliedVariables();
|
|
|
|
let localSmartInteract = new plugins.smartinteract.SmartInteract();
|
|
|
|
for (let missingVariable of this.missingVariables) {
|
|
|
|
localSmartInteract.addQuestions([{
|
|
|
|
name: missingVariable,
|
|
|
|
type: 'input',
|
2017-05-25 16:32:53 +00:00
|
|
|
default: (() => {
|
2017-06-01 23:03:24 +00:00
|
|
|
if (this.defaultVariables && this.defaultVariables[missingVariable]) {
|
2017-05-25 16:32:53 +00:00
|
|
|
return this.defaultVariables[missingVariable];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 'undefined variable';
|
|
|
|
}
|
|
|
|
})(),
|
2017-05-06 23:23:03 +00:00
|
|
|
message: `What is the value of ${missingVariable}?`
|
|
|
|
}]);
|
|
|
|
}
|
2017-05-25 16:32:53 +00:00
|
|
|
let answerBucket = yield localSmartInteract.runQueue();
|
|
|
|
answerBucket.answerMap.forEach((answer) => __awaiter(this, void 0, void 0, function* () {
|
|
|
|
yield helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value);
|
|
|
|
}));
|
2017-04-28 22:44:23 +00:00
|
|
|
});
|
|
|
|
}
|
2017-05-26 13:32:50 +00:00
|
|
|
writeToDisk(destinationDirArg) {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let smartfileArrayToWrite = plugins.lodash.cloneDeep(this.templateSmartfileArray);
|
|
|
|
for (let smartfile of smartfileArrayToWrite) {
|
2017-05-27 21:52:58 +00:00
|
|
|
// render the template
|
2017-05-26 13:32:50 +00:00
|
|
|
let template = yield plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
|
|
|
|
let renderedTemplateString = template(this.suppliedVariables);
|
2017-05-27 21:52:58 +00:00
|
|
|
// handle frontmatter
|
|
|
|
let parsedTemplate = plugins.smartfm.parse(renderedTemplateString);
|
|
|
|
if (parsedTemplate.data.fileName) {
|
|
|
|
smartfile.updateFileName(parsedTemplate.data.fileName);
|
|
|
|
}
|
|
|
|
smartfile.contents = Buffer.from(parsedTemplate.content);
|
2017-05-26 13:32:50 +00:00
|
|
|
}
|
|
|
|
yield plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
_findVariablesInTemplate() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2017-05-25 16:32:53 +00:00
|
|
|
let templateVariables = [];
|
|
|
|
for (let templateSmartfile of this.templateSmartfileArray) {
|
|
|
|
let localTemplateVariables = yield plugins.smarthbs.findVarsInHbsString(templateSmartfile.contents.toString());
|
|
|
|
templateVariables = plugins.lodash.concat(templateVariables, localTemplateVariables);
|
2017-05-03 07:45:22 +00:00
|
|
|
}
|
2017-05-25 16:32:53 +00:00
|
|
|
templateVariables = plugins.lodash.uniq(templateVariables);
|
2017-04-28 22:44:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* checks if supplied Variables satisfy the template
|
|
|
|
*/
|
2017-05-06 23:23:03 +00:00
|
|
|
_checkSuppliedVariables() {
|
2017-04-28 22:44:23 +00:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
2017-05-03 07:45:22 +00:00
|
|
|
let missingVars = [];
|
2017-05-25 16:32:53 +00:00
|
|
|
for (let templateSmartfile of this.templateSmartfileArray) {
|
|
|
|
let localMissingVars = yield plugins.smarthbs.checkVarsSatisfaction(templateSmartfile.contents.toString(), this.suppliedVariables);
|
2017-05-03 07:45:22 +00:00
|
|
|
missingVars = plugins.lodash.concat(missingVars, localMissingVars);
|
|
|
|
}
|
2017-05-25 16:32:53 +00:00
|
|
|
missingVars = plugins.lodash.uniq(missingVars);
|
2017-05-03 07:45:22 +00:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
_checkDefaultVariables() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
let defaultsSmartfile = this.templateSmartfileArray.filter(smartfileArg => {
|
|
|
|
return smartfileArg.parsedPath.base === 'defaults.yml';
|
|
|
|
})[0];
|
|
|
|
if (defaultsSmartfile) {
|
|
|
|
let defaultObject = yield plugins.smartyaml.yamlStringToObject(defaultsSmartfile.contents.toString());
|
|
|
|
this.defaultVariables = defaultObject;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.defaultVariables = {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-04-28 22:44:23 +00:00
|
|
|
}
|
|
|
|
exports.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