UNPKG

4.63 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 });
17var path_1 = __importDefault(require("path"));
18var utils_1 = require("@omni-door/utils");
19var templates_1 = require("./templates");
20function $new(_a) {
21 var _b;
22 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;
23 utils_1.logTime('创建组件');
24 utils_1.logInfo("\u5F00\u59CB\u521B\u5EFA " + componentName + " " + (type === 'cc' ? '类' : '函数') + "\u7EC4\u4EF6 (Start create " + componentName + " " + (type === 'cc' ? 'class' : 'functional') + " component)");
25 var custom_tpl_new_list = {};
26 try {
27 custom_tpl_new_list = typeof tpls === 'function'
28 ? tpls(templates_1.tpls_origin_new)
29 : custom_tpl_new_list;
30 var _loop_1 = function (tpl_name) {
31 var name_1 = tpl_name;
32 var list = custom_tpl_new_list;
33 var tpl_1 = list[name_1];
34 var tplFactory = function (config) {
35 try {
36 return tpl_1 && tpl_1(config);
37 }
38 catch (err) {
39 utils_1.logWarn(err);
40 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\uFF01(The custom template [" + name_1 + "] parsing occured error, the default template will be used for initialization!)");
41 }
42 return templates_1.tpls_new[name_1](config);
43 };
44 list[name_1] = tplFactory;
45 };
46 for (var tpl_name in custom_tpl_new_list) {
47 _loop_1(tpl_name);
48 }
49 }
50 catch (err_tpls) {
51 utils_1.logWarn(err_tpls);
52 utils_1.logWarn('生成自定义模板出错,将全部使用默认模板进行创建组件!(The custom template generating occured error, all will be initializated with the default template!)');
53 }
54 var tpl = __assign(__assign({}, templates_1.tpls_new), custom_tpl_new_list);
55 var params = {
56 ts: ts,
57 test: test,
58 componentName: componentName,
59 style: stylesheet,
60 md: md
61 };
62 // component tpl
63 var content_index = tpl.component_index(params);
64 var content_interface = ts && tpl.component_interface(__assign(__assign({}, params), { cc: type === 'cc' }));
65 var content_cc = type === 'cc' && tpl.component_class(params);
66 var content_fc = type === 'fc' && tpl.component_functional(params);
67 var content_readme = md === 'md' && tpl.component_readme(params);
68 var content_mdx = md === 'mdx' && tpl.component_mdx(params);
69 var content_stories = hasStorybook && tpl.component_stories(params);
70 var content_style = stylesheet && tpl.component_style(params);
71 var content_stylesheet = stylesheet && tpl.component_stylesheet(params);
72 var content_test = test && tpl.component_test(params);
73 var pathToFileContentMap = (_b = {},
74 _b["index." + (ts ? 'ts' : 'js')] = content_index,
75 _b[componentName + "." + (ts ? 'tsx' : 'jsx')] = content_fc || content_cc,
76 _b['interface.ts'] = content_interface,
77 _b["style/index." + (ts ? 'ts' : 'js')] = content_style,
78 _b["style/" + componentName + "." + stylesheet] = content_stylesheet,
79 _b["__test__/index.test." + (ts
80 ? 'tsx'
81 : 'jsx')] = content_test,
82 _b["__stories__/index.stories." + (ts
83 ? 'tsx'
84 : 'jsx')] = content_stories,
85 _b['README.md'] = content_readme,
86 _b['README.mdx'] = content_mdx,
87 _b);
88 /**
89 * create files
90 */
91 var file_path = function (p) { return path_1.default.resolve(newPath, p); };
92 for (var p in pathToFileContentMap) {
93 utils_1.output_file({
94 file_path: file_path(p),
95 file_content: pathToFileContentMap[p]
96 });
97 }
98 utils_1.logTime('创建组件', true);
99}
100exports.$new = $new;
101exports.default = $new;