1 | "use strict";
|
2 | var __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 | };
|
13 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
14 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
15 | };
|
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
17 | exports.$new = void 0;
|
18 | var path_1 = __importDefault(require("path"));
|
19 | var utils_1 = require("@omni-door/utils");
|
20 | var templates_1 = require("./templates");
|
21 | function $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, type = _a.type, hasStorybook = _a.hasStorybook, tpls = _a.tpls;
|
24 | utils_1.logTime('CREATE(创建组件)');
|
25 | utils_1.logInfo("Start create " + componentName + " " + (type === 'cc' ? 'class' : 'functional') + " component");
|
26 | utils_1.logInfo("\u5F00\u59CB\u521B\u5EFA " + componentName + " " + (type === 'cc' ? '类' : '函数') + "\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 |
|
68 | var content_index = tpl.component_index(params);
|
69 | var content_interface = ts && tpl.component_interface(__assign(__assign({}, params), { cc: type === 'cc' }));
|
70 | var content_cc = type === 'cc' && tpl.component_class(params);
|
71 | var content_fc = type === 'fc' && tpl.component_functional(params);
|
72 | var content_readme = md === 'md' && tpl.component_readme(params);
|
73 | var content_mdx = md === 'mdx' && tpl.component_mdx(params);
|
74 | var content_stories = hasStorybook && tpl.component_stories(params);
|
75 | var content_style = stylesheet && tpl.component_style(params);
|
76 | var content_stylesheet = stylesheet && tpl.component_stylesheet(params);
|
77 | var content_test = test && tpl.component_test(params);
|
78 | var pathToFileContentMap = (_b = {},
|
79 | _b["index." + (ts ? 'ts' : 'js')] = content_index,
|
80 | _b[componentName + "." + (ts ? 'tsx' : 'jsx')] = content_fc || content_cc,
|
81 | _b['interface.ts'] = content_interface,
|
82 | _b["style/index." + (ts ? 'ts' : 'js')] = content_style,
|
83 | _b["style/" + componentName + "." + stylesheet] = content_stylesheet,
|
84 | _b["__test__/index.test." + (ts
|
85 | ? 'tsx'
|
86 | : 'jsx')] = content_test,
|
87 | _b["__stories__/index.stories." + (ts
|
88 | ? 'tsx'
|
89 | : 'jsx')] = content_stories,
|
90 | _b['README.md'] = content_readme,
|
91 | _b['README.mdx'] = content_mdx,
|
92 | _b);
|
93 | |
94 |
|
95 |
|
96 | var file_path = function (p) { return path_1.default.resolve(newPath, p); };
|
97 | for (var p in pathToFileContentMap) {
|
98 | utils_1.outputFile({
|
99 | file_path: file_path(p),
|
100 | file_content: pathToFileContentMap[p]
|
101 | });
|
102 | }
|
103 | }
|
104 | catch (e) {
|
105 | var err = e;
|
106 | utils_1.logErr(err.name + ": " + err.message + " at \n" + err.stack);
|
107 | utils_1.logErr('The process of create component failed');
|
108 | utils_1.logErr('创建组件失败');
|
109 | process.exit(1);
|
110 | }
|
111 | utils_1.logTime('CREATE(创建组件)', true);
|
112 | }
|
113 | exports.$new = $new;
|
114 | exports.default = $new;
|