UNPKG

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

Version:
309 lines (308 loc) 21.2 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 utils_1 = require("@omni-door/utils"); var templates_1 = require("./templates"); var dependencies_1 = require("./configs/dependencies"); var dependencies_stable_map_1 = require("./configs/dependencies_stable_map"); function $init(_a) { var _b = _a.strategy, strategy = _b === void 0 ? 'stable' : _b, project_name = _a.projectName, initPath = _a.initPath, _c = _a.configFileName, configFileName = _c === void 0 ? 'omni.config.js' : _c, _d = _a.git, git = _d === void 0 ? '' : _d, _e = _a.npm, npm = _e === void 0 ? '' : _e, devServer = _a.devServer, ts = _a.ts, test = _a.test, eslint = _a.eslint, prettier = _a.prettier, commitlint = _a.commitlint, style = _a.style, stylelint = _a.stylelint, install = _a.install, tpls = _a.tpls, _f = _a.pkgtool, pkgtool = _f === void 0 ? 'yarn' : _f, isSlient = _a.isSlient, dependencies_custom = _a.dependencies, devDependencies_custom = _a.devDependencies, _g = _a.error, error = _g === void 0 ? function () { utils_1.logErr('组件库项目安装失败!(The component-library project installation has been occured some error!)'); process.exit(1); } : _g, _h = _a.success, success = _h === void 0 ? function () { return utils_1.logSuc('组件库项目安装完成!(The component-library project installation has been completed!)'); } : _h; return __awaiter(this, void 0, void 0, function () { var custom_tpl_list, _loop_1, tpl_name, tpl, project_type, params, pathToFileContentMap, file_path, p, installCliPrefix, installDevCliPrefix, _j, depArr, depStr, dependencies_str, result, _k, add, _l, remove, _loop_2, i, installCli, _m, defaultDepArr, defaultDepStr, tsDepArr, tsDepStr, testDepStr, testDepArr, eslintDepArr, eslintDepStr, prettierDepArr, prettierDepStr, commitlintDepArr, commitlintDepStr, stylelintDepArr, stylelintDepStr, devServerDepArr, devServerDepStr, buildDepArr, buildDepStr, devDepArr, customDepStr, result, _o, add, _p, remove, _loop_3, i, installDevCli, installTsDevCli, installTestDevCli, installEslintDevCli, installPrettierDevCli, installCommitlintDevCli, installStylelintDevCli, installServerDevCli, installBuildDevCli, installCustomDevCli, processDepStr; var _q; return __generator(this, function (_r) { // 模板解析 utils_1.logTime('模板解析'); custom_tpl_list = {}; try { custom_tpl_list = typeof tpls === 'function' ? tpls(templates_1.tpls_origin_init) : custom_tpl_list; _loop_1 = function (tpl_name) { var name_1 = tpl_name; var list = custom_tpl_list; var tplFactory = function (customTpl, originTpl) { return function (config) { try { var result = customTpl && customTpl(config); if (typeof result === 'function') { // call originTpl here is resolving the address reference bug // because call the originTpl will affect the tpls_origin_init's tpl var originResult = originTpl(config); if (typeof originResult === 'function') { return tplFactory(result, originResult); } } return result; } 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\u521D\u59CB\u5316\uFF01(The custom template [" + name_1 + "] parsing occured error, the default template will be used for initialization!)"); return originTpl(config); } }; }; list[name_1] = tplFactory(list[name_1], templates_1.tpls_init[name_1]); }; for (tpl_name in custom_tpl_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!)'); } tpl = __assign(__assign({}, templates_1.tpls_init), custom_tpl_list); project_type = 'component-library-react'; utils_1.logTime('模板解析', true); // 生成项目文件 utils_1.logTime('生成文件'); params = { project_type: project_type, project_name: project_name, ts: ts, test: test, eslint: eslint, prettier: prettier, commitlint: commitlint, style: style, stylelint: stylelint, strategy: strategy, configFileName: configFileName }; pathToFileContentMap = (_q = {}, // default files _q["" + configFileName] = tpl.omni(__assign(__assign({}, params), { git: git, npm: npm, devServer: devServer })), _q['package.json'] = install && tpl.pkj({ type_react: dependencies_stable_map_1.devDependencies['@types/react'], project_name: project_name, devServer: devServer })(__assign(__assign({}, params), { install: install, dependencies: '', devDependencies: '' })), _q['.gitignore'] = tpl.gitignore(params), _q['.npmignore'] = tpl.npmignore(params), _q["src/components/index." + (ts ? 'ts' : 'js')] = tpl.source_index(params), _q['src/@types/global.d.ts'] = ts && tpl.source_d(params), _q['tsconfig.json'] = ts && tpl.tsconfig(params), _q['jest.config.js'] = test && tpl.jest(params), // lint files _q['.vscode/settings.json'] = tpl.vscode(params), _q['.editorconfig'] = (eslint || prettier) && tpl.editor(params), _q['.eslintrc.js'] = eslint && tpl.eslint(params), _q['.eslintignore'] = eslint && tpl.eslintignore(params), _q['prettier.config.js'] = prettier && tpl.prettier(params), _q['.prettierignore'] = prettier && tpl.prettierignore(params), _q['stylelint.config.js'] = stylelint && tpl.stylelint(params), _q['commitlint.config.js'] = commitlint && tpl.commitlint(params), _q['babel.config.js'] = (devServer === 'storybook' || devServer === 'styleguidist') && tpl.babel(params), _q['README.md'] = tpl.readme(params), // server files _q['src/index.mdx'] = devServer === 'docz' && tpl.mdx(params), _q['bisheng.config.js'] = devServer === 'bisheng' && tpl.bisheng(params), _q['posts/README.md'] = devServer === 'bisheng' && tpl.posts_readme()(params), _q['.storybook/addons.js'] = devServer === 'storybook' && tpl.storybook_addons(params), _q['.storybook/config.js'] = devServer === 'storybook' && tpl.storybook_config(params), _q['.storybook/manager-head.html'] = devServer === 'storybook' && tpl.storybook_mhead(params), _q['.storybook/webpack.config.js'] = devServer === 'storybook' && tpl.storybook_webpack(params), _q['doczrc.js'] = devServer === 'docz' && tpl.doczrc(params), _q['gatsby-config.js'] = devServer === 'docz' && tpl.gatsby(params), _q['styleguide.config.js'] = devServer === 'styleguidist' && tpl.styleguidist(__assign(__assign({}, params), { git: git })), _q); file_path = function (p) { return path_1.default.resolve(initPath, p); }; for (p in pathToFileContentMap) { utils_1.output_file({ file_path: file_path(p), file_content: pathToFileContentMap[p] }); } utils_1.logTime('生成文件', true); // 项目依赖解析 utils_1.logTime('依赖解析'); 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; } _j = dependencies_1.dependencies(strategy), depArr = _j.depArr, depStr = _j.depStr; dependencies_str = depStr; if (typeof dependencies_custom === 'function') { result = dependencies_custom(depArr); if (result instanceof Array) { dependencies_str = depStr + " " + utils_1.arr2str(result); } else { _k = result.add, add = _k === void 0 ? [] : _k, _l = result.remove, remove = _l === void 0 ? [] : _l; _loop_2 = function (i) { var item_rm = remove[i]; depArr = __spreadArrays(utils_1.intersection(depArr, depArr.filter(function (v) { return v !== item_rm; }))); }; for (i = 0; i < remove.length; i++) { _loop_2(i); } dependencies_str = utils_1.arr2str(depArr) + " " + utils_1.arr2str(add); } } installCli = dependencies_str ? installCliPrefix + " " + dependencies_str : ''; _m = dependencies_1.devDependencies(strategy, { ts: ts, eslint: eslint, prettier: prettier, commitlint: commitlint, style: style, stylelint: stylelint, test: test, devServer: devServer }), defaultDepArr = _m.defaultDepArr, defaultDepStr = _m.defaultDepStr, tsDepArr = _m.tsDepArr, tsDepStr = _m.tsDepStr, testDepStr = _m.testDepStr, testDepArr = _m.testDepArr, eslintDepArr = _m.eslintDepArr, eslintDepStr = _m.eslintDepStr, prettierDepArr = _m.prettierDepArr, prettierDepStr = _m.prettierDepStr, commitlintDepArr = _m.commitlintDepArr, commitlintDepStr = _m.commitlintDepStr, stylelintDepArr = _m.stylelintDepArr, stylelintDepStr = _m.stylelintDepStr, devServerDepArr = _m.devServerDepArr, devServerDepStr = _m.devServerDepStr, buildDepArr = _m.buildDepArr, buildDepStr = _m.buildDepStr, devDepArr = _m.devDepArr; if (typeof devDependencies_custom === 'function') { result = devDependencies_custom(devDepArr); if (result instanceof Array) { customDepStr = utils_1.arr2str(result); } else { _o = result.add, add = _o === void 0 ? [] : _o, _p = result.remove, remove = _p === void 0 ? [] : _p; _loop_3 = function (i) { var item_rm = remove[i]; defaultDepArr = __spreadArrays(utils_1.intersection(defaultDepArr, defaultDepArr.filter(function (v) { return v !== item_rm; }))); tsDepArr = __spreadArrays(utils_1.intersection(tsDepArr, tsDepArr.filter(function (v) { return v !== item_rm; }))); testDepArr = __spreadArrays(utils_1.intersection(testDepArr, testDepArr.filter(function (v) { return v !== item_rm; }))); eslintDepArr = __spreadArrays(utils_1.intersection(eslintDepArr, eslintDepArr.filter(function (v) { return v !== item_rm; }))); prettierDepArr = __spreadArrays(utils_1.intersection(prettierDepArr, prettierDepArr.filter(function (v) { return v !== item_rm; }))); commitlintDepArr = __spreadArrays(utils_1.intersection(commitlintDepArr, commitlintDepArr.filter(function (v) { return v !== item_rm; }))); stylelintDepArr = __spreadArrays(utils_1.intersection(stylelintDepArr, stylelintDepArr.filter(function (v) { return v !== item_rm; }))); devServerDepArr = __spreadArrays(utils_1.intersection(devServerDepArr, devServerDepArr.filter(function (v) { return v !== item_rm; }))); buildDepArr = __spreadArrays(utils_1.intersection(buildDepArr, buildDepArr.filter(function (v) { return v !== item_rm; }))); }; for (i = 0; i < remove.length; i++) { _loop_3(i); } defaultDepStr = utils_1.arr2str(defaultDepArr); tsDepStr = utils_1.arr2str(tsDepArr); testDepStr = utils_1.arr2str(testDepArr); eslintDepStr = utils_1.arr2str(eslintDepArr); prettierDepStr = utils_1.arr2str(prettierDepArr); commitlintDepStr = utils_1.arr2str(commitlintDepArr); stylelintDepStr = utils_1.arr2str(stylelintDepArr); devServerDepStr = utils_1.arr2str(devServerDepArr); buildDepStr = utils_1.arr2str(buildDepArr); customDepStr = utils_1.arr2str(add); } } installDevCli = defaultDepStr ? installDevCliPrefix + " " + defaultDepStr : ''; installTsDevCli = tsDepStr ? installDevCliPrefix + " " + tsDepStr : ''; installTestDevCli = testDepStr ? installDevCliPrefix + " " + testDepStr : ''; installEslintDevCli = eslintDepStr ? installDevCliPrefix + " " + eslintDepStr : ''; installPrettierDevCli = prettierDepStr ? installDevCliPrefix + " " + prettierDepStr : ''; installCommitlintDevCli = commitlintDepStr ? installDevCliPrefix + " " + commitlintDepStr : ''; installStylelintDevCli = stylelintDepStr ? installDevCliPrefix + " " + stylelintDepStr : ''; installServerDevCli = devServerDepStr ? installDevCliPrefix + " " + devServerDepStr : ''; installBuildDevCli = buildDepStr ? installDevCliPrefix + " " + buildDepStr : ''; installCustomDevCli = customDepStr ? installDevCliPrefix + " " + customDepStr : ''; utils_1.logTime('依赖解析', true); if (install) { // 项目依赖安装 utils_1.logTime('安装依赖'); utils_1.exec([ installCli, installDevCli, installTsDevCli, installTestDevCli, installEslintDevCli, installPrettierDevCli, installCommitlintDevCli, installStylelintDevCli, installServerDevCli, installBuildDevCli, installCustomDevCli ], function (res) { utils_1.logTime('安装依赖', true); success(res); }, error, isSlient); } else { utils_1.logTime('生成静态依赖文件'); processDepStr = function (str, prefix) { if (!str) return ''; var result = ''; var arr = str.split(' ').filter(function (v) { return !!v; }); for (var i = 0; i < arr.length; i++) { var item = arr[i]; if (!item) continue; var lastInd = item.lastIndexOf('@'); var name_2 = lastInd === 0 ? item : item.substr(0, lastInd); var version = lastInd === 0 ? 'latest' : item.substr(lastInd + 1); if (i + 1 === arr.length) { result += " \"" + name_2 + "\": \"" + version + "\""; } else { result += " \"" + name_2 + "\": \"" + version + "\",\n"; } } return "\"" + prefix + "\": {\n" + result + "\n },"; }; utils_1.output_file({ file_path: path_1.default.resolve(initPath, 'package.json'), file_content: tpl.pkj({ type_react: dependencies_stable_map_1.devDependencies['@types/react'], project_name: project_name, devServer: devServer })(__assign(__assign({}, params), { install: install, dependencies: processDepStr(dependencies_str, 'dependencies'), devDependencies: processDepStr((defaultDepStr || '') + " " + (tsDepStr || '') + " " + (testDepStr || '') + " " + (eslintDepStr || '') + " " + (prettierDepStr || '') + " " + (commitlintDepStr || '') + " " + (stylelintDepStr || '') + " " + (devServerDepStr || '') + " " + (buildDepStr || '') + " " + (customDepStr || ''), 'devDependencies') })) }); utils_1.logTime('生成静态依赖文件', true); success([]); } return [2 /*return*/]; }); }); } exports.$init = $init; exports.default = $init;