@tmsfe/tmskit 0.0.6 → 0.0.7

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.
Files changed (42) hide show
  1. package/README.md +27 -27
  2. package/dist/index.cjs.js +332 -407
  3. package/main.js +3 -3
  4. package/package.json +75 -75
  5. package/src/config/constant.js +71 -71
  6. package/src/config/defaultTmsConfig.js +16 -16
  7. package/src/entry.js +60 -60
  8. package/src/gulp/build.js +5 -5
  9. package/src/gulp/compile.js +81 -87
  10. package/src/gulp/dev.js +102 -102
  11. package/src/gulp/plugins/less.js +116 -116
  12. package/src/gulp/plugins/mpCommonDep.js +131 -131
  13. package/src/gulp/plugins/mpJsonDep.js +108 -108
  14. package/src/gulp/plugins/mpWxmlDep.js +194 -194
  15. package/src/gulp/plugins/postcss-font-base64.js +72 -72
  16. package/src/gulp/plugins/replaceEnv.js +29 -29
  17. package/src/gulp/plugins/utils/pluginError.js +25 -25
  18. package/src/index.js +62 -62
  19. package/src/init.js +33 -33
  20. package/src/scripts/create/ask.js +63 -63
  21. package/src/scripts/create/generator.js +25 -25
  22. package/src/scripts/create/ignoreFiles.js +7 -7
  23. package/src/scripts/create/index.js +72 -72
  24. package/src/scripts/create/render.js +19 -19
  25. package/src/scripts/run/build/index.js +17 -17
  26. package/src/scripts/run/dev/index.js +84 -84
  27. package/src/scripts/run/index.js +68 -68
  28. package/src/scripts/run/init/index.js +87 -87
  29. package/src/scripts/run/install/index.js +29 -29
  30. package/src/utils/buildAppJson.js +221 -221
  31. package/src/utils/checkDependencies.js +77 -77
  32. package/src/utils/cliUtils.js +35 -35
  33. package/src/utils/cloneModules.js +116 -116
  34. package/src/utils/findCssImport.js +30 -30
  35. package/src/utils/global.js +36 -36
  36. package/src/utils/handleError.js +16 -16
  37. package/src/utils/io.js +106 -106
  38. package/src/utils/log.js +44 -44
  39. package/src/utils/mpCiUtils.js +73 -73
  40. package/src/utils/npmUtils.js +166 -166
  41. package/src/utils/tkitUtils.js +158 -158
  42. package/src/utils/widgets.js +167 -167
package/src/index.js CHANGED
@@ -1,62 +1,62 @@
1
- const chalk = require('chalk');
2
- const commander = require('commander');
3
- const { log, suggestCommands } = require('./utils/widgets');
4
- const { TMS_NAME } = require('./config/constant.js');
5
- const commands = require('./entry');
6
- const init = require('./init');
7
-
8
- init();
9
-
10
- const program = new commander.Command(TMS_NAME);
11
-
12
- program
13
- .version(`${TMS_NAME} ${require('../package.json').version}`, '-v, -V, --version');
14
-
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);
22
-
23
- program.addCommand(childProgram);
24
- } else {
25
- const command = program.command(cmd.command);
26
-
27
- cmd.usage && command.usage(cmd.usage);
28
-
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);
39
-
40
- program.on('--help', () => {
41
- log();
42
- log(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
43
- log();
44
- });
45
-
46
- // 捕获未注册的命令
47
- program
48
- .arguments('<command>')
49
- .action((cmd) => {
50
- program.outputHelp();
51
- log(` ${chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)}`);
52
- log();
53
- suggestCommands(cmd);
54
- process.exitCode = 1;
55
- });
56
-
57
- if (!process.argv.slice(2).length) {
58
- program.outputHelp();
59
- process.exit(-1);
60
- }
61
-
62
- program.parse(process.argv);
1
+ const chalk = require('chalk');
2
+ const commander = require('commander');
3
+ const { log, suggestCommands } = require('./utils/widgets');
4
+ const { TMS_NAME } = require('./config/constant.js');
5
+ const commands = require('./entry');
6
+ const init = require('./init');
7
+
8
+ init();
9
+
10
+ const program = new commander.Command(TMS_NAME);
11
+
12
+ program
13
+ .version(`${TMS_NAME} ${require('../package.json').version}`, '-v, -V, --version');
14
+
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);
22
+
23
+ program.addCommand(childProgram);
24
+ } else {
25
+ const command = program.command(cmd.command);
26
+
27
+ cmd.usage && command.usage(cmd.usage);
28
+
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);
39
+
40
+ program.on('--help', () => {
41
+ log();
42
+ log(` Run ${chalk.cyan(`${TMS_NAME} <command> --help`)} for detailed usage of given command.`);
43
+ log();
44
+ });
45
+
46
+ // 捕获未注册的命令
47
+ program
48
+ .arguments('<command>')
49
+ .action((cmd) => {
50
+ program.outputHelp();
51
+ log(` ${chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)}`);
52
+ log();
53
+ suggestCommands(cmd);
54
+ process.exitCode = 1;
55
+ });
56
+
57
+ if (!process.argv.slice(2).length) {
58
+ program.outputHelp();
59
+ process.exit(-1);
60
+ }
61
+
62
+ program.parse(process.argv);
package/src/init.js CHANGED
@@ -1,33 +1,33 @@
1
- const semver = require('semver');
2
- const packageJson = require('../package.json');
3
- const chalk = require('chalk');
4
- const { log } = require('./utils/widgets');
5
-
6
- const requiredVersion = packageJson.engines.node;
7
- const packName = packageJson.name;
8
-
9
- /**
10
- * 检查node版本
11
- * @param {String} wanted 希望的node版本
12
- * @param {String} id 某node特性
13
- * @returns {Undefined} 无需返回值
14
- */
15
- const checkNodeVersion = (wanted, id) => {
16
- if (!semver.satisfies(process.version, wanted, { includePrerelease: true })) {
17
- log(chalk.red(`你正在使用的Node版本: ${process.version}, 但是此版本的 ${id
18
- } 需要的Node最小版本 ${wanted}.\n请先升级你的Node版本.`));
19
- process.exit(1);
20
- }
21
- };
22
-
23
- /**
24
- * 检查运行环境
25
- * @returns {Undefined} 无需返回值
26
- */
27
- function initCliContext() {
28
- // 执行操作前检查node版本
29
- // 旧版本node直接提示升级,退出脚本
30
- checkNodeVersion(requiredVersion, packName);
31
- }
32
-
33
- module.exports = initCliContext;
1
+ const semver = require('semver');
2
+ const packageJson = require('../package.json');
3
+ const chalk = require('chalk');
4
+ const { log } = require('./utils/widgets');
5
+
6
+ const requiredVersion = packageJson.engines.node;
7
+ const packName = packageJson.name;
8
+
9
+ /**
10
+ * 检查node版本
11
+ * @param {String} wanted 希望的node版本
12
+ * @param {String} id 某node特性
13
+ * @returns {Undefined} 无需返回值
14
+ */
15
+ const checkNodeVersion = (wanted, id) => {
16
+ if (!semver.satisfies(process.version, wanted, { includePrerelease: true })) {
17
+ log(chalk.red(`你正在使用的Node版本: ${process.version}, 但是此版本的 ${id
18
+ } 需要的Node最小版本 ${wanted}.\n请先升级你的Node版本.`));
19
+ process.exit(1);
20
+ }
21
+ };
22
+
23
+ /**
24
+ * 检查运行环境
25
+ * @returns {Undefined} 无需返回值
26
+ */
27
+ function initCliContext() {
28
+ // 执行操作前检查node版本
29
+ // 旧版本node直接提示升级,退出脚本
30
+ checkNodeVersion(requiredVersion, packName);
31
+ }
32
+
33
+ module.exports = initCliContext;
@@ -1,63 +1,63 @@
1
- const fs = require('fs');
2
- const inquirer = require('inquirer');
3
- const { resolve } = require('../../utils/widgets');
4
- const { TEMPLATE_TKIT_DIR } = require('../../config/constant');
5
- /**
6
- * 获取模板内的问题
7
- * @param {string} dir questions.json所在的目录
8
- * @returns {Array} inquirer 问题数组
9
- */
10
- const parseTemplateQuestions = (dir) => {
11
- let prompts = [];
12
- if (!fs.existsSync(`${dir}/questions.json`)) {
13
- return prompts;
14
- }
15
- try {
16
- const json = JSON.parse(fs.readFileSync(`${dir}/questions.json`));
17
- if (Array.isArray(json) && json.length > 0) {
18
- json.forEach((item, index) => {
19
- if (!isQuestionType(item)) {
20
- throw new Error(`第 ${index} 个问题不符合"inquirer"的格式要求`);
21
- }
22
- });
23
- } else {
24
- throw new Error('问题文件只能是数组');
25
- }
26
- prompts = json;
27
- } catch (err) {
28
- throw new Error(`模板异常,${err.message}`);
29
- }
30
- return prompts;
31
- };
32
- const isQuestionType = (result) => {
33
- if (!('message' in result)) {
34
- return false;
35
- }
36
- if (!('name' in result)) {
37
- return false;
38
- }
39
- return true;
40
- };
41
-
42
- const ask = templateDir => (files, metalsmith, next) => {
43
- const prompts = parseTemplateQuestions(resolve(templateDir, TEMPLATE_TKIT_DIR));
44
- const metadata = metalsmith.metadata();
45
- const filteredPrompts = prompts.filter((prompt) => {
46
- if (metadata[prompt.name] && `${metadata[prompt.name]}`.trim() !== '') {
47
- return false;
48
- }
49
- return true;
50
- });
51
- inquirer.prompt(filteredPrompts)
52
- .then((res) => {
53
- for (const prompt of filteredPrompts) {
54
- metadata[prompt.name] = res[prompt.name];
55
- }
56
- next();
57
- })
58
- .catch((err) => {
59
- next(err);
60
- });
61
- };
62
-
63
- module.exports = ask;
1
+ const fs = require('fs');
2
+ const inquirer = require('inquirer');
3
+ const { resolve } = require('../../utils/widgets');
4
+ const { TEMPLATE_TKIT_DIR } = require('../../config/constant');
5
+ /**
6
+ * 获取模板内的问题
7
+ * @param {string} dir questions.json所在的目录
8
+ * @returns {Array} inquirer 问题数组
9
+ */
10
+ const parseTemplateQuestions = (dir) => {
11
+ let prompts = [];
12
+ if (!fs.existsSync(`${dir}/questions.json`)) {
13
+ return prompts;
14
+ }
15
+ try {
16
+ const json = JSON.parse(fs.readFileSync(`${dir}/questions.json`));
17
+ if (Array.isArray(json) && json.length > 0) {
18
+ json.forEach((item, index) => {
19
+ if (!isQuestionType(item)) {
20
+ throw new Error(`第 ${index} 个问题不符合"inquirer"的格式要求`);
21
+ }
22
+ });
23
+ } else {
24
+ throw new Error('问题文件只能是数组');
25
+ }
26
+ prompts = json;
27
+ } catch (err) {
28
+ throw new Error(`模板异常,${err.message}`);
29
+ }
30
+ return prompts;
31
+ };
32
+ const isQuestionType = (result) => {
33
+ if (!('message' in result)) {
34
+ return false;
35
+ }
36
+ if (!('name' in result)) {
37
+ return false;
38
+ }
39
+ return true;
40
+ };
41
+
42
+ const ask = templateDir => (files, metalsmith, next) => {
43
+ const prompts = parseTemplateQuestions(resolve(templateDir, TEMPLATE_TKIT_DIR));
44
+ const metadata = metalsmith.metadata();
45
+ const filteredPrompts = prompts.filter((prompt) => {
46
+ if (metadata[prompt.name] && `${metadata[prompt.name]}`.trim() !== '') {
47
+ return false;
48
+ }
49
+ return true;
50
+ });
51
+ inquirer.prompt(filteredPrompts)
52
+ .then((res) => {
53
+ for (const prompt of filteredPrompts) {
54
+ metadata[prompt.name] = res[prompt.name];
55
+ }
56
+ next();
57
+ })
58
+ .catch((err) => {
59
+ next(err);
60
+ });
61
+ };
62
+
63
+ module.exports = ask;
@@ -1,25 +1,25 @@
1
- const Metalsmith = require('metalsmith');
2
- const render = require('./render');
3
- const ask = require('./ask');
4
- const FILES_TO_IGNORE = require('./ignoreFiles');
5
-
6
-
7
- const generator = (buildDir, distDir, preMetadata) => new Promise((resolve, reject) => {
8
- Metalsmith(buildDir)
9
- .metadata(preMetadata)
10
- .ignore(FILES_TO_IGNORE)
11
- .clean(false)
12
- .use(ask(buildDir))
13
- .source('./')
14
- .destination(distDir)
15
- .use(render)
16
- .build((err) => {
17
- if (err) {
18
- reject(err);
19
- } else {
20
- resolve('finish');
21
- }
22
- });
23
- });
24
-
25
- module.exports = generator;
1
+ const Metalsmith = require('metalsmith');
2
+ const render = require('./render');
3
+ const ask = require('./ask');
4
+ const FILES_TO_IGNORE = require('./ignoreFiles');
5
+
6
+
7
+ const generator = (buildDir, distDir, preMetadata) => new Promise((resolve, reject) => {
8
+ Metalsmith(buildDir)
9
+ .metadata(preMetadata)
10
+ .ignore(FILES_TO_IGNORE)
11
+ .clean(false)
12
+ .use(ask(buildDir))
13
+ .source('./')
14
+ .destination(distDir)
15
+ .use(render)
16
+ .build((err) => {
17
+ if (err) {
18
+ reject(err);
19
+ } else {
20
+ resolve('finish');
21
+ }
22
+ });
23
+ });
24
+
25
+ module.exports = generator;
@@ -1,7 +1,7 @@
1
-
2
- // 在metal smith遍历时需要忽略的文件,这些文件会引起metalsmith的parse error
3
- const FILES_TO_IGNORE = [
4
- 'node_modules',
5
- ];
6
-
7
- module.exports = FILES_TO_IGNORE;
1
+
2
+ // 在metal smith遍历时需要忽略的文件,这些文件会引起metalsmith的parse error
3
+ const FILES_TO_IGNORE = [
4
+ 'node_modules',
5
+ ];
6
+
7
+ module.exports = FILES_TO_IGNORE;
@@ -1,72 +1,72 @@
1
- const path = require('path');
2
- const fs = require('fs');
3
- const shelljs = require('shelljs');
4
- const { TEMPLATE_DIR, TEMPLATE_PATH, TEMPLATE_TKIT_DIR } = require('../../config/constant.js');
5
- const { downloadRepoForGit, createTask, resolve } = require('../../utils/widgets');
6
- const io = require('../../utils/io');
7
- const { fail, succeed } = require('../../utils/log');
8
- const generator = require('./generator');
9
-
10
- /**
11
- * 如果该目录下面存在文件,换个名字
12
- * @param { string } targetDir 当前文件夹
13
- * @returns { undefined }
14
- */
15
- async function createAppDir(targetDir) {
16
- // 如果目录非空或者已经存在,提示用户,做选择
17
- if (fs.existsSync(targetDir)) {
18
- if (!await io.isDirEmpty(targetDir)) {
19
- fail('该目录名已经存在,换个项目名字吧~');
20
- process.exit(1);
21
- }
22
- } else {
23
- shelljs.mkdir('-p', targetDir);
24
- }
25
- }
26
-
27
- /**
28
- * 创建本地小程序运行环境
29
- * @param { string } appType 项目类型
30
- * @param { string } appName 项目名称
31
- */
32
- async function create(appName) {
33
- const cwd = process.cwd();
34
- const targetDir = path.resolve(cwd, appName);
35
- const appType = 'mp';
36
- await createAppDir(targetDir);
37
-
38
- // 创建缓存目录
39
- io.ensureDirExist(TEMPLATE_DIR);
40
- // 拉取git模板
41
- await createTask(downloadRepoForGit, '拉取模板仓库', '拉取模板仓库完成')(
42
- 'https://git.woa.com/tmsfe/tms-frontend.git',
43
- TEMPLATE_DIR,
44
- 'master',
45
- );
46
-
47
- // 生成模板(1. 询问问题, 2. ejs生成模板 3.生成到目标目录)
48
- generator(path.join(TEMPLATE_PATH, appType), targetDir, {
49
- appName,
50
- appType,
51
- }).then(() => {
52
- shelljs.cd(appName);
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
- });
62
- }
63
- shelljs.rm('-rf', resolve(appName, TEMPLATE_TKIT_DIR));
64
- succeed('项目创建完成.');
65
- })
66
- .catch((err) => {
67
- fail(err.message);
68
- console.log('详细的错误信息:', err);
69
- });
70
- }
71
-
72
- module.exports = create;
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+ const shelljs = require('shelljs');
4
+ const { TEMPLATE_DIR, TEMPLATE_PATH, TEMPLATE_TKIT_DIR } = require('../../config/constant.js');
5
+ const { downloadRepoForGit, createTask, resolve } = require('../../utils/widgets');
6
+ const io = require('../../utils/io');
7
+ const { fail, succeed } = require('../../utils/log');
8
+ const generator = require('./generator');
9
+
10
+ /**
11
+ * 如果该目录下面存在文件,换个名字
12
+ * @param { string } targetDir 当前文件夹
13
+ * @returns { undefined }
14
+ */
15
+ async function createAppDir(targetDir) {
16
+ // 如果目录非空或者已经存在,提示用户,做选择
17
+ if (fs.existsSync(targetDir)) {
18
+ if (!await io.isDirEmpty(targetDir)) {
19
+ fail('该目录名已经存在,换个项目名字吧~');
20
+ process.exit(1);
21
+ }
22
+ } else {
23
+ shelljs.mkdir('-p', targetDir);
24
+ }
25
+ }
26
+
27
+ /**
28
+ * 创建本地小程序运行环境
29
+ * @param { string } appType 项目类型
30
+ * @param { string } appName 项目名称
31
+ */
32
+ async function create(appName) {
33
+ const cwd = process.cwd();
34
+ const targetDir = path.resolve(cwd, appName);
35
+ const appType = 'mp';
36
+ await createAppDir(targetDir);
37
+
38
+ // 创建缓存目录
39
+ io.ensureDirExist(TEMPLATE_DIR);
40
+ // 拉取git模板
41
+ await createTask(downloadRepoForGit, '拉取模板仓库', '拉取模板仓库完成')(
42
+ 'https://git.woa.com/tmsfe/tms-frontend.git',
43
+ TEMPLATE_DIR,
44
+ 'master',
45
+ );
46
+
47
+ // 生成模板(1. 询问问题, 2. ejs生成模板 3.生成到目标目录)
48
+ generator(path.join(TEMPLATE_PATH, appType), targetDir, {
49
+ appName,
50
+ appType,
51
+ }).then(() => {
52
+ shelljs.cd(appName);
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
+ });
62
+ }
63
+ shelljs.rm('-rf', resolve(appName, TEMPLATE_TKIT_DIR));
64
+ succeed('项目创建完成.');
65
+ })
66
+ .catch((err) => {
67
+ fail(err.message);
68
+ console.log('详细的错误信息:', err);
69
+ });
70
+ }
71
+
72
+ module.exports = create;
@@ -1,19 +1,19 @@
1
-
2
- const async = require('async');
3
- const ejs = require('ejs');
4
-
5
- const render = (files, metalsmith, next) => {
6
- const keys = Object.keys(files);
7
- const metadata = metalsmith.metadata();
8
- const run = (file, next) => {
9
- const str = files[file].contents.toString();
10
- const newStr = ejs.render(str, metadata);
11
- // eslint-disable-next-line
12
- files[file].contents = Buffer.from(newStr);
13
- next();
14
- };
15
-
16
- async.each(keys, run, next);
17
- };
18
-
19
- module.exports = render;
1
+
2
+ const async = require('async');
3
+ const ejs = require('ejs');
4
+
5
+ const render = (files, metalsmith, next) => {
6
+ const keys = Object.keys(files);
7
+ const metadata = metalsmith.metadata();
8
+ const run = (file, next) => {
9
+ const str = files[file].contents.toString();
10
+ const newStr = ejs.render(str, metadata);
11
+ // eslint-disable-next-line
12
+ files[file].contents = Buffer.from(newStr);
13
+ next();
14
+ };
15
+
16
+ async.each(keys, run, next);
17
+ };
18
+
19
+ module.exports = render;
@@ -1,17 +1,17 @@
1
- const shelljs = require('shelljs');
2
- const { resolve } = require('../../../utils/widgets');
3
- const init = require('../init/index');
4
- const gulpBuild = require('../../../gulp/build');
5
-
6
- async function build(tmsConfig, targetModules, env) {
7
- // 开始构建前,清理输出目录
8
- await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
9
-
10
- const isDev = false;
11
- const { targetModules: newModules } = await init(tmsConfig, targetModules, isDev);
12
-
13
- gulpBuild(tmsConfig, newModules, env);
14
- }
15
-
16
- module.exports = build;
17
-
1
+ const shelljs = require('shelljs');
2
+ const { resolve } = require('../../../utils/widgets');
3
+ const init = require('../init/index');
4
+ const gulpBuild = require('../../../gulp/build');
5
+
6
+ async function build(tmsConfig, targetModules, env) {
7
+ // 开始构建前,清理输出目录
8
+ await shelljs.rm('-rf', resolve(tmsConfig.outputDir));
9
+
10
+ const isDev = false;
11
+ const { targetModules: newModules } = await init(tmsConfig, targetModules, isDev);
12
+
13
+ gulpBuild(tmsConfig, newModules, env);
14
+ }
15
+
16
+ module.exports = build;
17
+