UNPKG

21.2 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15 return new (P || (P = Promise))(function (resolve, reject) {
16 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19 step((generator = generator.apply(thisArg, _arguments || [])).next());
20 });
21};
22var __generator = (this && this.__generator) || function (thisArg, body) {
23 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25 function verb(n) { return function (v) { return step([n, v]); }; }
26 function step(op) {
27 if (f) throw new TypeError("Generator is already executing.");
28 while (_) try {
29 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;
30 if (y = 0, t) op = [op[0] & 2, t.value];
31 switch (op[0]) {
32 case 0: case 1: t = op; break;
33 case 4: _.label++; return { value: op[1], done: false };
34 case 5: _.label++; y = op[1]; op = [0]; continue;
35 case 7: op = _.ops.pop(); _.trys.pop(); continue;
36 default:
37 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41 if (t[2]) _.ops.pop();
42 _.trys.pop(); continue;
43 }
44 op = body.call(thisArg, _);
45 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47 }
48};
49var __spreadArrays = (this && this.__spreadArrays) || function () {
50 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
51 for (var r = Array(s), k = 0, i = 0; i < il; i++)
52 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
53 r[k] = a[j];
54 return r;
55};
56var __importDefault = (this && this.__importDefault) || function (mod) {
57 return (mod && mod.__esModule) ? mod : { "default": mod };
58};
59Object.defineProperty(exports, "__esModule", { value: true });
60var path_1 = __importDefault(require("path"));
61var utils_1 = require("@omni-door/utils");
62var templates_1 = require("./templates");
63var dependencies_1 = require("./configs/dependencies");
64var dependencies_stable_map_1 = require("./configs/dependencies_stable_map");
65function $init(_a) {
66 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 () {
67 utils_1.logErr('组件库项目安装失败!(The component-library project installation has been occured some error!)');
68 process.exit(1);
69 } : _g, _h = _a.success, success = _h === void 0 ? function () { return utils_1.logSuc('组件库项目安装完成!(The component-library project installation has been completed!)'); } : _h;
70 return __awaiter(this, void 0, void 0, function () {
71 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;
72 var _q;
73 return __generator(this, function (_r) {
74 // 模板解析
75 utils_1.logTime('模板解析');
76 custom_tpl_list = {};
77 try {
78 custom_tpl_list = typeof tpls === 'function'
79 ? tpls(templates_1.tpls_origin_init)
80 : custom_tpl_list;
81 _loop_1 = function (tpl_name) {
82 var name_1 = tpl_name;
83 var list = custom_tpl_list;
84 var tplFactory = function (customTpl, originTpl) {
85 return function (config) {
86 try {
87 var result = customTpl && customTpl(config);
88 if (typeof result === 'function') {
89 // call originTpl here is resolving the address reference bug
90 // because call the originTpl will affect the tpls_origin_init's tpl
91 var originResult = originTpl(config);
92 if (typeof originResult === 'function') {
93 return tplFactory(result, originResult);
94 }
95 }
96 return result;
97 }
98 catch (err) {
99 utils_1.logWarn(err);
100 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!)");
101 return originTpl(config);
102 }
103 };
104 };
105 list[name_1] = tplFactory(list[name_1], templates_1.tpls_init[name_1]);
106 };
107 for (tpl_name in custom_tpl_list) {
108 _loop_1(tpl_name);
109 }
110 }
111 catch (err_tpls) {
112 utils_1.logWarn(err_tpls);
113 utils_1.logWarn('生成自定义模板出错,将全部使用默认模板进行初始化!(The custom template generating occured error, all will be initializated with the default template!)');
114 }
115 tpl = __assign(__assign({}, templates_1.tpls_init), custom_tpl_list);
116 project_type = 'component-library-react';
117 utils_1.logTime('模板解析', true);
118 // 生成项目文件
119 utils_1.logTime('生成文件');
120 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 };
121 pathToFileContentMap = (_q = {},
122 // default files
123 _q["" + configFileName] = tpl.omni(__assign(__assign({}, params), { git: git, npm: npm, devServer: devServer })),
124 _q['package.json'] = install && tpl.pkj({
125 type_react: dependencies_stable_map_1.devDependencies['@types/react'],
126 project_name: project_name,
127 devServer: devServer
128 })(__assign(__assign({}, params), { install: install, dependencies: '', devDependencies: '' })),
129 _q['.gitignore'] = tpl.gitignore(params),
130 _q['.npmignore'] = tpl.npmignore(params),
131 _q["src/components/index." + (ts ? 'ts' : 'js')] = tpl.source_index(params),
132 _q['src/@types/global.d.ts'] = ts && tpl.source_d(params),
133 _q['tsconfig.json'] = ts && tpl.tsconfig(params),
134 _q['jest.config.js'] = test && tpl.jest(params),
135 // lint files
136 _q['.vscode/settings.json'] = tpl.vscode(params),
137 _q['.editorconfig'] = (eslint || prettier) && tpl.editor(params),
138 _q['.eslintrc.js'] = eslint && tpl.eslint(params),
139 _q['.eslintignore'] = eslint && tpl.eslintignore(params),
140 _q['prettier.config.js'] = prettier && tpl.prettier(params),
141 _q['.prettierignore'] = prettier && tpl.prettierignore(params),
142 _q['stylelint.config.js'] = stylelint && tpl.stylelint(params),
143 _q['commitlint.config.js'] = commitlint && tpl.commitlint(params),
144 _q['babel.config.js'] = (devServer === 'storybook' || devServer === 'styleguidist') && tpl.babel(params),
145 _q['README.md'] = tpl.readme(params),
146 // server files
147 _q['src/index.mdx'] = devServer === 'docz' && tpl.mdx(params),
148 _q['bisheng.config.js'] = devServer === 'bisheng' && tpl.bisheng(params),
149 _q['posts/README.md'] = devServer === 'bisheng' && tpl.posts_readme()(params),
150 _q['.storybook/addons.js'] = devServer === 'storybook' && tpl.storybook_addons(params),
151 _q['.storybook/config.js'] = devServer === 'storybook' && tpl.storybook_config(params),
152 _q['.storybook/manager-head.html'] = devServer === 'storybook' && tpl.storybook_mhead(params),
153 _q['.storybook/webpack.config.js'] = devServer === 'storybook' && tpl.storybook_webpack(params),
154 _q['doczrc.js'] = devServer === 'docz' && tpl.doczrc(params),
155 _q['gatsby-config.js'] = devServer === 'docz' && tpl.gatsby(params),
156 _q['styleguide.config.js'] = devServer === 'styleguidist' && tpl.styleguidist(__assign(__assign({}, params), { git: git })),
157 _q);
158 file_path = function (p) { return path_1.default.resolve(initPath, p); };
159 for (p in pathToFileContentMap) {
160 utils_1.output_file({
161 file_path: file_path(p),
162 file_content: pathToFileContentMap[p]
163 });
164 }
165 utils_1.logTime('生成文件', true);
166 // 项目依赖解析
167 utils_1.logTime('依赖解析');
168 installCliPrefix = pkgtool === 'yarn' ? pkgtool + " add --cwd " + initPath : pkgtool + " install --save --prefix " + initPath;
169 installDevCliPrefix = pkgtool === 'yarn' ? pkgtool + " add -D --cwd " + initPath : pkgtool + " install --save-dev --prefix " + initPath;
170 if (pkgtool === 'cnpm' && initPath !== process.cwd()) {
171 installCliPrefix = "cd " + initPath + " && " + installCliPrefix;
172 installDevCliPrefix = "cd " + initPath + " && " + installDevCliPrefix;
173 }
174 _j = dependencies_1.dependencies(strategy), depArr = _j.depArr, depStr = _j.depStr;
175 dependencies_str = depStr;
176 if (typeof dependencies_custom === 'function') {
177 result = dependencies_custom(depArr);
178 if (result instanceof Array) {
179 dependencies_str = depStr + " " + utils_1.arr2str(result);
180 }
181 else {
182 _k = result.add, add = _k === void 0 ? [] : _k, _l = result.remove, remove = _l === void 0 ? [] : _l;
183 _loop_2 = function (i) {
184 var item_rm = remove[i];
185 depArr = __spreadArrays(utils_1.intersection(depArr, depArr.filter(function (v) { return v !== item_rm; })));
186 };
187 for (i = 0; i < remove.length; i++) {
188 _loop_2(i);
189 }
190 dependencies_str = utils_1.arr2str(depArr) + " " + utils_1.arr2str(add);
191 }
192 }
193 installCli = dependencies_str ? installCliPrefix + " " + dependencies_str : '';
194 _m = dependencies_1.devDependencies(strategy, {
195 ts: ts,
196 eslint: eslint,
197 prettier: prettier,
198 commitlint: commitlint,
199 style: style,
200 stylelint: stylelint,
201 test: test,
202 devServer: devServer
203 }), 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;
204 if (typeof devDependencies_custom === 'function') {
205 result = devDependencies_custom(devDepArr);
206 if (result instanceof Array) {
207 customDepStr = utils_1.arr2str(result);
208 }
209 else {
210 _o = result.add, add = _o === void 0 ? [] : _o, _p = result.remove, remove = _p === void 0 ? [] : _p;
211 _loop_3 = function (i) {
212 var item_rm = remove[i];
213 defaultDepArr = __spreadArrays(utils_1.intersection(defaultDepArr, defaultDepArr.filter(function (v) { return v !== item_rm; })));
214 tsDepArr = __spreadArrays(utils_1.intersection(tsDepArr, tsDepArr.filter(function (v) { return v !== item_rm; })));
215 testDepArr = __spreadArrays(utils_1.intersection(testDepArr, testDepArr.filter(function (v) { return v !== item_rm; })));
216 eslintDepArr = __spreadArrays(utils_1.intersection(eslintDepArr, eslintDepArr.filter(function (v) { return v !== item_rm; })));
217 prettierDepArr = __spreadArrays(utils_1.intersection(prettierDepArr, prettierDepArr.filter(function (v) { return v !== item_rm; })));
218 commitlintDepArr = __spreadArrays(utils_1.intersection(commitlintDepArr, commitlintDepArr.filter(function (v) { return v !== item_rm; })));
219 stylelintDepArr = __spreadArrays(utils_1.intersection(stylelintDepArr, stylelintDepArr.filter(function (v) { return v !== item_rm; })));
220 devServerDepArr = __spreadArrays(utils_1.intersection(devServerDepArr, devServerDepArr.filter(function (v) { return v !== item_rm; })));
221 buildDepArr = __spreadArrays(utils_1.intersection(buildDepArr, buildDepArr.filter(function (v) { return v !== item_rm; })));
222 };
223 for (i = 0; i < remove.length; i++) {
224 _loop_3(i);
225 }
226 defaultDepStr = utils_1.arr2str(defaultDepArr);
227 tsDepStr = utils_1.arr2str(tsDepArr);
228 testDepStr = utils_1.arr2str(testDepArr);
229 eslintDepStr = utils_1.arr2str(eslintDepArr);
230 prettierDepStr = utils_1.arr2str(prettierDepArr);
231 commitlintDepStr = utils_1.arr2str(commitlintDepArr);
232 stylelintDepStr = utils_1.arr2str(stylelintDepArr);
233 devServerDepStr = utils_1.arr2str(devServerDepArr);
234 buildDepStr = utils_1.arr2str(buildDepArr);
235 customDepStr = utils_1.arr2str(add);
236 }
237 }
238 installDevCli = defaultDepStr ? installDevCliPrefix + " " + defaultDepStr : '';
239 installTsDevCli = tsDepStr ? installDevCliPrefix + " " + tsDepStr : '';
240 installTestDevCli = testDepStr ? installDevCliPrefix + " " + testDepStr : '';
241 installEslintDevCli = eslintDepStr ? installDevCliPrefix + " " + eslintDepStr : '';
242 installPrettierDevCli = prettierDepStr ? installDevCliPrefix + " " + prettierDepStr : '';
243 installCommitlintDevCli = commitlintDepStr ? installDevCliPrefix + " " + commitlintDepStr : '';
244 installStylelintDevCli = stylelintDepStr ? installDevCliPrefix + " " + stylelintDepStr : '';
245 installServerDevCli = devServerDepStr ? installDevCliPrefix + " " + devServerDepStr : '';
246 installBuildDevCli = buildDepStr ? installDevCliPrefix + " " + buildDepStr : '';
247 installCustomDevCli = customDepStr ? installDevCliPrefix + " " + customDepStr : '';
248 utils_1.logTime('依赖解析', true);
249 if (install) {
250 // 项目依赖安装
251 utils_1.logTime('安装依赖');
252 utils_1.exec([
253 installCli,
254 installDevCli,
255 installTsDevCli,
256 installTestDevCli,
257 installEslintDevCli,
258 installPrettierDevCli,
259 installCommitlintDevCli,
260 installStylelintDevCli,
261 installServerDevCli,
262 installBuildDevCli,
263 installCustomDevCli
264 ], function (res) {
265 utils_1.logTime('安装依赖', true);
266 success(res);
267 }, error, isSlient);
268 }
269 else {
270 utils_1.logTime('生成静态依赖文件');
271 processDepStr = function (str, prefix) {
272 if (!str)
273 return '';
274 var result = '';
275 var arr = str.split(' ').filter(function (v) { return !!v; });
276 for (var i = 0; i < arr.length; i++) {
277 var item = arr[i];
278 if (!item)
279 continue;
280 var lastInd = item.lastIndexOf('@');
281 var name_2 = lastInd === 0 ? item : item.substr(0, lastInd);
282 var version = lastInd === 0 ? 'latest' : item.substr(lastInd + 1);
283 if (i + 1 === arr.length) {
284 result += " \"" + name_2 + "\": \"" + version + "\"";
285 }
286 else {
287 result += " \"" + name_2 + "\": \"" + version + "\",\n";
288 }
289 }
290 return "\"" + prefix + "\": {\n" + result + "\n },";
291 };
292 utils_1.output_file({
293 file_path: path_1.default.resolve(initPath, 'package.json'),
294 file_content: tpl.pkj({
295 type_react: dependencies_stable_map_1.devDependencies['@types/react'],
296 project_name: project_name,
297 devServer: devServer
298 })(__assign(__assign({}, params), { install: install, dependencies: processDepStr(dependencies_str, 'dependencies'), devDependencies: processDepStr((defaultDepStr || '') + " " + (tsDepStr || '') + " " + (testDepStr || '') + " " + (eslintDepStr || '') + " " + (prettierDepStr || '') + " " + (commitlintDepStr || '') + " " + (stylelintDepStr || '') + " " + (devServerDepStr || '') + " " + (buildDepStr || '') + " " + (customDepStr || ''), 'devDependencies') }))
299 });
300 utils_1.logTime('生成静态依赖文件', true);
301 success([]);
302 }
303 return [2 /*return*/];
304 });
305 });
306}
307exports.$init = $init;
308exports.default = $init;