@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.2",
3
+ "version": "0.0.5-beta.1",
4
4
  "description": "tmskit",
5
5
  "main": "main.js",
6
6
  "bin": {
@@ -21,50 +21,40 @@
21
21
  "@rollup/plugin-babel": "^5.0.2",
22
22
  "@rollup/plugin-commonjs": "^19.0.0",
23
23
  "@rollup/plugin-dynamic-import-vars": "^1.1.1",
24
- "@rollup/plugin-json": "^4.0.3"
25
- },
26
- "dependencies": {
27
- "@babel/core": "^7.15.0",
28
- "@babel/plugin-transform-modules-commonjs": "^7.16.0",
29
- "@babel/preset-env": "^7.16.0",
30
- "async": "^3.2.2",
31
- "babel-core": "^6.26.3",
32
- "babel-loader": "^8.2.3",
33
- "copy-webpack-plugin": "^9.1.0",
34
- "cross-env": "^7.0.3",
35
- "leven": "3.1.0",
36
- "metalsmith": "^2.3.0",
37
- "minimist": "^1.2.5",
24
+ "@rollup/plugin-json": "^4.0.3",
38
25
  "rollup": "^2.6.1",
39
26
  "rollup-plugin-node-resolve": "^5.2.0",
27
+ "rollup-plugin-replace": "^2.2.0",
40
28
  "rollup-plugin-terser": "^6.1.0",
41
- "rollup-plugin-typescript2": "0.27.0",
42
- "ts-loader": "^9.2.6",
43
- "url-loader": "^4.1.1",
44
- "webpack": "^5.64.0",
45
- "webpack-cli": "^4.9.1",
46
- "axios": "^0.21.4",
29
+ "rollup-plugin-typescript2": "0.27.0"
30
+ },
31
+ "dependencies": {
32
+ "async": "^3.2.2",
47
33
  "chalk": "^4.1.0",
48
- "commander": "^6.2.1",
34
+ "commander": "^8.3.0",
35
+ "copy-webpack-plugin": "^9.1.0",
49
36
  "download-git-repo": "^3.0.2",
50
37
  "ejs": "^3.1.5",
51
- "file-loader": "^6.2.0",
52
- "fs-extra": "^9.0.1",
38
+ "glob-ignore": "^1.0.2",
39
+ "gulp": "^4.0.2",
40
+ "gulp-image": "^5.1.0",
41
+ "gulp-less": "^5.0.0",
42
+ "gulp-px-to-rpx": "^1.0.7",
43
+ "gulp-rename": "^2.0.0",
44
+ "gulp-watch": "^5.0.1",
53
45
  "inquirer": "^7.3.3",
46
+ "leven": "3.1.0",
54
47
  "lodash": "^4.17.21",
55
- "mini-css-extract-plugin": "^2.4.5",
48
+ "metalsmith": "^2.3.0",
56
49
  "miniprogram-ci": "1.4.13",
57
50
  "ora": "^5.1.0",
51
+ "precinct": "^8.3.1",
58
52
  "replace-ext": "^2.0.0",
59
- "rollup-plugin-replace": "^2.2.0",
60
53
  "shelljs": "^0.8.4",
61
- "tslib": "^1.14.1",
62
- "typescript": "^3.8.3",
63
- "username": "5.1.0",
64
- "webpack-chain": "^6.5.1"
54
+ "through2": "^4.0.2"
65
55
  },
66
56
  "engines": {
67
- "node": "^12.17.0 || >= 14.0.0"
57
+ "node": "^12.17.0 || >= 14.13.1"
68
58
  },
69
59
  "jest": {
70
60
  "testEnvironment": "jest-environment-node"
@@ -34,7 +34,7 @@ const DEFAULT_WEBPACK_ENTRY = {
34
34
  };
35
35
 
36
36
  // 默认从源码拷贝到编译后的配置
37
- const DEFAULT_COPY_CONFIG = ['package.json', 'app.js', 'app.ts', 'app.less', 'app.wxss', 'sitemap.json'];
37
+ const DEFAULT_COPY_CONFIG = ['package.json', 'sitemap.json'];
38
38
 
39
39
  // 开发模式
40
40
  const MODE = {
@@ -48,6 +48,8 @@ const ENV = {
48
48
  prod: 'production',
49
49
  };
50
50
 
51
+ const TEMPLATE_TKIT_DIR = '_tmskit';
52
+
51
53
  export {
52
54
  HOME_DIR,
53
55
  CACHE_DIR,
@@ -62,4 +64,5 @@ export {
62
64
  MODULE_CODE_DIR,
63
65
  MODE,
64
66
  ENV,
67
+ TEMPLATE_TKIT_DIR,
65
68
  };
@@ -4,14 +4,15 @@ module.exports = {
4
4
  envData: {},
5
5
  // 模块配置信息
6
6
  modules: [],
7
- webpack: {
7
+ cloudDir: 'cloud',
8
+ gulp: {
9
+ // 第三方依赖代码需要拷贝到本项目的
10
+ dependencies: {},
8
11
  /** 编译输出文件夹位置 */
9
12
  outputDir: 'dist',
10
13
  /** 源码监听路径 */
11
14
  sourceDir: './',
12
15
  /** 源码Map */
13
- sourceMap: 'none',
14
- /** 别名 */
15
- alias: {},
16
+ copyDir: [],
16
17
  },
17
18
  };
package/src/entry.js CHANGED
@@ -7,14 +7,54 @@ module.exports = [
7
7
  },
8
8
  },
9
9
  {
10
- command: 'run <command-name>',
11
- options: [
12
- ['-m, --module [moduleName]', '模块名称'],
13
- ['-e, --env [env]', '环境变量'],
10
+ name: 'run',
11
+ type: 'child',
12
+ description: '项目开发使用的命令',
13
+ commands: [
14
+ {
15
+ command: 'install',
16
+ description: '安装依赖',
17
+ options: [
18
+ ['-m, --module [moduleName]', '模块名称'],
19
+ ['-e, --env [env]', '环境变量'],
20
+ ],
21
+ action: (cmd) => {
22
+ require('./scripts/run/index')('install', cmd);
23
+ },
24
+ },
25
+ {
26
+ command: 'dev',
27
+ description: 'dev 打包编译',
28
+ options: [
29
+ ['-m, --module [moduleName]', '模块名称'],
30
+ ['-e, --env [env]', '环境变量'],
31
+ ],
32
+ action: (cmd) => {
33
+ require('./scripts/run/index')('dev', cmd);
34
+ },
35
+ },
36
+ {
37
+ command: 'build',
38
+ description: 'prod 打包编译',
39
+ options: [
40
+ ['-m, --module [moduleName]', '模块名称'],
41
+ ['-e, --env [env]', '环境变量'],
42
+ ],
43
+ action: (cmd) => {
44
+ require('./scripts/run/index')('build', cmd);
45
+ },
46
+ },
47
+ {
48
+ command: 'init',
49
+ description: '根据模块配置初始化项目(eg: 动态拷贝模块、下载依赖、生成app.json等)',
50
+ options: [
51
+ ['-m, --module [moduleName]', '模块名称'],
52
+ ['-e, --env [env]', '环境变量'],
53
+ ],
54
+ action: (cmd) => {
55
+ require('./scripts/run/index')('init', cmd);
56
+ },
57
+ },
14
58
  ],
15
- description: '运行模块',
16
- action: (commandName, cmd) => {
17
- require('./scripts/run/index')(commandName, cmd);
18
- },
19
59
  },
20
60
  ];
@@ -0,0 +1,6 @@
1
+
2
+ const dev = require('./dev');
3
+
4
+ module.exports = async (tmsConfig, newModules) => {
5
+ dev(tmsConfig, newModules, false);
6
+ };
@@ -0,0 +1,67 @@
1
+ const { src, dest } = require('gulp');
2
+ const px2rpx = require('gulp-px-to-rpx');
3
+ const less = require('gulp-less');
4
+ const rename = require('gulp-rename');
5
+ const watch = require('gulp-watch');
6
+ const image = require('gulp-image');
7
+ const replaceEnv = require('./replaceEnv');
8
+ const includeDep = require('./includeDep');
9
+
10
+ module.exports = function (
11
+ tmsConfig,
12
+ {
13
+ glob,
14
+ destPath,
15
+ srcOption,
16
+ module,
17
+ watchOption = { events: ['change', 'add', 'unlink'] },
18
+ isWatch,
19
+ },
20
+ ) {
21
+ Object.keys(glob).forEach((globKey) => {
22
+ const globValue = glob[globKey];
23
+
24
+ let srcPipe = src(globValue, srcOption);
25
+ if (isWatch) {
26
+ srcPipe = srcPipe.pipe(watch(globValue, watchOption));
27
+ }
28
+
29
+ switch (globKey) {
30
+ case 'js':
31
+ srcPipe
32
+ .pipe(replaceEnv(/process\.env(\.(\w*))?/g, tmsConfig.envData))
33
+ .pipe(includeDep(tmsConfig, module))
34
+ .pipe(dest(destPath));
35
+ break;
36
+ case 'css':
37
+ srcPipe
38
+ .pipe(less())
39
+ .pipe(px2rpx({
40
+ designWidth: 375, // 设计稿宽度,默认为750
41
+ precision: 2, // 小数最大精度,默认为6
42
+ }))
43
+ .pipe(rename({ extname: '.wxss' }))
44
+ .pipe(dest(destPath));
45
+ break;
46
+ case 'json':
47
+ srcPipe
48
+ .pipe(dest(destPath));
49
+ break;
50
+ case 'html':
51
+ srcPipe
52
+ .pipe(dest(destPath));
53
+ break;
54
+ case 'image':
55
+ srcPipe
56
+ .pipe(image())
57
+ .pipe(dest(destPath));
58
+ break;
59
+ case 'other':
60
+ srcPipe
61
+ .pipe(dest(destPath));
62
+ break;
63
+ default:
64
+ break;
65
+ }
66
+ });
67
+ };
@@ -0,0 +1,83 @@
1
+ const watch = require('gulp-watch');
2
+ const { resolve } = require('../utils/widgets');
3
+ const { buildOutputAppJson } = require('../utils/buildAppJson');
4
+ const { DEFAULT_COPY_CONFIG } = require('../config/constant');
5
+ const compile = require('./compile');
6
+
7
+ module.exports = async (tmsConfig, newModules, isWatch = true) => {
8
+ // 监听app.json
9
+ if (isWatch) {
10
+ watch(resolve('app.json'), { ignoreInitial: false, events: ['add', 'change'] }, () => {
11
+ buildOutputAppJson(tmsConfig, newModules);
12
+ });
13
+ } else {
14
+ buildOutputAppJson(tmsConfig, newModules);
15
+ }
16
+
17
+ // 监听根目录的文件
18
+ compile(tmsConfig, {
19
+ glob: {
20
+ json: DEFAULT_COPY_CONFIG.map(item => resolve(item)),
21
+ css: ['app.less', 'app.wxss'].map(item => resolve(item)),
22
+ js: ['app.js', 'app.ts'].map(item => resolve(item)),
23
+ },
24
+ module: { from: '', to: '' },
25
+ destPath: resolve(tmsConfig.gulp.outputDir),
26
+ srcOption: { allowEmpty: true },
27
+ isWatch,
28
+ });
29
+
30
+ // 监听模块的文件
31
+ for (const module of newModules) {
32
+ if (isWatch) {
33
+ // 监听模块配置文件
34
+ watch(`${resolve(module.path)}/**/module.config.json`, { events: ['change'] }, () => {
35
+ buildOutputAppJson(tmsConfig, newModules);
36
+ });
37
+ } else {
38
+ buildOutputAppJson(tmsConfig, newModules);
39
+ }
40
+
41
+ const glob = {
42
+ js: `${resolve(module.path)}/**/*.{js,ts,wxs}`,
43
+ json: `${resolve(module.path)}/**/*.json`,
44
+ css: `${resolve(module.path)}/**/*.{less,wxss}`,
45
+ html: `${resolve(module.path)}/**/*.wxml`,
46
+ image: `${resolve(module.path)}/**/*.{png,jpg,jpeg,gif,svg}`,
47
+ };
48
+ compile(tmsConfig, {
49
+ glob: {
50
+ ...glob,
51
+ other: [`${resolve(module.path)}/**/*`, ...Object.keys(glob).map(key => `!${glob[key]}`)],
52
+ },
53
+ destPath: resolve(tmsConfig.gulp.outputDir, module.root),
54
+ module: { from: module.path, to: module.root },
55
+ srcOption: { allowEmpty: true },
56
+ isWatch,
57
+ });
58
+ }
59
+
60
+ // 监听copy模块
61
+ if (tmsConfig?.gulp?.copyDir.length > 0) {
62
+ for (const item of tmsConfig.gulp?.copyDir) {
63
+ const glob = {
64
+ js: `${item.from}/**/*.{js,ts,wxs}`,
65
+ json: `${item.from}/**/*.json`,
66
+ css: `${item.from}/**/*.{less,wxss}`,
67
+ html: `${item.from}/**/*.wxml`,
68
+ image: `${item.from}/**/*.{png,jpg,jpeg,gif,svg}`,
69
+ };
70
+
71
+ compile(tmsConfig, {
72
+ glob: {
73
+ ...glob,
74
+ other: [`${item.from}/**/*`, ...Object.keys(glob).map(key => `!${glob[key]}`)],
75
+ },
76
+ destPath: item.to,
77
+ module: item,
78
+ srcOption: { allowEmpty: true },
79
+ isWatch,
80
+ });
81
+ }
82
+ }
83
+ };
@@ -0,0 +1,130 @@
1
+ const through = require('through2');
2
+ const precinct = require('precinct');
3
+ const path = require('path');
4
+ const { copyFile, ext, fileInDir } = require('../utils/io');
5
+ const { resolve } = require('../utils/widgets');
6
+ const fs = require('fs');
7
+ const { fail } = require('../utils/log');
8
+
9
+ const dfsDep = function (includePath, anaFileOriginFile, anaFileDestFile, extensions) {
10
+ const contents = fs.readFileSync(anaFileOriginFile, 'utf8');
11
+ const deps = precinct(contents);
12
+ deps.forEach((depItem) => {
13
+ if (depItem.startsWith('.')) {
14
+ // 被依赖文件的存在的绝对路径
15
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
16
+ // 被依赖文件加上后缀
17
+ const depOriginPathExt = ext(depOriginPath, extensions);
18
+
19
+ if (!depOriginPathExt) {
20
+ fail(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
21
+ }
22
+
23
+ // 被依赖文件是否存在 include的path中 (只处理include的文件)
24
+ if (depOriginPathExt && fileInDir(includePath, depOriginPathExt.file)) {
25
+ const depOriginFile = depOriginPathExt.file;
26
+ const depDestPath = resolve(path.dirname(anaFileDestFile), depItem);
27
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext)
28
+ ? depDestPath
29
+ : depDestPath + depOriginPathExt.ext;
30
+
31
+ if (fs.existsSync(depDestFile)) {
32
+ const depDestContent = fs.readFileSync(depDestFile, 'utf8');
33
+ const depOriginContent = fs.readFileSync(depOriginFile, 'utf8');
34
+ if (depDestContent !== depOriginContent) {
35
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
36
+ copyFile(depOriginFile, depDestFile);
37
+ }
38
+ } else {
39
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
40
+ copyFile(depOriginFile, depDestFile);
41
+ }
42
+ dfsDep(includePath, depOriginFile, depDestFile, extensions);
43
+ }
44
+ }
45
+ });
46
+ };
47
+
48
+ function includeThirdParty(tmsConfig, module, extensions = ['.js', '.ts', '.wxs']) {
49
+ const stream = through.obj(function (file, enc, cb) {
50
+ // 依赖分析的文件
51
+ const anaFileOriginFile = file.history[0];
52
+ const anaFileRelativeModule = path.relative(resolve(module.from), anaFileOriginFile);
53
+ const anaFileDestFile = resolve(tmsConfig.gulp.outputDir, module.to, anaFileRelativeModule);
54
+
55
+ if (file.isBuffer()) {
56
+ let contents = String(file.contents);
57
+ const deps = precinct(contents);
58
+ const copyModules = [];
59
+
60
+ Object.keys(tmsConfig.gulp.dependencies).forEach((includeName) => {
61
+ const includePath = tmsConfig.gulp.dependencies[includeName];
62
+ deps.forEach((depItem) => {
63
+ if (depItem.indexOf(includeName) > -1) {
64
+ // 被依赖文件的存在的绝对路径 (eg: /User/thirdparty/loadsh)
65
+ const depOriginPath = path.join(path.dirname(anaFileOriginFile), depItem);
66
+ // 被依赖文件加上后缀
67
+ const depOriginPathExt = ext(depOriginPath, extensions);
68
+
69
+ if (!depOriginPathExt) {
70
+ fail(`${anaFileOriginFile}的${depItem}引用路径存找不到文件,请检查引用路径`);
71
+ }
72
+
73
+ // 被依赖文件是否存在 include的path中 (只处理include的文件)
74
+ if (depOriginPathExt && fileInDir(includePath, depOriginPathExt.file)) {
75
+ // eslint-disable-next-line
76
+ const reg = new RegExp(`^(\.\.\/)+.*\/${includeName}\/(.*)`);
77
+ const regRes = depItem.match(reg) || [];
78
+ if (regRes[2]) {
79
+ const depDestPath = resolve(tmsConfig.gulp.outputDir, module.to, includeName, regRes[2]);
80
+ const depDestFile = depDestPath.endsWith(depOriginPathExt.ext)
81
+ ? depDestPath
82
+ : depDestPath + depOriginPathExt.ext;
83
+ // {
84
+ // depOriginFile: '/Users/odile/workspace/tms-frontend1/miniprogram/thirdparty/libs/timer.js',
85
+ // depDestFile: `/Users/odile/workspace/tms-frontend1/tools/demo1/dist/modules/
86
+ // aggredriving/thirdparty/libs/timer.js`,
87
+ // beforeDepPath: '../../../../thirdparty/libs/timer',
88
+ // afterDepPath: '../../thirdparty/libs/timer'
89
+ // }
90
+ copyModules.push({
91
+ depOriginFile: depOriginPathExt.file,
92
+ depDestFile,
93
+ beforeDepPath: depItem,
94
+ afterDepPath: path.relative(path.dirname(anaFileDestFile), depDestPath),
95
+ includePath,
96
+ });
97
+ }
98
+ }
99
+ }
100
+ });
101
+ });
102
+
103
+ copyModules.forEach(({ includePath, depOriginFile, depDestFile, beforeDepPath, afterDepPath }) => {
104
+ if (fs.existsSync(depDestFile)) {
105
+ const depDestContent = fs.readFileSync(depDestFile, 'utf8');
106
+ const depOriginContent = fs.readFileSync(depOriginFile, 'utf8');
107
+ if (depDestContent !== depOriginContent) {
108
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
109
+ copyFile(depOriginFile, depDestFile);
110
+ }
111
+ } else {
112
+ console.log(`拷贝${depOriginFile}内容到${depDestFile}`);
113
+ copyFile(depOriginFile, depDestFile);
114
+ }
115
+ contents = contents.replace(beforeDepPath, afterDepPath);
116
+ dfsDep(includePath, depOriginFile, depDestFile, extensions);
117
+ });
118
+
119
+ // eslint-disable-next-line
120
+ file.contents = new Buffer(contents);
121
+ }
122
+
123
+ this.push(file);
124
+ cb();
125
+ });
126
+
127
+ return stream;
128
+ }
129
+
130
+ module.exports = includeThirdParty;
@@ -0,0 +1,29 @@
1
+ const through = require('through2');
2
+ function replaceEnv(reg = /process\.env(\.(\w*))?/g, envData) {
3
+ const stream = through.obj(function (file, enc, cb) {
4
+ if (file.isBuffer()) {
5
+ let contents = String(file.contents);
6
+ let resReg;
7
+ // eslint-disable-next-line
8
+ while ((resReg = reg.exec(contents)) !== null) {
9
+ const [reg1, reg2, reg3] = resReg;
10
+ if (reg1 && reg2 && reg3) {
11
+ const value = typeof envData[reg3] === 'object' ? JSON.stringify(envData[reg3]) : envData[reg3];
12
+ contents = contents.replace(reg1, value);
13
+ } else if (reg1 && !reg2 && !reg3) {
14
+ contents = contents.replace(reg1, JSON.stringify(envData));
15
+ }
16
+ }
17
+
18
+ // eslint-disable-next-line
19
+ file.contents = new Buffer(contents);
20
+ }
21
+
22
+ this.push(file);
23
+ cb();
24
+ });
25
+
26
+ return stream;
27
+ }
28
+
29
+ module.exports = replaceEnv;
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const chalk = require('chalk');
2
- const program = require('commander');
2
+ const commander = require('commander');
3
3
  const { log, suggestCommands } = require('./utils/widgets');
4
4
  const { TMS_NAME } = require('./config/constant.js');
5
5
  const commands = require('./entry');
@@ -7,24 +7,40 @@ const init = require('./init');
7
7
 
8
8
  init();
9
9
 
10
+ const program = new commander.Command(TMS_NAME);
11
+
10
12
  program
11
- .version(`${TMS_NAME} ${require('../package.json').version}`);
13
+ .version(`${TMS_NAME} ${require('../package.json').version}`, '-v, -V, --version');
12
14
 
13
- commands.forEach((cmd) => {
14
- const command = program.command(cmd.command);
15
+ function registerCommand(program, commands) {
16
+ commands.forEach((cmd) => {
17
+ if (cmd.type === 'child') {
18
+ const childProgram = new commander.Command(cmd.name);
19
+ cmd.usage && childProgram.usage(cmd.usage);
20
+ cmd.description && childProgram.description(cmd.description);
21
+ registerCommand(childProgram, cmd.commands);
15
22
 
16
- cmd.usage && command.usage(cmd.usage);
23
+ program.addCommand(childProgram);
24
+ } else {
25
+ const command = program.command(cmd.command);
17
26
 
18
- cmd.description && command.description(cmd.description);
27
+ cmd.usage && command.usage(cmd.usage);
19
28
 
20
- cmd.options?.forEach(opt => command.option(...opt));
29
+ cmd.description && command.description(cmd.description);
30
+
31
+ cmd.options?.forEach(opt => command.option(...opt));
32
+
33
+ command.action(cmd.action);
34
+ }
35
+ });
36
+ }
37
+
38
+ registerCommand(program, commands);
21
39
 
22
- command.action(cmd.action);
23
- });
24
40
 
25
41
  program.on('--help', () => {
26
42
  log();
27
- log(` Run ${chalk.cyan('tms <command> --help')} for detailed usage of given command.`);
43
+ log(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
28
44
  log();
29
45
  });
30
46
 
package/src/init.js CHANGED
@@ -1,4 +1,3 @@
1
- const { exec } = require('shelljs');
2
1
  const semver = require('semver');
3
2
  const packageJson = require('../package.json');
4
3
  const chalk = require('chalk');
@@ -29,10 +28,6 @@ function initCliContext() {
29
28
  // 执行操作前检查node版本
30
29
  // 旧版本node直接提示升级,退出脚本
31
30
  checkNodeVersion(requiredVersion, packName);
32
-
33
-
34
- // 执行前配置正确的npm源
35
- exec('npm config set registry https://mirrors.tencent.com/npm/ --global', { silent: true });
36
31
  }
37
32
 
38
33
  module.exports = initCliContext;
@@ -1,17 +1,19 @@
1
1
  const fs = require('fs');
2
2
  const inquirer = require('inquirer');
3
+ const { resolve } = require('../../utils/widgets');
4
+ const { TEMPLATE_TKIT_DIR } = require('../../config/constant');
3
5
  /**
4
6
  * 获取模板内的问题
5
- * @param {string} templateDir 模板目录
7
+ * @param {string} dir questions.json所在的目录
6
8
  * @returns {Array} inquirer 问题数组
7
9
  */
8
- const parseTemplateQuestions = (templateDir) => {
10
+ const parseTemplateQuestions = (dir) => {
9
11
  let prompts = [];
10
- if (!fs.existsSync(`${templateDir}/questions.json`)) {
12
+ if (!fs.existsSync(`${dir}/questions.json`)) {
11
13
  return prompts;
12
14
  }
13
15
  try {
14
- const json = JSON.parse(fs.readFileSync(`${templateDir}/questions.json`));
16
+ const json = JSON.parse(fs.readFileSync(`${dir}/questions.json`));
15
17
  if (Array.isArray(json) && json.length > 0) {
16
18
  json.forEach((item, index) => {
17
19
  if (!isQuestionType(item)) {
@@ -38,7 +40,7 @@ const isQuestionType = (result) => {
38
40
  };
39
41
 
40
42
  const ask = templateDir => (files, metalsmith, next) => {
41
- const prompts = parseTemplateQuestions(templateDir);
43
+ const prompts = parseTemplateQuestions(resolve(templateDir, TEMPLATE_TKIT_DIR));
42
44
  const metadata = metalsmith.metadata();
43
45
  const filteredPrompts = prompts.filter((prompt) => {
44
46
  if (metadata[prompt.name] && `${metadata[prompt.name]}`.trim() !== '') {
@@ -1,5 +1,4 @@
1
1
  const Metalsmith = require('metalsmith');
2
- const fs = require('fs');
3
2
  const render = require('./render');
4
3
  const ask = require('./ask');
5
4
  const FILES_TO_IGNORE = require('./ignoreFiles');
@@ -18,7 +17,6 @@ const generator = (buildDir, distDir, preMetadata) => new Promise((resolve, reje
18
17
  if (err) {
19
18
  reject(err);
20
19
  } else {
21
- fs.unlinkSync(`${distDir}/questions.json`);
22
20
  resolve('finish');
23
21
  }
24
22
  });
@@ -1,7 +1,7 @@
1
1
  const path = require('path');
2
2
  const fs = require('fs');
3
3
  const shelljs = require('shelljs');
4
- const { TEMPLATE_DIR, TEMPLATE_PATH } = require('../../config/constant.js');
4
+ const { TEMPLATE_DIR, TEMPLATE_PATH, TEMPLATE_TKIT_DIR } = require('../../config/constant.js');
5
5
  const { downloadRepoForGit, createTask, resolve } = require('../../utils/widgets');
6
6
  const io = require('../../utils/io');
7
7
  const { fail, succeed } = require('../../utils/log');
@@ -33,7 +33,7 @@ async function create(appName) {
33
33
  const cwd = process.cwd();
34
34
  const targetDir = path.resolve(cwd, appName);
35
35
  const appType = 'mp';
36
- createAppDir(targetDir);
36
+ await createAppDir(targetDir);
37
37
 
38
38
  // 创建缓存目录
39
39
  io.ensureDirExist(TEMPLATE_DIR);
@@ -49,12 +49,19 @@ async function create(appName) {
49
49
  appName,
50
50
  appType,
51
51
  }).then(() => {
52
- succeed('项目创建完成,开始初始化...');
53
52
  shelljs.cd(appName);
54
- // 初始化构件主包的代码, 让用户打开项目可以跑起来
55
- if (fs.existsSync(resolve(`${appName}/tms.config.js`))) {
56
- shelljs.exec('tmskit run build');
53
+ const hooks = require(resolve(appName, TEMPLATE_TKIT_DIR, 'hooks.js'));
54
+ if (hooks.afterCreate) {
55
+ hooks.afterCreate.forEach((item) => {
56
+ if (typeof item === 'function') {
57
+ item.call(null, shelljs, { appName });
58
+ } else {
59
+ shelljs.exec(item);
60
+ }
61
+ });
57
62
  }
63
+ shelljs.rm('-rf', resolve(appName, TEMPLATE_TKIT_DIR));
64
+ succeed('项目创建完成.');
58
65
  })
59
66
  .catch((err) => {
60
67
  fail(err.message);