UNPKG

@omni-door/tpl-ssr-react

Version:
105 lines (104 loc) 4.5 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 }); 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, type = _a.type, tpls = _a.tpls; utils_1.logTime('CREATE(创建组件)'); utils_1.logInfo("Start create " + componentName + " " + (type === 'cc' ? 'class' : 'functional') + " component"); utils_1.logInfo("\u5F00\u59CB\u521B\u5EFA " + componentName + " " + (type === 'cc' ? '类' : '函数') + "\u7EC4\u4EF6"); 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 occured error, the default template will be used for initialization"); 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"); } 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'); utils_1.logWarn('生成自定义模板出错,将全部使用默认模板进行创建组件'); } 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_cc = type === 'cc' && tpl.component_class(params); var content_fc = type === 'fc' && tpl.component_functional(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 + "." + (ts ? 'tsx' : 'jsx')] = content_fc || content_cc, _b["style/" + componentName + ".module." + stylesheet] = content_style, _b["__test__/index.test." + (ts ? 'tsx' : 'jsx')] = 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('The process of create component failed'); utils_1.logErr('创建组件失败'); process.exit(1); } utils_1.logTime('CREATE(创建组件)', true); } exports.$new = $new; exports.default = $new;