@omni-door/tpl-spa-vue
Version:
The spa-vue project template
100 lines (99 loc) • 3.79 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.$new = void 0;
var path_1 = __importDefault(require("path"));
var utils_1 = require("@omni-door/utils");
var templates_1 = require("./templates");
function $new(_a) {
var _b;
var ts = _a.ts, test = _a.test, componentName = _a.componentName, stylesheet = _a.stylesheet, newPath = _a.newPath, md = _a.md, tpls = _a.tpls;
utils_1.logTime('CREATE');
utils_1.logInfo("Start creating " + componentName + " component");
var custom_tpl_new_list = {};
try {
custom_tpl_new_list = typeof tpls === 'function'
? tpls(templates_1.tpls_origin_new)
: custom_tpl_new_list;
var _loop_1 = function (tpl_name) {
var name_1 = tpl_name;
var list = custom_tpl_new_list;
var tpl_1 = list[name_1];
var tplFactory = function (config) {
try {
return tpl_1 && tpl_1(config);
}
catch (err) {
utils_1.logWarn(err);
utils_1.logWarn("The custom template \"" + name_1 + "\" parsing encountered an error; the default template will be used for initialization");
}
return templates_1.tpls_new[name_1](config);
};
list[name_1] = tplFactory;
};
for (var tpl_name in custom_tpl_new_list) {
_loop_1(tpl_name);
}
}
catch (err_tpls) {
utils_1.logWarn(err_tpls);
utils_1.logWarn('Custom template generation failed; all templates will be initialized with the default template');
}
var tpl = __assign(__assign({}, templates_1.tpls_new), custom_tpl_new_list);
var params = {
ts: ts,
test: test,
componentName: componentName,
style: stylesheet,
md: md
};
try {
// component tpl
var content_index = tpl.component_index(params);
var content_component = tpl.component(params);
var content_readme = md && tpl.component_readme(params);
var content_style = stylesheet && tpl.component_stylesheet(params);
var content_test = test && tpl.component_test(params);
var pathToFileContentMap = (_b = {},
_b[componentName + ".vue"] = content_component,
_b["style/" + componentName + "." + stylesheet] = content_style,
_b["__test__/index.test." + (ts
? 'ts'
: 'js')] = content_test,
_b["index." + (ts ? 'ts' : 'js')] = content_index,
_b['README.md'] = content_readme,
_b);
/**
* create files
*/
var file_path = function (p) { return path_1.default.resolve(newPath, p); };
for (var p in pathToFileContentMap) {
utils_1.outputFile({
file_path: file_path(p),
file_content: pathToFileContentMap[p]
});
}
}
catch (e) {
var err = e;
utils_1.logErr(err.name + ": " + err.message + " at \n" + err.stack);
utils_1.logErr('Component creation failed');
process.exit(1);
}
utils_1.logTime('CREATE', true);
}
exports.$new = $new;
exports.default = $new;