UNPKG

4.24 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __importDefault = (this && this.__importDefault) || function (mod) {
14 return (mod && mod.__esModule) ? mod : { "default": mod };
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.$new = void 0;
18var path_1 = __importDefault(require("path"));
19var utils_1 = require("@omni-door/utils");
20var templates_1 = require("./templates");
21function $new(_a) {
22 var _b;
23 var ts = _a.ts, test = _a.test, componentName = _a.componentName, stylesheet = _a.stylesheet, newPath = _a.newPath, md = _a.md, tpls = _a.tpls;
24 utils_1.logTime('CREATE(创建组件)');
25 utils_1.logInfo("Start create " + componentName + " component");
26 utils_1.logInfo("\u5F00\u59CB\u521B\u5EFA " + componentName + " \u7EC4\u4EF6");
27 var custom_tpl_new_list = {};
28 try {
29 custom_tpl_new_list = typeof tpls === 'function'
30 ? tpls(templates_1.tpls_origin_new)
31 : custom_tpl_new_list;
32 var _loop_1 = function (tpl_name) {
33 var name_1 = tpl_name;
34 var list = custom_tpl_new_list;
35 var tpl_1 = list[name_1];
36 var tplFactory = function (config) {
37 try {
38 return tpl_1 && tpl_1(config);
39 }
40 catch (err) {
41 utils_1.logWarn(err);
42 utils_1.logWarn("The custom template \"" + name_1 + "\" parsing occured error, the default template will be used for initialization");
43 utils_1.logWarn("\u81EA\u5B9A\u4E49\u6A21\u677F \"" + name_1 + "\" \u89E3\u6790\u51FA\u9519\uFF0C\u5C06\u4F7F\u7528\u9ED8\u8BA4\u6A21\u677F\u8FDB\u884C\u521B\u5EFA\u7EC4\u4EF6");
44 }
45 return templates_1.tpls_new[name_1](config);
46 };
47 list[name_1] = tplFactory;
48 };
49 for (var tpl_name in custom_tpl_new_list) {
50 _loop_1(tpl_name);
51 }
52 }
53 catch (err_tpls) {
54 utils_1.logWarn(err_tpls);
55 utils_1.logWarn('The custom template generating occured error, all will be initializated with the default template');
56 utils_1.logWarn('生成自定义模板出错,将全部使用默认模板进行创建模块');
57 }
58 var tpl = __assign(__assign({}, templates_1.tpls_new), custom_tpl_new_list);
59 var params = {
60 ts: ts,
61 test: test,
62 componentName: componentName,
63 style: stylesheet,
64 md: md
65 };
66 try {
67 // component tpl
68 var content_index = tpl.component_index(params);
69 var content_component = tpl.component(params);
70 var content_readme = md && tpl.component_readme(params);
71 var content_style = stylesheet && tpl.component_stylesheet(params);
72 var content_test = test && tpl.component_test(params);
73 var pathToFileContentMap = (_b = {},
74 _b[componentName + ".vue"] = content_component,
75 _b["style/" + componentName + "." + stylesheet] = content_style,
76 _b["__test__/index.test." + (ts
77 ? 'ts'
78 : 'js')] = content_test,
79 _b["index." + (ts ? 'ts' : 'js')] = content_index,
80 _b['README.md'] = content_readme,
81 _b);
82 /**
83 * create files
84 */
85 var file_path = function (p) { return path_1.default.resolve(newPath, p); };
86 for (var p in pathToFileContentMap) {
87 utils_1.outputFile({
88 file_path: file_path(p),
89 file_content: pathToFileContentMap[p]
90 });
91 }
92 }
93 catch (err) {
94 utils_1.logErr(err.name + ": " + err.message + " at \n" + err.stack);
95 utils_1.logErr('The process of create component failed');
96 utils_1.logErr('创建组件失败');
97 process.exit(1);
98 }
99 utils_1.logTime('CREATE(创建组件)', true);
100}
101exports.$new = $new;
102exports.default = $new;