@tmsfe/tmskit 0.0.23 → 0.0.24-beta.0

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.
@@ -0,0 +1,43 @@
1
+ const moment = require('moment');
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+ const { getGitUser, getAbsolutePath } = require('../../../utils/widgets');
5
+ const { ensureDirExist } = require('../../../utils/io');
6
+
7
+ function getAllSize(data = {}) {
8
+ let allSize;
9
+ for (const item of data?.subPackageInfo) {
10
+ if (item.name === '__FULL__') {
11
+ allSize = item.size;
12
+ break;
13
+ }
14
+ }
15
+ return allSize;
16
+ }
17
+
18
+ const getDefaultDesc = () => {
19
+ const user = getGitUser();
20
+ const date = moment().format('YYYY-MM-DD HH:mm:ss');
21
+ return `构建人: ${user}; 构建时间:${date}`;
22
+ };
23
+
24
+ const outputInfo = (infoOutput, data) => {
25
+ const outPath = getAbsolutePath(infoOutput);
26
+ const dir = path.dirname(outPath);
27
+ ensureDirExist(dir);
28
+ fs.writeFileSync(outPath, JSON.stringify(data, null, 2));
29
+ };
30
+
31
+ const getDetaultVersion = () => {
32
+ const version = moment().format('gggg,mm')
33
+ .split(',');
34
+ version.push('0');
35
+ return version.join('.');
36
+ };
37
+
38
+ module.exports = {
39
+ getAllSize,
40
+ getDefaultDesc,
41
+ outputInfo,
42
+ getDetaultVersion,
43
+ };
@@ -0,0 +1,65 @@
1
+ const mpCi = require('../../../core/mpCi');
2
+ const { resolve, createTask, filterField } = require('../../../utils/widgets');
3
+ const { handleError } = require('../../../core/handleError');
4
+ const { info } = require('../../../utils/log');
5
+ const { global } = require('../../../utils/global');
6
+ const { getAllSize, outputInfo, getDefaultDesc } = require('../preview/utils');
7
+ const report = require('../../../core/report');
8
+
9
+ const handleParams = (tmsConfig, cmdOptions) => {
10
+ const params = {
11
+ ...(tmsConfig.upload ? tmsConfig.upload : {}),
12
+ ...cmdOptions,
13
+ };
14
+
15
+ if (!params.version) {
16
+ throw new Error('请指定传入版本号 eg: tmskit run upload -v 2022.28.5');
17
+ }
18
+ return {
19
+ ...params,
20
+ appId: params.appId || tmsConfig.appId,
21
+ projectPath: resolve(tmsConfig.outputDir),
22
+ privateKey: params.privateKey || tmsConfig.privateKey,
23
+ robot: params.robot || 30,
24
+ desc: params.desc || getDefaultDesc(),
25
+ };
26
+ };
27
+
28
+ /**
29
+ * 上传
30
+ * @param {object} tmsConfig
31
+ * @param {object} cmdOptions {version: '2022.28.5', desc: '', robot: 2, infoOutput: './a.txt' }
32
+ */
33
+ async function upload(tmsConfig, cmdOptions) {
34
+ try {
35
+ const params = handleParams(tmsConfig, cmdOptions);
36
+ if (typeof tmsConfig?.hooks?.beforeUpload === 'function') {
37
+ await tmsConfig?.hooks?.beforeUpload({
38
+ tmsConfig: filterField(tmsConfig, ['gitAccount']),
39
+ cmdOptions: global.getData('cmdOptions'),
40
+ params,
41
+ });
42
+ report('hooks:beforeUpload');
43
+ };
44
+ const uploadRes = await createTask(
45
+ mpCi.uploadMp,
46
+ '正在构建预览码',
47
+ '构建预览码完成',
48
+ )({
49
+ ...params,
50
+ });
51
+
52
+ const allSize = getAllSize(uploadRes);
53
+ if (params.infoOutput) {
54
+ outputInfo(params.infoOutput, {
55
+ sourceCode: params.projectPath,
56
+ ...uploadRes,
57
+ });
58
+ }
59
+ info(`上传包大小: ${allSize}k; 上传包版本: ${params.version}`);
60
+ } catch (e) {
61
+ console.log('详细错误:', e);
62
+ handleError(`上传错误: ${e.message}`, true);
63
+ }
64
+ }
65
+ module.exports = upload;
@@ -231,6 +231,11 @@ function versionCompare(v1, v2) {
231
231
  return (arr1.length > arr2.length) ? 1 : -1;
232
232
  }
233
233
 
234
+ function getGitUser() {
235
+ const res = shelljs.exec('git config user.name', { async: false, silent: true });
236
+ return res.stdout;
237
+ }
238
+
234
239
  module.exports = {
235
240
  resolve,
236
241
  isObject,
@@ -247,4 +252,5 @@ module.exports = {
247
252
  getAbsolutePath,
248
253
  getNpmRegistry,
249
254
  versionCompare,
255
+ getGitUser,
250
256
  };