UNPKG

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

Version:
363 lines (362 loc) 22.3 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; 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 tpl_utils_1 = require("@omni-door/tpl-utils"); var templates_1 = require("./templates"); var dependencies_1 = require("./configs/dependencies"); var default_tpl_list = { babel: templates_1.babel, commitlint: templates_1.commitlint, eslint: templates_1.eslint, eslintignore: templates_1.eslintignore, gitignore: templates_1.gitignore, jest: templates_1.jest, npmignore: templates_1.npmignore, omni: templates_1.omni, pkj: templates_1.pkj, readme: templates_1.readme, stylelint: templates_1.stylelint, tsconfig: templates_1.tsconfig, source_index: templates_1.source_index, source_d: templates_1.source_d, storybook_addons: templates_1.storybook_addons, storybook_config: templates_1.storybook_config, storybook_mhead: templates_1.storybook_mhead, storybook_webpack: templates_1.storybook_webpack, doczrc: templates_1.doczrc, bisheng: templates_1.bisheng, posts_readme: templates_1.posts_readme, mdx: templates_1.mdx, component_class: templates_1.component_class, component_functional: templates_1.component_functional, component_index: templates_1.component_index, component_readme: templates_1.component_readme, component_stylesheet: templates_1.component_stylesheet, component_test: templates_1.component_test, component_mdx: templates_1.component_mdx, component_stories: templates_1.component_stories }; function init(_a) { var _b = _a.strategy, strategy = _b === void 0 ? 'stable' : _b, name = _a.projectName, initPath = _a.initPath, _c = _a.configFileName, configFileName = _c === void 0 ? 'omni.config.js' : _c, devServer = _a.devServer, ts = _a.ts, test = _a.test, eslint = _a.eslint, commitlint = _a.commitlint, style = _a.style, stylelint = _a.stylelint, tpls = _a.tpls, _d = _a.pkgtool, pkgtool = _d === void 0 ? 'yarn' : _d, isSlient = _a.isSlient, dependencies_custom = _a.dependencies, devDependencies_custom = _a.devDependencies, _e = _a.error, error = _e === void 0 ? function () { tpl_utils_1.logErr('组件库项目初始化失败!(The component-library project initialization has been occured some error!)'); process.exit(1); } : _e, _f = _a.success, success = _f === void 0 ? function () { return tpl_utils_1.logSuc('组件库项目初始化完成!(The component-library project initialization has been completed!)'); } : _f; return __awaiter(this, void 0, void 0, function () { var custom_tpl_list, _loop_1, tpl_name, tpl, project_type, content_omni, content_pkg, content_gitignore, content_indexTpl, content_ts, content_d, content_jest, content_eslintrc, content_eslintignore, content_stylelint, content_commitlint, content_babel, content_bisheng, content_postReadMe, content_storybook_addons, content_storybook_config, content_storybook_mhead, content_storybook_webpack, content_doczrc, content_doczmdx, content_readMe, pathToFileContentMap, file_path, p, installCliPrefix, installDevCliPrefix, _g, depArr, depStr, dependencies_str, result, _h, add, _j, remove, _loop_2, i, installCli, _k, defaultDepArr, defaultDepStr, tsDepArr, tsDepStr, testDepStr, testDepArr, eslintDepArr, eslintDepStr, commitlintDepArr, commitlintDepStr, stylelintDepArr, stylelintDepStr, devServerDepArr, devServerDepStr, devDepArr, customDepStr, result, _l, add, _m, remove, _loop_3, i, installDevCli, installTsDevCli, installTestDevCli, installEslintDevCli, installCommitlintDevCli, installStylelintDevCli, installServerDevCli, installCustomDevCli; var _o; return __generator(this, function (_p) { custom_tpl_list = {}; try { custom_tpl_list = typeof tpls === 'function' ? tpls(default_tpl_list) : custom_tpl_list; _loop_1 = function (tpl_name) { var name_1 = tpl_name; var list = custom_tpl_list; var tpl_1 = list[name_1]; var tplFactory = function (config) { try { return tpl_1 && tpl_1(config); } catch (err) { tpl_utils_1.logWarn(JSON.stringify(err)); tpl_utils_1.logWarn("\u81EA\u5B9A\u4E49\u6A21\u677F [" + name_1 + "] \u89E3\u6790\u51FA\u9519\uFF0C\u5C06\u4F7F\u7528\u9ED8\u8BA4\u6A21\u677F\u8FDB\u884C\u521D\u59CB\u5316\uFF01(The custom template [" + name_1 + "] parsing occured error, the default template will be used for initialization!)"); } return default_tpl_list[name_1](config); }; list[name_1] = tplFactory; }; for (tpl_name in custom_tpl_list) { _loop_1(tpl_name); } } catch (err_tpls) { tpl_utils_1.logWarn(JSON.stringify(err_tpls)); tpl_utils_1.logWarn('生成自定义模板出错,将全部使用默认模板进行初始化!(The custom template generating occured error, all will be initializated with the default template!)'); } tpl = __assign(__assign({}, default_tpl_list), custom_tpl_list); project_type = 'component-library-react'; content_omni = tpl.omni({ project_type: project_type, ts: ts, test: test, eslint: eslint, commitlint: commitlint, style: style, stylelint: stylelint, mdx: devServer === 'docz' }); content_pkg = tpl.pkj({ project_type: project_type, name: name, ts: ts, devServer: devServer, test: test, eslint: eslint, commitlint: commitlint, stylelint: stylelint, strategy: strategy }); content_gitignore = tpl.gitignore(); content_indexTpl = tpl.source_index(); content_ts = ts && tpl.tsconfig(); content_d = ts && tpl.source_d({ style: style }); content_jest = test && tpl.jest({ ts: ts }); content_eslintrc = eslint && tpl.eslint({ ts: ts }); content_eslintignore = eslint && tpl.eslintignore(); content_stylelint = stylelint && tpl.stylelint({ style: style }); content_commitlint = commitlint && tpl.commitlint({ name: name }); content_babel = devServer === 'storybook' && tpl.babel({ ts: ts }); content_bisheng = devServer === 'bisheng' && tpl.bisheng({ name: name }); content_postReadMe = devServer === 'bisheng' && tpl.posts_readme(); content_storybook_addons = devServer === 'storybook' && tpl.storybook_addons(); content_storybook_config = devServer === 'storybook' && tpl.storybook_config({ name: name }); content_storybook_mhead = devServer === 'storybook' && tpl.storybook_mhead({ name: name }); content_storybook_webpack = devServer === 'storybook' && tpl.storybook_webpack({ ts: ts, style: style }); content_doczrc = devServer === 'docz' && tpl.doczrc({ name: name, ts: ts, style: style }); content_doczmdx = devServer === 'docz' && tpl.mdx({ name: name }); content_readMe = tpl.readme({ name: name, configFileName: configFileName }); pathToFileContentMap = (_o = {}, _o["" + configFileName] = content_omni, _o['package.json'] = content_pkg, _o['.gitignore'] = content_gitignore, _o["src/components/index." + (ts ? 'ts' : 'js')] = content_indexTpl, _o['src/@types/global.d.ts'] = content_d, _o['tsconfig.json'] = content_ts, _o['jest.config.js'] = content_jest, _o['.eslintrc.js'] = content_eslintrc, _o['.eslintignore'] = content_eslintignore, _o['stylelint.config.js'] = content_stylelint, _o['commitlint.config.js'] = content_commitlint, _o['babel.config.js'] = content_babel, _o['README.md'] = content_readMe, _o['src/index.mdx'] = content_doczmdx, _o['bisheng.config.js'] = content_bisheng, _o['posts/README.md'] = content_postReadMe, _o['.storybook/addons.js'] = content_storybook_addons, _o['.storybook/config.js'] = content_storybook_config, _o['.storybook/manager-head.html'] = content_storybook_mhead, _o['.storybook/webpack.config.js'] = content_storybook_webpack, _o['doczrc.js'] = content_doczrc, _o); file_path = function (p) { return path_1.default.resolve(initPath, p); }; for (p in pathToFileContentMap) { tpl_utils_1.output_file({ file_path: file_path(p), file_content: pathToFileContentMap[p] }); } installCliPrefix = pkgtool === 'yarn' ? pkgtool + " add --cwd " + initPath : pkgtool + " install --save --prefix " + initPath; installDevCliPrefix = pkgtool === 'yarn' ? pkgtool + " add -D --cwd " + initPath : pkgtool + " install --save-dev --prefix " + initPath; if (pkgtool === 'cnpm' && initPath !== process.cwd()) { installCliPrefix = "cd " + initPath + " && " + installCliPrefix; installDevCliPrefix = "cd " + initPath + " && " + installDevCliPrefix; } _g = dependencies_1.dependencies(strategy), depArr = _g.depArr, depStr = _g.depStr; dependencies_str = depStr; if (typeof dependencies_custom === 'function') { result = dependencies_custom(depArr); if (result instanceof Array) { dependencies_str = depStr + " " + tpl_utils_1.arr2str(result); } else { _h = result.add, add = _h === void 0 ? [] : _h, _j = result.remove, remove = _j === void 0 ? [] : _j; _loop_2 = function (i) { var item_rm = remove[i]; depArr = __spreadArrays(tpl_utils_1.intersection(depArr, depArr.filter(function (v) { return v !== item_rm; }))); }; for (i = 0; i < remove.length; i++) { _loop_2(i); } dependencies_str = tpl_utils_1.arr2str(depArr) + " " + tpl_utils_1.arr2str(add); } } installCli = dependencies_str ? installCliPrefix + " " + dependencies_str : ''; _k = dependencies_1.devDependencies(strategy, { ts: ts, eslint: eslint, commitlint: commitlint, style: style, stylelint: stylelint, test: test, devServer: devServer }), defaultDepArr = _k.defaultDepArr, defaultDepStr = _k.defaultDepStr, tsDepArr = _k.tsDepArr, tsDepStr = _k.tsDepStr, testDepStr = _k.testDepStr, testDepArr = _k.testDepArr, eslintDepArr = _k.eslintDepArr, eslintDepStr = _k.eslintDepStr, commitlintDepArr = _k.commitlintDepArr, commitlintDepStr = _k.commitlintDepStr, stylelintDepArr = _k.stylelintDepArr, stylelintDepStr = _k.stylelintDepStr, devServerDepArr = _k.devServerDepArr, devServerDepStr = _k.devServerDepStr, devDepArr = _k.devDepArr; if (typeof devDependencies_custom === 'function') { result = devDependencies_custom(devDepArr); if (result instanceof Array) { customDepStr = tpl_utils_1.arr2str(result); } else { _l = result.add, add = _l === void 0 ? [] : _l, _m = result.remove, remove = _m === void 0 ? [] : _m; _loop_3 = function (i) { var item_rm = remove[i]; defaultDepArr = __spreadArrays(tpl_utils_1.intersection(defaultDepArr, defaultDepArr.filter(function (v) { return v !== item_rm; }))); tsDepArr = __spreadArrays(tpl_utils_1.intersection(tsDepArr, tsDepArr.filter(function (v) { return v !== item_rm; }))); testDepArr = __spreadArrays(tpl_utils_1.intersection(testDepArr, testDepArr.filter(function (v) { return v !== item_rm; }))); eslintDepArr = __spreadArrays(tpl_utils_1.intersection(eslintDepArr, eslintDepArr.filter(function (v) { return v !== item_rm; }))); commitlintDepArr = __spreadArrays(tpl_utils_1.intersection(commitlintDepArr, commitlintDepArr.filter(function (v) { return v !== item_rm; }))); stylelintDepArr = __spreadArrays(tpl_utils_1.intersection(stylelintDepArr, stylelintDepArr.filter(function (v) { return v !== item_rm; }))); devServerDepArr = __spreadArrays(tpl_utils_1.intersection(devServerDepArr, devServerDepArr.filter(function (v) { return v !== item_rm; }))); }; for (i = 0; i < remove.length; i++) { _loop_3(i); } defaultDepStr = tpl_utils_1.arr2str(defaultDepArr); tsDepStr = tpl_utils_1.arr2str(tsDepArr); testDepStr = tpl_utils_1.arr2str(testDepArr); eslintDepStr = tpl_utils_1.arr2str(eslintDepArr); commitlintDepStr = tpl_utils_1.arr2str(commitlintDepArr); stylelintDepStr = tpl_utils_1.arr2str(stylelintDepArr); devServerDepStr = tpl_utils_1.arr2str(devServerDepArr); customDepStr = tpl_utils_1.arr2str(add); } } installDevCli = defaultDepStr ? installDevCliPrefix + " " + defaultDepStr : ''; installTsDevCli = tsDepStr ? installDevCliPrefix + " " + tsDepStr : ''; installTestDevCli = testDepStr ? installDevCliPrefix + " " + testDepStr : ''; installEslintDevCli = eslintDepStr ? installDevCliPrefix + " " + eslintDepStr : ''; installCommitlintDevCli = commitlintDepStr ? installDevCliPrefix + " " + commitlintDepStr : ''; installStylelintDevCli = stylelintDepStr ? installDevCliPrefix + " " + stylelintDepStr : ''; installServerDevCli = devServerDepStr ? installDevCliPrefix + " " + devServerDepStr : ''; installCustomDevCli = customDepStr ? installDevCliPrefix + " " + customDepStr : ''; tpl_utils_1.exec([ installCli, installDevCli, installTsDevCli, installTestDevCli, installEslintDevCli, installCommitlintDevCli, installStylelintDevCli, installServerDevCli, installCustomDevCli ], success, error, isSlient); return [2 /*return*/]; }); }); } function newTpl(_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; tpl_utils_1.logInfo("\u5F00\u59CB\u521B\u5EFA " + componentName + " " + (type === 'cc' ? '类' : '函数') + "\u7EC4\u4EF6 (Start create " + componentName + " " + (type === 'cc' ? 'class' : 'functional') + " component)"); var custom_tpl_list = {}; try { custom_tpl_list = typeof tpls === 'function' ? tpls(default_tpl_list) : custom_tpl_list; var _loop_4 = function (tpl_name) { var name_2 = tpl_name; var list = custom_tpl_list; var tpl_2 = list[name_2]; var tplFactory = function (config) { try { return tpl_2 && tpl_2(config); } catch (err) { tpl_utils_1.logWarn(JSON.stringify(err)); tpl_utils_1.logWarn("\u81EA\u5B9A\u4E49\u6A21\u677F [" + name_2 + "] \u89E3\u6790\u51FA\u9519\uFF0C\u5C06\u4F7F\u7528\u9ED8\u8BA4\u6A21\u677F\u8FDB\u884C\u521B\u5EFA\u7EC4\u4EF6\uFF01(The custom template [" + name_2 + "] parsing occured error, the default template will be used for initialization!)"); } return default_tpl_list[name_2](config); }; list[name_2] = tplFactory; }; for (var tpl_name in custom_tpl_list) { _loop_4(tpl_name); } } catch (err_tpls) { tpl_utils_1.logWarn(JSON.stringify(err_tpls)); tpl_utils_1.logWarn('生成自定义模板出错,将全部使用默认模板进行创建组件!(The custom template generating occured error, all will be initializated with the default template!)'); } var tpl = __assign(__assign({}, default_tpl_list), custom_tpl_list); // component tpl var content_index = tpl.component_index({ ts: ts, componentName: componentName }); var content_cc = type === 'cc' && tpl.component_class({ ts: ts, componentName: componentName, style: stylesheet }); var content_fc = type === 'fc' && tpl.component_functional({ ts: ts, componentName: componentName, style: stylesheet }); var content_readme = md === 'md' && tpl.component_readme({ componentName: componentName }); var content_mdx = md === 'mdx' && tpl.component_mdx({ componentName: componentName }); var content_stories = hasStorybook && tpl.component_stories({ componentName: componentName }); var content_style = stylesheet && tpl.component_stylesheet({ componentName: componentName }); var content_test = test && tpl.component_test({ componentName: componentName }); var pathToFileContentMap = (_b = {}, _b["index." + (ts ? 'ts' : 'js')] = content_index, _b[componentName + "." + (ts ? 'tsx' : 'jsx')] = content_fc || content_cc, _b["style/" + componentName + "." + stylesheet] = content_style, _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) { tpl_utils_1.output_file({ file_path: file_path(p), file_content: pathToFileContentMap[p] }); } } exports.newTpl = newTpl; exports.default = init;