UNPKG

26.6 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 });
60exports.$init = void 0;
61var path_1 = __importDefault(require("path"));
62var utils_1 = require("@omni-door/utils");
63var templates_1 = require("./templates");
64var dependencies_1 = require("./configs/dependencies");
65var dependencies_stable_map_1 = require("./configs/dependencies_stable_map");
66function $init(_a) {
67 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, 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, _e = _a.pkgtool, pkgtool = _e === void 0 ? 'pnpm' : _e, isSlient = _a.isSlient, _f = _a.ssrServer, ssrServer = _f === void 0 ? 'next' : _f, tag = _a.tag, dependencies_custom = _a.dependencies, devDependencies_custom = _a.devDependencies, _g = _a.error, error = _g === void 0 ? function () { return process.exit(1); } : _g, _h = _a.success, success = _h === void 0 ? function () { } : _h;
68 return __awaiter(this, void 0, void 0, function () {
69 var installCliPrefix, installDevCliPrefix, installReadMe, runScript, paramScript, custom_tpl_list, _loop_1, tpl_name, tpl, project_type, params, suffix_stylesheet, pathToFileContentMap, file_path, p, err, dependenciesOptions, _j, depArr, depStr, dependencies_str, result, _k, add, _l, remove, _loop_2, i, installCli, _m, defaultDepArr, defaultDepStr, nextDepArr, nextDepStr, tsDepArr, tsDepStr, testDepStr, testDepArr, eslintDepArr, eslintDepStr, prettierDepArr, prettierDepStr, commitlintDepArr, commitlintDepStr, stylelintDepArr, stylelintDepStr, serverDepArr, serverDepStr, devDepArr, customDepStr, result, _o, add, _p, remove, _loop_3, i, installDevCliArr, installDevCli, processDepStr;
70 var _q;
71 return __generator(this, function (_r) {
72 switch (_r.label) {
73 case 0:
74 switch (pkgtool) {
75 case 'pnpm':
76 installCliPrefix = pkgtool + " add -P --save-exact --prefix " + initPath;
77 installDevCliPrefix = pkgtool + " add -D --save-exact --prefix " + initPath;
78 installReadMe = pkgtool + " install";
79 runScript = "" + pkgtool;
80 paramScript = '-- -';
81 break;
82 case 'yarn':
83 installCliPrefix = pkgtool + " add --cwd " + initPath;
84 installDevCliPrefix = pkgtool + " add -D --cwd " + initPath;
85 installReadMe = "" + pkgtool;
86 runScript = "" + pkgtool;
87 paramScript = '-';
88 break;
89 case 'npm':
90 default:
91 installCliPrefix = pkgtool + " install --save --save-exact --prefix " + initPath;
92 installDevCliPrefix = pkgtool + " install --save-dev --save-exact --prefix " + initPath;
93 installReadMe = pkgtool + " install";
94 runScript = pkgtool + " run";
95 paramScript = '-- -';
96 }
97 // 模板解析
98 utils_1.logTime('PARSE(模板解析)');
99 custom_tpl_list = {};
100 try {
101 custom_tpl_list = typeof tpls === 'function'
102 ? tpls(templates_1.tpls_origin_init)
103 : custom_tpl_list;
104 _loop_1 = function (tpl_name) {
105 var name_1 = tpl_name;
106 var list = custom_tpl_list;
107 var tplFactory = function (customTpl, originTpl) {
108 return function (config) {
109 try {
110 var result = customTpl && customTpl(config);
111 if (typeof result === 'function') {
112 // call originTpl here is resolving the address reference bug
113 // because call the originTpl will affect the tpls_origin_init's tpl
114 var originResult = originTpl(config);
115 if (typeof originResult === 'function') {
116 return tplFactory(result, originResult);
117 }
118 }
119 return result;
120 }
121 catch (err) {
122 utils_1.logWarn(err);
123 utils_1.logWarn("The custom template \"" + name_1 + "\" parsing occured error, the default template will be used for initialization");
124 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");
125 return originTpl(config);
126 }
127 };
128 };
129 list[name_1] = tplFactory(list[name_1], templates_1.tpls_init[name_1]);
130 };
131 for (tpl_name in custom_tpl_list) {
132 _loop_1(tpl_name);
133 }
134 }
135 catch (err_tpls) {
136 utils_1.logWarn(err_tpls);
137 utils_1.logWarn('The custom template generating occured error, all will be initializated with the default template');
138 utils_1.logWarn('生成自定义模板出错,将全部使用默认模板进行初始化');
139 }
140 tpl = __assign(__assign({}, templates_1.tpls_init), custom_tpl_list);
141 project_type = 'ssr-react';
142 utils_1.logTime('PARSE(模板解析)', true);
143 // 生成项目文件
144 utils_1.logTime('CREATE(创建文件)');
145 params = { project_type: project_type, project_name: project_name, ts: ts, test: test, eslint: eslint, prettier: prettier, commitlint: commitlint, style: style, stylelint: !!style && stylelint, strategy: strategy, configFileName: configFileName, serverType: ssrServer };
146 try {
147 suffix_stylesheet = style && style === 'all' ? 'scss' : style;
148 pathToFileContentMap = (_q = {},
149 // default
150 _q["configs/" + configFileName] = tpl.omni(__assign(__assign({}, params), { git: git })),
151 _q['package.json'] = install && tpl.pkj(dependencies_stable_map_1.devDependencies['@types/react'])(__assign(__assign({}, params), { install: install, dependencies: '', devDependencies: '' })),
152 _q['.gitignore'] = tpl.gitignore(params),
153 _q["src/routes.js"] = ssrServer === 'koa-next' && tpl.source_routes(params),
154 _q["src/styles/reset." + suffix_stylesheet] = style && tpl.source_index_reset(params),
155 // pages
156 _q["pages/" + (ssrServer === 'koa-next' ? 'home' : 'index') + "." + (ts ? 'tsx' : 'jsx')] = tpl.source_page_index(__assign(__assign({}, params), { pageName: 'Home' })),
157 _q["pages/start." + (ts ? 'tsx' : 'jsx')] = tpl.source_page_index(__assign(__assign({}, params), { pageName: 'Start' })),
158 _q["pages/docs." + (ts ? 'tsx' : 'jsx')] = tpl.source_page_index(__assign(__assign({}, params), { pageName: 'Docs' })),
159 _q["pages/_app." + (ts ? 'tsx' : 'jsx')] = tpl.source_page_app(params),
160 // components - Home
161 _q["src/components/Home/index." + (ts ? 'ts' : 'js')] = tpl.source_component_index(__assign(__assign({}, params), { componentName: 'Home' })),
162 _q["src/components/Home/Home." + (ts ? 'tsx' : 'jsx')] = tpl.source_component_cp(__assign(__assign({}, params), { componentName: 'Home' })),
163 _q["src/components/Home/style/Home.module." + suffix_stylesheet] = style && tpl.source_component_style(__assign(__assign({}, params), { componentName: 'Home' })),
164 // components - Start
165 _q["src/components/Start/index." + (ts ? 'ts' : 'js')] = tpl.source_component_index(__assign(__assign({}, params), { componentName: 'Start' })),
166 _q["src/components/Start/Start." + (ts ? 'tsx' : 'jsx')] = tpl.source_component_cp(__assign(__assign({}, params), { componentName: 'Start' })),
167 _q["src/components/Start/style/Start.module." + suffix_stylesheet] = style && tpl.source_component_style(__assign(__assign({}, params), { componentName: 'Start' })),
168 // components - Docs
169 _q["src/components/Docs/index." + (ts ? 'ts' : 'js')] = tpl.source_component_index(__assign(__assign({}, params), { componentName: 'Docs' })),
170 _q["src/components/Docs/Docs." + (ts ? 'tsx' : 'jsx')] = tpl.source_component_cp(__assign(__assign({}, params), { componentName: 'Docs' })),
171 _q["src/components/Docs/style/Docs.module." + suffix_stylesheet] = style && tpl.source_component_style(__assign(__assign({}, params), { componentName: 'Docs' })),
172 // components - Layout
173 _q["src/components/Layout/index." + (ts ? 'ts' : 'js')] = tpl.source_component_index(__assign(__assign({}, params), { componentName: 'Layout' })),
174 _q["src/components/Layout/Layout." + (ts ? 'tsx' : 'jsx')] = tpl.source_component_layout(__assign(__assign({}, params), { componentName: 'Layout' })),
175 _q["src/components/Layout/style/Layout.module." + suffix_stylesheet] = style && tpl.source_component_layout_style(__assign(__assign({}, params), { componentName: 'Layout' })),
176 // components - Link
177 _q["src/components/Link/index." + (ts ? 'ts' : 'js')] = ssrServer === 'koa-next' && tpl.source_component_index(__assign(__assign({}, params), { componentName: 'Link' })),
178 _q["src/components/Link/Link." + (ts ? 'tsx' : 'jsx')] = ssrServer === 'koa-next' && tpl.source_component_link(__assign(__assign({}, params), { componentName: 'Link' })),
179 // utils
180 _q["src/utils/mapCtxToProps." + (ts ? 'ts' : 'js')] = tpl.source_utils_mapctx(params),
181 _q["src/utils/paramsToQueryString." + (ts ? 'ts' : 'js')] = tpl.source_utils_params(params),
182 // next configs
183 _q['next.config.js'] = tpl.nextConfig(params),
184 _q['next-env.d.ts'] = ts && tpl.nextDeclartion(params),
185 _q['postcss.config.js'] = style && tpl.postcss(params),
186 // webpack
187 _q['configs/webpack.config.js'] = tpl.webpack(params),
188 // typescript
189 _q['tsconfig.json'] = ts && tpl.tsconfig(params),
190 // unit test
191 _q['configs/jest.config.js'] = test && tpl.jest(params),
192 // lint
193 _q['.vscode/settings.json'] = tpl.vscode(params),
194 _q['.editorconfig'] = (eslint || prettier) && tpl.editor(params),
195 _q['configs/.eslintrc.js'] = eslint && tpl.eslint(params),
196 _q['.eslintignore'] = eslint && tpl.eslintignore(params),
197 _q['configs/prettier.config.js'] = prettier && tpl.prettier(params),
198 _q['.prettierignore'] = prettier && tpl.prettierignore(params),
199 _q['configs/stylelint.config.js'] = stylelint && tpl.stylelint(params),
200 _q['configs/commitlint.config.js'] = commitlint && tpl.commitlint(params),
201 // build
202 _q['babel.config.js'] = tpl.babel(params),
203 // docs
204 _q['README.md'] = tpl.readme(__assign(__assign({}, params), { install: installReadMe, runScript: runScript, paramScript: paramScript })),
205 _q['README.zh-CN.md'] = tpl.readme_cn(__assign(__assign({}, params), { install: installReadMe, runScript: runScript, paramScript: paramScript })),
206 _q);
207 file_path = function (p) { return path_1.default.resolve(initPath, p); };
208 for (p in pathToFileContentMap) {
209 utils_1.outputFile({
210 file_path: file_path(p),
211 file_content: pathToFileContentMap[p]
212 });
213 }
214 }
215 catch (e) {
216 err = e;
217 utils_1.logErr(err.name + ": " + err.message + " at \n" + err.stack);
218 error ? error(err) : process.exit(1);
219 }
220 utils_1.logTime('CREATE(创建文件)', true);
221 // 项目依赖解析
222 utils_1.logTime('DEPENDENCY(依赖解析)');
223 dependenciesOptions = {
224 ts: ts,
225 eslint: eslint,
226 prettier: prettier,
227 commitlint: commitlint,
228 style: style,
229 stylelint: stylelint,
230 test: test,
231 ssrServer: ssrServer,
232 tag: tag
233 };
234 return [4 /*yield*/, dependencies_1.dependencies(strategy, dependenciesOptions)];
235 case 1:
236 _j = _r.sent(), depArr = _j.depArr, depStr = _j.depStr;
237 dependencies_str = depStr;
238 if (typeof dependencies_custom === 'function') {
239 result = dependencies_custom(depArr);
240 if (result instanceof Array) {
241 dependencies_str = depStr + " " + utils_1.arr2str(result);
242 }
243 else {
244 _k = result.add, add = _k === void 0 ? [] : _k, _l = result.remove, remove = _l === void 0 ? [] : _l;
245 _loop_2 = function (i) {
246 var item_rm = remove[i];
247 depArr = __spreadArrays(utils_1.intersection(depArr, depArr.filter(function (v) { return v !== item_rm; })));
248 };
249 for (i = 0; i < remove.length; i++) {
250 _loop_2(i);
251 }
252 dependencies_str = utils_1.arr2str(depArr) + " " + utils_1.arr2str(add);
253 }
254 }
255 installCli = dependencies_str ? installCliPrefix + " " + dependencies_str : '';
256 return [4 /*yield*/, dependencies_1.devDependencies(strategy, dependenciesOptions)];
257 case 2:
258 _m = _r.sent(), defaultDepArr = _m.defaultDepArr, defaultDepStr = _m.defaultDepStr, nextDepArr = _m.nextDepArr, nextDepStr = _m.nextDepStr, 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, serverDepArr = _m.serverDepArr, serverDepStr = _m.serverDepStr, devDepArr = _m.devDepArr;
259 if (typeof devDependencies_custom === 'function') {
260 result = devDependencies_custom(devDepArr);
261 if (result instanceof Array) {
262 customDepStr = utils_1.arr2str(result);
263 }
264 else {
265 _o = result.add, add = _o === void 0 ? [] : _o, _p = result.remove, remove = _p === void 0 ? [] : _p;
266 _loop_3 = function (i) {
267 var item_rm = remove[i];
268 defaultDepArr = __spreadArrays(utils_1.intersection(defaultDepArr, defaultDepArr.filter(function (v) { return v !== item_rm; })));
269 nextDepArr = __spreadArrays(utils_1.intersection(nextDepArr, nextDepArr.filter(function (v) { return v !== item_rm; })));
270 tsDepArr = __spreadArrays(utils_1.intersection(tsDepArr, tsDepArr.filter(function (v) { return v !== item_rm; })));
271 testDepArr = __spreadArrays(utils_1.intersection(testDepArr, testDepArr.filter(function (v) { return v !== item_rm; })));
272 eslintDepArr = __spreadArrays(utils_1.intersection(eslintDepArr, eslintDepArr.filter(function (v) { return v !== item_rm; })));
273 prettierDepArr = __spreadArrays(utils_1.intersection(prettierDepArr, prettierDepArr.filter(function (v) { return v !== item_rm; })));
274 commitlintDepArr = __spreadArrays(utils_1.intersection(commitlintDepArr, commitlintDepArr.filter(function (v) { return v !== item_rm; })));
275 stylelintDepArr = __spreadArrays(utils_1.intersection(stylelintDepArr, stylelintDepArr.filter(function (v) { return v !== item_rm; })));
276 serverDepArr = __spreadArrays(utils_1.intersection(serverDepArr, serverDepArr.filter(function (v) { return v !== item_rm; })));
277 };
278 for (i = 0; i < remove.length; i++) {
279 _loop_3(i);
280 }
281 defaultDepStr = utils_1.arr2str(defaultDepArr);
282 nextDepStr = utils_1.arr2str(nextDepArr);
283 tsDepStr = utils_1.arr2str(tsDepArr);
284 testDepStr = utils_1.arr2str(testDepArr);
285 eslintDepStr = utils_1.arr2str(eslintDepArr);
286 prettierDepStr = utils_1.arr2str(prettierDepArr);
287 commitlintDepStr = utils_1.arr2str(commitlintDepArr);
288 stylelintDepStr = utils_1.arr2str(stylelintDepArr);
289 serverDepStr = utils_1.arr2str(serverDepArr);
290 customDepStr = utils_1.arr2str(add);
291 }
292 }
293 installDevCliArr = [];
294 defaultDepStr && installDevCliArr.push(defaultDepStr);
295 nextDepStr && installDevCliArr.push(nextDepStr);
296 tsDepStr && installDevCliArr.push(tsDepStr);
297 testDepStr && installDevCliArr.push(testDepStr);
298 eslintDepStr && installDevCliArr.push(eslintDepStr);
299 prettierDepStr && installDevCliArr.push(prettierDepStr);
300 commitlintDepStr && installDevCliArr.push(commitlintDepStr);
301 stylelintDepStr && installDevCliArr.push(stylelintDepStr);
302 serverDepStr && installDevCliArr.push(serverDepStr);
303 customDepStr && installDevCliArr.push(customDepStr);
304 installDevCli = installDevCliPrefix + " " + installDevCliArr.join(' ');
305 utils_1.logTime('DEPENDENCY(依赖解析)', true);
306 // 项目依赖安装
307 if (install) {
308 utils_1.logTime('INSTALL(安装依赖)');
309 utils_1.exec([
310 installCli,
311 installDevCli
312 ], function (res) {
313 utils_1.logTime('INSTALL(安装依赖)', true);
314 success(res);
315 }, error, isSlient);
316 }
317 else {
318 utils_1.logTime('STATIC(生成静态依赖文件)');
319 processDepStr = function (str, prefix) {
320 if (!str)
321 return '';
322 var result = '';
323 var arr = str.split(' ').filter(function (v) { return !!v; });
324 for (var i = 0; i < arr.length; i++) {
325 var item = arr[i];
326 if (!item)
327 continue;
328 var lastInd = item.lastIndexOf('@');
329 var name_2 = lastInd === 0 ? item : item.substr(0, lastInd);
330 var version = lastInd === 0 ? 'latest' : item.substr(lastInd + 1);
331 if (i + 1 === arr.length) {
332 result += " \"" + name_2 + "\": \"" + version + "\"";
333 }
334 else {
335 result += " \"" + name_2 + "\": \"" + version + "\",\n";
336 }
337 }
338 return "\"" + prefix + "\": {\n" + result + "\n },";
339 };
340 utils_1.outputFile({
341 file_path: path_1.default.resolve(initPath, 'package.json'),
342 file_content: tpl.pkj(dependencies_stable_map_1.devDependencies['@types/react'])(__assign(__assign({}, params), { install: install, dependencies: processDepStr(dependencies_str, 'dependencies'), devDependencies: processDepStr((defaultDepStr || '') + " " + (customDepStr || ''), 'devDependencies') }))
343 });
344 utils_1.logTime('STATIC(生成静态依赖文件)', true);
345 success([]);
346 }
347 return [2 /*return*/];
348 }
349 });
350 });
351}
352exports.$init = $init;
353exports.default = $init;