smartscaf/dist/smartscaf.classes.smartscaf.js

141 lines
11 KiB
JavaScript

"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");
const helpers = require("./smartscaf.helpers");
class ScafTemplate {
constructor() {
this.suppliedVariables = {};
this.missingVariables = [];
}
/**
* read a template from a directory
*/
readTemplateFromDir(dirPathArg) {
return __awaiter(this, void 0, void 0, function* () {
let dirPath = plugins.path.resolve(dirPathArg);
this.templateSmartfileArray = yield plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*');
yield this._findVariablesInTemplate();
yield this._checkSuppliedVariables();
yield this._checkDefaultVariables();
});
}
/**
* supply the variables to render the teplate with
* @param variablesArg gets merged with this.suppliedVariables
*/
supplyVariables(variablesArg) {
return __awaiter(this, void 0, void 0, function* () {
this.suppliedVariables = plugins.lodash.merge(this.suppliedVariables, variablesArg);
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',
default: (() => {
if (this.defaultVariables[missingVariable]) {
return this.defaultVariables[missingVariable];
}
else {
return 'undefined variable';
}
})(),
message: `What is the value of ${missingVariable}?`
}]);
}
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);
}));
});
}
writeToDisk(destinationDirArg) {
return __awaiter(this, void 0, void 0, function* () {
let smartfileArrayToWrite = plugins.lodash.cloneDeep(this.templateSmartfileArray);
for (let smartfile of smartfileArrayToWrite) {
// render the template
let template = yield plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
let renderedTemplateString = template(this.suppliedVariables);
// handle frontmatter
let parsedTemplate = plugins.smartfm.parse(renderedTemplateString);
if (parsedTemplate.data.fileName) {
smartfile.updateFileName(parsedTemplate.data.fileName);
}
smartfile.contents = Buffer.from(parsedTemplate.content);
}
yield plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
});
}
/**
* finds all variables in a Template in as string
* e.g. myobject.someKey and myobject.someOtherKey
*/
_findVariablesInTemplate() {
return __awaiter(this, void 0, void 0, function* () {
let templateVariables = [];
for (let templateSmartfile of this.templateSmartfileArray) {
let localTemplateVariables = yield plugins.smarthbs.findVarsInHbsString(templateSmartfile.contents.toString());
templateVariables = plugins.lodash.concat(templateVariables, localTemplateVariables);
}
templateVariables = plugins.lodash.uniq(templateVariables);
});
}
/**
* checks if supplied Variables satisfy the template
*/
_checkSuppliedVariables() {
return __awaiter(this, void 0, void 0, function* () {
let missingVars = [];
for (let templateSmartfile of this.templateSmartfileArray) {
let localMissingVars = yield plugins.smarthbs.checkVarsSatisfaction(templateSmartfile.contents.toString(), this.suppliedVariables);
missingVars = plugins.lodash.concat(missingVars, localMissingVars);
}
missingVars = plugins.lodash.uniq(missingVars);
return missingVars;
});
}
/**
* 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 = {};
}
});
}
}
exports.ScafTemplate = ScafTemplate;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzY2FmLmNsYXNzZXMuc21hcnRzY2FmLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzY2FmLmNsYXNzZXMuc21hcnRzY2FmLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFBQSwrQ0FBOEM7QUFDOUMsK0NBQThDO0FBVzlDO0lBQUE7UUFNRSxzQkFBaUIsR0FBUSxFQUFFLENBQUE7UUFDM0IscUJBQWdCLEdBQWEsRUFBRSxDQUFBO0lBOEhqQyxDQUFDO0lBNUhDOztPQUVHO0lBQ0csbUJBQW1CLENBQUUsVUFBa0I7O1lBQzNDLElBQUksT0FBTyxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLFVBQVUsQ0FBQyxDQUFBO1lBQzlDLElBQUksQ0FBQyxzQkFBc0IsR0FBRyxNQUFNLE9BQU8sQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLGdCQUFnQixDQUFDLE9BQU8sRUFBRSxNQUFNLENBQUMsQ0FBQTtZQUMxRixNQUFNLElBQUksQ0FBQyx3QkFBd0IsRUFBRSxDQUFBO1lBQ3JDLE1BQU0sSUFBSSxDQUFDLHVCQUF1QixFQUFFLENBQUE7WUFDcEMsTUFBTSxJQUFJLENBQUMsc0JBQXNCLEVBQUUsQ0FBQTtRQUNyQyxDQUFDO0tBQUE7SUFFRDs7O09BR0c7SUFDRyxlQUFlLENBQUUsWUFBWTs7WUFDakMsSUFBSSxDQUFDLGlCQUFpQixHQUFHLE9BQU8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