upload-miniprogram 1.0.2 → 1.1.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/README.md CHANGED
@@ -45,9 +45,10 @@ upload-miniprogram \
45
45
  |------|------|------|------|------|
46
46
  | --appid | -a | 是 | 小程序ID |每个项目固定1-2个,可能跟环境不同,确定后不需再修改|
47
47
  | --project-path | -p | 是 | 小程序项目打包后的代码路径 |一般是 ./dist 或 ./dist/weapp 每个项目固定1个路径,确定后不需再修改|
48
- | --env | -e | | 环境变量 (development\|staging\|uat\|production) | 按每个Jerkins链接的环境设置|
48
+ | --env | -e | | 环境变量 (development\|staging\|uat\|production) | 按每个Jerkins链接的环境设置|
49
49
  | --app-version | -V | 是 | 上传至小程序的版本号 |每次构建时需要重新填写|
50
50
  | --desc | -d | 否 | 版本描述 |每次构建时需要重新填写|
51
+ | --robot | -r | 否 | 机器人编号 |每个环境有默认值,支持部分范围输入|
51
52
 
52
53
  ### 编程方式
53
54
 
@@ -91,10 +92,11 @@ ${WORKSPACE}/../mp-ci-upload-key/private.{appid}.key
91
92
 
92
93
  | 环境 | 版本后缀 | 机器人 | 描述 |
93
94
  |------|----------|--------|------|
94
- | development | .dev | 1 | 开发环境 |
95
- | staging | .staging | 2 | 测试环境 |
96
- | uat | .uat | 3 | 预发布环境 |
97
- | production | (无) | 5 | 生产环境 |
95
+ | development | .dev | 默认 1,支持传入 6-10| 开发环境 |
96
+ | staging | .staging | 默认 2,支持传入 11-15 | 测试环境 |
97
+ | uat | .uat | 默认 3,支持传入 15-20 | 预发布环境 |
98
+ | 自定义(如xxx) | .xxx | 默认 4,支持传入 21-25 | 自定义环境 |
99
+ | production | (无) | 默认 5,支持传入 26-30 | 生产环境 |
98
100
 
99
101
  ## Jenkins集成
100
102
 
@@ -11,6 +11,7 @@ program
11
11
  .option('-d, --desc <desc>', '版本描述')
12
12
  .option('-u, --user <user>', '上传用户')
13
13
  .option('-e, --env <env>', '环境变量 (development|staging|uat|production)', 'development')
14
+ .option('-r, --robot <robot>', '机器人编号,默认可不传')
14
15
  .parse(process.argv);
15
16
 
16
17
  const options = program.opts();
@@ -27,7 +28,8 @@ if (options.appVersion && options.appid && options.projectPath && options.env)
27
28
  projectPath: options.projectPath,
28
29
  version: options.appVersion,
29
30
  desc: options.desc,
30
- user: options.user
31
+ user: options.user,
32
+ robot: options.robot
31
33
  });
32
34
  } else {
33
35
  console.error("执行失败: 缺少必要参数 appid, project-path(projectPath), app-version(appVersion), env(value in development|staging|uat|production)");
package/lib/index.js CHANGED
@@ -11,10 +11,18 @@ class UploadMiniprogram {
11
11
  this.ENV = process.env.NODE_ENV || 'development';
12
12
 
13
13
  this.ENV_MAP = {
14
- development: { v: ".dev", desc: "【Dev环境】", robot: 1 },
15
- staging: { v: ".staging", desc: "【Staging环境】", robot: 2 },
16
- uat: { v: ".uat", desc: "【Uat环境】", robot: 3 },
17
- production: { v: "", desc: "【正式环境】", robot: 5 },
14
+ development: { v: ".dev", desc: "【Dev环境】", robot: 1, robotRange: [6, 10] },
15
+ staging: { v: ".staging", desc: "【Staging环境】", robot: 2, robotRange: [11, 15] },
16
+ uat: { v: ".uat", desc: "【Uat环境】", robot: 3, robotRange: [15, 20] },
17
+ production: { v: "", desc: "【正式环境】", robot: 5, robotRange: [26, 30] },
18
+ };
19
+
20
+ // 自定义环境配置
21
+ this.CUSTOM_ENV_CONFIG = {
22
+ v: `.${this.ENV}`,
23
+ desc: `【${this.ENV}环境】`,
24
+ robot: 4,
25
+ robotRange: [21, 25]
18
26
  };
19
27
  }
20
28
 
@@ -52,24 +60,50 @@ class UploadMiniprogram {
52
60
  return template.replace("${WORKSPACE}", this.WORKSPACE || "");
53
61
  }
54
62
 
63
+ // 验证机器人编号
64
+ validateRobot(robot, envConfig) {
65
+ if (!robot) {
66
+ return envConfig.robot; // 使用默认值
67
+ }
68
+
69
+ const robotNum = parseInt(robot, 10);
70
+ if (isNaN(robotNum)) {
71
+ throw new Error(`机器人编号必须为数字: ${robot}`);
72
+ }
73
+
74
+ const [min, max] = envConfig.robotRange;
75
+ if (robotNum < min || robotNum > max) {
76
+ throw new Error(`环境 ${this.ENV} 的机器人编号必须在 ${min}-${max} 范围内,当前为: ${robotNum}`);
77
+ }
78
+
79
+ return robotNum;
80
+ }
81
+
55
82
  async executeUpload(options) {
56
83
  const {
57
84
  appid,
58
85
  projectPath,
59
86
  version,
60
87
  desc = "",
61
- user = ""
88
+ user = "",
89
+ robot = ""
62
90
  } = options;
63
91
 
64
92
  const gitEmail = this.getGitEmail();
65
93
  const jenkinsURL = process.env.BUILD_URL ?? "";
66
94
  const isJenkins = Boolean(jenkinsURL);
67
95
 
68
- const envConfig = this.ENV_MAP[this.ENV];
96
+ // 获取环境配置,支持自定义环境
97
+ let envConfig = this.ENV_MAP[this.ENV];
69
98
  if (!envConfig) {
70
- throw new Error(`不支持的env环境: ${this.ENV}`);
99
+ // 如果是自定义环境,使用自定义配置
100
+ envConfig = this.CUSTOM_ENV_CONFIG;
101
+ console.log(`使用自定义环境配置: ${this.ENV}`);
71
102
  }
72
103
 
104
+ // 验证并获取机器人编号
105
+ const finalRobot = this.validateRobot(robot, envConfig);
106
+
73
107
  const fullVersion = `${version}${envConfig.v}`;
74
108
  const fullDesc = `${envConfig.desc} ${desc} upload by ${
75
109
  gitEmail || user || "Jenkins "
@@ -87,7 +121,7 @@ class UploadMiniprogram {
87
121
  console.log("========= 运行上传小程序函数 ==========");
88
122
  const uploadResult = await ci.upload({
89
123
  project,
90
- robot: envConfig.robot,
124
+ robot: finalRobot,
91
125
  version: fullVersion,
92
126
  desc: fullDesc,
93
127
  setting: {
@@ -101,7 +135,7 @@ class UploadMiniprogram {
101
135
  const qrCodePath = `${this.getPreviewCodePath()}/code.jpg`;
102
136
  const previewResult = await ci.preview({
103
137
  project,
104
- robot: envConfig.robot,
138
+ robot: finalRobot,
105
139
  desc: `${fullVersion} ${fullDesc}`,
106
140
  setting: {
107
141
  useProjectConfig: true,
@@ -113,7 +147,7 @@ class UploadMiniprogram {
113
147
  console.log("预览并生成二维码结果 previewResult: ", previewResult);
114
148
 
115
149
  console.log("========= 小程序上传结果 ==========");
116
- console.log(`由机器人${envConfig.robot}上传`);
150
+ console.log(`由机器人${finalRobot}上传`);
117
151
  console.log(`版本:${fullVersion}`);
118
152
  console.log(`描述:${fullDesc}`);
119
153
  console.log("=================================");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "upload-miniprogram",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "description": "chatlabs 集成 微信小程序CI/CD上传工具,支持自动上传代码和生成预览二维码",
5
5
  "main": "lib/index.js",
6
6
  "bin": {