@tmsfe/tmskit 0.0.7 → 0.0.8

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 (46) hide show
  1. package/README.md +27 -27
  2. package/dist/index.cjs.js +1284 -983
  3. package/package.json +73 -75
  4. package/src/{gulp → compile}/build.js +5 -5
  5. package/src/{gulp → compile}/compile.js +90 -81
  6. package/src/{gulp → compile}/dev.js +129 -102
  7. package/src/{gulp → compile}/plugins/less.js +116 -116
  8. package/src/{gulp → compile}/plugins/mpCommonDep.js +131 -131
  9. package/src/{gulp → compile}/plugins/mpJsonDep.js +112 -108
  10. package/src/{gulp → compile}/plugins/mpWxmlDep.js +194 -194
  11. package/src/{gulp → compile}/plugins/postcss-font-base64.js +72 -72
  12. package/src/{gulp → compile}/plugins/replaceEnv.js +29 -29
  13. package/src/{gulp → compile}/plugins/utils/pluginError.js +25 -25
  14. package/src/config/constant.js +69 -71
  15. package/src/config/defaultTmsConfig.js +16 -16
  16. package/src/{utils → core}/buildAppJson.js +166 -221
  17. package/src/{utils → core}/checkDependencies.js +77 -77
  18. package/src/core/cloneModules.js +203 -0
  19. package/src/{utils → core}/handleError.js +18 -16
  20. package/src/core/isInIt.js +69 -0
  21. package/src/{utils/mpCiUtils.js → core/mpCi.js} +73 -73
  22. package/src/core/npm.js +218 -0
  23. package/src/core/symbolicLink.js +24 -0
  24. package/src/{utils/tkitUtils.js → core/tmsMpconfig.js} +234 -158
  25. package/src/entry.js +62 -60
  26. package/src/index.js +63 -62
  27. package/src/init.js +33 -33
  28. package/src/scripts/create/ask.js +63 -63
  29. package/src/scripts/create/generator.js +25 -25
  30. package/src/scripts/create/ignoreFiles.js +7 -7
  31. package/src/scripts/create/index.js +72 -72
  32. package/src/scripts/create/render.js +19 -19
  33. package/src/scripts/run/build/index.js +16 -17
  34. package/src/scripts/run/dev/index.js +42 -84
  35. package/src/scripts/run/index.js +97 -68
  36. package/src/scripts/run/init/index.js +95 -87
  37. package/src/scripts/run/install/index.js +31 -29
  38. package/src/utils/findCssImport.js +30 -30
  39. package/src/utils/global.js +22 -36
  40. package/src/utils/io.js +107 -106
  41. package/src/utils/log.js +47 -44
  42. package/src/utils/widgets.js +178 -167
  43. package/main.js +0 -3
  44. package/src/utils/cliUtils.js +0 -35
  45. package/src/utils/cloneModules.js +0 -116
  46. package/src/utils/npmUtils.js +0 -166
@@ -1,35 +0,0 @@
1
- const fs = require('fs');
2
- const cp = require('child_process');
3
-
4
- const getDevtoolCliPath = async () => {
5
- let cliPath;
6
- if (process.platform === 'darwin') {
7
- cliPath = '/Applications/wechatwebdevtools.app/Contents/MacOS/cli';
8
- } else if (process.platform === 'win32') {
9
- cliPath = 'C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat';
10
- } else {
11
- // 其余平台暂不支持
12
- throw new Error('unsupported platform');
13
- }
14
- const exists = await new Promise(resolve => fs.exists(cliPath, resolve));
15
- if (!exists) {
16
- // 找不到开发工具
17
- throw new Error(`未找到微信小程序开发者工具,请确认是否安装,如未安装请前往
18
- https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 下载`);
19
- }
20
- return cliPath;
21
- };
22
-
23
- const openDevtool = async (path) => {
24
- const cliPath = await getDevtoolCliPath();
25
- const child = cp.spawn(cliPath, ['open', '--project', path], {
26
- detached: true,
27
- stdio: 'inherit',
28
- });
29
- child.unref();
30
- };
31
-
32
- module.exports = {
33
- getDevtoolCliPath,
34
- openDevtool,
35
- };
@@ -1,116 +0,0 @@
1
- const MetalSmith = require('metalsmith');
2
- const { getGlobalInstance } = require('./global.js');
3
- const { downloadRepoForGit, resolve } = require('./widgets');
4
- const { readTmsPrivateCf } = require('../utils/tkitUtils');
5
- const { fail } = require('./log');
6
- const fs = require('fs');
7
- const shelljs = require('shelljs');
8
- const { handleError } = require('./handleError.js');
9
-
10
- /**
11
- * 处理用户没有clone git仓库权限问题——拼接账号信息
12
- * @param {*} httpRepoUrl
13
- * @param {*} moduleName
14
- * @returns
15
- */
16
- function replaceGitUrlAccount(httpRepoUrl, moduleName, privateCf) {
17
- let gitUrl = httpRepoUrl;
18
- const { username = '', pass = '' } = privateCf?.gitAccout?.[moduleName] || {};
19
-
20
- const urlPrefixReg = /http(s)?:\/\//;
21
- if (username && pass && urlPrefixReg.test(gitUrl)) {
22
- gitUrl = gitUrl.replace(urlPrefixReg, val => `${val}${encodeURIComponent(username)}:${pass}@`);
23
- }
24
-
25
- return gitUrl;
26
- }
27
-
28
- /**
29
- * 对克隆下来的模块进行相应的文件处理操作,比如收集处理模块信息,进行信息缓存等操作
30
- * @param { string } sourceDir 缓存文件夹
31
- * @param { string } targetDir 目标文件夹
32
- * @param { arrary } ignore
33
- * @returns { undefined } no return
34
- */
35
- function moveFile(sourceDir, targetDir, ignore = []) {
36
- // 删除不是文件夹的文件
37
- return new Promise((resolve, reject) => {
38
- MetalSmith(__dirname)
39
- .ignore(ignore)
40
- .source(sourceDir)
41
- .destination(targetDir)
42
- .build((e) => {
43
- if (e) {
44
- fail(`${sourceDir} moveFile ${targetDir}出现错误: ${e}`);
45
- reject(e);
46
- }
47
- resolve();
48
- });
49
- });
50
- }
51
-
52
- /**
53
- * 下载目标模块
54
- * @param { string } sourceDir 缓存文件夹
55
- * @param { string } targetDir 目标文件夹
56
- * @param { boolean } isDev 是否是dev
57
- * @returns { array } modules 描述模块的列表
58
- */
59
- async function cloneModules(sourceDir, targetDir, modules, isDev) {
60
- // 用户本地的私有项目配置(用来配置环境\模块信息\账号信息)
61
- const privateCf = readTmsPrivateCf();
62
-
63
- // 根据小程序的配置文件下载模块, 并且处理信息
64
- for (const moduleInfo of modules) {
65
- if (moduleInfo.repoInfo) {
66
- await downLoadAndMoveModule(sourceDir, targetDir, moduleInfo, privateCf, isDev);
67
- }
68
- }
69
- }
70
-
71
- /**
72
- * 下载模块信息并且将它移动到对应的位置
73
- * @param { string } sourceDir 代码缓存文件夹
74
- * @param { string } targetDir 代码要放到的目标文件夹
75
- * @param { boolean } isDev 是否是dev
76
- * @returns { array } moduleInfo 描述模块的信息
77
- */
78
- async function downLoadAndMoveModule(sourceDir, targetDir, moduleInfo, privateCf, isDev) {
79
- const { repoInfo: { buildGitTag, httpRepoUrl }, path, name } = moduleInfo;
80
-
81
- // 源码临时存在的源目录
82
- let sourcePath = resolve(sourceDir, name);
83
- // 源码要放到目标目录
84
- const targetPath = resolve(targetDir, path);
85
- // 设置模块的构建分支
86
- const cloneBranch = buildGitTag && typeof buildGitTag === 'string' ? buildGitTag : 'master';
87
-
88
- // 检查缓存中有没有
89
- const globalInstance = getGlobalInstance();
90
- const moduleInCache = globalInstance.getModuleCache(httpRepoUrl, cloneBranch);
91
-
92
- try {
93
- if (!moduleInCache) {
94
- // 处理仓库权限问题
95
- const gitUrl = replaceGitUrlAccount(httpRepoUrl, name, privateCf);
96
- await downloadRepoForGit(gitUrl, sourcePath, cloneBranch);
97
- globalInstance.setModuleCache(httpRepoUrl, cloneBranch, sourcePath);
98
- } else {
99
- sourcePath = globalInstance.getModuleCache(httpRepoUrl, cloneBranch).dest;
100
- }
101
-
102
- if (fs.existsSync(targetPath)) {
103
- shelljs.rm('-rf', `${targetPath}/*`);
104
- }
105
- await moveFile(sourcePath, targetPath, [
106
- 'node_modules',
107
- '.git',
108
- ]);
109
- } catch (e) {
110
- handleError(e, isDev);
111
- }
112
- }
113
-
114
- module.exports = {
115
- cloneModules,
116
- };
@@ -1,166 +0,0 @@
1
- /**
2
- * 本文件主要负责项目或者分包依赖的npm的安装
3
- */
4
- const fs = require('fs');
5
- const fsExtra = require('fs-extra');
6
- const crypto = require('crypto');
7
- const path = require('path');
8
- const shell = require('shelljs');
9
- const glob = require('glob-ignore');
10
- const LOG = require('./log');
11
-
12
- const shellJsOption = { async: false, silent: true };
13
- const dirPath = process.cwd(); // 项目根目录
14
-
15
- const install = async (packageJsonPath = '', retry = true, cacheDir) => {
16
- const obj = {};
17
- const packageContent = fs.readFileSync(packageJsonPath);
18
- const packageJson = JSON.parse(packageContent);
19
- obj.dependencies = packageJson.dependencies || {};
20
- // obj.devDependencies = packageJson.devDependencies || {};
21
-
22
- // dependencies和devDependencies字段为空,没有需要安装的npm包,直接return
23
- // if (Object.keys(obj.dependencies).length === 0 && Object.keys(obj.devDependencies).length === 0) {
24
- if (Object.keys(obj.dependencies).length === 0) {
25
- return;
26
- }
27
-
28
- // 以package.json中dependencies字段stringify后的md5值作为缓存key
29
- // 任意包名或者版本号的差异,将导致md5后的差异,从而导致缓存失效,重新下载
30
- // 如此来保证npm缓存的准确性
31
- const str = JSON.stringify(obj);
32
- const key = crypto.createHash('md5').update(str)
33
- .digest('hex');
34
- const keyPath = path.join(cacheDir, key);
35
- const tarFile = path.join(keyPath, 'node_modules.tar.gz');
36
-
37
- // npm缓存路径不存在
38
- // 或者缓存的包大小小于512byte,认为缓存不合法
39
- // 清空缓存目录,重新下载npm包缓存
40
- const missCache = !fsExtra.pathExistsSync(keyPath)
41
- || !fsExtra.existsSync(tarFile)
42
- || fsExtra.statSync(tarFile).size < 512;
43
- if (missCache) {
44
- console.log(`未命中缓存,下载${packageJsonPath}依赖`);
45
- fsExtra.emptydirSync(keyPath);
46
- shell.cp('-f', packageJsonPath, keyPath);
47
- shell.cd(keyPath);
48
- try {
49
- shell.exec('npx yarn --production --registry http://mirrors.tencent.com/npm/', shellJsOption);
50
- } catch (err) {
51
- console.log('err', err);
52
- if (retry) {
53
- return await install(packageJsonPath, false);
54
- }
55
- throw err;
56
- }
57
- shell.exec('tar -zcvf ./node_modules.tar.gz ./node_modules', shellJsOption);
58
- shell.exec('rm -rf ./node_modules', shellJsOption);
59
- }
60
- shell.cp('-Rf', tarFile, `${path.dirname(packageJsonPath)}/`);
61
- shell.cd(path.dirname(packageJsonPath));
62
-
63
- shell.exec('tar -xzvf ./node_modules.tar.gz -C ./', shellJsOption);
64
- shell.exec('rm -rf ./node_modules.tar.gz', shellJsOption);
65
- return { missCache, key };
66
- };
67
-
68
-
69
- // 遍历安装指定目录下所有项目的npm依赖
70
- const mpNpmInstallAll = async (modules, contextDir, cacheDir) => {
71
- const packageJsonFiles = await findAllPackageJson(modules, contextDir);
72
- await Promise.all(packageJsonFiles.map(file => install(file, true, cacheDir)));
73
- };
74
-
75
- /**
76
- * 递归查找指定条件的文件
77
- * @param {String} startPath 开始查找的根路径
78
- * @param {String} filter 匹配的字符串
79
- * @returns {Array<String>} 查找到的文件路径列表
80
- */
81
- const findFilesByFilter = (startPath, filter) => {
82
- const result = [];
83
- /**
84
- * 根据指定的筛选器查找文件
85
- * @param {String} startPath 开始查找的文件夹路径
86
- * @param {String} filter 筛选器
87
- * @returns {Undefined} 无需返回值
88
- */
89
- const find = (startPath, filter) => {
90
- // 目录不存在
91
- if (!fs.existsSync(startPath)) {
92
- LOG.fail(`${startPath}目录不存在`);
93
- process.exit(-1);
94
- return;
95
- }
96
-
97
- // 当前目录下的所有文件 / 文件夹
98
- const exceptDir = ['node_modules', 'miniprogram_npm'];
99
- if (exceptDir.find(item => startPath.indexOf(item) > -1)) {
100
- return;
101
- }
102
- const files = fs.readdirSync(startPath);
103
- files.forEach((file) => {
104
- const filename = path.join(startPath, file);
105
- const stat = fs.lstatSync(filename);
106
- // 当前文件是文件夹类型,继续递归
107
- if (stat.isDirectory()) {
108
- find(filename, filter);
109
- } else if (filename.indexOf(filter) >= 0) {
110
- // 文件类型
111
- result.push(filename);
112
- };
113
- });
114
- };
115
-
116
- find(startPath, filter);
117
- return result;
118
- };
119
-
120
- /**
121
- * 找到项目中所有的package.json文件
122
- * @param {Array<String>} subRoots 需要安装npm依赖的路径
123
- * @param {String} contextDir 命令运行的目录
124
- * @returns {Array<String>} 找到的所有package.json文件的路径
125
- */
126
- const findAllPackageJson = (subRoots = [], contextDir) => {
127
- const packageJsonName = 'package.json'; // 查找文件名
128
- const cwd = contextDir || dirPath;
129
- const result = [path.join(cwd, packageJsonName)]; // 默认填充根目录下的package.json
130
-
131
- subRoots.forEach((subRoot) => {
132
- if (!subRoot.root) {
133
- LOG.fail(`请检查${subRoot.name}的module.config.json是否有root字段`);
134
- process.exit(1);
135
- }
136
- const toppath = path.join(cwd, subRoot.root); // 从该目录开始查找package.json文件
137
- const list = findFilesByFilter(toppath, packageJsonName);
138
-
139
- result.push(...list);
140
- });
141
-
142
- return result;
143
- };
144
-
145
- function cloudNpmInstall(contextDir) {
146
- return new Promise((resolve, reject) => {
147
- glob(`${contextDir}/**/package.json`, ['node_modules', 'miniprogram_npm'], (err, files) => {
148
- if (err) {
149
- reject(err);
150
- }
151
- files.forEach((file) => {
152
- const dir = path.dirname(file);
153
- shell.cd(dir);
154
- shell.exec('npx npm install --production --registry http://mirrors.tencent.com/npm/', { silent: false });
155
- });
156
- resolve();
157
- });
158
- });
159
- }
160
-
161
-
162
- module.exports = {
163
- cloudNpmInstall,
164
- mpNpmInstallAll,
165
- findAllPackageJson,
166
- };