@tmsfe/tmskit 0.0.2 → 0.0.5-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -4,22 +4,26 @@ var require$$0 = require('commander');
4
4
  var require$$1 = require('leven');
5
5
  var require$$2 = require('ora');
6
6
  var require$$1$1 = require('path');
7
- var require$$1$2 = require('fs');
7
+ var require$$0$1 = require('fs');
8
8
  var require$$5 = require('shelljs');
9
9
  var require$$6$1 = require('download-git-repo');
10
- var require$$0$1 = require('chalk');
11
- var require$$0$2 = require('async');
12
- var require$$1$3 = require('ejs');
13
- var require$$1$4 = require('inquirer');
14
- var require$$0$3 = require('metalsmith');
15
- var require$$0$4 = require('miniprogram-ci');
10
+ var require$$0$2 = require('chalk');
11
+ var require$$0$3 = require('async');
12
+ var require$$1$2 = require('ejs');
13
+ var require$$1$3 = require('inquirer');
14
+ var require$$0$4 = require('metalsmith');
16
15
  var require$$0$5 = require('lodash');
17
- var require$$7 = require('replace-ext');
18
- var require$$0$6 = require('webpack-chain');
19
- var require$$0$7 = require('webpack');
20
- var require$$0$8 = require('webpack/lib/SingleEntryPlugin');
21
- var require$$3$1 = require('copy-webpack-plugin');
22
- var require$$1$5 = require('semver');
16
+ var require$$0$6 = require('miniprogram-ci');
17
+ var require$$3$1 = require('glob-ignore');
18
+ var require$$0$7 = require('through2');
19
+ var require$$1$4 = require('precinct');
20
+ var require$$0$8 = require('gulp');
21
+ var require$$1$5 = require('gulp-px-to-rpx');
22
+ var require$$2$1 = require('gulp-less');
23
+ var require$$3$2 = require('gulp-rename');
24
+ var require$$4 = require('gulp-watch');
25
+ var require$$5$1 = require('gulp-image');
26
+ var require$$1$6 = require('semver');
23
27
 
24
28
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
25
29
 
@@ -27,22 +31,26 @@ var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0);
27
31
  var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
28
32
  var require$$2__default = /*#__PURE__*/_interopDefaultLegacy(require$$2);
29
33
  var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
30
- var require$$1__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$1$2);
34
+ var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
31
35
  var require$$5__default = /*#__PURE__*/_interopDefaultLegacy(require$$5);
32
36
  var require$$6__default = /*#__PURE__*/_interopDefaultLegacy(require$$6$1);
33
- var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
34
37
  var require$$0__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
35
- var require$$1__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$1$3);
36
- var require$$1__default$4 = /*#__PURE__*/_interopDefaultLegacy(require$$1$4);
37
38
  var require$$0__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$0$3);
39
+ var require$$1__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$1$2);
40
+ var require$$1__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$1$3);
38
41
  var require$$0__default$4 = /*#__PURE__*/_interopDefaultLegacy(require$$0$4);
39
42
  var require$$0__default$5 = /*#__PURE__*/_interopDefaultLegacy(require$$0$5);
40
- var require$$7__default = /*#__PURE__*/_interopDefaultLegacy(require$$7);
41
43
  var require$$0__default$6 = /*#__PURE__*/_interopDefaultLegacy(require$$0$6);
44
+ var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3$1);
42
45
  var require$$0__default$7 = /*#__PURE__*/_interopDefaultLegacy(require$$0$7);
46
+ var require$$1__default$4 = /*#__PURE__*/_interopDefaultLegacy(require$$1$4);
43
47
  var require$$0__default$8 = /*#__PURE__*/_interopDefaultLegacy(require$$0$8);
44
- var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3$1);
45
48
  var require$$1__default$5 = /*#__PURE__*/_interopDefaultLegacy(require$$1$5);
49
+ var require$$2__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$2$1);
50
+ var require$$3__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$3$2);
51
+ var require$$4__default = /*#__PURE__*/_interopDefaultLegacy(require$$4);
52
+ var require$$5__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$5$1);
53
+ var require$$1__default$6 = /*#__PURE__*/_interopDefaultLegacy(require$$1$6);
46
54
 
47
55
  function getAugmentedNamespace(n) {
48
56
  if (n.__esModule) return n;
@@ -59,24 +67,24 @@ function getAugmentedNamespace(n) {
59
67
  return a;
60
68
  }
61
69
 
62
- var src$1 = {};
70
+ var src$2 = {};
63
71
 
64
72
  const program$1 = require$$0__default;
65
73
  const leven = require$$1__default;
66
74
  const ora = require$$2__default;
67
- const path$6 = require$$1__default$1;
68
- const fs$c = require$$1__default$2;
75
+ const path$7 = require$$1__default$1;
76
+ const fs$b = require$$0__default$1;
69
77
  const shelljs$5 = require$$5__default;
70
78
  const download = require$$6__default;
71
- const chalk$3 = require$$0__default$1;
79
+ const chalk$3 = require$$0__default$2;
72
80
  const shelljsOptons = {
73
81
  slient: true
74
82
  }; // 获取当前目录
75
83
 
76
84
  const cwd = process.cwd();
77
85
 
78
- function resolve$a(...args) {
79
- return path$6.resolve(cwd, ...args);
86
+ function resolve$b(...args) {
87
+ return path$7.resolve(cwd, ...args);
80
88
  }
81
89
  /**
82
90
  * 封装logs
@@ -145,7 +153,7 @@ function downloadRepo(dest, downloadOptions = {
145
153
  branch
146
154
  } = downloadOptions;
147
155
 
148
- if (fs$c.existsSync(dest)) {
156
+ if (fs$b.existsSync(dest)) {
149
157
  shelljs$5.rm('-rf', dest);
150
158
  }
151
159
 
@@ -175,23 +183,17 @@ function downloadRepo(dest, downloadOptions = {
175
183
 
176
184
  function downloadRepoForGit$2(url, dest, branch) {
177
185
  const cwd = process.cwd();
178
- return new Promise((resolve, reject) => {
186
+ return new Promise(resolve => {
179
187
  // 如果目标目录不存在
180
- if (fs$c.existsSync(dest)) {
181
- shelljs$5.rm('-rf', path$6.join(dest));
188
+ if (fs$b.existsSync(dest)) {
189
+ shelljs$5.rm('-rf', path$7.join(dest));
182
190
  }
183
191
 
184
192
  shelljs$5.mkdir('-p', dest);
185
193
  shelljs$5.cd(dest);
186
- shelljs$5.exec(`git clone ${url} ${dest} --depth=1`, shelljsOptons);
187
- shelljs$5.exec(`git checkout ${branch}`, code => {
188
- if (code === 0) {
189
- shelljs$5.cd(cwd);
190
- resolve();
191
- }
192
-
193
- reject();
194
- });
194
+ shelljs$5.exec(`git clone ${url} ${dest} --branch ${branch} --depth 1`, shelljsOptons);
195
+ shelljs$5.cd(cwd);
196
+ resolve();
195
197
  });
196
198
  }
197
199
  /**
@@ -241,7 +243,7 @@ function createTask$4(task, startText, endText) {
241
243
  const camelize = str => str.replace(/-(\w)/g, (a, c) => c ? c.toUpperCase() : '');
242
244
 
243
245
  var widgets = {
244
- resolve: resolve$a,
246
+ resolve: resolve$b,
245
247
  log: log$3,
246
248
  isObject: isObject$2,
247
249
  isArray: isArray$1,
@@ -252,32 +254,32 @@ var widgets = {
252
254
  camelize
253
255
  };
254
256
 
255
- const path$5 = require('path'); // 用户目录
257
+ const path$6 = require('path'); // 用户目录
256
258
 
257
259
 
258
260
  const HOME_DIR = process.env.HOME; // 所有文件的缓存目录
259
261
 
260
- const CACHE_DIR$1 = path$5.resolve(HOME_DIR, '.tmskit'); // 脚手架模板代码所在目录
262
+ const CACHE_DIR$1 = path$6.resolve(HOME_DIR, '.tmskit'); // 脚手架模板代码所在目录
261
263
 
262
- const TEMPLATE_DIR$1 = path$5.resolve(CACHE_DIR$1, 'template'); // 第三方模块源码存放的临时缓存目录
264
+ const TEMPLATE_DIR$1 = path$6.resolve(CACHE_DIR$1, 'template'); // 第三方模块源码存放的临时缓存目录
263
265
 
264
- const MODULE_CODE_DIR$1 = path$5.resolve(CACHE_DIR$1, 'modules_code'); // 脚手架模板代码的具体路径
266
+ const MODULE_CODE_DIR$1 = path$6.resolve(CACHE_DIR$1, 'modules_code'); // 脚手架模板代码的具体路径
265
267
 
266
- const TEMPLATE_PATH$1 = path$5.resolve(TEMPLATE_DIR$1, 'tools/tms-cli-template'); // 脚手架的名称
268
+ const TEMPLATE_PATH$1 = path$6.resolve(TEMPLATE_DIR$1, 'tools/tms-cli-template'); // 脚手架的名称
267
269
 
268
270
  const TMS_NAME$2 = 'tmskit'; // 脚手架的配置名称
269
271
 
270
272
  const TMS_CONFIG_FILENAME$1 = 'tms.config.js'; // 模块代码的默认在modules子目录
271
273
 
272
- const DEFAULT_MODULE_DIR$5 = 'modules'; // 模块的配置文件的名称
274
+ const DEFAULT_MODULE_DIR$4 = 'modules'; // 模块的配置文件的名称
273
275
 
274
276
  const MODULE_CONFIG_FILENAME$2 = 'module.config.json'; // 默认的webpack entry
275
277
 
276
- const DEFAULT_WEBPACK_ENTRY$2 = {
277
- app: path$5.resolve(process.cwd(), 'app')
278
+ const DEFAULT_WEBPACK_ENTRY = {
279
+ app: path$6.resolve(process.cwd(), 'app')
278
280
  }; // 默认从源码拷贝到编译后的配置
279
281
 
280
- const DEFAULT_COPY_CONFIG$3 = ['package.json', 'app.js', 'app.ts', 'app.less', 'app.wxss', 'sitemap.json']; // 开发模式
282
+ const DEFAULT_COPY_CONFIG$1 = ['package.json', 'sitemap.json']; // 开发模式
281
283
 
282
284
  const MODE$1 = {
283
285
  main: 'tkitmain',
@@ -288,6 +290,7 @@ const ENV = {
288
290
  dev: 'development',
289
291
  prod: 'production'
290
292
  };
293
+ const TEMPLATE_TKIT_DIR$2 = '_tmskit';
291
294
 
292
295
  var constant = /*#__PURE__*/Object.freeze({
293
296
  __proto__: null,
@@ -297,25 +300,27 @@ var constant = /*#__PURE__*/Object.freeze({
297
300
  TEMPLATE_PATH: TEMPLATE_PATH$1,
298
301
  TMS_NAME: TMS_NAME$2,
299
302
  TMS_CONFIG_FILENAME: TMS_CONFIG_FILENAME$1,
300
- DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$5,
303
+ DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$4,
301
304
  MODULE_CONFIG_FILENAME: MODULE_CONFIG_FILENAME$2,
302
- DEFAULT_WEBPACK_ENTRY: DEFAULT_WEBPACK_ENTRY$2,
303
- DEFAULT_COPY_CONFIG: DEFAULT_COPY_CONFIG$3,
305
+ DEFAULT_WEBPACK_ENTRY: DEFAULT_WEBPACK_ENTRY,
306
+ DEFAULT_COPY_CONFIG: DEFAULT_COPY_CONFIG$1,
304
307
  MODULE_CODE_DIR: MODULE_CODE_DIR$1,
305
308
  MODE: MODE$1,
306
- ENV: ENV
309
+ ENV: ENV,
310
+ TEMPLATE_TKIT_DIR: TEMPLATE_TKIT_DIR$2
307
311
  });
308
312
 
309
313
  var require$$3 = /*@__PURE__*/getAugmentedNamespace(constant);
310
314
 
311
- const fs$b = require$$1__default$2;
315
+ const fs$a = require$$0__default$1;
316
+ const path$5 = require$$1__default$1;
312
317
  /**
313
318
  * 判断目录是否为空
314
319
  * @param {string} dirname 目录名
315
320
  * @returns
316
321
  */
317
322
 
318
- const isDirEmpty = dirname => fs$b.promises.readdir(dirname).then(files => files.length === 0);
323
+ const isDirEmpty = dirname => fs$a.promises.readdir(dirname).then(files => files.length === 0);
319
324
  /**
320
325
  * 确保目录存在,不存在就创建一个
321
326
  * @param {*} dirname 目录名
@@ -323,19 +328,70 @@ const isDirEmpty = dirname => fs$b.promises.readdir(dirname).then(files => files
323
328
 
324
329
 
325
330
  const ensureDirExist = dirname => {
326
- if (!fs$b.existsSync(dirname)) {
327
- fs$b.mkdirSync(dirname, {
331
+ if (!fs$a.existsSync(dirname)) {
332
+ fs$a.mkdirSync(dirname, {
328
333
  recursive: true
329
334
  });
330
335
  }
336
+ }; // 复制文件
337
+
338
+
339
+ const copyFile$1 = function (src, dest) {
340
+ if (fs$a.existsSync(dest)) {
341
+ fs$a.unlinkSync(dest);
342
+ }
343
+
344
+ const dir = dest.substr(0, dest.lastIndexOf('/'));
345
+ ensureDirExist(dir);
346
+ fs$a.copyFileSync(src, dest);
347
+ }; // 添加后缀
348
+
349
+
350
+ function ext$1(filePath, extensions) {
351
+ let newFilePath = filePath;
352
+
353
+ try {
354
+ const stat = fs$a.lstatSync(newFilePath);
355
+
356
+ if (stat.isDirectory()) {
357
+ newFilePath += newFilePath[newFilePath.length - 1] === '/' ? 'index' : '/index';
358
+ }
359
+ } catch (e) {}
360
+
361
+ for (const ext of extensions) {
362
+ const file = newFilePath.endsWith(ext) ? newFilePath : newFilePath + ext;
363
+
364
+ if (fs$a.existsSync(file)) {
365
+ return {
366
+ file,
367
+ ext
368
+ };
369
+ }
370
+ }
371
+
372
+ return null;
373
+ } // 判断文件是否在某个目录
374
+
375
+
376
+ const fileInDir$1 = (dir, file) => {
377
+ const relativePath = path$5.relative(dir, file);
378
+
379
+ if (relativePath.startsWith('..')) {
380
+ return false;
381
+ }
382
+
383
+ return true;
331
384
  };
332
385
 
333
386
  var io$2 = {
334
387
  isDirEmpty,
335
- ensureDirExist
388
+ copyFile: copyFile$1,
389
+ ensureDirExist,
390
+ ext: ext$1,
391
+ fileInDir: fileInDir$1
336
392
  };
337
393
 
338
- const chalk$2 = require$$0__default$1;
394
+ const chalk$2 = require$$0__default$2;
339
395
  /**
340
396
  * 本文件提供无依赖的在终端打印彩色文字的方法。
341
397
  */
@@ -348,7 +404,7 @@ const resetCfg = decodeURIComponent('%1B%5B0m'); // \033[0m转义后的字符按
348
404
  * @returns {undefined} 无
349
405
  */
350
406
 
351
- const fail$a = (message = '') => {
407
+ const fail$8 = (message = '') => {
352
408
  const redStyleConfig = decodeURIComponent('%1B%5B41%3B30m'); // \033[41;30m转义后的字符按,console时输出红色文字
353
409
 
354
410
  const greenFontStyleConfig = decodeURIComponent('%1B%5B41%3B37m'); // \033[41;30m转义后的字符按,console时输出红底白色文字
@@ -381,13 +437,13 @@ const warn = message => {
381
437
  };
382
438
 
383
439
  var log$2 = {
384
- fail: fail$a,
440
+ fail: fail$8,
385
441
  succeed: succeed$1,
386
442
  warn
387
443
  };
388
444
 
389
- const async = require$$0__default$2;
390
- const ejs = require$$1__default$3;
445
+ const async = require$$0__default$3;
446
+ const ejs = require$$1__default$2;
391
447
 
392
448
  const render$1 = (files, metalsmith, next) => {
393
449
  const keys = Object.keys(files);
@@ -406,23 +462,29 @@ const render$1 = (files, metalsmith, next) => {
406
462
 
407
463
  var render_1 = render$1;
408
464
 
409
- const fs$a = require$$1__default$2;
410
- const inquirer = require$$1__default$4;
465
+ const fs$9 = require$$0__default$1;
466
+ const inquirer = require$$1__default$3;
467
+ const {
468
+ resolve: resolve$a
469
+ } = widgets;
470
+ const {
471
+ TEMPLATE_TKIT_DIR: TEMPLATE_TKIT_DIR$1
472
+ } = require$$3;
411
473
  /**
412
474
  * 获取模板内的问题
413
- * @param {string} templateDir 模板目录
475
+ * @param {string} dir questions.json所在的目录
414
476
  * @returns {Array} inquirer 问题数组
415
477
  */
416
478
 
417
- const parseTemplateQuestions = templateDir => {
479
+ const parseTemplateQuestions = dir => {
418
480
  let prompts = [];
419
481
 
420
- if (!fs$a.existsSync(`${templateDir}/questions.json`)) {
482
+ if (!fs$9.existsSync(`${dir}/questions.json`)) {
421
483
  return prompts;
422
484
  }
423
485
 
424
486
  try {
425
- const json = JSON.parse(fs$a.readFileSync(`${templateDir}/questions.json`));
487
+ const json = JSON.parse(fs$9.readFileSync(`${dir}/questions.json`));
426
488
 
427
489
  if (Array.isArray(json) && json.length > 0) {
428
490
  json.forEach((item, index) => {
@@ -455,7 +517,7 @@ const isQuestionType = result => {
455
517
  };
456
518
 
457
519
  const ask$1 = templateDir => (files, metalsmith, next) => {
458
- const prompts = parseTemplateQuestions(templateDir);
520
+ const prompts = parseTemplateQuestions(resolve$a(templateDir, TEMPLATE_TKIT_DIR$1));
459
521
  const metadata = metalsmith.metadata();
460
522
  const filteredPrompts = prompts.filter(prompt => {
461
523
  if (metadata[prompt.name] && `${metadata[prompt.name]}`.trim() !== '') {
@@ -480,8 +542,7 @@ var ask_1 = ask$1;
480
542
  const FILES_TO_IGNORE$1 = ['node_modules'];
481
543
  var ignoreFiles = FILES_TO_IGNORE$1;
482
544
 
483
- const Metalsmith = require$$0__default$3;
484
- const fs$9 = require$$1__default$2;
545
+ const Metalsmith = require$$0__default$4;
485
546
  const render = render_1;
486
547
  const ask = ask_1;
487
548
  const FILES_TO_IGNORE = ignoreFiles;
@@ -491,7 +552,6 @@ const generator$1 = (buildDir, distDir, preMetadata) => new Promise((resolve, re
491
552
  if (err) {
492
553
  reject(err);
493
554
  } else {
494
- fs$9.unlinkSync(`${distDir}/questions.json`);
495
555
  resolve('finish');
496
556
  }
497
557
  });
@@ -500,11 +560,12 @@ const generator$1 = (buildDir, distDir, preMetadata) => new Promise((resolve, re
500
560
  var generator_1 = generator$1;
501
561
 
502
562
  const path$4 = require$$1__default$1;
503
- const fs$8 = require$$1__default$2;
563
+ const fs$8 = require$$0__default$1;
504
564
  const shelljs$4 = require$$5__default;
505
565
  const {
506
566
  TEMPLATE_DIR,
507
- TEMPLATE_PATH
567
+ TEMPLATE_PATH,
568
+ TEMPLATE_TKIT_DIR
508
569
  } = require$$3;
509
570
  const {
510
571
  downloadRepoForGit: downloadRepoForGit$1,
@@ -513,7 +574,7 @@ const {
513
574
  } = widgets;
514
575
  const io$1 = io$2;
515
576
  const {
516
- fail: fail$9,
577
+ fail: fail$7,
517
578
  succeed
518
579
  } = log$2;
519
580
  const generator = generator_1;
@@ -527,7 +588,7 @@ async function createAppDir(targetDir) {
527
588
  // 如果目录非空或者已经存在,提示用户,做选择
528
589
  if (fs$8.existsSync(targetDir)) {
529
590
  if (!(await io$1.isDirEmpty(targetDir))) {
530
- fail$9('该目录名已经存在,换个项目名字吧~');
591
+ fail$7('该目录名已经存在,换个项目名字吧~');
531
592
  process.exit(1);
532
593
  }
533
594
  } else {
@@ -545,7 +606,7 @@ async function create(appName) {
545
606
  const cwd = process.cwd();
546
607
  const targetDir = path$4.resolve(cwd, appName);
547
608
  const appType = 'mp';
548
- createAppDir(targetDir); // 创建缓存目录
609
+ await createAppDir(targetDir); // 创建缓存目录
549
610
 
550
611
  io$1.ensureDirExist(TEMPLATE_DIR); // 拉取git模板
551
612
 
@@ -555,27 +616,39 @@ async function create(appName) {
555
616
  appName,
556
617
  appType
557
618
  }).then(() => {
558
- succeed('项目创建完成,开始初始化...');
559
- shelljs$4.cd(appName); // 初始化构件主包的代码, 让用户打开项目可以跑起来
619
+ shelljs$4.cd(appName);
560
620
 
561
- if (fs$8.existsSync(resolve$9(`${appName}/tms.config.js`))) {
562
- shelljs$4.exec('tmskit run build');
621
+ const hooks = require(resolve$9(appName, TEMPLATE_TKIT_DIR, 'hooks.js'));
622
+
623
+ if (hooks.afterCreate) {
624
+ hooks.afterCreate.forEach(item => {
625
+ if (typeof item === 'function') {
626
+ item.call(null, shelljs$4, {
627
+ appName
628
+ });
629
+ } else {
630
+ shelljs$4.exec(item);
631
+ }
632
+ });
563
633
  }
634
+
635
+ shelljs$4.rm('-rf', resolve$9(appName, TEMPLATE_TKIT_DIR));
636
+ succeed('项目创建完成.');
564
637
  }).catch(err => {
565
- fail$9(err.message);
638
+ fail$7(err.message);
566
639
  console.log('详细的错误信息:', err);
567
640
  });
568
641
  }
569
642
 
570
643
  var create_1 = create;
571
644
 
572
- const fs$7 = require$$1__default$2;
645
+ const fs$7 = require$$0__default$1;
573
646
  const {
574
- DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$4,
647
+ DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$3,
575
648
  MODULE_CONFIG_FILENAME: MODULE_CONFIG_FILENAME$1
576
649
  } = require$$3;
577
650
  const {
578
- fail: fail$8
651
+ fail: fail$6
579
652
  } = log$2;
580
653
  const {
581
654
  resolve: resolve$8,
@@ -655,7 +728,7 @@ function updateMainPackages(appJson, mainPackages = []) {
655
728
 
656
729
  foundMainPackages.forEach(subpackage => {
657
730
  if (!subpackage.pages || !subpackage.pages.length) {
658
- fail$8(`主包 ${subpackage} 不能没有 pages`);
731
+ fail$6(`主包 ${subpackage} 不能没有 pages`);
659
732
  process.exit(-1);
660
733
  }
661
734
 
@@ -682,7 +755,7 @@ function updateMainPackages(appJson, mainPackages = []) {
682
755
 
683
756
  const getAppJsonContent = sourceAppJsonPath => {
684
757
  if (!fs$7.existsSync(sourceAppJsonPath)) {
685
- fail$8(`当前路径 ${sourceAppJsonPath} 没找到app.json`);
758
+ fail$6(`当前路径 ${sourceAppJsonPath} 没找到app.json`);
686
759
  process.exit(1);
687
760
  }
688
761
 
@@ -717,168 +790,291 @@ const updateSubpackages = (appJson, modulesConfig) => {
717
790
  */
718
791
 
719
792
 
720
- function buildOutputAppJson$2(tmsConfig, modules) {
793
+ function buildOutputAppJson$1(tmsConfig, modules) {
721
794
  // 获取当前 modules 下的所有子模块的配置内容
722
- const modulesConfig = getLocalModuleConfig(modules, tmsConfig.appName, DEFAULT_MODULE_DIR$4, MODULE_CONFIG_FILENAME$1); // 获取app.json的配置
795
+ const modulesConfig = getLocalModuleConfig(modules, tmsConfig.appName, DEFAULT_MODULE_DIR$3, MODULE_CONFIG_FILENAME$1); // 获取app.json的配置
723
796
 
724
797
  const appJson = getAppJsonContent(resolve$8('./app.json')); // 更新app.json中的subpackages
725
798
 
726
799
  updateSubpackages(appJson, modulesConfig); // 更新主包,需在subpackages处理完成后执行, pages/
727
800
 
728
801
  updateMainPackages(appJson, tmsConfig.mainPackages);
729
- fs$7.writeFileSync(resolve$8(`${tmsConfig.webpack.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
802
+ fs$7.writeFileSync(resolve$8(`${tmsConfig.gulp.outputDir}/app.json`), JSON.stringify(appJson, null, 2), 'utf8');
730
803
  return appJson;
731
804
  }
732
805
 
733
806
  var buildAppJson = {
734
807
  setModuleConfig: setModuleConfig$1,
735
- buildOutputAppJson: buildOutputAppJson$2
808
+ buildOutputAppJson: buildOutputAppJson$1
736
809
  };
737
810
 
738
- /**
739
- * 本文件主要负责项目或者分包依赖的npm的安装
740
- */
741
- const fs$6 = require$$1__default$2;
742
- const path$3 = require$$1__default$1;
743
- const shell = require$$5__default;
744
- const LOG = log$2;
745
- const dirpath = process.cwd(); // 项目根目录
746
-
747
- const getTarNpmFilename = targetDir => `${targetDir.replaceAll('/', '-')}.tar.gz`; // 缓存npm包
811
+ class Globale {
812
+ constructor() {
813
+ this.moduleCache = {};
814
+ }
748
815
 
816
+ setModuleCache(url, branch, dest) {
817
+ const instance = getGlobalInstance$1();
818
+ const key = `${branch}:${url}`;
819
+ instance.moduleCache[key] = {
820
+ dest
821
+ };
822
+ return instance.moduleCache[key];
823
+ }
749
824
 
750
- const npmCache = function (targetDir, cacheDir) {
751
- if (!fs$6.existsSync(cacheDir)) {
752
- fs$6.mkdirSync(cacheDir);
825
+ getModuleCache(url, branch) {
826
+ const instance = getGlobalInstance$1();
827
+ return instance.moduleCache[`${branch}:${url}`];
753
828
  }
754
829
 
755
- const tarNpmFilename = getTarNpmFilename(targetDir);
756
- const tarNpmFilePath = `${cacheDir}/${tarNpmFilename}`;
830
+ }
757
831
 
758
- if (fs$6.existsSync(tarNpmFilePath)) {
759
- shell.rm('-rf', tarNpmFilePath);
832
+ let instance;
833
+
834
+ function getGlobalInstance$1() {
835
+ if (instance) {
836
+ return instance;
760
837
  }
761
838
 
762
- const cmd = `tar -zcvf ${tarNpmFilePath} ./node_modules`;
763
- shell.exec(cmd, {
764
- async: true,
765
- silent: true
766
- }); // tar -zcvf /Users/odile/.tmskit/node_modules.tar.gz ./node_modules
767
- }; // 获取缓存npm包
839
+ return instance = new Globale();
840
+ }
768
841
 
842
+ var global = {
843
+ getGlobalInstance: getGlobalInstance$1
844
+ };
769
845
 
770
- const getNpmCache = function (targetDir, cacheDir) {
771
- const tarNpmFilename = getTarNpmFilename(targetDir);
772
- const tarNpmFilePath = `${cacheDir}/${tarNpmFilename}`;
846
+ const MetalSmith = require$$0__default$4;
847
+ const {
848
+ getGlobalInstance
849
+ } = global;
850
+ const {
851
+ downloadRepoForGit,
852
+ resolve: resolve$7
853
+ } = widgets;
854
+ const {
855
+ fail: fail$5
856
+ } = log$2;
857
+ const fs$6 = require$$0__default$1;
858
+ const shelljs$3 = require$$5__default;
859
+ /**
860
+ * 对克隆下来的模块进行相应的文件处理操作,比如收集处理模块信息,进行信息缓存等操作
861
+ * @param { string } sourceDir 缓存文件夹
862
+ * @param { string } targetDir 目标文件夹
863
+ * @param { arrary } ignore
864
+ * @returns { undefined } no return
865
+ */
773
866
 
774
- if (fs$6.existsSync(tarNpmFilePath)) {
775
- const cmd = `tar -zxvf ${tarNpmFilePath} -C ./`;
776
- shell.exec(cmd, {
777
- async: false,
778
- silent: true
779
- });
780
- } // tar -zxvf /Users/odile/.tmskit/node_modules.tar.gz -C ./
867
+ function moveFile(sourceDir, targetDir, ignore = []) {
868
+ // 删除不是文件夹的文件
869
+ return new Promise(resolve => {
870
+ MetalSmith(__dirname).ignore(ignore).source(sourceDir).destination(targetDir).build(e => {
871
+ if (e) {
872
+ fail$5(e); // eslint-disable-line
781
873
 
782
- }; // 遍历安装指定目录下所有项目的npm依赖
874
+ console.log('MetalSmith 详细的错误信息:', e);
875
+ }
783
876
 
877
+ resolve();
878
+ });
879
+ });
880
+ }
881
+ /**
882
+ * 下载目标模块
883
+ * @param { string } sourceDir 缓存文件夹
884
+ * @param { string } targetDir 目标文件夹
885
+ * @returns { array } modules 描述模块的列表
886
+ */
784
887
 
785
- const npmInstallAll$1 = async (modules, contextDir, cacheDir) => {
786
- const packageJsonFiles = await findAllPackageJson$1(modules, contextDir);
787
- await Promise.all(packageJsonFiles.map(file => new Promise(resolve => {
788
- const dir = path$3.dirname(file);
789
- shell.cd(dir);
790
888
 
791
- if (!fs$6.existsSync(`${dir}/node_modules`)) {
792
- getNpmCache(dir, cacheDir);
889
+ async function cloneModules$1(sourceDir, targetDir, modules) {
890
+ // 根据小程序的配置文件下载模块, 并且处理信息
891
+ for (const moduleInfo of modules) {
892
+ // eslint-disable-line
893
+ if (moduleInfo.repoInfo) {
894
+ await downLoadAndMoveModule(sourceDir, targetDir, moduleInfo);
793
895
  }
794
-
795
- shell.exec('npx pnpm install --prod --registry http://mirrors.tencent.com/npm/', {
796
- silent: false
797
- });
798
- resolve();
799
- npmCache(dir, cacheDir);
800
- })));
801
- };
896
+ }
897
+ }
802
898
  /**
803
- * 递归查找指定条件的文件
804
- * @param {String} startPath 开始查找的根路径
805
- * @param {String} filter 匹配的字符串
806
- * @returns {Array<String>} 查找到的文件路径列表
899
+ * 下载模块信息并且将它移动到对应的位置
900
+ * @param { string } sourceDir 代码缓存文件夹
901
+ * @param { string } targetDir 代码要放到的目标文件夹
902
+ * @returns { array } moduleInfo 描述模块的信息
807
903
  */
808
904
 
809
905
 
810
- const findFilesByFilter = (startPath, filter) => {
811
- const result = [];
812
- /**
813
- * 根据指定的筛选器查找文件
814
- * @param {String} startPath 开始查找的文件夹路径
815
- * @param {String} filter 筛选器
816
- * @returns {Undefined} 无需返回值
817
- */
906
+ async function downLoadAndMoveModule(sourceDir, targetDir, moduleInfo) {
907
+ const {
908
+ repoInfo: {
909
+ buildGitTag,
910
+ httpRepoUrl
911
+ },
912
+ path
913
+ } = moduleInfo; // 源码临时存在的源目录
818
914
 
819
- const find = (startPath, filter) => {
820
- // 目录不存在
821
- if (!fs$6.existsSync(startPath)) {
822
- LOG.fail(`${startPath}目录不存在`);
823
- process.exit(-1);
824
- return;
825
- } // 当前目录下的所有文件 / 文件夹
915
+ let sourcePath = resolve$7(sourceDir, path); // 源码要放到目标目录
826
916
 
917
+ const targetPath = resolve$7(targetDir, path); // 设置模块的构建分支
827
918
 
828
- const exceptDir = ['node_modules', 'miniprogram_npm'];
919
+ const cloneBranch = buildGitTag && typeof buildGitTag === 'string' ? buildGitTag : 'master'; // 检查缓存中有没有
829
920
 
830
- if (exceptDir.find(item => startPath.indexOf(item) > -1)) {
831
- return;
921
+ const globalInstance = getGlobalInstance();
922
+ const moduleInCache = globalInstance.getModuleCache(httpRepoUrl, cloneBranch);
923
+
924
+ try {
925
+ if (!moduleInCache) {
926
+ await downloadRepoForGit(httpRepoUrl, sourcePath, cloneBranch);
927
+ globalInstance.setModuleCache(httpRepoUrl, cloneBranch, sourcePath);
928
+ } else {
929
+ sourcePath = globalInstance.getModuleCache(httpRepoUrl, cloneBranch).dest;
832
930
  }
833
931
 
834
- const files = fs$6.readdirSync(startPath);
835
- files.forEach(file => {
836
- const filename = path$3.join(startPath, file);
837
- const stat = fs$6.lstatSync(filename); // 当前文件是文件夹类型,继续递归
932
+ if (fs$6.existsSync(targetPath)) {
933
+ shelljs$3.rm('-rf', targetPath);
934
+ }
838
935
 
839
- if (stat.isDirectory()) {
840
- find(filename, filter);
841
- } else if (filename.indexOf(filter) >= 0) {
842
- // 文件类型
843
- result.push(filename);
844
- }
845
- });
846
- };
936
+ await moveFile(sourcePath, targetPath, ['node_modules', '.git']);
937
+ } catch (e) {
938
+ fail$5(`downLoadAndMoveModule ${e}`); // eslint-disable-line
847
939
 
848
- find(startPath, filter);
849
- return result;
850
- };
851
- /**
852
- * 找到项目中所有的package.json文件
853
- * @param {Array<String>} subRoots 需要安装npm依赖的路径
854
- * @param {String} contextDir 命令运行的目录
855
- * @returns {Array<String>} 找到的所有package.json文件的路径
856
- */
940
+ process.exit(-1);
941
+ }
942
+ }
857
943
 
944
+ var cloneModules_1 = {
945
+ cloneModules: cloneModules$1
946
+ };
858
947
 
859
- const findAllPackageJson$1 = (subRoots = [], contextDir) => {
860
- const packageJsonName = 'package.json'; // 查找文件名
948
+ var defaultTmsConfig$1 = {
949
+ // 全局的环境配置项
950
+ envData: {},
951
+ // 模块配置信息
952
+ modules: [],
953
+ cloudDir: 'cloud',
954
+ gulp: {
955
+ // 第三方依赖代码需要拷贝到本项目的
956
+ dependencies: {},
861
957
 
862
- const cwd = contextDir || dirpath;
863
- const result = [path$3.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
958
+ /** 编译输出文件夹位置 */
959
+ outputDir: 'dist',
864
960
 
865
- subRoots.forEach(subRoot => {
866
- const toppath = path$3.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
961
+ /** 源码监听路径 */
962
+ sourceDir: './',
867
963
 
868
- const list = findFilesByFilter(toppath, packageJsonName);
869
- result.push(...list);
870
- });
871
- return result;
964
+ /** 源码Map */
965
+ copyDir: []
966
+ }
872
967
  };
873
968
 
874
- var npmUtils = {
875
- npmInstallAll: npmInstallAll$1,
876
- findAllPackageJson: findAllPackageJson$1
969
+ const loadash = require$$0__default$5;
970
+ const fs$5 = require$$0__default$1;
971
+ const {
972
+ TMS_NAME: TMS_NAME$1,
973
+ TMS_CONFIG_FILENAME,
974
+ MODULE_CONFIG_FILENAME
975
+ } = require$$3;
976
+ const {
977
+ resolve: resolve$6,
978
+ isObject
979
+ } = widgets;
980
+ const {
981
+ setModuleConfig
982
+ } = buildAppJson;
983
+ const defaultTmsConfig = defaultTmsConfig$1;
984
+ const {
985
+ fail: fail$4
986
+ } = log$2;
987
+ /**
988
+ * 读取tms.config.json
989
+ * @param env {string} 环境变量
990
+ */
991
+
992
+ const readTmsConfig$1 = function (env) {
993
+ const tmsConfigPath = resolve$6(TMS_CONFIG_FILENAME);
994
+
995
+ if (!fs$5.existsSync(tmsConfigPath)) {
996
+ fail$4('当前执行目录没有tms.config.js的配置项,请进行配置');
997
+ process.exit(1);
998
+ }
999
+
1000
+ const tmsConfigFn = require(tmsConfigPath);
1001
+
1002
+ const tmsConfig = tmsConfigFn({
1003
+ env
1004
+ }); // 合并默认值
1005
+
1006
+ loadash.mergeWith(tmsConfig, defaultTmsConfig);
1007
+ return tmsConfig;
1008
+ };
1009
+ /**
1010
+ * 从tms.config.json中检索用户传入的有效modules
1011
+ * @param { object } tmsConfig
1012
+ * @param { array } modules
1013
+ * @returns
1014
+ */
1015
+
1016
+
1017
+ const checkModules$1 = function (tmsConfig, modules) {
1018
+ const targetModules = [];
1019
+ modules.forEach(moduleName => {
1020
+ const module = tmsConfig.modules.find(module => module.name === moduleName);
1021
+ module && targetModules.push(module);
1022
+ });
1023
+
1024
+ if (targetModules.length === 0) {
1025
+ fail$4(`你启动的模块无效,尝试 ${TMS_NAME$1} -m moduleName`);
1026
+ process.exit(1);
1027
+ }
1028
+
1029
+ return targetModules;
1030
+ };
1031
+ /**
1032
+ * tms.config.js的modules 合并 module.config.json的配置项
1033
+ * @param {array} modules
1034
+ * @param {string} appName
1035
+ * @param {string} moduleDir
1036
+ * @returns
1037
+ */
1038
+
1039
+
1040
+ const tmsModulesMergeLocalModuleCfg$3 = (modules, appName, moduleDir) => {
1041
+ const newModules = [];
1042
+ modules.forEach(({
1043
+ path: relativePath,
1044
+ name: moduleName
1045
+ }, moduleIndex) => {
1046
+ const moduleConfigPath = resolve$6(relativePath, MODULE_CONFIG_FILENAME);
1047
+
1048
+ if (fs$5.existsSync(moduleConfigPath)) {
1049
+ let moduleConfigContent = fs$5.readFileSync(moduleConfigPath, 'utf-8');
1050
+ moduleConfigContent = setModuleConfig(moduleConfigContent, appName, moduleDir);
1051
+ const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
1052
+ moduleContentArr.forEach(({
1053
+ name
1054
+ }, moduleContentArrIndex) => {
1055
+ if (name === moduleName) {
1056
+ newModules.push({ ...modules[moduleIndex],
1057
+ ...moduleContentArr[moduleContentArrIndex]
1058
+ });
1059
+ }
1060
+ });
1061
+ } else {
1062
+ newModules.push({ ...modules[moduleIndex]
1063
+ });
1064
+ }
1065
+ });
1066
+ return newModules;
1067
+ };
1068
+
1069
+ var tkitUtils = {
1070
+ readTmsConfig: readTmsConfig$1,
1071
+ checkModules: checkModules$1,
1072
+ tmsModulesMergeLocalModuleCfg: tmsModulesMergeLocalModuleCfg$3
877
1073
  };
878
1074
 
879
1075
  /* eslint-disable require-jsdoc */
880
- const ci = require$$0__default$4;
881
- const path$2 = require$$1__default$1;
1076
+ const ci = require$$0__default$6;
1077
+ const path$3 = require$$1__default$1;
882
1078
  /**
883
1079
  * 获取小程序ci的Project对象
884
1080
  * @returns {Object} 小程序ci对象
@@ -892,7 +1088,7 @@ const getMpCi = ({
892
1088
  }) => {
893
1089
  var _cfgJsonContent$packO;
894
1090
 
895
- const cfgJsonContent = require(path$2.join(projectPath, 'project.config.json'));
1091
+ const cfgJsonContent = require(path$3.join(projectPath, 'project.config.json'));
896
1092
 
897
1093
  const ignores = (cfgJsonContent === null || cfgJsonContent === void 0 ? void 0 : (_cfgJsonContent$packO = cfgJsonContent.packOptions) === null || _cfgJsonContent$packO === void 0 ? void 0 : _cfgJsonContent$packO.ignore.map(({
898
1094
  value
@@ -958,302 +1154,235 @@ var mpCiUtils = {
958
1154
  buildMpNpm: buildMpNpm$1
959
1155
  };
960
1156
 
961
- class Globale {
962
- constructor() {
963
- this.moduleCache = {};
964
- }
1157
+ /**
1158
+ * 本文件主要负责项目或者分包依赖的npm的安装
1159
+ */
1160
+ const fs$4 = require$$0__default$1;
1161
+ const path$2 = require$$1__default$1;
1162
+ const shell = require$$5__default;
1163
+ const glob = require$$3__default;
1164
+ const LOG = log$2;
1165
+ const dirpath = process.cwd(); // 项目根目录
965
1166
 
966
- setModuleCache(url, branch, dest) {
967
- const instance = getGlobalInstance$1();
968
- const key = `${branch}:${url}`;
969
- instance.moduleCache[key] = {
970
- dest
971
- };
972
- return instance.moduleCache[key];
973
- }
1167
+ const getTarNpmFilename = targetDir => `${targetDir.replace(/\//g, '-')}.tar.gz`; // 缓存npm包
974
1168
 
975
- getModuleCache(url, branch) {
976
- const instance = getGlobalInstance$1();
977
- return instance.moduleCache[`${branch}:${url}`];
978
- }
979
1169
 
980
- }
1170
+ const npmCache = function (targetDir, cacheDir) {
1171
+ if (!fs$4.existsSync(cacheDir)) {
1172
+ fs$4.mkdirSync(cacheDir);
1173
+ }
981
1174
 
982
- let instance;
1175
+ const tarNpmFilename = getTarNpmFilename(targetDir);
1176
+ const tarNpmFilePath = `${cacheDir}/${tarNpmFilename}`;
983
1177
 
984
- function getGlobalInstance$1() {
985
- if (instance) {
986
- return instance;
1178
+ if (fs$4.existsSync(tarNpmFilePath)) {
1179
+ shell.rm('-rf', tarNpmFilePath);
987
1180
  }
988
1181
 
989
- return instance = new Globale();
990
- }
1182
+ const cmd = `tar -zcvf ${tarNpmFilePath} ./node_modules`;
1183
+ shell.exec(cmd, {
1184
+ async: true,
1185
+ silent: true
1186
+ }); // tar -zcvf /Users/odile/.tmskit/node_modules.tar.gz ./node_modules
1187
+ }; // 获取缓存npm包
991
1188
 
992
- var global = {
993
- getGlobalInstance: getGlobalInstance$1
994
- };
995
1189
 
996
- const MetalSmith = require$$0__default$3;
997
- const {
998
- getGlobalInstance
999
- } = global;
1000
- const {
1001
- downloadRepoForGit,
1002
- resolve: resolve$7
1003
- } = widgets;
1004
- const {
1005
- fail: fail$7
1006
- } = log$2;
1007
- const fs$5 = require$$1__default$2;
1008
- const shelljs$3 = require$$5__default;
1009
- /**
1010
- * 对克隆下来的模块进行相应的文件处理操作,比如收集处理模块信息,进行信息缓存等操作
1011
- * @param { string } sourceDir 缓存文件夹
1012
- * @param { string } targetDir 目标文件夹
1013
- * @param { arrary } ignore
1014
- * @returns { undefined } no return
1015
- */
1190
+ const getNpmCache = function (targetDir, cacheDir) {
1191
+ const tarNpmFilename = getTarNpmFilename(targetDir);
1192
+ const tarNpmFilePath = `${cacheDir}/${tarNpmFilename}`;
1016
1193
 
1017
- function moveFile(sourceDir, targetDir, ignore = []) {
1018
- // 删除不是文件夹的文件
1019
- return new Promise(resolve => {
1020
- MetalSmith(__dirname).ignore(ignore).source(sourceDir).destination(targetDir).build(e => {
1021
- if (e) {
1022
- fail$7(e); // eslint-disable-line
1194
+ if (fs$4.existsSync(tarNpmFilePath)) {
1195
+ const cmd = `tar -zxvf ${tarNpmFilePath} -C ./`;
1196
+ shell.exec(cmd, {
1197
+ async: false,
1198
+ silent: true
1199
+ });
1200
+ } // tar -zxvf /Users/odile/.tmskit/node_modules.tar.gz -C ./
1023
1201
 
1024
- console.log('MetalSmith 详细的错误信息:', e);
1025
- }
1202
+ }; // 遍历安装指定目录下所有项目的npm依赖
1026
1203
 
1027
- resolve();
1028
- });
1029
- });
1030
- }
1031
- /**
1032
- * 下载目标模块
1033
- * @param { string } sourceDir 缓存文件夹
1034
- * @param { string } targetDir 目标文件夹
1035
- * @returns { array } modules 描述模块的列表
1036
- */
1037
1204
 
1205
+ const mpNpmInstallAll$1 = async (modules, contextDir, cacheDir) => {
1206
+ const packageJsonFiles = await findAllPackageJson(modules, contextDir);
1207
+ await Promise.all(packageJsonFiles.map(file => new Promise(resolve => {
1208
+ const dir = path$2.dirname(file);
1209
+ shell.cd(dir);
1038
1210
 
1039
- async function cloneModules$1(sourceDir, targetDir, modules) {
1040
- // 根据小程序的配置文件下载模块, 并且处理信息
1041
- for (const moduleInfo of modules) {
1042
- // eslint-disable-line
1043
- if (moduleInfo.repoInfo) {
1044
- await downLoadAndMoveModule(sourceDir, targetDir, moduleInfo);
1211
+ if (!fs$4.existsSync(`${dir}/node_modules`)) {
1212
+ getNpmCache(dir, cacheDir);
1045
1213
  }
1046
- }
1047
- }
1214
+
1215
+ shell.exec('npx pnpm install --prod --registry http://mirrors.tencent.com/npm/', {
1216
+ silent: false
1217
+ });
1218
+ resolve();
1219
+ npmCache(dir, cacheDir);
1220
+ })));
1221
+ };
1048
1222
  /**
1049
- * 下载模块信息并且将它移动到对应的位置
1050
- * @param { string } sourceDir 代码缓存文件夹
1051
- * @param { string } targetDir 代码要放到的目标文件夹
1052
- * @returns { array } moduleInfo 描述模块的信息
1223
+ * 递归查找指定条件的文件
1224
+ * @param {String} startPath 开始查找的根路径
1225
+ * @param {String} filter 匹配的字符串
1226
+ * @returns {Array<String>} 查找到的文件路径列表
1053
1227
  */
1054
1228
 
1055
1229
 
1056
- async function downLoadAndMoveModule(sourceDir, targetDir, moduleInfo) {
1057
- const {
1058
- repoInfo: {
1059
- buildGitTag,
1060
- httpRepoUrl
1061
- },
1062
- path
1063
- } = moduleInfo; // 源码临时存在的源目录
1064
-
1065
- let sourcePath = resolve$7(sourceDir, path); // 源码要放到目标目录
1066
-
1067
- const targetPath = resolve$7(targetDir, path); // 设置模块的构建分支
1230
+ const findFilesByFilter = (startPath, filter) => {
1231
+ const result = [];
1232
+ /**
1233
+ * 根据指定的筛选器查找文件
1234
+ * @param {String} startPath 开始查找的文件夹路径
1235
+ * @param {String} filter 筛选器
1236
+ * @returns {Undefined} 无需返回值
1237
+ */
1068
1238
 
1069
- const cloneBranch = buildGitTag && typeof buildGitTag === 'string' ? buildGitTag : 'master'; // 检查缓存中有没有
1239
+ const find = (startPath, filter) => {
1240
+ // 目录不存在
1241
+ if (!fs$4.existsSync(startPath)) {
1242
+ LOG.fail(`${startPath}目录不存在`);
1243
+ process.exit(-1);
1244
+ return;
1245
+ } // 当前目录下的所有文件 / 文件夹
1070
1246
 
1071
- const globalInstance = getGlobalInstance();
1072
- const moduleInCache = globalInstance.getModuleCache(httpRepoUrl, cloneBranch);
1073
1247
 
1074
- try {
1075
- if (!moduleInCache) {
1076
- await downloadRepoForGit(httpRepoUrl, sourcePath, cloneBranch);
1077
- globalInstance.setModuleCache(httpRepoUrl, cloneBranch, sourcePath);
1078
- } else {
1079
- sourcePath = globalInstance.getModuleCache(httpRepoUrl, cloneBranch).dest;
1080
- }
1248
+ const exceptDir = ['node_modules', 'miniprogram_npm'];
1081
1249
 
1082
- if (fs$5.existsSync(targetPath)) {
1083
- shelljs$3.rm('-rf', targetPath);
1250
+ if (exceptDir.find(item => startPath.indexOf(item) > -1)) {
1251
+ return;
1084
1252
  }
1085
1253
 
1086
- await moveFile(sourcePath, targetPath, ['node_modules', '.git']);
1087
- } catch (e) {
1088
- fail$7(`downLoadAndMoveModule ${e}`); // eslint-disable-line
1089
-
1090
- process.exit(-1);
1091
- }
1092
- }
1093
-
1094
- var cloneModules_1 = {
1095
- cloneModules: cloneModules$1
1096
- };
1097
-
1098
- var defaultTmsConfig$1 = {
1099
- // 全局的环境配置项
1100
- envData: {},
1101
- // 模块配置信息
1102
- modules: [],
1103
- webpack: {
1104
- /** 编译输出文件夹位置 */
1105
- outputDir: 'dist',
1106
-
1107
- /** 源码监听路径 */
1108
- sourceDir: './',
1254
+ const files = fs$4.readdirSync(startPath);
1255
+ files.forEach(file => {
1256
+ const filename = path$2.join(startPath, file);
1257
+ const stat = fs$4.lstatSync(filename); // 当前文件是文件夹类型,继续递归
1109
1258
 
1110
- /** 源码Map */
1111
- sourceMap: 'none',
1259
+ if (stat.isDirectory()) {
1260
+ find(filename, filter);
1261
+ } else if (filename.indexOf(filter) >= 0) {
1262
+ // 文件类型
1263
+ result.push(filename);
1264
+ }
1265
+ });
1266
+ };
1112
1267
 
1113
- /** 别名 */
1114
- alias: {}
1115
- }
1268
+ find(startPath, filter);
1269
+ return result;
1116
1270
  };
1117
-
1118
- const loadash = require$$0__default$5;
1119
- const fs$4 = require$$1__default$2;
1120
- const {
1121
- TMS_NAME: TMS_NAME$1,
1122
- TMS_CONFIG_FILENAME,
1123
- MODULE_CONFIG_FILENAME
1124
- } = require$$3;
1125
- const {
1126
- resolve: resolve$6,
1127
- isObject
1128
- } = widgets;
1129
- const {
1130
- setModuleConfig
1131
- } = buildAppJson;
1132
- const defaultTmsConfig = defaultTmsConfig$1;
1133
- const {
1134
- fail: fail$6
1135
- } = log$2;
1136
1271
  /**
1137
- * 读取tms.config.json
1138
- * @param env {string} 环境变量
1272
+ * 找到项目中所有的package.json文件
1273
+ * @param {Array<String>} subRoots 需要安装npm依赖的路径
1274
+ * @param {String} contextDir 命令运行的目录
1275
+ * @returns {Array<String>} 找到的所有package.json文件的路径
1139
1276
  */
1140
1277
 
1141
- const readTmsConfig$1 = function (env) {
1142
- const tmsConfigPath = resolve$6(TMS_CONFIG_FILENAME);
1143
-
1144
- if (!fs$4.existsSync(tmsConfigPath)) {
1145
- fail$6('当前执行目录没有tms.config.js的配置项,请进行配置');
1146
- process.exit(1);
1147
- }
1148
1278
 
1149
- const tmsConfigFn = require(tmsConfigPath);
1279
+ const findAllPackageJson = (subRoots = [], contextDir) => {
1280
+ const packageJsonName = 'package.json'; // 查找文件名
1150
1281
 
1151
- const tmsConfig = tmsConfigFn({
1152
- env
1153
- }); // 合并默认值
1282
+ const cwd = contextDir || dirpath;
1283
+ const result = [path$2.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
1154
1284
 
1155
- loadash.mergeWith(tmsConfig, defaultTmsConfig);
1156
- return tmsConfig;
1157
- };
1158
- /**
1159
- * 从tms.config.json中检索用户传入的有效modules
1160
- * @param { object } tmsConfig
1161
- * @param { array } modules
1162
- * @returns
1163
- */
1285
+ subRoots.forEach(subRoot => {
1286
+ if (!subRoot.root) {
1287
+ LOG.fail(`请检查${subRoot.name}的module.config.json是否有root字段`);
1288
+ process.exit(1);
1289
+ }
1164
1290
 
1291
+ const toppath = path$2.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
1165
1292
 
1166
- const checkModules$1 = function (tmsConfig, modules) {
1167
- const targetModules = [];
1168
- modules.forEach(moduleName => {
1169
- const module = tmsConfig.modules.find(module => module.name === moduleName);
1170
- module && targetModules.push(module);
1293
+ const list = findFilesByFilter(toppath, packageJsonName);
1294
+ result.push(...list);
1171
1295
  });
1172
-
1173
- if (targetModules.length === 0) {
1174
- fail$6(`你启动的模块无效,尝试 ${TMS_NAME$1} -m moduleName`);
1175
- process.exit(1);
1176
- }
1177
-
1178
- return targetModules;
1296
+ return result;
1179
1297
  };
1180
- /**
1181
- * tms.config.js的modules 合并 module.config.json的配置项
1182
- * @param {array} modules
1183
- * @param {string} appName
1184
- * @param {string} moduleDir
1185
- * @returns
1186
- */
1187
-
1188
1298
 
1189
- const tmsModulesMergeLocalModuleCfg$3 = (modules, appName, moduleDir) => {
1190
- const newModules = [];
1191
- modules.forEach(({
1192
- path: relativePath,
1193
- name: moduleName
1194
- }, moduleIndex) => {
1195
- const moduleConfigPath = resolve$6(relativePath, MODULE_CONFIG_FILENAME);
1299
+ function cloudNpmInstall$1(contextDir) {
1300
+ return new Promise((resolve, reject) => {
1301
+ glob(`${contextDir}/**/package.json`, ['node_modules', 'miniprogram_npm'], (err, files) => {
1302
+ if (err) {
1303
+ reject(err);
1304
+ }
1196
1305
 
1197
- if (fs$4.existsSync(moduleConfigPath)) {
1198
- let moduleConfigContent = fs$4.readFileSync(moduleConfigPath, 'utf-8');
1199
- moduleConfigContent = setModuleConfig(moduleConfigContent, appName, moduleDir);
1200
- const moduleContentArr = isObject(moduleConfigContent) ? [moduleConfigContent] : moduleConfigContent;
1201
- moduleContentArr.forEach(({
1202
- name
1203
- }, moduleContentArrIndex) => {
1204
- if (name === moduleName) {
1205
- newModules.push({ ...modules[moduleIndex],
1206
- ...moduleContentArr[moduleContentArrIndex]
1207
- });
1208
- }
1209
- });
1210
- } else {
1211
- newModules.push({ ...modules[moduleIndex]
1306
+ files.forEach(file => {
1307
+ const dir = path$2.dirname(file);
1308
+ shell.cd(dir);
1309
+ shell.exec('npx npm install --production --registry http://mirrors.tencent.com/npm/', {
1310
+ silent: false
1311
+ });
1212
1312
  });
1213
- }
1313
+ resolve();
1314
+ });
1214
1315
  });
1215
- return newModules;
1316
+ }
1317
+
1318
+ var npmUtils = {
1319
+ cloudNpmInstall: cloudNpmInstall$1,
1320
+ mpNpmInstallAll: mpNpmInstallAll$1,
1321
+ findAllPackageJson
1216
1322
  };
1217
1323
 
1218
- var tkitUtils = {
1219
- readTmsConfig: readTmsConfig$1,
1220
- checkModules: checkModules$1,
1221
- tmsModulesMergeLocalModuleCfg: tmsModulesMergeLocalModuleCfg$3
1222
- };
1324
+ const {
1325
+ createTask: createTask$2,
1326
+ resolve: resolve$5
1327
+ } = widgets;
1328
+ const {
1329
+ buildMpNpm
1330
+ } = mpCiUtils;
1331
+ const {
1332
+ CACHE_DIR,
1333
+ DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$2
1334
+ } = require$$3;
1335
+ const {
1336
+ cloudNpmInstall,
1337
+ mpNpmInstallAll
1338
+ } = npmUtils;
1339
+ const {
1340
+ tmsModulesMergeLocalModuleCfg: tmsModulesMergeLocalModuleCfg$2
1341
+ } = tkitUtils;
1342
+
1343
+ async function install$2(tmsConfig, modules, isCloud = true) {
1344
+ const newModules = tmsModulesMergeLocalModuleCfg$2(modules, tmsConfig.appName, DEFAULT_MODULE_DIR$2); // 小程序npm install
1345
+
1346
+ await createTask$2(mpNpmInstallAll, '小程序 开始npm install', '小程序npm install 完成')(newModules, resolve$5(tmsConfig.gulp.outputDir), `${CACHE_DIR}/node_modules`); // 构建miniprograme_npm
1347
+
1348
+ await createTask$2(buildMpNpm, '开始构建miniprograme_npm', '构建miniprograme_npm 完成')({
1349
+ appId: tmsConfig.appId,
1350
+ projectPath: resolve$5('./'),
1351
+ privateKey: tmsConfig.privateKey
1352
+ }); // 安装云函数的
1353
+
1354
+ isCloud && createTask$2(cloudNpmInstall, '云函数npm install', '云函数npm install安装完毕')(resolve$5(tmsConfig.cloudDir));
1355
+ }
1356
+
1357
+ var install_1 = install$2;
1223
1358
 
1224
1359
  const shelljs$2 = require$$5__default;
1225
- const fs$3 = require$$1__default$2;
1360
+ const fs$3 = require$$0__default$1;
1226
1361
  const io = io$2;
1227
1362
  const {
1228
- resolve: resolve$5,
1229
- createTask: createTask$2
1363
+ resolve: resolve$4,
1364
+ createTask: createTask$1
1230
1365
  } = widgets;
1231
1366
  const {
1232
- buildOutputAppJson: buildOutputAppJson$1
1367
+ buildOutputAppJson
1233
1368
  } = buildAppJson;
1234
- const {
1235
- npmInstallAll
1236
- } = npmUtils;
1237
- const {
1238
- buildMpNpm
1239
- } = mpCiUtils;
1240
1369
  const {
1241
1370
  MODULE_CODE_DIR,
1242
- CACHE_DIR,
1243
- DEFAULT_COPY_CONFIG: DEFAULT_COPY_CONFIG$2,
1244
- DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$3
1371
+ DEFAULT_COPY_CONFIG,
1372
+ DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$1
1245
1373
  } = require$$3;
1246
1374
  const {
1247
1375
  cloneModules
1248
1376
  } = cloneModules_1;
1249
1377
  const {
1250
- tmsModulesMergeLocalModuleCfg: tmsModulesMergeLocalModuleCfg$2
1378
+ tmsModulesMergeLocalModuleCfg: tmsModulesMergeLocalModuleCfg$1
1251
1379
  } = tkitUtils;
1252
1380
  const {
1253
- fail: fail$5
1381
+ fail: fail$3
1254
1382
  } = log$2;
1383
+ const install$1 = install_1;
1255
1384
  /**
1256
- * 拷贝package.json\模块的代码到编译输出目录
1385
+ * 拷贝相关配置文件到编译输出目录
1257
1386
  * @param { object } tmsConfig
1258
1387
  * @param { array } modules
1259
1388
  * @param { array } defaultFiles 默认需要拷贝的配置项
@@ -1261,54 +1390,40 @@ const {
1261
1390
  */
1262
1391
 
1263
1392
  const cpFilesToOutput = function (tmsConfig, targetModules, defaultFiles) {
1264
- const outputDir = resolve$5(tmsConfig.webpack.outputDir);
1393
+ const outputDir = resolve$4(tmsConfig.gulp.outputDir);
1265
1394
  io.ensureDirExist(outputDir);
1266
1395
  defaultFiles.forEach(item => {
1267
- if (fs$3.existsSync(resolve$5(item))) {
1268
- shelljs$2.cp('-rf', resolve$5(item), resolve$5(tmsConfig.webpack.outputDir, item));
1396
+ if (fs$3.existsSync(resolve$4(item))) {
1397
+ shelljs$2.cp('-rf', resolve$4(item), resolve$4(tmsConfig.gulp.outputDir, item));
1269
1398
  }
1270
- }); // 拷贝模块的代码到编译输出目录
1399
+ }); // 拷贝模块的package.json到编译输出目录
1271
1400
 
1272
1401
  targetModules.forEach(item => {
1273
- const outputModuleDir = resolve$5(`${tmsConfig.webpack.outputDir}/${item.root}`);
1402
+ const outputModuleDir = resolve$4(`${tmsConfig.gulp.outputDir}/${item.root}`);
1274
1403
 
1275
- if (!fs$3.existsSync(resolve$5(item.path))) {
1276
- fail$5(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
1404
+ if (!fs$3.existsSync(resolve$4(item.path))) {
1405
+ fail$3(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
1277
1406
  process.exit(1);
1278
1407
  }
1279
1408
 
1280
- if (!fs$3.existsSync(outputModuleDir)) {
1281
- shelljs$2.mkdir('-p', outputModuleDir);
1282
- } else {
1283
- // 删除除了node_modules、miniprogram_npm 的其他文件
1284
- // eslint-disable-next-line
1285
- shelljs$2.exec('find . -not \( -name node_modules -or -name miniprogram_npm \) -delete', {
1286
- silent: true
1287
- });
1288
- }
1289
-
1290
- shelljs$2.cp('-Rf', `${resolve$5(item.path)}/*`, outputModuleDir);
1409
+ io.ensureDirExist(outputModuleDir);
1410
+ const modulePackagePath = resolve$4(item.path, 'package.json');
1411
+ if (fs$3.existsSync(modulePackagePath)) shelljs$2.cp('-Rf', modulePackagePath, outputModuleDir);
1291
1412
  });
1292
1413
  };
1293
1414
 
1294
1415
  async function task(tmsConfig, targetModules) {
1295
1416
  // 下载和移动代码
1296
- await createTask$2(cloneModules, '开始下载模块代码完成', '下载模块代码码完成')(MODULE_CODE_DIR, resolve$5('./'), targetModules); // tms.config.js的modules 合并 module.config.json的配置项
1417
+ await createTask$1(cloneModules, '开始下载模块代码完成', '下载模块代码码完成')(MODULE_CODE_DIR, resolve$4('./'), targetModules); // tms.config.js的modules 合并 module.config.json的配置项
1297
1418
 
1298
- const newModules = tmsModulesMergeLocalModuleCfg$2(targetModules, tmsConfig.appName, DEFAULT_MODULE_DIR$3);
1419
+ const newModules = tmsModulesMergeLocalModuleCfg$1(targetModules, tmsConfig.appName, DEFAULT_MODULE_DIR$1);
1299
1420
  console.log('当前init的有效模块', newModules.map(item => item.name)); // 拷贝相关配置文件到输出目录
1300
1421
 
1301
- await createTask$2(cpFilesToOutput, '开始拷贝文件到编译输出目录', '拷贝文件到编译输出目录完成')(tmsConfig, newModules, DEFAULT_COPY_CONFIG$2); // npm install
1302
-
1303
- await createTask$2(npmInstallAll, '开始npm install', 'npm install 完成')(newModules, resolve$5(tmsConfig.webpack.outputDir), `${CACHE_DIR}/node_modules`); // 构建miniprograme_npm
1422
+ await createTask$1(cpFilesToOutput, '开始拷贝文件到编译输出目录', '拷贝文件到编译输出目录完成')(tmsConfig, newModules, DEFAULT_COPY_CONFIG); // install
1304
1423
 
1305
- await createTask$2(buildMpNpm, '开始构建miniprograme_npm', '构建miniprograme_npm 完成')({
1306
- appId: tmsConfig.appId,
1307
- projectPath: resolve$5('./'),
1308
- privateKey: tmsConfig.privateKey
1309
- }); // 动态生成编译后的app.json;
1424
+ await install$1(tmsConfig, newModules, false); // 动态生成编译后的app.json;
1310
1425
 
1311
- await createTask$2(buildOutputAppJson$1, '开始生成编译后的app.json', '生成编译后的app.json完成')(tmsConfig, newModules);
1426
+ await createTask$1(buildOutputAppJson, '开始生成编译后的app.json', '生成编译后的app.json完成')(tmsConfig, newModules);
1312
1427
  return newModules;
1313
1428
  }
1314
1429
 
@@ -1321,443 +1436,363 @@ async function bootstrap(tmsConfig, targetModules) {
1321
1436
 
1322
1437
  var init$5 = bootstrap;
1323
1438
 
1324
- const path$1 = require$$1__default$1;
1325
- const fs$2 = require$$1__default$2;
1326
- const {
1327
- resolve: resolve$4
1328
- } = widgets;
1329
- const {
1330
- buildOutputAppJson
1331
- } = buildAppJson;
1332
- const {
1333
- tmsModulesMergeLocalModuleCfg: tmsModulesMergeLocalModuleCfg$1
1334
- } = tkitUtils;
1335
- const {
1336
- findAllPackageJson
1337
- } = npmUtils;
1338
- const {
1339
- fail: fail$4
1340
- } = log$2;
1341
- const replaceExt = require$$7__default;
1342
- const extensions = ['.ts', '.js'];
1343
-
1344
- function ext(entry, extensions) {
1345
- let newEntry = entry;
1346
-
1347
- try {
1348
- const stat = fs$2.lstatSync(newEntry);
1349
-
1350
- if (stat.isDirectory()) {
1351
- newEntry += newEntry[newEntry.length - 1] === '/' ? 'index' : '/index';
1352
- }
1353
- } catch (e) {}
1354
-
1355
- for (const ext of extensions) {
1356
- const file = replaceExt(newEntry, ext);
1357
-
1358
- if (fs$2.existsSync(file)) {
1359
- return {
1360
- file,
1361
- ext
1362
- };
1363
- }
1364
- }
1365
-
1366
- return null;
1367
- } // 获取所有的package.json里的依赖包Dir
1368
-
1369
-
1370
- function getAlias$1(modules) {
1371
- const alias = {};
1372
- const allPackages = findAllPackageJson(modules, resolve$4('./dist'));
1373
- allPackages.forEach(packageFilePath => {
1374
- const {
1375
- dependencies
1376
- } = require(packageFilePath);
1377
-
1378
- Object.keys(dependencies).forEach(dependence => {
1379
- alias[dependence] = path$1.join(path$1.dirname(packageFilePath), `./miniprogram_npm/${dependence}`);
1380
- });
1381
- });
1382
- return alias;
1383
- } // 根据用户选择的modules,找到module.config.json的配置信息,找到所有的page
1384
-
1385
-
1386
- function getPageEntry(modules, tmsConfig, moduleDir) {
1387
- const entry = {}; // tms.config.js的modules 合并 module.config.json的配置项
1439
+ var dev$3 = {exports: {}};
1388
1440
 
1389
- const newModules = tmsModulesMergeLocalModuleCfg$1(modules, tmsConfig.appName, moduleDir);
1390
- newModules.forEach(({
1391
- path: relativePath,
1392
- pages,
1393
- root
1394
- }) => {
1395
- pages.forEach(page => {
1396
- const pageJsonPath = `${resolve$4(relativePath, page)}.json`;
1441
+ const through$1 = require$$0__default$7;
1397
1442
 
1398
- if (fs$2.existsSync(pageJsonPath)) {
1399
- const pageJsonContent = JSON.parse(fs$2.readFileSync(pageJsonPath, 'utf-8'));
1400
- const pageDir = path$1.dirname(pageJsonPath); // 该页面所在的目录
1443
+ function replaceEnv$1(reg = /process\.env(\.(\w*))?/g, envData) {
1444
+ const stream = through$1.obj(function (file, enc, cb) {
1445
+ if (file.isBuffer()) {
1446
+ let contents = String(file.contents);
1447
+ let resReg; // eslint-disable-next-line
1401
1448
 
1402
- const extValue = ext(resolve$4(relativePath, page), extensions);
1449
+ while ((resReg = reg.exec(contents)) !== null) {
1450
+ const [reg1, reg2, reg3] = resReg;
1403
1451
 
1404
- if (!extValue) {
1405
- fail$4(`当前${page}找不到入口.js或.ts文件`);
1406
- process.exit(1);
1452
+ if (reg1 && reg2 && reg3) {
1453
+ const value = typeof envData[reg3] === 'object' ? JSON.stringify(envData[reg3]) : envData[reg3];
1454
+ contents = contents.replace(reg1, value);
1455
+ } else if (reg1 && !reg2 && !reg3) {
1456
+ contents = contents.replace(reg1, JSON.stringify(envData));
1407
1457
  }
1408
-
1409
- const entryKey = `${root}/${page}${extValue.ext}`;
1410
- Object.assign(entry, {
1411
- [entryKey]: extValue.file
1412
- }, getComponentEntry(pageJsonContent, pageDir, path$1.dirname(entryKey)));
1413
- }
1414
- });
1415
- });
1416
- return entry;
1417
- } // 根据appJson,获取所有的page
1418
-
1419
-
1420
- function getEntry$2(defaultWebpackEntry, modules, tmsConfig, moduleDir) {
1421
- const defaultEntry = {};
1422
- Object.keys(defaultWebpackEntry).forEach(key => {
1423
- const extValue = ext(defaultWebpackEntry[key], extensions);
1424
- defaultEntry[key + extValue.ext] = extValue.file;
1425
- });
1426
- return { ...defaultEntry,
1427
- ...getPageEntry(modules, tmsConfig, moduleDir)
1428
- };
1429
- } // 根据pageJson,filePath,获取页面引入的所有component
1458
+ } // eslint-disable-next-line
1430
1459
 
1431
1460
 
1432
- function getComponentEntry(pageJson, pagePath, pageKey) {
1433
- const componentEntry = {};
1434
-
1435
- function task(json, dir, rootKey) {
1436
- if (!json.usingComponents) {
1437
- return;
1461
+ file.contents = new Buffer(contents);
1438
1462
  }
1439
1463
 
1440
- const componentKeys = Object.keys(json.usingComponents); // 如果存在依赖的组件
1441
-
1442
- componentKeys.forEach(key => {
1443
- if (json.usingComponents[key].startsWith('.')) {
1444
- // 拼出组件所在位置的绝对路径
1445
- const comValue = path$1.join(dir, json.usingComponents[key]);
1446
- const extValue = ext(comValue, extensions);
1447
- const comKey = path$1.resolve('/', rootKey, json.usingComponents[key]);
1448
-
1449
- if (!extValue) {
1450
- fail$4(`当前page: ${pagePath} component: ${comValue}找不到入口.js或.ts文件`);
1451
- process.exit(1);
1452
- }
1453
-
1454
- componentEntry[`${comKey.slice(1)}${extValue.ext}`] = extValue.file;
1455
- const comJsonPath = `${comValue}.json`;
1464
+ this.push(file);
1465
+ cb();
1466
+ });
1467
+ return stream;
1468
+ }
1456
1469
 
1457
- if (fs$2.existsSync(comJsonPath)) {
1458
- const comJsonContent = JSON.parse(fs$2.readFileSync(comJsonPath, 'utf-8'));
1459
- const comDir = path$1.dirname(comJsonPath); // 该页面所在的目录
1470
+ var replaceEnv_1 = replaceEnv$1;
1460
1471
 
1461
- task(comJsonContent, comDir, path$1.dirname(comKey));
1462
- }
1463
- }
1464
- });
1465
- }
1472
+ const through = require$$0__default$7;
1473
+ const precinct = require$$1__default$4;
1474
+ const path$1 = require$$1__default$1;
1475
+ const {
1476
+ copyFile,
1477
+ ext,
1478
+ fileInDir
1479
+ } = io$2;
1480
+ const {
1481
+ resolve: resolve$3
1482
+ } = widgets;
1483
+ const fs$2 = require$$0__default$1;
1484
+ const {
1485
+ fail: fail$2
1486
+ } = log$2;
1466
1487
 
1467
- task(pageJson, pagePath, pageKey);
1468
- return componentEntry;
1469
- } // 根据modules处理需要拷贝的模块,copy-webpack-plugin需要的参数
1488
+ const dfsDep = function (includePath, anaFileOriginFile, anaFileDestFile, extensions) {
1489
+ const contents = fs$2.readFileSync(anaFileOriginFile, 'utf8');
1490
+ const deps = precinct(contents);
1491
+ deps.forEach(depItem => {
1492
+ if (depItem.startsWith('.')) {
1493
+ // 被依赖文件的存在的绝对路径
1494
+ const depOriginPath = path$1.join(path$1.dirname(anaFileOriginFile), depItem); // 被依赖文件加上后缀
1470
1495
 
1496
+ const depOriginPathExt = ext(depOriginPath, extensions);
1471
1497
 
1472
- function getCopyPlugin$2(defaultCopyConfig, modules, tmsConfig, env) {
1473
- const toPath = tmsConfig.webpack.outputDir;
1498
+ if (!depOriginPathExt) {
1499
+ fail$2(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
1500
+ } // 被依赖文件是否存在 include的path中 (只处理include的文件)
1474
1501
 
1475
- function generatorAppJson(tmsConfig, modules) {
1476
- try {
1477
- return buildOutputAppJson(tmsConfig, modules);
1478
- } catch (e) {
1479
- fail$4(`动态生成app.json出现错误${e} 请检查你的配置项`);
1480
- return {};
1481
- }
1482
- }
1483
1502
 
1484
- const patterns = []; // 默认的一些配置拷贝文件 package.json、sitemap.json等
1503
+ if (depOriginPathExt && fileInDir(includePath, depOriginPathExt.file)) {
1504
+ const depOriginFile = depOriginPathExt.file;
1505
+ const depDestPath = resolve$3(path$1.dirname(anaFileDestFile), depItem);
1506
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext) ? depDestPath : depDestPath + depOriginPathExt.ext;
1485
1507
 
1486
- defaultCopyConfig.forEach(item => {
1487
- if (fs$2.existsSync(resolve$4(item))) {
1488
- patterns.push({
1489
- from: resolve$4(item),
1490
- to: resolve$4(`./${toPath}/${item}`),
1491
- transform: {
1492
- cache: true
1493
- }
1494
- });
1495
- }
1496
- }); // 拷贝模块的代码
1508
+ if (fs$2.existsSync(depDestFile)) {
1509
+ const depDestContent = fs$2.readFileSync(depDestFile, 'utf8');
1510
+ const depOriginContent = fs$2.readFileSync(depOriginFile, 'utf8');
1497
1511
 
1498
- modules.forEach(item => {
1499
- patterns.push({
1500
- from: resolve$4(item.path),
1501
- to: resolve$4(`./${toPath}/${item.root}`),
1502
- globOptions: {
1503
- ignore: ['**/*.js', '*.js', '**/*.ts', '*.ts']
1504
- },
1505
- transform: {
1506
- cache: true,
1507
- transformer: env === 'dev' ? (content, absoluteFrom) => {
1508
- // 监听module.config.json的修改, 自动生成编译后的app.json
1509
- if (absoluteFrom.indexOf('module.config.json') > -1) {
1510
- generatorAppJson(tmsConfig, modules);
1512
+ if (depDestContent !== depOriginContent) {
1513
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
1514
+ copyFile(depOriginFile, depDestFile);
1511
1515
  }
1516
+ } else {
1517
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
1518
+ copyFile(depOriginFile, depDestFile);
1519
+ }
1512
1520
 
1513
- return content;
1514
- } : content => content
1521
+ dfsDep(includePath, depOriginFile, depDestFile, extensions);
1515
1522
  }
1516
- });
1523
+ }
1517
1524
  });
1525
+ };
1518
1526
 
1519
- if (env === 'dev') {
1520
- // 拷贝app.json时,自动生成编译后的app.json
1521
- const appJsonConfig = ['app.json'];
1522
- appJsonConfig.forEach(item => {
1523
- patterns.push({
1524
- from: resolve$4(item),
1525
- to: resolve$4(`./${toPath}/${item}`),
1526
- transform: {
1527
- cache: true,
1528
- transformer: () => {
1529
- const appJson = generatorAppJson(tmsConfig, modules);
1530
- return JSON.stringify(appJson, null, 2);
1527
+ function includeThirdParty(tmsConfig, module, extensions = ['.js', '.ts', '.wxs']) {
1528
+ const stream = through.obj(function (file, enc, cb) {
1529
+ // 依赖分析的文件
1530
+ const anaFileOriginFile = file.history[0];
1531
+ const anaFileRelativeModule = path$1.relative(resolve$3(module.from), anaFileOriginFile);
1532
+ const anaFileDestFile = resolve$3(tmsConfig.gulp.outputDir, module.to, anaFileRelativeModule);
1533
+
1534
+ if (file.isBuffer()) {
1535
+ let contents = String(file.contents);
1536
+ const deps = precinct(contents);
1537
+ const copyModules = [];
1538
+ Object.keys(tmsConfig.gulp.dependencies).forEach(includeName => {
1539
+ const includePath = tmsConfig.gulp.dependencies[includeName];
1540
+ deps.forEach(depItem => {
1541
+ if (depItem.indexOf(includeName) > -1) {
1542
+ // 被依赖文件的存在的绝对路径 (eg: /User/thirdparty/loadsh)
1543
+ const depOriginPath = path$1.join(path$1.dirname(anaFileOriginFile), depItem); // 被依赖文件加上后缀
1544
+
1545
+ const depOriginPathExt = ext(depOriginPath, extensions);
1546
+
1547
+ if (!depOriginPathExt) {
1548
+ fail$2(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
1549
+ } // 被依赖文件是否存在 include的path中 (只处理include的文件)
1550
+
1551
+
1552
+ if (depOriginPathExt && fileInDir(includePath, depOriginPathExt.file)) {
1553
+ // eslint-disable-next-line
1554
+ const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
1555
+ const regRes = depItem.match(reg) || [];
1556
+
1557
+ if (regRes[2]) {
1558
+ const depDestPath = resolve$3(tmsConfig.gulp.outputDir, module.to, includeName, regRes[2]);
1559
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext) ? depDestPath : depDestPath + depOriginPathExt.ext; // {
1560
+ // depOriginFile: '/Users/odile/workspace/tms-frontend1/miniprogram/thirdparty/libs/timer.js',
1561
+ // depDestFile: `/Users/odile/workspace/tms-frontend1/tools/demo1/dist/modules/
1562
+ // aggredriving/thirdparty/libs/timer.js`,
1563
+ // beforeDepPath: '../../../../thirdparty/libs/timer',
1564
+ // afterDepPath: '../../thirdparty/libs/timer'
1565
+ // }
1566
+
1567
+ copyModules.push({
1568
+ depOriginFile: depOriginPathExt.file,
1569
+ depDestFile,
1570
+ beforeDepPath: depItem,
1571
+ afterDepPath: path$1.relative(path$1.dirname(anaFileDestFile), depDestPath),
1572
+ includePath
1573
+ });
1574
+ }
1575
+ }
1531
1576
  }
1532
- }
1577
+ });
1533
1578
  });
1534
- });
1535
- }
1536
-
1537
- return patterns;
1538
- } // dev时,给webpack注入相应的事件
1539
-
1540
-
1541
- const setupDevWebPackHooks$1 = (context, firstDone) => {
1542
- let tempFirstDone = true;
1543
-
1544
- const invalid = () => {
1545
- // eslint-disable-next-line
1546
- context.stats = undefined;
1547
- };
1548
-
1549
- const done = stats => {
1550
- // eslint-disable-next-line
1551
- context.stats = stats;
1552
- process.nextTick(() => {
1553
- const {
1554
- stats
1555
- } = context;
1556
- if (!stats) return;
1557
-
1558
- if (tempFirstDone) {
1559
- tempFirstDone = false;
1560
- firstDone();
1561
- }
1562
- });
1563
- };
1564
-
1565
- const {
1566
- compiler
1567
- } = context;
1568
- compiler.hooks.watchRun.tap('miniprogram-dev', invalid);
1569
- compiler.hooks.invalid.tap('miniprogram-dev', invalid);
1570
- compiler.hooks.done.tap('miniprogram-dev', done);
1571
- };
1572
-
1573
- const stringified$1 = raw => ({
1574
- 'process.env': Object.keys(raw).reduce((env, key) => {
1575
- // eslint-disable-next-line
1576
- env[key] = JSON.stringify(raw[key]);
1577
- return env;
1578
- }, {})
1579
- });
1579
+ copyModules.forEach(({
1580
+ includePath,
1581
+ depOriginFile,
1582
+ depDestFile,
1583
+ beforeDepPath,
1584
+ afterDepPath
1585
+ }) => {
1586
+ if (fs$2.existsSync(depDestFile)) {
1587
+ const depDestContent = fs$2.readFileSync(depDestFile, 'utf8');
1588
+ const depOriginContent = fs$2.readFileSync(depOriginFile, 'utf8');
1589
+
1590
+ if (depDestContent !== depOriginContent) {
1591
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
1592
+ copyFile(depOriginFile, depDestFile);
1593
+ }
1594
+ } else {
1595
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
1596
+ copyFile(depOriginFile, depDestFile);
1597
+ }
1580
1598
 
1581
- var utils = {
1582
- getCopyPlugin: getCopyPlugin$2,
1583
- setupDevWebPackHooks: setupDevWebPackHooks$1,
1584
- getEntry: getEntry$2,
1585
- stringified: stringified$1,
1586
- getAlias: getAlias$1
1587
- };
1599
+ contents = contents.replace(beforeDepPath, afterDepPath);
1600
+ dfsDep(includePath, depOriginFile, depDestFile, extensions);
1601
+ }); // eslint-disable-next-line
1588
1602
 
1589
- const WebpackChain = require$$0__default$6;
1590
- const webpack$2 = require$$0__default$7;
1591
- const {
1592
- getEntry: getEntry$1,
1593
- stringified,
1594
- getAlias
1595
- } = utils;
1596
- const {
1597
- resolve: resolve$3
1598
- } = widgets;
1599
- const {
1600
- DEFAULT_WEBPACK_ENTRY: DEFAULT_WEBPACK_ENTRY$1,
1601
- DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$2
1602
- } = require$$3;
1603
+ file.contents = new Buffer(contents);
1604
+ }
1603
1605
 
1604
- var base = (tmsConfig, modules) => {
1605
- const {
1606
- envData = {},
1607
- webpack: tmsWebpack
1608
- } = tmsConfig;
1609
- let webpackConfig = new WebpackChain();
1610
- const alias = getAlias(modules);
1611
- const entry = getEntry$1(DEFAULT_WEBPACK_ENTRY$1, modules, tmsConfig, DEFAULT_MODULE_DIR$2);
1612
- webpackConfig.merge({
1613
- entry,
1614
- output: {
1615
- path: resolve$3(`./${tmsWebpack.outputDir}`),
1616
- libraryTarget: 'commonjs2',
1617
- filename: '[name]'
1618
- },
1619
- // TODO 有报错,先注释掉
1620
- // cache: {
1621
- // type: 'filesystem',
1622
- // cacheDirectory: resolve(`./${tmsConfig.webpack.outputDir}/node_modules/.cache`),
1623
- // },
1624
- resolve: {
1625
- extensions: ['.tsx', '.ts', '.js'],
1626
- alias: Object.assign(tmsWebpack.alias, alias)
1627
- },
1628
- devtool: tmsWebpack.sourceMap ? 'source-map' : false
1606
+ this.push(file);
1607
+ cb();
1629
1608
  });
1630
- webpackConfig.module.rule('ts-loader').test(/\.ts$/).use('ts-loader').loader(require.resolve('ts-loader')).options({
1631
- configFile: resolve$3('./tsconfig.json'),
1632
- // 只进行语法转换,不进行类型校验,提高构建速度
1633
- transpileOnly: true
1634
- }).end(); // webpackConfig.module
1635
- // .rule('babel')
1636
- // .test(/\.(js|mjs|jsx|ts|tsx)$/)
1637
- // .pre()
1638
- // .exclude.add(/(node_modules|miniprogram_npm)/).end()
1639
- // .use(require.resolve('babel-loader'))
1640
- // .loader(require.resolve('babel-loader'));
1641
-
1642
- webpackConfig.plugin('definePlugin').use(webpack$2.DefinePlugin, [stringified(envData)]).end(); // 执行tms.config.js自定义的webpackChain
1643
-
1644
- if (tmsWebpack.webpackChain) {
1645
- webpackConfig = tmsWebpack.webpackChain(webpackConfig, {
1646
- modules
1647
- });
1648
- }
1609
+ return stream;
1610
+ }
1649
1611
 
1650
- return webpackConfig;
1651
- };
1612
+ var includeDep$1 = includeThirdParty;
1652
1613
 
1653
- const SingleEntryPlugin = require$$0__default$8;
1654
- const {
1655
- DEFAULT_WEBPACK_ENTRY,
1656
- DEFAULT_MODULE_DIR: DEFAULT_MODULE_DIR$1
1657
- } = require$$3;
1658
1614
  const {
1659
- getEntry
1660
- } = utils;
1615
+ src: src$1,
1616
+ dest
1617
+ } = require$$0__default$8;
1618
+ const px2rpx = require$$1__default$5;
1619
+ const less = require$$2__default$1;
1620
+ const rename = require$$3__default$1;
1621
+ const watch = require$$4__default;
1622
+ const image = require$$5__default$1;
1623
+ const replaceEnv = replaceEnv_1;
1624
+ const includeDep = includeDep$1;
1625
+
1626
+ var compile = function (tmsConfig, {
1627
+ glob,
1628
+ destPath,
1629
+ srcOption,
1630
+ module,
1631
+ watchOption = {
1632
+ events: ['change', 'add', 'unlink']
1633
+ },
1634
+ isWatch
1635
+ }) {
1636
+ Object.keys(glob).forEach(globKey => {
1637
+ const globValue = glob[globKey];
1638
+ let srcPipe = src$1(globValue, srcOption);
1661
1639
 
1662
- class EntryExtraPlugin {
1663
- constructor(options = {}) {
1664
- this.options = options;
1665
- this.scriptExtensions = options.scriptExtensions || ['.ts', '.js'];
1666
- }
1640
+ if (isWatch) {
1641
+ srcPipe = srcPipe.pipe(watch(globValue, watchOption));
1642
+ }
1667
1643
 
1668
- applyEntry(compiler, entry) {
1669
- const {
1670
- context
1671
- } = compiler.options;
1672
- Object.keys(entry).forEach(key => {
1673
- new SingleEntryPlugin(context, entry[key], key).apply(compiler);
1674
- });
1675
- }
1644
+ switch (globKey) {
1645
+ case 'js':
1646
+ srcPipe.pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData)).pipe(includeDep(tmsConfig, module)).pipe(dest(destPath));
1647
+ break;
1648
+
1649
+ case 'css':
1650
+ srcPipe.pipe(less()).pipe(px2rpx({
1651
+ designWidth: 375,
1652
+ // 设计稿宽度,默认为750
1653
+ precision: 2 // 小数最大精度,默认为6
1654
+
1655
+ })).pipe(rename({
1656
+ extname: '.wxss'
1657
+ })).pipe(dest(destPath));
1658
+ break;
1659
+
1660
+ case 'json':
1661
+ srcPipe.pipe(dest(destPath));
1662
+ break;
1663
+
1664
+ case 'html':
1665
+ srcPipe.pipe(dest(destPath));
1666
+ break;
1667
+
1668
+ case 'image':
1669
+ srcPipe.pipe(image()).pipe(dest(destPath));
1670
+ break;
1671
+
1672
+ case 'other':
1673
+ srcPipe.pipe(dest(destPath));
1674
+ break;
1675
+ }
1676
+ });
1677
+ };
1676
1678
 
1677
- apply(compiler) {
1678
- const {
1679
- tmsConfig = {},
1680
- modules = []
1681
- } = this.options;
1682
- compiler.hooks.watchRun.tap('EntryExtraPlugin', () => {
1683
- const entry = getEntry(DEFAULT_WEBPACK_ENTRY, modules, tmsConfig, DEFAULT_MODULE_DIR$1);
1684
- this.applyEntry(compiler, entry);
1685
- });
1686
- }
1679
+ (function (module) {
1680
+ const watch = require$$4__default;
1681
+ const {
1682
+ resolve
1683
+ } = widgets;
1684
+ const {
1685
+ buildOutputAppJson
1686
+ } = buildAppJson;
1687
+ const {
1688
+ DEFAULT_COPY_CONFIG
1689
+ } = require$$3;
1690
+ const compile$1 = compile;
1691
+
1692
+ module.exports = async (tmsConfig, newModules, isWatch = true) => {
1693
+ var _tmsConfig$gulp;
1694
+
1695
+ // 监听app.json
1696
+ if (isWatch) {
1697
+ watch(resolve('app.json'), {
1698
+ ignoreInitial: false,
1699
+ events: ['add', 'change']
1700
+ }, () => {
1701
+ buildOutputAppJson(tmsConfig, newModules);
1702
+ });
1703
+ } else {
1704
+ buildOutputAppJson(tmsConfig, newModules);
1705
+ } // 监听根目录的文件
1687
1706
 
1688
- }
1689
1707
 
1690
- var entryExtractPlugin = EntryExtraPlugin;
1708
+ compile$1(tmsConfig, {
1709
+ glob: {
1710
+ json: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
1711
+ css: ['app.less', 'app.wxss'].map(item => resolve(item)),
1712
+ js: ['app.js', 'app.ts'].map(item => resolve(item))
1713
+ },
1714
+ module: {
1715
+ from: '',
1716
+ to: ''
1717
+ },
1718
+ destPath: resolve(tmsConfig.gulp.outputDir),
1719
+ srcOption: {
1720
+ allowEmpty: true
1721
+ },
1722
+ isWatch
1723
+ }); // 监听模块的文件
1724
+
1725
+ for (const module of newModules) {
1726
+ if (isWatch) {
1727
+ // 监听模块配置文件
1728
+ watch(`${resolve(module.path)}/**/module.config.json`, {
1729
+ events: ['change']
1730
+ }, () => {
1731
+ buildOutputAppJson(tmsConfig, newModules);
1732
+ });
1733
+ } else {
1734
+ buildOutputAppJson(tmsConfig, newModules);
1735
+ }
1691
1736
 
1692
- const webpackConfig$1 = base;
1693
- const {
1694
- DEFAULT_COPY_CONFIG: DEFAULT_COPY_CONFIG$1
1695
- } = require$$3;
1696
- const {
1697
- getCopyPlugin: getCopyPlugin$1
1698
- } = utils;
1699
-
1700
- var dev$2 = (...args) => {
1701
- const [tmsConfig, modules] = args;
1702
- const webpackDevConfig = webpackConfig$1(...args);
1703
- webpackDevConfig.devtool('source-map');
1704
- webpackDevConfig.mode('development');
1705
- const copyPluginParams = getCopyPlugin$1(DEFAULT_COPY_CONFIG$1, modules, tmsConfig, 'dev'); // console.log('copyPluginParams', copyPluginParams);
1706
-
1707
- webpackDevConfig.plugin('copy-webpack-plugin').use(require$$3__default, [{
1708
- patterns: copyPluginParams
1709
- }]).end();
1710
- webpackDevConfig.plugin('ExtractPlugin').use(entryExtractPlugin, [{
1711
- tmsConfig,
1712
- modules
1713
- }]).end();
1714
- return webpackDevConfig;
1715
- };
1737
+ const glob = {
1738
+ js: `${resolve(module.path)}/**/*.{js,ts,wxs}`,
1739
+ json: `${resolve(module.path)}/**/*.json`,
1740
+ css: `${resolve(module.path)}/**/*.{less,wxss}`,
1741
+ html: `${resolve(module.path)}/**/*.wxml`,
1742
+ image: `${resolve(module.path)}/**/*.{png,jpg,jpeg,gif,svg}`
1743
+ };
1744
+ compile$1(tmsConfig, {
1745
+ glob: { ...glob,
1746
+ other: [`${resolve(module.path)}/**/*`, ...Object.keys(glob).map(key => `!${glob[key]}`)]
1747
+ },
1748
+ destPath: resolve(tmsConfig.gulp.outputDir, module.root),
1749
+ module: {
1750
+ from: module.path,
1751
+ to: module.root
1752
+ },
1753
+ srcOption: {
1754
+ allowEmpty: true
1755
+ },
1756
+ isWatch
1757
+ });
1758
+ } // 监听copy模块
1716
1759
 
1717
- const webpack$1 = require$$0__default$7;
1718
- const webDevConfig = dev$2;
1719
- const {
1720
- setupDevWebPackHooks
1721
- } = utils;
1722
- const {
1723
- fail: fail$3
1724
- } = log$2;
1725
1760
 
1726
- var devServer = (...args) => {
1727
- const config = webDevConfig(...args);
1728
- const compiler = webpack$1(config.toConfig());
1729
- setupDevWebPackHooks({
1730
- compiler
1731
- }, () => {// TODO 判断open参数,打开微信开发者工具
1732
- // openDevtool(api.resolve(config.outputDir || 'dist'))
1733
- });
1734
- compiler.watch({
1735
- aggregateTimeout: 1000,
1736
- poll: undefined
1737
- }, (err, stats) => {
1738
- if (err) {
1739
- fail$3(err);
1740
- console.log('详细的错误信息:', err);
1741
- }
1761
+ if ((tmsConfig === null || tmsConfig === void 0 ? void 0 : (_tmsConfig$gulp = tmsConfig.gulp) === null || _tmsConfig$gulp === void 0 ? void 0 : _tmsConfig$gulp.copyDir.length) > 0) {
1762
+ for (const item of (_tmsConfig$gulp2 = tmsConfig.gulp) === null || _tmsConfig$gulp2 === void 0 ? void 0 : _tmsConfig$gulp2.copyDir) {
1763
+ var _tmsConfig$gulp2;
1742
1764
 
1743
- if (stats.hasErrors() || stats.hasWarnings()) {
1744
- console.log(stats.toString({
1745
- // 增加控制台颜色开关
1746
- colors: true
1747
- }));
1765
+ const glob = {
1766
+ js: `${item.from}/**/*.{js,ts,wxs}`,
1767
+ json: `${item.from}/**/*.json`,
1768
+ css: `${item.from}/**/*.{less,wxss}`,
1769
+ html: `${item.from}/**/*.wxml`,
1770
+ image: `${item.from}/**/*.{png,jpg,jpeg,gif,svg}`
1771
+ };
1772
+ compile$1(tmsConfig, {
1773
+ glob: { ...glob,
1774
+ other: [`${item.from}/**/*`, ...Object.keys(glob).map(key => `!${glob[key]}`)]
1775
+ },
1776
+ destPath: item.to,
1777
+ module: item,
1778
+ srcOption: {
1779
+ allowEmpty: true
1780
+ },
1781
+ isWatch
1782
+ });
1783
+ }
1748
1784
  }
1749
- });
1750
- return compiler;
1751
- };
1785
+ };
1786
+ })(dev$3);
1752
1787
 
1753
- const fs$1 = require$$1__default$2;
1754
- const semver$1 = require$$1__default$5;
1788
+ const fs$1 = require$$0__default$1;
1789
+ const semver$1 = require$$1__default$6;
1755
1790
  const {
1756
1791
  resolve: resolve$2
1757
1792
  } = widgets;
1758
1793
  const path = require$$1__default$1;
1759
1794
  const {
1760
- fail: fail$2
1795
+ fail: fail$1
1761
1796
  } = log$2;
1762
1797
  const shelljs$1 = require$$5__default;
1763
1798
 
@@ -1802,7 +1837,7 @@ const checkDependencies$1 = (modules, cwd, outputDir) => {
1802
1837
  try {
1803
1838
  dependencies = JSON.parse(packageJson).dependencies;
1804
1839
  } catch (e) {
1805
- fail$2(`解析${packageJson}报错,请检查是否是正确的json配置项`);
1840
+ fail$1(`解析${packageJson}报错,请检查是否是正确的json配置项`);
1806
1841
  process.exit(1);
1807
1842
  }
1808
1843
 
@@ -1842,8 +1877,8 @@ var checkDependencies_1 = {
1842
1877
  checkDependencies: checkDependencies$1
1843
1878
  };
1844
1879
 
1845
- const webpackServer = devServer;
1846
- const fs = require$$1__default$2;
1880
+ const gulpDev = dev$3.exports;
1881
+ const fs = require$$0__default$1;
1847
1882
  const {
1848
1883
  resolve: resolve$1
1849
1884
  } = widgets;
@@ -1858,7 +1893,7 @@ const {
1858
1893
  checkDependencies
1859
1894
  } = checkDependencies_1;
1860
1895
  const {
1861
- fail: fail$1
1896
+ fail
1862
1897
  } = log$2;
1863
1898
 
1864
1899
  function isInit(tmsConfig, targetModules, contextDir) {
@@ -1896,16 +1931,16 @@ function isInit(tmsConfig, targetModules, contextDir) {
1896
1931
 
1897
1932
 
1898
1933
  if (item.path && !fs.existsSync(resolve$1(item.path))) {
1899
- fail$1(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
1934
+ fail(`${item.path}模块代码路径不存在, 请检查tms.config.js的${item.name}模块的path`);
1900
1935
  process.exit(1);
1901
1936
  }
1902
1937
  } // 判断package.json的版本是否有新的版本
1903
1938
 
1904
1939
 
1905
- return checkDependencies(targetModules, resolve$1('./'), tmsConfig.webpack.outputDir);
1940
+ return checkDependencies(targetModules, resolve$1('./'), tmsConfig.gulp.outputDir);
1906
1941
  }
1907
1942
 
1908
- async function dev$1(tmsConfig, targetModules, env) {
1943
+ async function dev$2(tmsConfig, targetModules, env) {
1909
1944
  // tms.config.js的modules 合并 module.config.json的配置项
1910
1945
  let newModules = tmsModulesMergeLocalModuleCfg(targetModules, tmsConfig.appName, DEFAULT_MODULE_DIR); // 判断是否进行init命令
1911
1946
 
@@ -1916,88 +1951,31 @@ async function dev$1(tmsConfig, targetModules, env) {
1916
1951
  }
1917
1952
 
1918
1953
  console.log('当前dev启动的有效模块', newModules.map(item => item.name));
1919
- webpackServer(tmsConfig, newModules, env);
1920
- }
1921
-
1922
- var dev_1 = dev$1;
1923
-
1924
- const webpackConfig = base;
1925
- const {
1926
- DEFAULT_COPY_CONFIG
1927
- } = require$$3;
1928
- const {
1929
- getCopyPlugin
1930
- } = utils;
1931
-
1932
- var build$2 = (...args) => {
1933
- const [tmsConfig, modules] = args;
1934
- const webpackBuildConfig = webpackConfig(...args);
1935
- webpackBuildConfig.mode('production');
1936
- const copyPluginParams = getCopyPlugin(DEFAULT_COPY_CONFIG, modules, tmsConfig, 'build'); // console.log('copyPluginParams', copyPluginParams);
1937
-
1938
- webpackBuildConfig.plugin('copy-webpack-plugin').use(require$$3__default, [{
1939
- patterns: copyPluginParams
1940
- }]).end();
1941
- return webpackBuildConfig;
1942
- };
1943
-
1944
- const webpack = require$$0__default$7;
1945
- const webBuildConfig = build$2;
1946
- const {
1947
- createTask: createTask$1
1948
- } = widgets;
1949
- const {
1950
- fail
1951
- } = log$2;
1952
-
1953
- function compilerRun(compiler) {
1954
- return new Promise(resolve => {
1955
- compiler.run((err, stats) => {
1956
- resolve({
1957
- err,
1958
- stats
1959
- });
1960
- });
1961
- });
1954
+ gulpDev(tmsConfig, newModules, env);
1962
1955
  }
1963
1956
 
1964
- var buildServer = async (...args) => {
1965
- const config = webBuildConfig(...args);
1966
- const compiler = webpack(config.toConfig());
1967
- const {
1968
- err,
1969
- stats
1970
- } = await createTask$1(compilerRun, '开始webpack打包编译', 'webpack打包编译完成')(compiler);
1957
+ var dev_1 = dev$2;
1971
1958
 
1972
- if (err) {
1973
- fail(err);
1974
- console.log('详细的错误信息:', err);
1975
- }
1959
+ const dev$1 = dev$3.exports;
1976
1960
 
1977
- if (stats.hasErrors() || stats.hasWarnings()) {
1978
- console.log(stats.toString({
1979
- // 增加控制台颜色开关
1980
- colors: true,
1981
- errorDetails: true
1982
- }));
1983
- }
1984
- return compiler;
1961
+ var build$2 = async (tmsConfig, newModules) => {
1962
+ dev$1(tmsConfig, newModules, false);
1985
1963
  };
1986
1964
 
1987
1965
  const shelljs = require$$5__default;
1988
- const webpackBuildServer = buildServer;
1989
1966
  const {
1990
1967
  resolve
1991
1968
  } = widgets;
1992
1969
  const init$3 = init$5;
1970
+ const gulpBuild = build$2;
1993
1971
 
1994
1972
  async function build$1(tmsConfig, targetModules, env) {
1995
1973
  // 开始构建前,清理输出目录
1996
- await shelljs.rm('-rf', resolve('dist'));
1974
+ await shelljs.rm('-rf', resolve(tmsConfig.gulp.outputDir));
1997
1975
  const {
1998
1976
  targetModules: newModules
1999
1977
  } = await init$3(tmsConfig, targetModules);
2000
- webpackBuildServer(tmsConfig, newModules, env);
1978
+ gulpBuild(tmsConfig, newModules, env);
2001
1979
  }
2002
1980
 
2003
1981
  var build_1 = build$1;
@@ -2005,6 +1983,7 @@ var build_1 = build$1;
2005
1983
  const init$2 = init$5;
2006
1984
  const dev = dev_1;
2007
1985
  const build = build_1;
1986
+ const install = install_1;
2008
1987
  const {
2009
1988
  createTask
2010
1989
  } = widgets;
@@ -2023,8 +2002,13 @@ const handleModulesArg = cmd => {
2023
2002
  } // 单模块 或 多模块开发
2024
2003
 
2025
2004
 
2005
+ const {
2006
+ argv
2007
+ } = process;
2008
+ const reset = argv.indexOf('-m') > -1 ? argv.slice(argv.indexOf('-m') + 1) : [];
2009
+
2026
2010
  if (cmd.module) {
2027
- return [cmd.module, ...cmd.args];
2011
+ return [cmd.module, ...reset];
2028
2012
  } // 全量模块
2029
2013
 
2030
2014
 
@@ -2058,6 +2042,10 @@ async function run(commandName, cmd) {
2058
2042
  dev(tmsConfig, modules, env);
2059
2043
  return;
2060
2044
 
2045
+ case 'install':
2046
+ install(tmsConfig, modules, env);
2047
+ return;
2048
+
2061
2049
  case 'build':
2062
2050
  build(tmsConfig, modules, env);
2063
2051
  return;
@@ -2076,17 +2064,43 @@ var entry = [{
2076
2064
  create_1(appName, cmd);
2077
2065
  }
2078
2066
  }, {
2079
- command: 'run <command-name>',
2080
- options: [['-m, --module [moduleName]', '模块名称'], ['-e, --env [env]', '环境变量']],
2081
- description: '运行模块',
2082
- action: (commandName, cmd) => {
2083
- run_1(commandName, cmd);
2084
- }
2067
+ name: 'run',
2068
+ type: 'child',
2069
+ description: '项目开发使用的命令',
2070
+ commands: [{
2071
+ command: 'install',
2072
+ description: '安装依赖',
2073
+ options: [['-m, --module [moduleName]', '模块名称'], ['-e, --env [env]', '环境变量']],
2074
+ action: cmd => {
2075
+ run_1('install', cmd);
2076
+ }
2077
+ }, {
2078
+ command: 'dev',
2079
+ description: 'dev 打包编译',
2080
+ options: [['-m, --module [moduleName]', '模块名称'], ['-e, --env [env]', '环境变量']],
2081
+ action: cmd => {
2082
+ run_1('dev', cmd);
2083
+ }
2084
+ }, {
2085
+ command: 'build',
2086
+ description: 'prod 打包编译',
2087
+ options: [['-m, --module [moduleName]', '模块名称'], ['-e, --env [env]', '环境变量']],
2088
+ action: cmd => {
2089
+ run_1('build', cmd);
2090
+ }
2091
+ }, {
2092
+ command: 'init',
2093
+ description: '根据模块配置初始化项目(eg: 动态拷贝模块、下载依赖、生成app.json等)',
2094
+ options: [['-m, --module [moduleName]', '模块名称'], ['-e, --env [env]', '环境变量']],
2095
+ action: cmd => {
2096
+ run_1('init', cmd);
2097
+ }
2098
+ }]
2085
2099
  }];
2086
2100
 
2087
2101
  var require$$6 = {
2088
2102
  name: "@tmsfe/tmskit",
2089
- version: "0.0.2",
2103
+ version: "0.0.5-beta.1",
2090
2104
  description: "tmskit",
2091
2105
  main: "main.js",
2092
2106
  bin: {
@@ -2107,62 +2121,49 @@ var require$$6 = {
2107
2121
  "@rollup/plugin-babel": "^5.0.2",
2108
2122
  "@rollup/plugin-commonjs": "^19.0.0",
2109
2123
  "@rollup/plugin-dynamic-import-vars": "^1.1.1",
2110
- "@rollup/plugin-json": "^4.0.3"
2111
- },
2112
- dependencies: {
2113
- "@babel/core": "^7.15.0",
2114
- "@babel/plugin-transform-modules-commonjs": "^7.16.0",
2115
- "@babel/preset-env": "^7.16.0",
2116
- async: "^3.2.2",
2117
- "babel-core": "^6.26.3",
2118
- "babel-loader": "^8.2.3",
2119
- "copy-webpack-plugin": "^9.1.0",
2120
- "cross-env": "^7.0.3",
2121
- leven: "3.1.0",
2122
- metalsmith: "^2.3.0",
2123
- minimist: "^1.2.5",
2124
+ "@rollup/plugin-json": "^4.0.3",
2124
2125
  rollup: "^2.6.1",
2125
2126
  "rollup-plugin-node-resolve": "^5.2.0",
2127
+ "rollup-plugin-replace": "^2.2.0",
2126
2128
  "rollup-plugin-terser": "^6.1.0",
2127
- "rollup-plugin-typescript2": "0.27.0",
2128
- "ts-loader": "^9.2.6",
2129
- "url-loader": "^4.1.1",
2130
- webpack: "^5.64.0",
2131
- "webpack-cli": "^4.9.1",
2132
- axios: "^0.21.4",
2129
+ "rollup-plugin-typescript2": "0.27.0"
2130
+ },
2131
+ dependencies: {
2132
+ async: "^3.2.2",
2133
2133
  chalk: "^4.1.0",
2134
- commander: "^6.2.1",
2134
+ commander: "^8.3.0",
2135
+ "copy-webpack-plugin": "^9.1.0",
2135
2136
  "download-git-repo": "^3.0.2",
2136
2137
  ejs: "^3.1.5",
2137
- "file-loader": "^6.2.0",
2138
- "fs-extra": "^9.0.1",
2138
+ "glob-ignore": "^1.0.2",
2139
+ gulp: "^4.0.2",
2140
+ "gulp-image": "^5.1.0",
2141
+ "gulp-less": "^5.0.0",
2142
+ "gulp-px-to-rpx": "^1.0.7",
2143
+ "gulp-rename": "^2.0.0",
2144
+ "gulp-watch": "^5.0.1",
2139
2145
  inquirer: "^7.3.3",
2146
+ leven: "3.1.0",
2140
2147
  lodash: "^4.17.21",
2141
- "mini-css-extract-plugin": "^2.4.5",
2148
+ metalsmith: "^2.3.0",
2142
2149
  "miniprogram-ci": "1.4.13",
2143
2150
  ora: "^5.1.0",
2151
+ precinct: "^8.3.1",
2144
2152
  "replace-ext": "^2.0.0",
2145
- "rollup-plugin-replace": "^2.2.0",
2146
2153
  shelljs: "^0.8.4",
2147
- tslib: "^1.14.1",
2148
- typescript: "^3.8.3",
2149
- username: "5.1.0",
2150
- "webpack-chain": "^6.5.1"
2154
+ through2: "^4.0.2"
2151
2155
  },
2152
2156
  engines: {
2153
- node: "^12.17.0 || >= 14.0.0"
2157
+ node: "^12.17.0 || >= 14.13.1"
2154
2158
  },
2155
2159
  jest: {
2156
2160
  testEnvironment: "jest-environment-node"
2157
2161
  }
2158
2162
  };
2159
2163
 
2160
- const {
2161
- exec
2162
- } = require$$5__default;
2163
- const semver = require$$1__default$5;
2164
+ const semver = require$$1__default$6;
2164
2165
  const packageJson = require$$6;
2165
- const chalk$1 = require$$0__default$1;
2166
+ const chalk$1 = require$$0__default$2;
2166
2167
  const {
2167
2168
  log: log$1
2168
2169
  } = widgets;
@@ -2192,17 +2193,13 @@ const checkNodeVersion = (wanted, id) => {
2192
2193
  function initCliContext() {
2193
2194
  // 执行操作前检查node版本
2194
2195
  // 旧版本node直接提示升级,退出脚本
2195
- checkNodeVersion(requiredVersion, packName); // 执行前配置正确的npm源
2196
-
2197
- exec('npm config set registry https://mirrors.tencent.com/npm/ --global', {
2198
- silent: true
2199
- });
2196
+ checkNodeVersion(requiredVersion, packName);
2200
2197
  }
2201
2198
 
2202
2199
  var init$1 = initCliContext;
2203
2200
 
2204
- const chalk = require$$0__default$1;
2205
- const program = require$$0__default;
2201
+ const chalk = require$$0__default$2;
2202
+ const commander = require$$0__default;
2206
2203
  const {
2207
2204
  log,
2208
2205
  suggestCommands
@@ -2213,19 +2210,33 @@ const {
2213
2210
  const commands = entry;
2214
2211
  const init = init$1;
2215
2212
  init();
2216
- program.version(`${TMS_NAME} ${require$$6.version}`);
2217
- commands.forEach(cmd => {
2218
- var _cmd$options;
2219
-
2220
- const command = program.command(cmd.command);
2221
- cmd.usage && command.usage(cmd.usage);
2222
- cmd.description && command.description(cmd.description);
2223
- (_cmd$options = cmd.options) === null || _cmd$options === void 0 ? void 0 : _cmd$options.forEach(opt => command.option(...opt));
2224
- command.action(cmd.action);
2225
- });
2213
+ const program = new commander.Command(TMS_NAME);
2214
+ program.version(`${TMS_NAME} ${require$$6.version}`, '-v, -V, --version');
2215
+
2216
+ function registerCommand(program, commands) {
2217
+ commands.forEach(cmd => {
2218
+ if (cmd.type === 'child') {
2219
+ const childProgram = new commander.Command(cmd.name);
2220
+ cmd.usage && childProgram.usage(cmd.usage);
2221
+ cmd.description && childProgram.description(cmd.description);
2222
+ registerCommand(childProgram, cmd.commands);
2223
+ program.addCommand(childProgram);
2224
+ } else {
2225
+ var _cmd$options;
2226
+
2227
+ const command = program.command(cmd.command);
2228
+ cmd.usage && command.usage(cmd.usage);
2229
+ cmd.description && command.description(cmd.description);
2230
+ (_cmd$options = cmd.options) === null || _cmd$options === void 0 ? void 0 : _cmd$options.forEach(opt => command.option(...opt));
2231
+ command.action(cmd.action);
2232
+ }
2233
+ });
2234
+ }
2235
+
2236
+ registerCommand(program, commands);
2226
2237
  program.on('--help', () => {
2227
2238
  log();
2228
- log(` Run ${chalk.cyan('tms <command> --help')} for detailed usage of given command.`);
2239
+ log(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
2229
2240
  log();
2230
2241
  }); // 捕获未注册的命令
2231
2242
 
@@ -2243,6 +2254,6 @@ if (!process.argv.slice(2).length) {
2243
2254
  }
2244
2255
 
2245
2256
  program.parse(process.argv);
2246
- var src = src$1;
2257
+ var src = src$2;
2247
2258
 
2248
2259
  module.exports = src;