UNPKG

@omni-door/tpl-component-library-react

Version:
102 lines (101 loc) 4.63 kB
"use strict"; 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 }); 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, type = _a.type, hasStorybook = _a.hasStorybook, tpls = _a.tpls; utils_1.logTime('创建组件'); utils_1.logInfo("\u5F00\u59CB\u521B\u5EFA " + componentName + " " + (type === 'cc' ? '类' : '函数') + "\u7EC4\u4EF6 (Start create " + componentName + " " + (type === 'cc' ? 'class' : 'functional') + " 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("\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!)"); } 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('生成自定义模板出错,将全部使用默认模板进行创建组件!(The custom template generating occured error, all will be initializated 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 }; // component tpl var content_index = tpl.component_index(params); var content_interface = ts && tpl.component_interface(__assign(__assign({}, params), { cc: type === 'cc' })); var content_cc = type === 'cc' && tpl.component_class(params); var content_fc = type === 'fc' && tpl.component_functional(params); var content_readme = md === 'md' && tpl.component_readme(params); var content_mdx = md === 'mdx' && tpl.component_mdx(params); var content_stories = hasStorybook && tpl.component_stories(params); var content_style = stylesheet && tpl.component_style(params); var content_stylesheet = stylesheet && tpl.component_stylesheet(params); var content_test = test && tpl.component_test(params); var pathToFileContentMap = (_b = {}, _b["index." + (ts ? 'ts' : 'js')] = content_index, _b[componentName + "." + (ts ? 'tsx' : 'jsx')] = content_fc || content_cc, _b['interface.ts'] = content_interface, _b["style/index." + (ts ? 'ts' : 'js')] = content_style, _b["style/" + componentName + "." + stylesheet] = content_stylesheet, _b["__test__/index.test." + (ts ? 'tsx' : 'jsx')] = content_test, _b["__stories__/index.stories." + (ts ? 'tsx' : 'jsx')] = content_stories, _b['README.md'] = content_readme, _b['README.mdx'] = content_mdx, _b); /** * create files */ var file_path = function (p) { return path_1.default.resolve(newPath, p); }; for (var p in pathToFileContentMap) { utils_1.output_file({ file_path: file_path(p), file_content: pathToFileContentMap[p] }); } utils_1.logTime('创建组件', true); } exports.$new = $new; exports.default = $new;