@grouparoo/core
Version:
The Grouparoo Core
112 lines (111 loc) • 4.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigTemplate = void 0;
const mustacheUtils_1 = require("../modules/mustacheUtils");
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const glob_1 = __importDefault(require("glob"));
const configWriter_1 = require("../modules/configWriter");
class ConfigTemplate {
constructor() {
this.params = {};
this.inputs = {};
}
prepareParams(params) {
this.params = params;
// source from other params
Object.keys(this.inputs).forEach((k) => {
if (this.inputs[k].copyDefaultFrom &&
(!this.params[k] || this.params[k].toString().length === 0)) {
this.params[k] = this.params[this.inputs[k].copyDefaultFrom];
}
});
// assign defaults
Object.keys(this.inputs).forEach((k) => {
if (this.inputs[k].default !== undefined &&
(!this.params[k] || this.params[k].toString().length === 0)) {
this.params[k] = this.inputs[k].default;
}
if (!this.params[k] && this.inputs[k].default === null) {
this.params[k] = "null";
}
});
// parentId
if (this.parentId) {
params[this.parentId] = params["parent"];
if (!params[this.parentId])
throw new Error(`option parent (-p, --parent) is required - ${this.parentId} is needed for a ${this.name}`);
}
// format inputs
Object.keys(this.inputs).forEach((k) => {
if (typeof this.inputs[k].formatter === "function") {
this.params[k] = this.inputs[k].formatter(this.params[k].toString());
}
});
// escape for JSON/JS
for (const k in this.params) {
if (k === "path")
continue;
if (typeof this.params[k] === "string" && this.params[k] !== "null") {
this.params[k] = JSON.stringify(this.params[k]);
}
}
return this.params;
}
formatForFilesystem(s) {
return s
.toLowerCase()
.replace(/"/gi, "")
.replace(/'/gi, "")
.replace(/[^a-zA-Z0-9-_\/.]/gi, "_");
}
formatId(s) {
return configWriter_1.ConfigWriter.generateId(s);
}
/**
* The nominal case where all provided mustache files are computed and copied into the client project
*/
async mustacheAllFiles(params, files = this.files, destinationDir = this.destinationDir) {
const response = {};
const errorPrefix = "Missing required input";
const fileNames = await this.resolveFiles(files);
if (!params.path)
throw new Error(`params.path missing`);
for (const i in fileNames) {
const fileName = fileNames[i];
const relativeFileName = this.formatForFilesystem(mustacheUtils_1.MustacheUtils.strictlyRender(path_1.default.basename(fileName), params, errorPrefix, false));
const newFilePath = path_1.default
.join(params.path.toString(), destinationDir, relativeFileName)
.replace(/.template$/, "");
const content = fs_extra_1.default.readFileSync(fileName).toString();
const newContent = mustacheUtils_1.MustacheUtils.strictlyRender(content, params, errorPrefix, true);
response[newFilePath] = newContent;
}
return response;
}
async resolveFiles(filesList) {
let files = [];
for (const i in filesList) {
const foundFiles = glob_1.default.sync(filesList[i]);
files = files.concat(foundFiles);
}
if (files.length === 0) {
console.error(`no files found matching ${filesList}`);
}
return files;
}
unquotedId() {
if (!this.params.id)
return null;
return this.params.id.replace(/"/g, "");
}
extendId(extension) {
if (!this.params.id)
return null;
return `"${this.unquotedId()}_${extension}"`;
}
}
exports.ConfigTemplate = ConfigTemplate;