@tmsfe/tmskit 0.0.26 → 0.0.28

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.26",
3
+ "version": "0.0.28",
4
4
  "description": "tmskit",
5
5
  "main": "dist/index.cjs",
6
6
  "bin": {
@@ -37,4 +37,7 @@ module.exports = {
37
37
  autoPrefixWXSS: true,
38
38
  },
39
39
  },
40
+ npm: {
41
+ registry: 'https://registry.npmjs.org/',
42
+ },
40
43
  };
@@ -21,10 +21,15 @@ function replaceGitUrlAccount(httpRepoUrl, moduleName) {
21
21
  const tmsConfig = global.getData('tmsConfig');
22
22
 
23
23
  let gitUrl = httpRepoUrl;
24
-
25
- const { username = '', pass = '' } = tmsConfig?.gitAccount?.[moduleName] || tmsConfig?.gitAccount?.[httpRepoUrl] || {};
24
+ const gitGroupReg = /http(s)?:\/\/[^/]+\/[^/]+/;
25
+ const group = gitUrl.match(gitGroupReg)[0];
26
+
27
+ const { username = '', pass = '' } = tmsConfig?.gitAccount?.[moduleName]
28
+ || tmsConfig?.gitAccount?.[httpRepoUrl]
29
+ || tmsConfig?.gitAccount?.[group] || {};
26
30
 
27
31
  const urlPrefixReg = /http(s)?:\/\//;
32
+
28
33
  if (username && pass && urlPrefixReg.test(gitUrl)) {
29
34
  gitUrl = gitUrl.replace(urlPrefixReg, val => `${val}${encodeURIComponent(username)}:${pass}@`);
30
35
  }
@@ -160,7 +165,10 @@ function collectDownLoadTasksMap(sourceDir, targetDir, modules) {
160
165
  if (fs.existsSync(sourcePath) && fs.existsSync(`${sourcePath}/.git`)) {
161
166
  promiseTask = (gitUrl, sourcePath, branch, httpRepoUrl) => {
162
167
  info(`git pull:${httpRepoUrl} --branch: ${branch}`);
163
- return pullRepoForGit(sourcePath, branch);
168
+ return pullRepoForGit(sourcePath, branch).catch((e) => {
169
+ info(`pull代码失败:${e}, 开始git clone: ${httpRepoUrl}:${branch}`);
170
+ return downloadRepoForGit(gitUrl, sourcePath, branch);
171
+ });
164
172
  };
165
173
  } else {
166
174
  promiseTask = (gitUrl, sourcePath, branch, httpRepoUrl) => {
package/src/entry.js CHANGED
@@ -45,17 +45,6 @@ module.exports = [
45
45
  require('./scripts/run/index')('dev', cmdOptions);
46
46
  },
47
47
  },
48
- {
49
- command: 'cloud',
50
- description: '云函数开发',
51
- options: [
52
- ['-m, --module [moduleName]', '模块名称'],
53
- ['-e, --env [env]', '环境变量'],
54
- ],
55
- action: (cmdOptions) => {
56
- require('./scripts/run/index')('cloud', cmdOptions);
57
- },
58
- },
59
48
  {
60
49
  command: 'build',
61
50
  description: 'prod 打包编译',
@@ -92,6 +81,26 @@ module.exports = [
92
81
  require('./scripts/run/index')('upload', cmdOptions);
93
82
  },
94
83
  },
84
+ {
85
+ command: 'cloud-dev',
86
+ description: '云函数开发',
87
+ options: [
88
+ ['--cloud [cloud]', '模块名称'],
89
+ ],
90
+ action: (cmdOptions) => {
91
+ require('./scripts/run/index')('cloud-dev', cmdOptions);
92
+ },
93
+ },
94
+ // {
95
+ // command: 'cloud-link',
96
+ // description: '云函数开发',
97
+ // options: [
98
+ // ['--cloud [cloud]', '模块名称'],
99
+ // ],
100
+ // action: (cmdOptions) => {
101
+ // require('./scripts/run/index')('cloud-link', cmdOptions);
102
+ // },
103
+ // },
95
104
  ],
96
105
  },
97
106
  ];
@@ -0,0 +1,34 @@
1
+ const fs = require('fs');
2
+
3
+ /**
4
+ * 检查云函数的配置项
5
+ * @param {*} tmsConfig
6
+ */
7
+ function checkCloudConfig(tmsConfig) {
8
+ if (!tmsConfig.cloudModules) {
9
+ return '你没有在tms.config.js的cloudModules注册云函数';
10
+ }
11
+ if (!tmsConfig.cloudDir) {
12
+ return '你没有在tms.config.js配置云函数编译目录cloudDir';
13
+ }
14
+ return;
15
+ }
16
+
17
+ /**
18
+ * 检查云函数源码目录是否存在
19
+ * @param {Array} clouds [{name: '', sourcePath: '', targetPath: ''}]
20
+ */
21
+ function checkSrcPathIsExist(clouds) {
22
+ for (const item of clouds) {
23
+ if (!fs.existsSync(item.sourcePath)) {
24
+ return `${item.name}云函数的${item.sourcePath}目录不存在, 请检查tms.config.js>cloudModules>all云函数的配置目录`;
25
+ }
26
+ }
27
+ return;
28
+ }
29
+
30
+
31
+ module.exports = {
32
+ checkCloudConfig,
33
+ checkSrcPathIsExist,
34
+ };
@@ -0,0 +1,146 @@
1
+ const shellJs = require('shelljs');
2
+ const ora = require('ora');
3
+ const path = require('path');
4
+ const chalk = require('chalk');
5
+ const { resolve, createTask } = require('../../../utils/widgets');
6
+ const { handleError } = require('../../../core/handleError');
7
+ const { ensureDirExist } = require('../../../utils/io');
8
+ const { src, dest, parallel, series } = require('gulp');
9
+ const watch = require('../../../compile/plugins/gulp-watch');
10
+ const { info, fail } = require('../../../utils/log');
11
+ const getCloudsOfListen = require('./getClouds');
12
+ const { checkCloudConfig, checkSrcPathIsExist } = require('./check');
13
+ const { runInstall } = require('./install');
14
+
15
+ /**
16
+ * 获取云函数的编译任务
17
+ * @param {array} clouds [{name: '', sourcePath: '', targetPath: ''}]
18
+ * @param {object} srcOption gulp.src的参数
19
+ * @returns Map {'${sourcePath}': {taskFn: () => {}, targetPath: 'xxx'}}
20
+ */
21
+ const getCompileTasks = (clouds, srcOption) => {
22
+ const compileTasksMap = new Map();
23
+ clouds.forEach((item) => {
24
+ compileTasksMap.set(item.sourcePath, {
25
+ targetPath: item.targetPath,
26
+ taskFn: (sourceFile, targetPath) => {
27
+ const newGlobValue = Array.isArray(sourceFile) ? sourceFile : `${item.sourcePath}/**/*`;
28
+ const newDestPath = targetPath ? targetPath : item.targetPath;
29
+
30
+ const srcPipe = src(newGlobValue, srcOption);
31
+ return srcPipe
32
+ .pipe(dest(newDestPath))
33
+ .on('error', (err) => {
34
+ fail(`监听报错${err}`);
35
+ });
36
+ },
37
+ });
38
+ });
39
+ return compileTasksMap;
40
+ };
41
+
42
+ const getTargetFile = (sourceFile, sourcePath, targetPath) => {
43
+ const sourceFileRelativeModule = path.relative(sourcePath, sourceFile);
44
+ const targetFile = resolve(targetPath, sourceFileRelativeModule);
45
+ return targetFile;
46
+ };
47
+
48
+ /**
49
+ * 开始启动编译
50
+ * @param {Map} compileTasksMap {'${sourcePath}': {taskFn: () => {}, targetPath: 'xxx'}}
51
+ */
52
+ const runCompile = (compileTasksMap, callback) => {
53
+ async function end(next) {
54
+ // 监听其他文件
55
+ compileTasksMap.forEach(({ taskFn, targetPath }, sourcePath) => {
56
+ runWatch(
57
+ sourcePath,
58
+ { ignoreInitial: true, events: ['add', 'change', 'unlink', 'addDir', 'unlinkDir'] },
59
+ taskFn,
60
+ targetPath,
61
+ );
62
+ });
63
+ callback && callback();
64
+ next();
65
+ }
66
+
67
+ const compileTasksArr = [];
68
+ compileTasksMap.forEach(({ taskFn }) => {
69
+ compileTasksArr.push(taskFn);
70
+ });
71
+ // 一次性完成编译任务(编译完成后再添加watch任务-封装到end函数里面)
72
+ series(parallel(...compileTasksArr), end)();
73
+ };
74
+
75
+ /**
76
+ * 监听文件变动
77
+ * @param {*} sourcePath 云函数的源码目录
78
+ * @param {*} watchOptions gulp-watch的参数
79
+ * @param {*} callback 监听到变动的回调
80
+ * @param {*} targetPath 云函数的编译目录
81
+ */
82
+ const runWatch = (sourcePath, watchOptions, callback, targetPath) => {
83
+ watch(sourcePath, {
84
+ ...watchOptions,
85
+ }, (vinyl) => {
86
+ const sourceFile = vinyl.history[0];
87
+ const sourceFileDirArr = sourceFile.replace(/\\/g, '/').split('/');
88
+ const sourceFileName = sourceFileDirArr.slice(sourceFileDirArr.length - 2).join('/');
89
+
90
+ const targetFile = getTargetFile(sourceFile, sourcePath, targetPath);
91
+ if (vinyl.event === 'unlink' || vinyl.event === 'unlinkDir') {
92
+ info(`删除${sourceFileName}`);
93
+ shellJs.rm('-rf', targetFile);
94
+ return;
95
+ }
96
+ info(`${sourceFileName}有更新`);
97
+ return callback([sourceFile], path.dirname(targetFile));
98
+ });
99
+ };
100
+
101
+
102
+ module.exports = async (tmsConfig, cmdOptions) => {
103
+ try {
104
+ const sTime = new Date().getTime();
105
+ const spinner = ora();
106
+ spinner.start();
107
+
108
+ // 检查云函数的配置项
109
+ const configErrMsg = checkCloudConfig(tmsConfig);
110
+ if (configErrMsg) {
111
+ throw new Error(configErrMsg);
112
+ }
113
+
114
+ // 确保云函数的编译目录存在
115
+ ensureDirExist(resolve(tmsConfig.cloudDir));
116
+
117
+ // 获取需要监听的云函数列表
118
+ const clouds = getCloudsOfListen(tmsConfig, cmdOptions);
119
+ // 检查云函数源码目录是否存在
120
+ const srcNotExistErrMsg = checkSrcPathIsExist(clouds);
121
+ if (srcNotExistErrMsg) {
122
+ throw new Error(srcNotExistErrMsg);
123
+ }
124
+
125
+ // 打印启动云函数列表
126
+ info('当前启动云函数列表:', clouds.map(item => item.name).sort());
127
+
128
+ // npm install
129
+ await createTask(
130
+ runInstall,
131
+ '开始npm install',
132
+ 'npm install完成',
133
+ )(clouds, tmsConfig);
134
+
135
+ // 获取云函数列表的编译任务
136
+ const compileTasksMap = getCompileTasks(clouds, { allowEmpty: true });
137
+ runCompile(compileTasksMap, () => {
138
+ const eTime = new Date().getTime() - sTime;
139
+ spinner.succeed(chalk.green(`首次编译完成, 耗时${eTime / 1000}s, 微信开发者工具打开项目即可预览。`));
140
+ spinner.stop();
141
+ });
142
+ } catch (e) {
143
+ info('编译出现详细错误', e);
144
+ handleError(`编译出现错误: ${e}`, true);
145
+ }
146
+ };
@@ -0,0 +1,46 @@
1
+ const { resolve, getAbsolutePath } = require('../../../utils/widgets');
2
+
3
+ // 处理tms.config.cloudModules的兼容数据
4
+ function getTmsCloudModules(tmsConfig) {
5
+ const { cloudModules } = tmsConfig;
6
+ if (Array.isArray(tmsConfig.cloudModules)) {
7
+ return {
8
+ all: [...tmsConfig.cloudModules],
9
+ include: [],
10
+ };
11
+ }
12
+ return cloudModules;
13
+ }
14
+
15
+ /**
16
+ * 获取需要监听的云函数
17
+ * @param {*} tmsConfig {}
18
+ * @param {*} cmdOptions eg:{clouds: 'aggrecarshop,car'}
19
+ * @returns
20
+ * [{ name: '', sourcePath: '', targetPath: ''}]
21
+ */
22
+ function getCloudsOfListen(tmsConfig, cmdOptions) {
23
+ // 获取tmsconfig配置的云函数的配置项
24
+ const cloudModules = getTmsCloudModules(tmsConfig);
25
+
26
+ let includeClouds = cmdOptions?.cloud?.split(',') || cloudModules.include || [];
27
+
28
+ // 如果没有配置include, 默认使用所有的数据云函数列表
29
+ if (includeClouds.length === 0) {
30
+ includeClouds = cloudModules.all.map(item => item.name);
31
+ }
32
+
33
+ const result = [];
34
+ for (const item of cloudModules.all) {
35
+ if (includeClouds.includes(item.name)) {
36
+ result.push({
37
+ name: item.name,
38
+ sourcePath: `${getAbsolutePath(item.path)}`,
39
+ targetPath: resolve(tmsConfig.cloudDir, item.name),
40
+ });
41
+ }
42
+ }
43
+ return result;
44
+ }
45
+
46
+ module.exports = getCloudsOfListen;
@@ -0,0 +1,31 @@
1
+ const shellJs = require('shelljs');
2
+ const fs = require('fs');
3
+ const { resolve, npmInstall } = require('../../../utils/widgets');
4
+ const { ensureDirExist } = require('../../../utils/io');
5
+ const { info } = require('../../../utils/log');
6
+
7
+ /**
8
+ * npm install
9
+ * @param {array} clouds [{name: '', sourcePath: '', targetPath: ''}]
10
+ */
11
+ const runInstall = async (clouds, tmsConfig) => {
12
+ const promises = [];
13
+
14
+ for (const item of clouds) {
15
+ ensureDirExist(item.targetPath);
16
+ const packageFilePath = resolve(item.sourcePath, 'package.json');
17
+ if (fs.existsSync(packageFilePath)) {
18
+ shellJs.cp('-Rf', resolve(packageFilePath), item.targetPath);
19
+ promises.push(() => {
20
+ info(`云函数${item.name}: npm install`);
21
+ return npmInstall(item.targetPath, tmsConfig.npm);
22
+ });
23
+ }
24
+ };
25
+ await Promise.all(promises.map(item => item()));
26
+ };
27
+
28
+
29
+ module.exports = {
30
+ runInstall,
31
+ };
@@ -0,0 +1,37 @@
1
+ const fs = require('fs');
2
+ const { info } = require('../../../utils/log');
3
+ const { resolve } = require('../../../utils/widgets');
4
+ const { ensureDirExist } = require('../../../utils/io');
5
+ const { warn, succeed } = require('../../../utils/log');
6
+ const getCloudsOfListen = require('./getClouds');
7
+ const { handleError } = require('../../../core/handleError');
8
+ const { checkCloudConfig } = require('./check');
9
+
10
+ module.exports = async (tmsConfig, cmdOptions) => {
11
+ try {
12
+ ensureDirExist(resolve(tmsConfig.cloudDir));
13
+ checkCloudConfig(tmsConfig);
14
+
15
+ // 获取需要监听的云函数列表
16
+ const clouds = getCloudsOfListen(tmsConfig, cmdOptions);
17
+ // 打印启动云函数列表
18
+ info('当前启动云函数列表:', clouds.map(item => item.name).sort());
19
+
20
+
21
+ clouds.forEach((item) => {
22
+ if (!fs.existsSync(item.sourcePath)) {
23
+ warn(`云函数${item.sourcePath}不存在`);
24
+ return;
25
+ }
26
+ const stat = fs.lstatSync(item.targetPath);
27
+ if (!stat.isSymbolicLink()) {
28
+ fs.symlinkSync(item.sourcePath, item.targetPath);
29
+ }
30
+ });
31
+
32
+ succeed('云函数创建软链成功');
33
+ } catch (e) {
34
+ info('创建软链详细错误', e);
35
+ handleError(`创建软链错误: ${e}`, true);
36
+ }
37
+ };
@@ -5,7 +5,8 @@ const build = require('./build/index');
5
5
  const install = require('./install/index');
6
6
  const preview = require('./preview/index');
7
7
  const upload = require('./upload/index');
8
- const cloud = require('./cloud/index');
8
+ const cloudLink = require('./cloud/link');
9
+ const cloudDev = require('./cloud/dev');
9
10
  const { fail, info } = require('../../utils/log');
10
11
  const { global } = require('../../utils/global');
11
12
  const report = require('../../core/report');
@@ -57,9 +58,14 @@ async function run(commandName, cmdOptions) {
57
58
  tmsConfig,
58
59
  });
59
60
 
60
- if (commandName === 'cloud') {
61
- cloud(tmsConfig);
62
- report('run:cloud', { appName: tmsConfig?.appName });
61
+ if (commandName === 'cloud-link') {
62
+ cloudLink(tmsConfig, cmdOptions);
63
+ report('run:cloud-link', { appName: tmsConfig?.appName });
64
+ return;
65
+ }
66
+ if (commandName === 'cloud-dev') {
67
+ cloudDev(tmsConfig, cmdOptions);
68
+ report('run:cloud-dev', { appName: tmsConfig?.appName });
63
69
  return;
64
70
  }
65
71
  otherCommands(tmsConfig, commandName, cmdOptions);
package/src/.DS_Store DELETED
Binary file
@@ -1,33 +0,0 @@
1
- const fs = require('fs');
2
- const { resolve } = require('../utils/widgets');
3
- const { ensureDirExist } = require('../utils/io');
4
- const { warn, succeed } = require('../utils/log');
5
-
6
- /**
7
- * 根据相关配置创建软链接
8
- * @param { object } tmsConfig
9
- */
10
- const symLink = (tmsConfig) => {
11
- ensureDirExist(resolve(tmsConfig.cloudDir));
12
- if (tmsConfig.cloudModules) {
13
- tmsConfig.cloudModules.forEach((item) => {
14
- const sourcePath = resolve(item.path);
15
- const targetPath = resolve(tmsConfig.cloudDir, item.name);
16
-
17
- if (!fs.existsSync(sourcePath)) {
18
- warn(`云函数${sourcePath}不存在`);
19
- return;
20
- }
21
- if (!fs.existsSync(targetPath)) {
22
- fs.symlinkSync(sourcePath, targetPath);
23
- }
24
- });
25
- succeed('云函数创建软链成功');
26
- } else {
27
- warn('你没有在tms.config.js的cloudModules注册云函数');
28
- }
29
- };
30
-
31
- module.exports = {
32
- symLink,
33
- };
Binary file
@@ -1,10 +0,0 @@
1
- const { symLink } = require('../../../core/symbolicLink');
2
- const { handleError } = require('../../../core/handleError');
3
-
4
- module.exports = async (tmsConfig) => {
5
- try {
6
- await symLink(tmsConfig);
7
- } catch (e) {
8
- handleError(`创建软链错误: ${e}`, true);
9
- }
10
- };