UNPKG

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