@zebr/miot-cli 0.0.0-stage → 1.0.2
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 +85 -2
- package/package.json +24 -4
- package/upload/const.js +66 -0
- package/upload/index.js +232 -0
- package/upload/readme.md +206 -0
- package/upload/utils/auth.js +93 -0
- package/upload/utils/client.js +126 -0
- package/upload/utils/pack.js +99 -0
- package/upload/utils/publish.js +82 -0
- package/upload/utils/resolve.js +125 -0
- package/upload/utils/test.js +199 -0
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zebr/miot-cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
把插件安装包打包并上传到小米 IoT 开发者平台。安装后的命令是 `miot`。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @zebr/miot-cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
需要 Node.js 18 或更高版本。
|
|
12
|
+
|
|
13
|
+
## 常用命令
|
|
14
|
+
|
|
15
|
+
在插件项目目录执行。打包用当前目录名,上传用 `--model`。不传 `--model` 时,读取该项目 `README.md` 里的 `model:`。
|
|
16
|
+
|
|
17
|
+
登录态写在本包目录的 `.iot-auth.local.json`,默认是空 cookie。这个文件不会打进 npm 包。
|
|
18
|
+
|
|
19
|
+
### 完整上传
|
|
20
|
+
|
|
21
|
+
打包、上传、提交版本,然后默认等待 10 分钟再开启测试。
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
miot
|
|
25
|
+
miot --model xxx.xxx.xxx --changelog "问题修改"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`--model` 是上传到平台时使用的型号。`--changelog` 是本次版本更新说明,不传则用脚本里的默认说明。
|
|
29
|
+
|
|
30
|
+
### 只查询
|
|
31
|
+
|
|
32
|
+
切换企业组,并查找产品和扩展程序。不打包、不上传、不开启测试。
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
miot --prepare-only
|
|
36
|
+
miot --prepare-only --model xxx.xxx.xxx
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 上传后不开启测试
|
|
40
|
+
|
|
41
|
+
流程与完整上传相同,提交版本后结束。
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
miot --skip-test --model xxx.xxx.xxx
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 修改开启测试前的等待时间
|
|
48
|
+
|
|
49
|
+
提交成功后先等待指定分钟数,再查版本状态。默认 10 分钟。仍在打包时,每 30 秒再查一次,最多再等 10 分钟。
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
miot --test-delay 10 --model xxx.xxx.xxx
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 只为指定型号开启测试
|
|
56
|
+
|
|
57
|
+
不打包、不上传。按 `--model` 查找最新版本,状态已是「开发中」就立刻开启测试。必须带 `--model`。
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
miot --start-test --model xxx.xxx.xxx
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 更新登录态
|
|
64
|
+
|
|
65
|
+
把整段 Cookie 写入 `.iot-auth.local.json` 后退出。不打包、不上传。整段内容需要放在一对引号里。
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
miot --set-cookie "serviceToken=...; userId=...; xiaomiiot_ph=..."
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 参数
|
|
72
|
+
|
|
73
|
+
| 参数 | 作用 |
|
|
74
|
+
| --- | --- |
|
|
75
|
+
| `--model` | 上传或开启测试使用的型号。完整上传不传时读项目 `README.md` |
|
|
76
|
+
| `--changelog` | 本次版本更新说明 |
|
|
77
|
+
| `--project` | 插件项目目录。不传则使用当前目录 |
|
|
78
|
+
| `--file` | 安装包路径。完整上传会先打包,实际上传刚生成的 `build/publish.mpkg` |
|
|
79
|
+
| `--group-id` | 指定企业组 |
|
|
80
|
+
| `--product-id` | 指定产品 ID |
|
|
81
|
+
| `--plugin-id` | 指定扩展程序 ID |
|
|
82
|
+
| `--prepare-only` | 只查询,不打包、不上传、不开启测试 |
|
|
83
|
+
| `--skip-test` | 提交版本后不开启测试 |
|
|
84
|
+
| `--test-delay` | 提交后等待多少分钟再开启测试,默认 10 |
|
|
85
|
+
| `--start-test` | 按 `--model` 为最新版本开启测试。不打包、不上传 |
|
|
86
|
+
| `--set-cookie` | 写入整段 Cookie 后退出 |
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zebr/miot-cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "把插件安装包打包并上传到小米 IoT 开发者平台。",
|
|
5
|
+
"bin": {
|
|
6
|
+
"miot": "upload/index.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"upload",
|
|
10
|
+
"README.md",
|
|
11
|
+
"!upload/readme.md",
|
|
12
|
+
"!.iot-auth.local.json"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"license": "UNLICENSED",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"prepublishOnly": "node --check upload/index.js"
|
|
23
|
+
},
|
|
24
|
+
"main": "index.js",
|
|
25
|
+
"author": "LinQiang Shen"
|
|
26
|
+
}
|
package/upload/const.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 上传脚本共用的路径和开发者平台地址。
|
|
3
|
+
* SDK 根目录不写死,从当前项目目录向上找,或使用环境变量 IOT_SDK_ROOT。
|
|
4
|
+
* 登录态用仓库根目录的 .iot-auth.local.json。默认是空 cookie,真实内容不要提交。
|
|
5
|
+
*/
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
|
|
9
|
+
/** 本仓库根目录。本文件位于 upload/ 时,这里是仓库根。 */
|
|
10
|
+
const SCRIPT_ROOT = path.resolve(__dirname, "..");
|
|
11
|
+
|
|
12
|
+
/** 仓库内的登录态。npm link 后仍然指向这份文件,不写到用户主目录。 */
|
|
13
|
+
const AUTH_FILE = path.join(SCRIPT_ROOT, ".iot-auth.local.json");
|
|
14
|
+
|
|
15
|
+
/** 查询类接口前缀,例如企业组、产品和扩展程序。 */
|
|
16
|
+
const API_ROOT = "https://iot.mi.com/cgi-op/api/v1";
|
|
17
|
+
|
|
18
|
+
/** 提交类接口前缀,例如切换企业和上传安装包。 */
|
|
19
|
+
const POST_API_ROOT = "https://iot.mi.com/cgi-op/post/api/v1";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 从插件项目目录向上找 miot-plugin-sdk。
|
|
23
|
+
* 判定是:这一层 package.json 有 publish 脚本,并且旁边有 projects 目录。
|
|
24
|
+
* 环境变量 IOT_SDK_ROOT 优先,方便 SDK 不在当前目录的上级时手动指定。
|
|
25
|
+
* @param {string} startDir
|
|
26
|
+
*/
|
|
27
|
+
function findSdkRoot(startDir) {
|
|
28
|
+
const fromEnv = process.env.IOT_SDK_ROOT;
|
|
29
|
+
if (fromEnv) {
|
|
30
|
+
const root = path.resolve(fromEnv);
|
|
31
|
+
if (!fs.existsSync(path.join(root, "package.json")) || !fs.existsSync(path.join(root, "projects"))) {
|
|
32
|
+
throw new Error(`IOT_SDK_ROOT 不是 miot-plugin-sdk:${root}`);
|
|
33
|
+
}
|
|
34
|
+
return root;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let dir = path.resolve(startDir);
|
|
38
|
+
while (true) {
|
|
39
|
+
const pkgPath = path.join(dir, "package.json");
|
|
40
|
+
if (fs.existsSync(pkgPath) && fs.existsSync(path.join(dir, "projects"))) {
|
|
41
|
+
let pkg = {};
|
|
42
|
+
try {
|
|
43
|
+
pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
44
|
+
} catch (error) {
|
|
45
|
+
pkg = {};
|
|
46
|
+
}
|
|
47
|
+
if (pkg.scripts && pkg.scripts.publish) return dir;
|
|
48
|
+
}
|
|
49
|
+
const parent = path.dirname(dir);
|
|
50
|
+
if (parent === dir) break;
|
|
51
|
+
dir = parent;
|
|
52
|
+
}
|
|
53
|
+
throw new Error(`从 ${startDir} 向上找不到 miot-plugin-sdk。请在插件项目目录执行,或设置 IOT_SDK_ROOT`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const USER_AGENT =
|
|
57
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/158.0.0.0 Safari/537.36";
|
|
58
|
+
|
|
59
|
+
module.exports = {
|
|
60
|
+
SCRIPT_ROOT,
|
|
61
|
+
AUTH_FILE,
|
|
62
|
+
API_ROOT,
|
|
63
|
+
POST_API_ROOT,
|
|
64
|
+
findSdkRoot,
|
|
65
|
+
USER_AGENT
|
|
66
|
+
};
|
package/upload/index.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 把 build/publish.mpkg 上传到小米 IoT 开发者平台,并创建一条自定义插件版本。
|
|
4
|
+
*
|
|
5
|
+
* 登录由人工在浏览器完成。脚本不打开登录页。
|
|
6
|
+
* 凭证文件只需整段 Cookie,userId 和 xiaomiiot_ph 从 Cookie 里取。
|
|
7
|
+
* 登录态放在仓库根目录 .iot-auth.local.json。默认 cookie 为空,填写后不要提交。
|
|
8
|
+
*
|
|
9
|
+
* 打包用当前目录名。上传用 --model,不传则读该目录 README.md 的 model。
|
|
10
|
+
* 安装包默认是 build/publish.mpkg。不在项目目录时,用 --project 指定项目路径。
|
|
11
|
+
*
|
|
12
|
+
* 目录:
|
|
13
|
+
* - const.js 路径和接口前缀
|
|
14
|
+
* - utils/auth.js 读取和更新 Cookie
|
|
15
|
+
* - utils/client.js 平台请求
|
|
16
|
+
* - utils/resolve.js 切换企业组,并按型号查找产品和扩展程序
|
|
17
|
+
* - utils/pack.js 按当前项目目录名执行 npm run publish
|
|
18
|
+
* - utils/publish.js 上传安装包并提交版本
|
|
19
|
+
* - utils/test.js 等待打包完成后开启测试
|
|
20
|
+
*
|
|
21
|
+
* 用法:
|
|
22
|
+
* miot
|
|
23
|
+
* miot --changelog "修复首页文案"
|
|
24
|
+
* miot --prepare-only
|
|
25
|
+
* miot --project /path/to/project
|
|
26
|
+
* miot --set-cookie "serviceToken=...; userId=...; xiaomiiot_ph=..."
|
|
27
|
+
* miot --start-test --model xxx.xxx.xxx
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const fs = require("fs");
|
|
31
|
+
const path = require("path");
|
|
32
|
+
const { loadAuth, saveAuthCookie } = require("./utils/auth");
|
|
33
|
+
const { switchGroup, findProduct, findPlugin } = require("./utils/resolve");
|
|
34
|
+
const { packProject } = require("./utils/pack");
|
|
35
|
+
const { pickSources, uploadPackage, createVersion } = require("./utils/publish");
|
|
36
|
+
const { startTestAfterUpload, startTestForModel } = require("./utils/test");
|
|
37
|
+
|
|
38
|
+
/** 业务参数。产品 ID、插件 ID、企业组默认按项目 model 查询,命令行可覆盖。 */
|
|
39
|
+
const DEFAULTS = {
|
|
40
|
+
model: "",
|
|
41
|
+
productId: "",
|
|
42
|
+
pluginId: null,
|
|
43
|
+
groupId: null,
|
|
44
|
+
modelPluginPattern: 1,
|
|
45
|
+
packType: 1,
|
|
46
|
+
changeLog: "问题修改222",
|
|
47
|
+
file: "",
|
|
48
|
+
project: "",
|
|
49
|
+
prepareOnly: false,
|
|
50
|
+
skipTest: false,
|
|
51
|
+
testDelayMinutes: 10,
|
|
52
|
+
setCookie: "",
|
|
53
|
+
startTest: false
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 解析命令行。支持 --key value;--prepare-only 不带取值。
|
|
58
|
+
* @param {string[]} argv
|
|
59
|
+
*/
|
|
60
|
+
function parseArgs(argv) {
|
|
61
|
+
const options = { ...DEFAULTS };
|
|
62
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
63
|
+
const key = argv[i];
|
|
64
|
+
if (!key.startsWith("--")) continue;
|
|
65
|
+
if (key === "--prepare-only") {
|
|
66
|
+
options.prepareOnly = true;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (key === "--skip-test") {
|
|
70
|
+
options.skipTest = true;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (key === "--start-test") {
|
|
74
|
+
options.startTest = true;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (key === "--set-cookie") {
|
|
78
|
+
const value = argv[i + 1];
|
|
79
|
+
if (value == null || value.startsWith("--")) {
|
|
80
|
+
throw new Error("参数 --set-cookie 缺少整段 Cookie");
|
|
81
|
+
}
|
|
82
|
+
options.setCookie = value;
|
|
83
|
+
i += 1;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const value = argv[i + 1];
|
|
87
|
+
if (value == null || value.startsWith("--")) {
|
|
88
|
+
throw new Error(`参数 ${key} 缺少取值`);
|
|
89
|
+
}
|
|
90
|
+
i += 1;
|
|
91
|
+
switch (key) {
|
|
92
|
+
case "--product-id":
|
|
93
|
+
options.productId = value;
|
|
94
|
+
break;
|
|
95
|
+
case "--plugin-id":
|
|
96
|
+
options.pluginId = Number(value);
|
|
97
|
+
break;
|
|
98
|
+
case "--group-id":
|
|
99
|
+
options.groupId = Number(value);
|
|
100
|
+
break;
|
|
101
|
+
case "--model":
|
|
102
|
+
options.model = value;
|
|
103
|
+
break;
|
|
104
|
+
case "--changelog":
|
|
105
|
+
options.changeLog = value;
|
|
106
|
+
break;
|
|
107
|
+
case "--file":
|
|
108
|
+
options.file = value;
|
|
109
|
+
break;
|
|
110
|
+
case "--project":
|
|
111
|
+
options.project = value;
|
|
112
|
+
break;
|
|
113
|
+
case "--test-delay":
|
|
114
|
+
options.testDelayMinutes = Number(value);
|
|
115
|
+
break;
|
|
116
|
+
default:
|
|
117
|
+
throw new Error(`未知参数 ${key}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (options.pluginId != null && !Number.isFinite(options.pluginId)) {
|
|
121
|
+
throw new Error("--plugin-id 必须是数字");
|
|
122
|
+
}
|
|
123
|
+
if (options.groupId != null && !Number.isFinite(options.groupId)) {
|
|
124
|
+
throw new Error("--group-id 必须是数字");
|
|
125
|
+
}
|
|
126
|
+
if (!Number.isFinite(options.testDelayMinutes) || options.testDelayMinutes < 0) {
|
|
127
|
+
throw new Error("--test-delay 必须是大于等于 0 的分钟数");
|
|
128
|
+
}
|
|
129
|
+
// 只更新登录态或只开启测试时,不必在插件目录里打包。
|
|
130
|
+
if (options.setCookie) return options;
|
|
131
|
+
if (options.startTest) {
|
|
132
|
+
if (!options.model) throw new Error("请用 --model 指定要开启测试的型号");
|
|
133
|
+
return options;
|
|
134
|
+
}
|
|
135
|
+
// 当前目录决定打哪个包。上传型号单独用 --model,不传才读 README。
|
|
136
|
+
options.project = path.resolve(options.project || process.cwd());
|
|
137
|
+
options.file = options.file
|
|
138
|
+
? path.resolve(options.project, options.file)
|
|
139
|
+
: path.join(options.project, "build/publish.mpkg");
|
|
140
|
+
if (!options.model) options.model = readProjectModel(options.project);
|
|
141
|
+
return options;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 项目 model 写在该项目 README 的 `model:` 行,例如 carrie.airc.xiaoz1。
|
|
146
|
+
* @param {string} projectRoot
|
|
147
|
+
*/
|
|
148
|
+
function readProjectModel(projectRoot) {
|
|
149
|
+
const readmePath = path.join(projectRoot, "README.md");
|
|
150
|
+
if (!fs.existsSync(readmePath)) {
|
|
151
|
+
throw new Error(`找不到 ${readmePath},请在插件项目目录执行,或用 --project 指定`);
|
|
152
|
+
}
|
|
153
|
+
const readme = fs.readFileSync(readmePath, "utf8");
|
|
154
|
+
const matched = readme.match(/^model:\s*(\S+)\s*$/m);
|
|
155
|
+
if (!matched) {
|
|
156
|
+
throw new Error(`${readmePath} 中没有 model 字段,请用 --model 传入`);
|
|
157
|
+
}
|
|
158
|
+
return matched[1];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function main() {
|
|
162
|
+
const options = parseArgs(process.argv.slice(2));
|
|
163
|
+
if (options.setCookie) {
|
|
164
|
+
const saved = saveAuthCookie(options.setCookie);
|
|
165
|
+
console.log(`已更新 Cookie:${saved.file}`);
|
|
166
|
+
console.log(`userId=${saved.userId}`);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (options.startTest) {
|
|
170
|
+
console.log(`开启测试型号 ${options.model}`);
|
|
171
|
+
const auth = loadAuth();
|
|
172
|
+
const group = await switchGroup(options, auth);
|
|
173
|
+
console.log(`已切换企业组 ${group.compName}(groupId=${group.groupId},shortName=${group.shortName})`);
|
|
174
|
+
const product = await findProduct(options, auth);
|
|
175
|
+
options.productId = String(product.pdId);
|
|
176
|
+
console.log(`产品 ${product.name || options.model}(productId=${options.productId})`);
|
|
177
|
+
const plugin = await findPlugin(options, auth, options.productId);
|
|
178
|
+
options.pluginId = plugin.pluginId;
|
|
179
|
+
console.log(`扩展程序 ${plugin.name || options.pluginId}(pluginId=${options.pluginId})`);
|
|
180
|
+
await startTestForModel(options, auth);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
console.log(`项目目录 ${options.project}`);
|
|
184
|
+
console.log(`打包项目 ${path.basename(options.project)}`);
|
|
185
|
+
console.log(`上传型号 ${options.model}`);
|
|
186
|
+
|
|
187
|
+
// 只查询时不打包。真正上传前,先按当前目录名打出最新的 publish.mpkg。
|
|
188
|
+
if (!options.prepareOnly) {
|
|
189
|
+
await packProject(options);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const auth = loadAuth();
|
|
193
|
+
const group = await switchGroup(options, auth);
|
|
194
|
+
console.log(`已切换企业组 ${group.compName}(groupId=${group.groupId},shortName=${group.shortName})`);
|
|
195
|
+
|
|
196
|
+
const product = await findProduct(options, auth);
|
|
197
|
+
options.productId = String(product.pdId);
|
|
198
|
+
console.log(`产品 ${product.name || options.model}(productId=${options.productId})`);
|
|
199
|
+
|
|
200
|
+
const plugin = await findPlugin(options, auth, options.productId);
|
|
201
|
+
options.pluginId = plugin.pluginId;
|
|
202
|
+
console.log(`扩展程序 ${plugin.name || options.pluginId}(pluginId=${options.pluginId})`);
|
|
203
|
+
|
|
204
|
+
if (options.prepareOnly) return;
|
|
205
|
+
if (!fs.existsSync(options.file)) {
|
|
206
|
+
throw new Error(`找不到安装包:${options.file}`);
|
|
207
|
+
}
|
|
208
|
+
const sizeMb = (fs.statSync(options.file).size / 1024 / 1024).toFixed(1);
|
|
209
|
+
console.log(`安装包 ${options.file}(${sizeMb} MB),更新说明:${options.changeLog}`);
|
|
210
|
+
console.log("开始上传安装包…");
|
|
211
|
+
|
|
212
|
+
const uploaded = await uploadPackage(options, auth);
|
|
213
|
+
const sources = pickSources(uploaded);
|
|
214
|
+
if (!sources) {
|
|
215
|
+
throw new Error(`上传成功但没有解析到 sources 路径:${JSON.stringify(uploaded)}`);
|
|
216
|
+
}
|
|
217
|
+
console.log(`上传完成,sources=${sources}`);
|
|
218
|
+
console.log("开始提交插件版本…");
|
|
219
|
+
|
|
220
|
+
const submittedAt = Date.now();
|
|
221
|
+
const created = await createVersion(options, auth, sources);
|
|
222
|
+
console.log("提交完成:");
|
|
223
|
+
console.log(JSON.stringify(created, null, 2));
|
|
224
|
+
|
|
225
|
+
if (options.skipTest) return;
|
|
226
|
+
await startTestAfterUpload(options, auth, submittedAt);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
main().catch((error) => {
|
|
230
|
+
console.error(error instanceof Error ? error.message : error);
|
|
231
|
+
process.exit(1);
|
|
232
|
+
});
|
package/upload/readme.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# 插件上传说明
|
|
2
|
+
|
|
3
|
+
`miot` 把 `build/publish.mpkg` 上传到小米 IoT 开发者平台,并创建一条自定义插件版本。仓库放在哪都可以,安装后命令名都是 `miot`。登录在浏览器里完成,Cookie 写在仓库根目录的 `.iot-auth.local.json`。这份文件默认是空 cookie,填入真实登录态后不要提交。
|
|
4
|
+
|
|
5
|
+
在本仓库执行一次:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm link
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
之后在插件项目目录里使用 `miot`。SDK 根目录从当前目录向上找:那一层要有 `projects` 目录,并且 `package.json` 里有 `publish` 脚本。找不到时设置环境变量 `IOT_SDK_ROOT`。
|
|
12
|
+
|
|
13
|
+
## 上传前准备
|
|
14
|
+
|
|
15
|
+
1. 在对应的插件项目目录里执行上传脚本。打包用当前目录名,上传用 `--model`。例如当前目录是 `.../projects/bull.bhf_light.yub01`、参数是 `--model xxx.xxx.xxx` 时,先执行 `npm run publish bull.bhf_light.yub01`,再按 `xxx.xxx.xxx` 上传,并打印新生成的 `build/publish.mpkg` 的修改时间。
|
|
16
|
+
2. 在浏览器登录 [iot.mi.com](https://iot.mi.com)。
|
|
17
|
+
3. 执行 `miot --set-cookie`,把已登录请求的整段 Cookie 写入仓库根目录的 `.iot-auth.local.json`。文件默认是 `{ "cookie": "" }`,填入后不要提交。
|
|
18
|
+
|
|
19
|
+
## Cookie
|
|
20
|
+
|
|
21
|
+
文件只需要整段 Cookie。`userId` 和 `xiaomiiot_ph` 由脚本从 Cookie 里解析,不用单独填写。
|
|
22
|
+
|
|
23
|
+
JSON 写法:
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
{
|
|
27
|
+
"cookie": "serviceToken=...; userId=...; xiaomiiot_ph=...; xiaomiiot_slh=..."
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
也可以不写 JSON,把整段 Cookie 直接作为文件内容。
|
|
32
|
+
|
|
33
|
+
Cookie 里必须能看到这三项:
|
|
34
|
+
|
|
35
|
+
| 字段 | 作用 |
|
|
36
|
+
| -------------- | ------------------------------------------- |
|
|
37
|
+
| `serviceToken` | 登录凭证。没有它,接口会直接失败 |
|
|
38
|
+
| `userId` | 当前小米账号,脚本会放到接口 URL 上 |
|
|
39
|
+
| `xiaomiiot_ph` | 和登录态配套的校验值,脚本会放到接口 URL 上 |
|
|
40
|
+
|
|
41
|
+
`serviceToken` 是 HttpOnly,控制台执行 `document.cookie` 看不到。复制方式:
|
|
42
|
+
|
|
43
|
+
1. 打开开发者工具的 Network。
|
|
44
|
+
2. 选一条发往 `iot.mi.com/cgi-op/` 的请求。
|
|
45
|
+
3. 复制该请求头里的整段 Cookie。
|
|
46
|
+
|
|
47
|
+
`Key_Org_Id`、`Key_Obj_Key`、`s3`、`lastOpen` 可以留在整段 Cookie 里,脚本不依赖它们判断是否已登录。浏览器退出登录不会使这份本地 Cookie 失效;只有服务端会话过期,或 Cookie 里不再包含 `serviceToken` 时,才需要重新复制。
|
|
48
|
+
|
|
49
|
+
## 脚本做了什么
|
|
50
|
+
|
|
51
|
+
打包取当前目录的最后一段。在 `bull.bhf_light.yub01` 目录执行时,打包项目是 `bull.bhf_light.yub01`。上传型号用 `--model`。
|
|
52
|
+
|
|
53
|
+
1. 读取当前项目目录名。目录必须在 SDK 的 `projects` 下,例如 `.../projects/bull.bhf_light.yub01`。
|
|
54
|
+
2. 切到找到的 SDK 根目录,执行 `npm run publish bull.bhf_light.yub01`。打包过程会原样打印。成功后打印最新 `build/publish.mpkg` 的修改时间,再继续。
|
|
55
|
+
3. 调用企业组列表,用型号第一段匹配企业组 `shortName` 并切换。`carrie.airc.xiaoz1` 对应 `carrie`(开利暖通空调经营(上海)有限公司,`groupId=19832`)。
|
|
56
|
+
4. 在该企业下按完整型号查产品。当前产品是东芝 XIAOZHI 中央空调,`productId=84881`。
|
|
57
|
+
5. 在扩展程序关联关系里只按 `model` 匹配。当前扩展程序 `pluginId=1046933`。
|
|
58
|
+
6. 上传刚打出来的 `build/publish.mpkg`。
|
|
59
|
+
7. 用上传返回的 `sources` 提交自定义插件版本。更新说明默认是脚本里的 `changeLog`。
|
|
60
|
+
8. 默认再等 10 分钟。版本状态变成「开发中」后,调用开启测试,测试范围为全部成员。
|
|
61
|
+
|
|
62
|
+
企业组、产品、扩展程序都能自动解析。同一厂商前缀匹配到多家企业,或同一型号匹配到多个扩展程序时,脚本会停下来,再用下面的参数指定。
|
|
63
|
+
|
|
64
|
+
## 用法
|
|
65
|
+
|
|
66
|
+
在插件项目目录执行。打包取该目录名,上传用 `--model`。安装包是该目录的 `build/publish.mpkg`。
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
miot
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
只解析企业组、产品和扩展程序,不上传:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
miot --prepare-only
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
指定更新说明和安装包:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
miot --changelog "修复首页文案" --file build/publish.mpkg
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
可选参数:
|
|
85
|
+
|
|
86
|
+
| 参数 | 作用 |
|
|
87
|
+
| ---------------- | ------------------------------------- |
|
|
88
|
+
| `--changelog` | 本次版本更新说明 |
|
|
89
|
+
| `--file` | 安装包路径,相对项目根目录 |
|
|
90
|
+
| `--model` | 上传时使用的型号。不传则读 README.md |
|
|
91
|
+
| `--group-id` | 指定企业组 |
|
|
92
|
+
| `--product-id` | 指定产品 ID |
|
|
93
|
+
| `--plugin-id` | 指定扩展程序 ID |
|
|
94
|
+
| `--prepare-only` | 只查询,不打包、不上传、不提交 |
|
|
95
|
+
| `--project` | 插件项目目录。不传则使用当前目录 |
|
|
96
|
+
| `--skip-test` | 提交版本后不开启测试 |
|
|
97
|
+
| `--test-delay` | 提交后等待多少分钟再开启测试,默认 10 |
|
|
98
|
+
| `--set-cookie` | 把整段 Cookie 写入登录态文件后退出。不打包、不上传 |
|
|
99
|
+
| `--start-test` | 按 `--model` 为最新版本开启测试。不打包、不上传 |
|
|
100
|
+
|
|
101
|
+
只为指定型号开启测试,不打包、不上传:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
miot --start-test --model xxx.xxx.xxx
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
会按这个型号切换企业组、查找产品和扩展程序,再为最新版本开启测试。状态已是「开发中」就立刻开启。还在打包时每 30 秒查一次,最多再等 10 分钟。必须带 `--model`。
|
|
108
|
+
|
|
109
|
+
更新登录态,不上传:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
miot --set-cookie "serviceToken=...; userId=...; xiaomiiot_ph=..."
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
整段 Cookie 需要放在一对引号里。脚本检查 `serviceToken`、`userId`、`xiaomiiot_ph` 都在之后,写入仓库根目录 `.iot-auth.local.json` 的 `cookie` 字段,然后结束。
|
|
116
|
+
|
|
117
|
+
## 每条命令的执行流程
|
|
118
|
+
|
|
119
|
+
打包用当前目录名,上传用 `--model`。当前目录是 `/Users/linsen/MIOT/miot-plugin-sdk/projects/bull.bhf_light.yub01` 且传入 `--model xxx.xxx.xxx` 时,先打包 `bull.bhf_light.yub01`,再按 `xxx.xxx.xxx` 查产品、扩展程序并提交。
|
|
120
|
+
|
|
121
|
+
### `miot`
|
|
122
|
+
|
|
123
|
+
完整上传。先打包,再查询、传包、提交版本。
|
|
124
|
+
|
|
125
|
+
1. 用当前目录名打包。在 `projects/bull.bhf_light.yub01` 里执行时,会在找到的 SDK 根目录执行 `npm run publish bull.bhf_light.yub01`。打包过程原样打印。成功后打印 `build/publish.mpkg` 的修改时间和大小,再继续。
|
|
126
|
+
2. 读取仓库根目录 `.iot-auth.local.json` 的整段 Cookie,解析出 `userId`、`xiaomiiot_ph`。缺少 `serviceToken`、`userId` 或 `xiaomiiot_ph` 时在这里失败。
|
|
127
|
+
3. 请求企业组列表 `userGroupList`。用型号第一段 `carrie` 匹配 `shortName`,必须恰好一家。
|
|
128
|
+
4. 调用 `setCurrEnterprise`,把当前企业切到开利(`groupId=19832`)。这一步会改服务器上的当前企业。脚本再在本次请求的 Cookie 里写入 `Key_Org_Id=19832`、`Key_Obj_Key=carrie.airc.xiaoz1`。
|
|
129
|
+
5. 用完整型号搜索产品,只保留 `shortName` 完全等于 `carrie.airc.xiaoz1` 的一条,得到 `productId=84881`。
|
|
130
|
+
6. 拉取该企业的扩展程序,查关联关系。只保留 `model` 等于 `carrie.airc.xiaoz1` 的一条,当前是 `pluginId=1046933`。
|
|
131
|
+
7. 上传刚打出来的安装包。接口返回 `sources` 路径。
|
|
132
|
+
8. 用该 `sources`、`pluginId`、型号和更新说明提交自定义插件版本。成功时打印 `status: 200`。
|
|
133
|
+
9. 等待 10 分钟,再按 `pluginId` 拉取版本列表。用本次更新说明和提交时间找到新版本。状态是「开发中」时调用 `/plugin/package/status/update` 开启测试,`type=2` 表示全部成员。若仍在打包,每 30 秒再查一次,最多再等 10 分钟。
|
|
134
|
+
|
|
135
|
+
### `miot --prepare-only`
|
|
136
|
+
|
|
137
|
+
只核对登录态、企业组、产品和扩展程序。不打包。走到企业组、产品和扩展程序这三项并打印后结束。
|
|
138
|
+
|
|
139
|
+
不检查安装包,不调用上传接口,也不提交版本。切换企业仍会执行。正常结束时退出码为 0,输出类似:
|
|
140
|
+
|
|
141
|
+
```text
|
|
142
|
+
项目型号 carrie.airc.xiaoz1
|
|
143
|
+
已切换企业组 开利暖通空调经营(上海)有限公司(groupId=19832,shortName=carrie)
|
|
144
|
+
产品 东芝XIAOZHI中央空调(productId=84881)
|
|
145
|
+
扩展程序 东芝XIAOZHI中央空调_carrie.airc.xiaoz1_c6vGVN_x2(pluginId=1046933)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### `miot --changelog "修复首页文案" --file build/publish.mpkg`
|
|
149
|
+
|
|
150
|
+
流程与不带参数的完整上传相同。这两个参数不增加接口,只替换提交时用的值:
|
|
151
|
+
|
|
152
|
+
- `--changelog` 替换请求体里的 `changeLog`。不传则用脚本里的默认说明。
|
|
153
|
+
- 完整上传会先打包,实际上传的是刚生成的 `build/publish.mpkg`。`--file` 不会改掉这个文件。
|
|
154
|
+
|
|
155
|
+
### `miot --model carrie.airc.xiaoz1`
|
|
156
|
+
|
|
157
|
+
`--model` 只用于上传。在 `bull.bhf_light.yub01` 目录里执行 `--model xxx.xxx.xxx` 时,打包仍是 `bull.bhf_light.yub01`,企业组、产品和扩展程序按 `xxx.xxx.xxx` 查找。
|
|
158
|
+
|
|
159
|
+
### `miot --group-id 19832`
|
|
160
|
+
|
|
161
|
+
企业组不再按 `shortName` 猜测,改为在列表里找这个 `groupId`。找到后仍然调用 `setCurrEnterprise`。找不到,或需要区分多家企业时,用这个参数。产品和扩展程序的查询步骤不变。
|
|
162
|
+
|
|
163
|
+
### `miot --product-id 84881`
|
|
164
|
+
|
|
165
|
+
跳过产品搜索接口,直接把这个 ID 当作 `productId`。企业组切换仍会执行。扩展程序只按型号核对,不拿这个 ID 做匹配。
|
|
166
|
+
|
|
167
|
+
### `miot --plugin-id 1046933`
|
|
168
|
+
|
|
169
|
+
跳过扩展程序列表和关联关系查询,上传、提交都用这个 `pluginId`。企业组和产品查询仍会执行。ID 和当前产品不一致时,失败发生在平台的上传或提交接口,而不是本地匹配。
|
|
170
|
+
|
|
171
|
+
### 如何执行
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
linsen@LinsenMacBook-Pro bull.bhf_light.yub01 % miot --model xxx.xxx.xxx --changelog "问题修改"
|
|
175
|
+
项目目录 /Users/linsen/MIOT/miot-plugin-sdk/projects/bull.bhf_light.yub01
|
|
176
|
+
项目型号 xxx.xxx.xxx
|
|
177
|
+
已切换企业组 公牛集团股份有限公司(groupId=3758,shortName=bull)
|
|
178
|
+
产品 公牛浴霸Y36A(productId=41263)
|
|
179
|
+
扩展程序 公牛浴霸Y36A_xxx.xxx.xxx_l4WoYzIa7(pluginId=1046911)
|
|
180
|
+
安装包 /Users/linsen/MIOT/miot-plugin-sdk/projects/bull.bhf_light.yub01/build/publish.mpkg(11.0 MB),更新说明:问题修改
|
|
181
|
+
开始上传安装包…
|
|
182
|
+
上传完成,sources=2026-11-25/1046911-48347f5dc771447ec38596a38e9a87c3-publish.mpkg
|
|
183
|
+
开始提交插件版本…
|
|
184
|
+
提交完成:
|
|
185
|
+
{
|
|
186
|
+
"status": 200,
|
|
187
|
+
"message": "成功",
|
|
188
|
+
"result": null,
|
|
189
|
+
"timestamp": "2026-11-25 16:50:31.386",
|
|
190
|
+
"requestId": "34d3a926974edefa4ea077e95f347385"
|
|
191
|
+
}
|
|
192
|
+
版本已提交,10 分钟后开启测试
|
|
193
|
+
距离开启测试还有 10 分钟
|
|
194
|
+
距离开启测试还有 9 分钟
|
|
195
|
+
距离开启测试还有 8 分钟
|
|
196
|
+
距离开启测试还有 7 分钟
|
|
197
|
+
距离开启测试还有 6 分钟
|
|
198
|
+
距离开启测试还有 5 分钟
|
|
199
|
+
距离开启测试还有 4 分钟
|
|
200
|
+
距离开启测试还有 3 分钟
|
|
201
|
+
距离开启测试还有 2 分钟
|
|
202
|
+
距离开启测试还有 1 分钟
|
|
203
|
+
版本 9 当前是开发中
|
|
204
|
+
已开启测试,版本 9,测试范围为全部成员
|
|
205
|
+
linsen@LinsenMacBook-Pro bull.bhf_light.yub01 %
|
|
206
|
+
```
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 读取浏览器登录后的 Cookie,并解析接口需要的账号字段。
|
|
3
|
+
* 文件里只需要整段 Cookie,userId 和 xiaomiiot_ph 不单独保存。
|
|
4
|
+
*/
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const { AUTH_FILE } = require("../const");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 从 Cookie 串里取某个字段。值若被 URL 编码,这里解回原文。
|
|
10
|
+
* @param {string} cookie
|
|
11
|
+
* @param {string} name
|
|
12
|
+
*/
|
|
13
|
+
function readCookieValue(cookie, name) {
|
|
14
|
+
const prefix = `${name}=`;
|
|
15
|
+
const item = String(cookie)
|
|
16
|
+
.split(";")
|
|
17
|
+
.map((part) => part.trim())
|
|
18
|
+
.find((part) => part.startsWith(prefix));
|
|
19
|
+
if (!item) return "";
|
|
20
|
+
const raw = item.slice(prefix.length);
|
|
21
|
+
try {
|
|
22
|
+
return decodeURIComponent(raw);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
return raw;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 读取本地登录态。支持 JSON 的 cookie 字段,也支持文件内容直接是整段 Cookie。
|
|
30
|
+
* @returns {{ cookie: string, userId: string, xiaomiiotPh: string }}
|
|
31
|
+
*/
|
|
32
|
+
function loadAuth() {
|
|
33
|
+
const text = fs.readFileSync(AUTH_FILE, "utf8").trim();
|
|
34
|
+
let cookie = text;
|
|
35
|
+
if (text.startsWith("{")) {
|
|
36
|
+
const saved = JSON.parse(text);
|
|
37
|
+
cookie = String(saved.cookie || "").trim();
|
|
38
|
+
}
|
|
39
|
+
if (!cookie.includes("serviceToken=")) {
|
|
40
|
+
throw new Error('cookie 里没有 serviceToken。请从浏览器已登录请求的 Cookie 请求头整段复制,再执行 miot --set-cookie "整段 Cookie"');
|
|
41
|
+
}
|
|
42
|
+
const userId = readCookieValue(cookie, "userId");
|
|
43
|
+
const xiaomiiotPh = readCookieValue(cookie, "xiaomiiot_ph");
|
|
44
|
+
const missing = [!userId ? "userId" : "", !xiaomiiotPh ? "xiaomiiot_ph" : ""].filter(Boolean);
|
|
45
|
+
if (missing.length > 0) {
|
|
46
|
+
throw new Error(`cookie 里缺少 ${missing.join("、")}`);
|
|
47
|
+
}
|
|
48
|
+
return { cookie, userId, xiaomiiotPh };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 把整段 Cookie 写入本地登录态。只保存 cookie 字段,账号信息仍从这段里解析。
|
|
53
|
+
* 写入前先检查上传接口必需的三项,避免把空登录态存下来。
|
|
54
|
+
* @param {string} cookie
|
|
55
|
+
* @returns {{ cookie: string, userId: string, xiaomiiotPh: string }}
|
|
56
|
+
*/
|
|
57
|
+
function saveAuthCookie(cookie) {
|
|
58
|
+
const normalized = String(cookie || "").trim();
|
|
59
|
+
if (!normalized.includes("serviceToken=")) {
|
|
60
|
+
throw new Error("cookie 里没有 serviceToken。请从浏览器已登录请求的 Cookie 请求头整段复制");
|
|
61
|
+
}
|
|
62
|
+
const userId = readCookieValue(normalized, "userId");
|
|
63
|
+
const xiaomiiotPh = readCookieValue(normalized, "xiaomiiot_ph");
|
|
64
|
+
const missing = [!userId ? "userId" : "", !xiaomiiotPh ? "xiaomiiot_ph" : ""].filter(Boolean);
|
|
65
|
+
if (missing.length > 0) {
|
|
66
|
+
throw new Error(`cookie 里缺少 ${missing.join("、")}`);
|
|
67
|
+
}
|
|
68
|
+
fs.writeFileSync(AUTH_FILE, `${JSON.stringify({ cookie: normalized }, null, 2)}\n`, "utf8");
|
|
69
|
+
return { cookie: normalized, userId, xiaomiiotPh, file: AUTH_FILE };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 替换或追加一个 Cookie。切换企业后把 Key_Org_Id 写成目标企业组。
|
|
74
|
+
* @param {string} cookie
|
|
75
|
+
* @param {string} name
|
|
76
|
+
* @param {string | number} value
|
|
77
|
+
*/
|
|
78
|
+
function upsertCookie(cookie, name, value) {
|
|
79
|
+
const parts = String(cookie)
|
|
80
|
+
.split(";")
|
|
81
|
+
.map((item) => item.trim())
|
|
82
|
+
.filter(Boolean)
|
|
83
|
+
.filter((item) => item.slice(0, name.length + 1) !== `${name}=`);
|
|
84
|
+
parts.push(`${name}=${value}`);
|
|
85
|
+
return parts.join("; ");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = {
|
|
89
|
+
readCookieValue,
|
|
90
|
+
loadAuth,
|
|
91
|
+
saveAuthCookie,
|
|
92
|
+
upsertCookie
|
|
93
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 开发者平台请求封装。查询和上传都通过这里带上 userId、xiaomiiot_ph 和 Cookie。
|
|
3
|
+
*/
|
|
4
|
+
const { USER_AGENT } = require("../const");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 拼开发者平台接口地址。xiaomiiot_ph 由 URLSearchParams 编码。
|
|
8
|
+
* @param {string} base
|
|
9
|
+
* @param {{ userId: string, xiaomiiotPh: string }} auth
|
|
10
|
+
* @param {Record<string, string | number>} [params]
|
|
11
|
+
*/
|
|
12
|
+
function withAuthQuery(base, auth, params) {
|
|
13
|
+
const url = new URL(base);
|
|
14
|
+
url.searchParams.set("userId", auth.userId);
|
|
15
|
+
url.searchParams.set("xiaomiiot_ph", auth.xiaomiiotPh);
|
|
16
|
+
if (params) {
|
|
17
|
+
for (const [key, value] of Object.entries(params)) {
|
|
18
|
+
url.searchParams.set(key, String(value));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return url;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 页面上 XHR 会带的来源信息。已知产品时,Referer 指向扩展程序配置页。
|
|
26
|
+
* @param {string} productId
|
|
27
|
+
* @param {string} cookie
|
|
28
|
+
* @param {Record<string, string>} [extra]
|
|
29
|
+
*/
|
|
30
|
+
function buildHeaders(productId, cookie, extra) {
|
|
31
|
+
const referer = productId
|
|
32
|
+
? `https://iot.mi.com/fe-op/productCenter/config/extension?productId=${productId}`
|
|
33
|
+
: "https://iot.mi.com/fe-op/";
|
|
34
|
+
return {
|
|
35
|
+
accept: "application/json, text/plain, */*",
|
|
36
|
+
"accept-language": "zh-CN,zh;q=0.9",
|
|
37
|
+
origin: "https://iot.mi.com",
|
|
38
|
+
referer,
|
|
39
|
+
"user-agent": USER_AGENT,
|
|
40
|
+
"x-requested-with": "XMLHttpRequest",
|
|
41
|
+
cookie,
|
|
42
|
+
...extra
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {unknown} value
|
|
48
|
+
* @param {string} key
|
|
49
|
+
*/
|
|
50
|
+
function nested(value, key) {
|
|
51
|
+
if (!value || typeof value !== "object") return undefined;
|
|
52
|
+
return /** @type {Record<string, unknown>} */ (value)[key];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 平台成功码是 0 或 200。其它 code/status 视为业务失败。
|
|
57
|
+
* @param {unknown} payload
|
|
58
|
+
* @param {string} action
|
|
59
|
+
*/
|
|
60
|
+
function assertOk(payload, action) {
|
|
61
|
+
if (!payload || typeof payload !== "object") {
|
|
62
|
+
throw new Error(`${action} 返回的不是 JSON`);
|
|
63
|
+
}
|
|
64
|
+
const body = /** @type {Record<string, unknown>} */ (payload);
|
|
65
|
+
const code = body.code ?? body.status;
|
|
66
|
+
if (code == null) return;
|
|
67
|
+
if (code === 0 || code === 200) return;
|
|
68
|
+
const message = body.message || body.msg || body.error || JSON.stringify(body);
|
|
69
|
+
throw new Error(`${action} 失败(code=${code}):${message}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 读取响应正文。HTTP 失败或返回 HTML 登录页时,把片段放进错误信息。
|
|
74
|
+
* @param {Response} response
|
|
75
|
+
* @param {string} action
|
|
76
|
+
*/
|
|
77
|
+
async function readBody(response, action) {
|
|
78
|
+
const text = await response.text();
|
|
79
|
+
let payload;
|
|
80
|
+
try {
|
|
81
|
+
payload = text ? JSON.parse(text) : null;
|
|
82
|
+
} catch (error) {
|
|
83
|
+
const preview = text.slice(0, 300).replace(/\s+/g, " ");
|
|
84
|
+
// HTML 登录页或网关错误页都不是业务 JSON,提示重新写入 Cookie。
|
|
85
|
+
const expired = /<!doctype html|<html|login/i.test(text);
|
|
86
|
+
const hint = expired
|
|
87
|
+
? "Cookie 可能已失效。请从浏览器已登录请求重新复制整段 Cookie,再执行 miot --set-cookie \"整段 Cookie\""
|
|
88
|
+
: "请确认登录态仍有效。需要更新时执行 miot --set-cookie \"整段 Cookie\"";
|
|
89
|
+
console.error(hint);
|
|
90
|
+
throw new Error(`${action} 返回的不是 JSON(HTTP ${response.status}):${preview}`);
|
|
91
|
+
}
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
throw new Error(`${action} HTTP ${response.status}:${JSON.stringify(payload)}`);
|
|
94
|
+
}
|
|
95
|
+
assertOk(payload, action);
|
|
96
|
+
return payload;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 发送 JSON 接口。上传安装包使用 multipart,不走这个方法。
|
|
101
|
+
* @param {string} action
|
|
102
|
+
* @param {string} url
|
|
103
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
104
|
+
* @param {{ method?: string, productId?: string, params?: Record<string, string | number>, body?: unknown }} [request]
|
|
105
|
+
*/
|
|
106
|
+
async function requestJson(action, url, auth, request) {
|
|
107
|
+
const options = request || {};
|
|
108
|
+
const headers = buildHeaders(options.productId || "", auth.cookie);
|
|
109
|
+
/** @type {RequestInit} */
|
|
110
|
+
const init = { method: options.method || "GET", headers };
|
|
111
|
+
if (options.body != null) {
|
|
112
|
+
headers["content-type"] = "application/json";
|
|
113
|
+
init.body = JSON.stringify(options.body);
|
|
114
|
+
}
|
|
115
|
+
const response = await fetch(withAuthQuery(url, auth, options.params), init);
|
|
116
|
+
const payload = await readBody(response, action);
|
|
117
|
+
return { payload, auth };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = {
|
|
121
|
+
withAuthQuery,
|
|
122
|
+
buildHeaders,
|
|
123
|
+
nested,
|
|
124
|
+
readBody,
|
|
125
|
+
requestJson
|
|
126
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 上传前先在 miot-plugin-sdk 里打开发布包。
|
|
3
|
+
* 项目名取当前窗口目录的最后一段,例如
|
|
4
|
+
* /Users/linsen/MIOT/miot-plugin-sdk/projects/XXX.XXX.XXX
|
|
5
|
+
* 对应命令:npm run publish XXX.XXX.XXX
|
|
6
|
+
*/
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
const { spawn } = require("child_process");
|
|
10
|
+
const { findSdkRoot } = require("../const");
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 当前目录必须是 projects 下的某个插件项目。
|
|
14
|
+
* @param {string} projectDir
|
|
15
|
+
* @returns {{ packageName: string, mpkgPath: string }}
|
|
16
|
+
*/
|
|
17
|
+
function resolvePackage(projectDir) {
|
|
18
|
+
const sdkRoot = findSdkRoot(projectDir);
|
|
19
|
+
const projectsRoot = path.join(sdkRoot, "projects");
|
|
20
|
+
const packageName = path.basename(projectDir);
|
|
21
|
+
const expectedDir = path.join(projectsRoot, packageName);
|
|
22
|
+
if (path.resolve(projectDir) !== path.resolve(expectedDir)) {
|
|
23
|
+
throw new Error(`请在插件项目目录执行。当前目录是 ${projectDir},需要是 ${expectedDir}`);
|
|
24
|
+
}
|
|
25
|
+
if (!fs.existsSync(path.join(expectedDir, "project.json"))) {
|
|
26
|
+
throw new Error(`找不到项目 ${expectedDir},无法执行 npm run publish ${packageName}`);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
sdkRoot,
|
|
30
|
+
packageName,
|
|
31
|
+
mpkgPath: path.join(expectedDir, "build", "publish.mpkg")
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {Date} date
|
|
37
|
+
*/
|
|
38
|
+
function formatTime(date) {
|
|
39
|
+
const pad = (value) => String(value).padStart(2, "0");
|
|
40
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 在 SDK 根目录执行打包,并把 npm 的输出原样打出来。
|
|
45
|
+
* @param {string} packageName
|
|
46
|
+
* @param {string} sdkRoot
|
|
47
|
+
* @returns {Promise<void>}
|
|
48
|
+
*/
|
|
49
|
+
function runPublish(packageName, sdkRoot) {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const child = spawn("npm", ["run", "publish", "--", packageName], {
|
|
52
|
+
cwd: sdkRoot,
|
|
53
|
+
stdio: "inherit",
|
|
54
|
+
env: process.env
|
|
55
|
+
});
|
|
56
|
+
child.on("error", (error) => {
|
|
57
|
+
reject(new Error(`无法执行 npm run publish ${packageName}:${error.message}`));
|
|
58
|
+
});
|
|
59
|
+
child.on("close", (code) => {
|
|
60
|
+
if (code === 0) {
|
|
61
|
+
resolve();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
reject(new Error(`打包失败,npm run publish ${packageName} 退出码 ${code}`));
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 先打包,确认 publish.mpkg 的修改时间是这次生成的,再交给后续上传。
|
|
71
|
+
* @param {{ project: string, model: string, file: string }} options
|
|
72
|
+
*/
|
|
73
|
+
async function packProject(options) {
|
|
74
|
+
const { sdkRoot, packageName, mpkgPath } = resolvePackage(options.project);
|
|
75
|
+
// 只决定打哪个包,不改上传用的 --model。
|
|
76
|
+
|
|
77
|
+
const startedAt = Date.now();
|
|
78
|
+
console.log(`当前目录 ${options.project}`);
|
|
79
|
+
console.log(`SDK 根目录 ${sdkRoot}`);
|
|
80
|
+
console.log(`执行 npm run publish ${packageName}`);
|
|
81
|
+
await runPublish(packageName, sdkRoot);
|
|
82
|
+
|
|
83
|
+
if (!fs.existsSync(mpkgPath)) {
|
|
84
|
+
throw new Error(`打包命令已结束,但没有生成 ${mpkgPath}`);
|
|
85
|
+
}
|
|
86
|
+
const stat = fs.statSync(mpkgPath);
|
|
87
|
+
// 命令成功却没写出新文件时,不能拿旧包继续上传。
|
|
88
|
+
if (stat.mtimeMs + 2000 < startedAt) {
|
|
89
|
+
throw new Error(`安装包时间没有更新:${mpkgPath},文件时间 ${formatTime(stat.mtime)}`);
|
|
90
|
+
}
|
|
91
|
+
const sizeMb = (stat.size / 1024 / 1024).toFixed(1);
|
|
92
|
+
console.log(`打包完成:${mpkgPath}`);
|
|
93
|
+
console.log(`最新包文件时间:${formatTime(stat.mtime)}(${sizeMb} MB)`);
|
|
94
|
+
options.file = mpkgPath;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = {
|
|
98
|
+
packProject
|
|
99
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 上传 publish.mpkg,并用返回的 sources 创建自定义插件版本。
|
|
3
|
+
*/
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { API_ROOT, POST_API_ROOT } = require("../const");
|
|
7
|
+
const { withAuthQuery, buildHeaders, nested, readBody, requestJson } = require("./client");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 从上传接口响应里取出 sources 路径。
|
|
11
|
+
* 成功时一般是 "2026-11-24/1046933-xxxx-publish.mpkg"。
|
|
12
|
+
* @param {unknown} payload
|
|
13
|
+
* @returns {string | null}
|
|
14
|
+
*/
|
|
15
|
+
function pickSources(payload) {
|
|
16
|
+
if (!payload || typeof payload !== "object") return null;
|
|
17
|
+
const body = /** @type {Record<string, unknown>} */ (payload);
|
|
18
|
+
const candidates = [
|
|
19
|
+
body.result,
|
|
20
|
+
body.data,
|
|
21
|
+
body.sources,
|
|
22
|
+
nested(body.result, "sources"),
|
|
23
|
+
nested(body.result, "url"),
|
|
24
|
+
nested(body.result, "path"),
|
|
25
|
+
nested(body.data, "sources"),
|
|
26
|
+
nested(body.data, "url"),
|
|
27
|
+
nested(body.data, "path")
|
|
28
|
+
];
|
|
29
|
+
for (const item of candidates) {
|
|
30
|
+
if (typeof item !== "string") continue;
|
|
31
|
+
if (item.includes(".mpkg") || /^\d{4}-\d{2}-\d{2}\//.test(item)) return item;
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 把安装包传到资源接口。返回值里带后续创建版本要用的 sources 路径。
|
|
38
|
+
* @param {{ file: string, pluginId: number, productId: string }} options
|
|
39
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
40
|
+
*/
|
|
41
|
+
async function uploadPackage(options, auth) {
|
|
42
|
+
const bytes = fs.readFileSync(options.file);
|
|
43
|
+
const filename = path.basename(options.file);
|
|
44
|
+
const form = new FormData();
|
|
45
|
+
form.append("pluginId", String(options.pluginId));
|
|
46
|
+
form.append("sources", new Blob([bytes], { type: "application/octet-stream" }), filename);
|
|
47
|
+
|
|
48
|
+
const response = await fetch(withAuthQuery(`${POST_API_ROOT}/plugin/sources/upload`, auth), {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: buildHeaders(options.productId, auth.cookie, { accept: "*/*" }),
|
|
51
|
+
body: form
|
|
52
|
+
});
|
|
53
|
+
return readBody(response, "上传安装包");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 用上传得到的 sources 创建自定义插件版本。
|
|
58
|
+
* @param {{ pluginId: number, model: string, modelPluginPattern: number, changeLog: string, packType: number, productId: string }} options
|
|
59
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
60
|
+
* @param {string} sources
|
|
61
|
+
*/
|
|
62
|
+
async function createVersion(options, auth, sources) {
|
|
63
|
+
const { payload } = await requestJson("提交插件版本", `${API_ROOT}/productcenter/rnPlugin/customize/create`, auth, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
productId: options.productId,
|
|
66
|
+
body: {
|
|
67
|
+
pluginId: options.pluginId,
|
|
68
|
+
model: options.model,
|
|
69
|
+
modelPluginPattern: options.modelPluginPattern,
|
|
70
|
+
sources,
|
|
71
|
+
changeLog: options.changeLog,
|
|
72
|
+
packType: options.packType
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return payload;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = {
|
|
79
|
+
pickSources,
|
|
80
|
+
uploadPackage,
|
|
81
|
+
createVersion
|
|
82
|
+
};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 按项目型号定位企业组、产品和扩展程序。
|
|
3
|
+
* 这一步会调用切换企业接口,但不会上传安装包。
|
|
4
|
+
*/
|
|
5
|
+
const { API_ROOT, POST_API_ROOT } = require("../const");
|
|
6
|
+
const { upsertCookie } = require("./auth");
|
|
7
|
+
const { requestJson } = require("./client");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* model 第一段是企业组 shortName。carrie.airc.xiaoz1 对应 shortName=carrie。
|
|
11
|
+
* @param {string} model
|
|
12
|
+
*/
|
|
13
|
+
function vendorKey(model) {
|
|
14
|
+
const vendor = String(model).split(".")[0];
|
|
15
|
+
if (!vendor) throw new Error(`无法从 model 解析企业组:${model}`);
|
|
16
|
+
return vendor;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 拉取当前账号加入的企业组。这个接口只校验登录态,不需要 Key_Org_Id。
|
|
21
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
22
|
+
*/
|
|
23
|
+
async function listGroups(auth) {
|
|
24
|
+
const { payload } = await requestJson("获取企业组", `${API_ROOT}/managercenter/group/userGroupList`, auth);
|
|
25
|
+
const groups = Array.isArray(payload.result) ? payload.result : [];
|
|
26
|
+
if (groups.length === 0) throw new Error("企业组列表为空");
|
|
27
|
+
return groups;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 用 model 厂商前缀定位企业组,再调用后台的切换企业接口。
|
|
32
|
+
* 传入 groupId 时按企业组 ID 精确匹配。
|
|
33
|
+
* @param {{ model: string, groupId: number | null }} options
|
|
34
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
35
|
+
*/
|
|
36
|
+
async function switchGroup(options, auth) {
|
|
37
|
+
const groups = await listGroups(auth);
|
|
38
|
+
const vendor = vendorKey(options.model);
|
|
39
|
+
const matched = options.groupId
|
|
40
|
+
? groups.filter((group) => Number(group.groupId) === options.groupId)
|
|
41
|
+
: groups.filter((group) => group.shortName === vendor);
|
|
42
|
+
if (matched.length !== 1) {
|
|
43
|
+
const hint = matched.map((group) => `${group.groupId}:${group.shortName}`).join(", ");
|
|
44
|
+
throw new Error(
|
|
45
|
+
matched.length === 0
|
|
46
|
+
? `没有 shortName=${vendor} 的企业组,可用 --group-id 指定`
|
|
47
|
+
: `匹配到多个企业组(${hint}),请用 --group-id 指定`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const group = matched[0];
|
|
51
|
+
await requestJson("切换企业组", `${POST_API_ROOT}/personCenter/setCurrEnterprise`, auth, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
body: {
|
|
54
|
+
groupId: group.groupId,
|
|
55
|
+
shortName: group.shortName,
|
|
56
|
+
compName: group.compName
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
// 后续产品查询沿用同一 serviceToken,Cookie 里的当前企业改成刚切换的组。
|
|
60
|
+
auth.cookie = upsertCookie(auth.cookie, "Key_Org_Id", group.groupId);
|
|
61
|
+
auth.cookie = upsertCookie(auth.cookie, "Key_Obj_Key", options.model);
|
|
62
|
+
return group;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 产品列表里的 shortName 就是 model。只接受完全相同的一条。
|
|
67
|
+
* 已传入 productId 时不再请求产品列表。
|
|
68
|
+
* @param {{ model: string, productId: string }} options
|
|
69
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
70
|
+
*/
|
|
71
|
+
async function findProduct(options, auth) {
|
|
72
|
+
if (options.productId) {
|
|
73
|
+
return { pdId: options.productId, name: "", shortName: options.model };
|
|
74
|
+
}
|
|
75
|
+
const { payload } = await requestJson("按型号查询产品", `${API_ROOT}/product/list/get`, auth, {
|
|
76
|
+
params: {
|
|
77
|
+
searchWords: options.model,
|
|
78
|
+
region: -1,
|
|
79
|
+
productTypeId: -1,
|
|
80
|
+
connectType: -1
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
const products = (Array.isArray(payload.result) ? payload.result : []).filter((item) => item.shortName === options.model);
|
|
84
|
+
if (products.length !== 1) {
|
|
85
|
+
throw new Error(`型号 ${options.model} 匹配到 ${products.length} 个产品`);
|
|
86
|
+
}
|
|
87
|
+
return products[0];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 用扩展程序关联关系里的 model 定位插件,不核对名称和 productId。
|
|
92
|
+
* 已传入 pluginId 时不再请求扩展程序列表。
|
|
93
|
+
* @param {{ model: string, pluginId: number | null }} options
|
|
94
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
95
|
+
* @param {string | number} productId
|
|
96
|
+
*/
|
|
97
|
+
async function findPlugin(options, auth, productId) {
|
|
98
|
+
if (options.pluginId) return { pluginId: options.pluginId, name: "" };
|
|
99
|
+
const { payload } = await requestJson("获取扩展程序列表", `${API_ROOT}/plugin/list/get`, auth, {
|
|
100
|
+
productId
|
|
101
|
+
});
|
|
102
|
+
const plugins = Array.isArray(payload.result) ? payload.result : [];
|
|
103
|
+
/** @type {{ pluginId: number, name: string }[]} */
|
|
104
|
+
const confirmed = [];
|
|
105
|
+
for (const plugin of plugins) {
|
|
106
|
+
const { payload: relationPayload } = await requestJson("核对扩展程序关联", `${API_ROOT}/plugin/relation/list/get`, auth, {
|
|
107
|
+
productId,
|
|
108
|
+
params: { pluginId: plugin.pluginId }
|
|
109
|
+
});
|
|
110
|
+
const relations = Array.isArray(relationPayload.result) ? relationPayload.result : [];
|
|
111
|
+
// 只认关联上的型号,产品 ID 以产品查询结果为准。
|
|
112
|
+
const linked = relations.some((item) => item.model === options.model);
|
|
113
|
+
if (linked) confirmed.push(plugin);
|
|
114
|
+
}
|
|
115
|
+
if (confirmed.length !== 1) {
|
|
116
|
+
throw new Error(`型号 ${options.model} 匹配到 ${confirmed.length} 个扩展程序,请用 --plugin-id 指定`);
|
|
117
|
+
}
|
|
118
|
+
return confirmed[0];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
module.exports = {
|
|
122
|
+
switchGroup,
|
|
123
|
+
findProduct,
|
|
124
|
+
findPlugin
|
|
125
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 版本提交成功后等待打包,再开启测试。
|
|
3
|
+
* 平台包状态:1 打包中,2 打包失败,3 开发中,4 测试中,10 开启测试中,11 安全扫描失败。
|
|
4
|
+
* 开启测试只在状态变为「开发中」后调用。测试范围用全部成员(type=2)。
|
|
5
|
+
*/
|
|
6
|
+
const { API_ROOT } = require("../const");
|
|
7
|
+
const { requestJson } = require("./client");
|
|
8
|
+
|
|
9
|
+
const PACKAGE_STATUS = {
|
|
10
|
+
PACKING: 1,
|
|
11
|
+
FAILED: 2,
|
|
12
|
+
DEVELOPING: 3,
|
|
13
|
+
TESTING: 4,
|
|
14
|
+
STARTING_TEST: 10,
|
|
15
|
+
SECURE_SCAN_FAILED: 11
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** 全部成员。指定企业成员是 1。 */
|
|
19
|
+
const TEST_TYPE_ALL_USERS = 2;
|
|
20
|
+
|
|
21
|
+
/** 满等待时间后若仍在打包,继续查询的最长时间。 */
|
|
22
|
+
const EXTRA_WAIT_MS = 10 * 60 * 1000;
|
|
23
|
+
/** 继续查询时的间隔。 */
|
|
24
|
+
const POLL_MS = 30 * 1000;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {number} ms
|
|
28
|
+
*/
|
|
29
|
+
function sleep(ms) {
|
|
30
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 按分钟打印剩余时间,避免长时间等待时看起来像卡住。
|
|
35
|
+
* @param {number} ms
|
|
36
|
+
* @param {string} label
|
|
37
|
+
*/
|
|
38
|
+
async function sleepWithCountdown(ms, label) {
|
|
39
|
+
const end = Date.now() + ms;
|
|
40
|
+
while (Date.now() < end) {
|
|
41
|
+
const left = end - Date.now();
|
|
42
|
+
console.log(`${label}还有 ${Math.max(1, Math.ceil(left / 60000))} 分钟`);
|
|
43
|
+
await sleep(Math.min(left, 60 * 1000));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
49
|
+
* @param {number} pluginId
|
|
50
|
+
* @param {string} productId
|
|
51
|
+
*/
|
|
52
|
+
async function listPackages(auth, pluginId, productId) {
|
|
53
|
+
const { payload } = await requestJson("获取插件版本列表", `${API_ROOT}/productcenter/rnPlugin/package/list/get`, auth, {
|
|
54
|
+
productId,
|
|
55
|
+
params: { pluginId }
|
|
56
|
+
});
|
|
57
|
+
return Array.isArray(payload.result) ? payload.result : [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 用更新说明和提交时间定位刚创建的版本。
|
|
62
|
+
* @param {Array<Record<string, unknown>>} packages
|
|
63
|
+
* @param {{ changeLog: string, submittedAt: number }} marker
|
|
64
|
+
*/
|
|
65
|
+
function pickUploadedPackage(packages, marker) {
|
|
66
|
+
return packages
|
|
67
|
+
.filter((item) => item.changeLog === marker.changeLog && Number(item.createTime) >= marker.submittedAt - 2 * 60 * 1000)
|
|
68
|
+
.sort((a, b) => Number(b.createTime) - Number(a.createTime))[0];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {number} status
|
|
73
|
+
*/
|
|
74
|
+
function statusText(status) {
|
|
75
|
+
const names = {
|
|
76
|
+
1: "打包中",
|
|
77
|
+
2: "打包失败",
|
|
78
|
+
3: "开发中",
|
|
79
|
+
4: "测试中",
|
|
80
|
+
10: "开启测试中",
|
|
81
|
+
11: "安全扫描失败"
|
|
82
|
+
};
|
|
83
|
+
return names[status] || `状态 ${status}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 调用扩展程序版本开始测试。参数与页面弹窗提交一致。
|
|
88
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
89
|
+
* @param {{ pluginId: number, version: number, status: number }} pkg
|
|
90
|
+
* @param {{ model: string, productId: string }} options
|
|
91
|
+
*/
|
|
92
|
+
async function startPackageTest(auth, pkg, options) {
|
|
93
|
+
const { payload } = await requestJson("开启测试", `${API_ROOT}/plugin/package/status/update`, auth, {
|
|
94
|
+
productId: options.productId,
|
|
95
|
+
params: {
|
|
96
|
+
pluginId: pkg.pluginId,
|
|
97
|
+
version: pkg.version,
|
|
98
|
+
model: options.model,
|
|
99
|
+
status: pkg.status,
|
|
100
|
+
type: TEST_TYPE_ALL_USERS
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return payload;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 按版本号取最新一条。版本号相同再用创建时间。
|
|
108
|
+
* @param {Array<Record<string, unknown>>} packages
|
|
109
|
+
*/
|
|
110
|
+
function pickLatestPackage(packages) {
|
|
111
|
+
return [...packages].sort((a, b) => {
|
|
112
|
+
const versionDiff = Number(b.version) - Number(a.version);
|
|
113
|
+
if (versionDiff !== 0) return versionDiff;
|
|
114
|
+
return Number(b.createTime) - Number(a.createTime);
|
|
115
|
+
})[0];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 开发中就开启测试。已经在测试流程里则直接返回。打包中交给调用方继续等。
|
|
120
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
121
|
+
* @param {Record<string, unknown>} pkg
|
|
122
|
+
* @param {{ model: string, productId: string }} options
|
|
123
|
+
* @returns {Promise<{ done: boolean, result?: unknown }>}
|
|
124
|
+
*/
|
|
125
|
+
async function actOnPackage(auth, pkg, options) {
|
|
126
|
+
const status = Number(pkg.status);
|
|
127
|
+
console.log(`版本 ${pkg.version} 当前是${statusText(status)}`);
|
|
128
|
+
if (status === PACKAGE_STATUS.DEVELOPING) {
|
|
129
|
+
const started = await startPackageTest(auth, pkg, options);
|
|
130
|
+
console.log(`已开启测试,版本 ${pkg.version},测试范围为全部成员`);
|
|
131
|
+
return { done: true, result: started };
|
|
132
|
+
}
|
|
133
|
+
if (status === PACKAGE_STATUS.TESTING || status === PACKAGE_STATUS.STARTING_TEST) {
|
|
134
|
+
console.log(`版本 ${pkg.version} 已在测试流程中`);
|
|
135
|
+
return { done: true, result: pkg };
|
|
136
|
+
}
|
|
137
|
+
if (status === PACKAGE_STATUS.FAILED || status === PACKAGE_STATUS.SECURE_SCAN_FAILED) {
|
|
138
|
+
throw new Error(`版本 ${pkg.version}${statusText(status)},不能开启测试`);
|
|
139
|
+
}
|
|
140
|
+
return { done: false };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 不上传,直接为指定型号的最新版本开启测试。
|
|
145
|
+
* 最新版本还在打包时,每 30 秒查一次,最多再等 10 分钟。
|
|
146
|
+
* @param {{ pluginId: number, model: string, productId: string }} options
|
|
147
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
148
|
+
*/
|
|
149
|
+
async function startTestForModel(options, auth) {
|
|
150
|
+
const deadline = Date.now() + EXTRA_WAIT_MS;
|
|
151
|
+
console.log(`按型号 ${options.model} 查找最新版本并开启测试`);
|
|
152
|
+
while (true) {
|
|
153
|
+
const packages = await listPackages(auth, options.pluginId, options.productId);
|
|
154
|
+
const pkg = pickLatestPackage(packages);
|
|
155
|
+
if (!pkg) {
|
|
156
|
+
throw new Error(`型号 ${options.model} 没有可开启测试的版本`);
|
|
157
|
+
}
|
|
158
|
+
const acted = await actOnPackage(auth, pkg, options);
|
|
159
|
+
if (acted.done) return acted.result;
|
|
160
|
+
if (Date.now() >= deadline) {
|
|
161
|
+
throw new Error(`版本 ${pkg.version} 等待后仍是${statusText(Number(pkg.status))},未开启测试`);
|
|
162
|
+
}
|
|
163
|
+
console.log("打包尚未完成,30 秒后再次查询");
|
|
164
|
+
await sleep(POLL_MS);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 先等待指定分钟,再查询版本列表并开启测试。
|
|
170
|
+
* @param {{ pluginId: number, model: string, productId: string, changeLog: string, testDelayMinutes: number }} options
|
|
171
|
+
* @param {{ userId: string, xiaomiiotPh: string, cookie: string }} auth
|
|
172
|
+
* @param {number} submittedAt
|
|
173
|
+
*/
|
|
174
|
+
async function startTestAfterUpload(options, auth, submittedAt) {
|
|
175
|
+
const delayMs = options.testDelayMinutes * 60 * 1000;
|
|
176
|
+
console.log(`版本已提交,${options.testDelayMinutes} 分钟后开启测试`);
|
|
177
|
+
await sleepWithCountdown(delayMs, "距离开启测试");
|
|
178
|
+
|
|
179
|
+
const deadline = Date.now() + EXTRA_WAIT_MS;
|
|
180
|
+
while (true) {
|
|
181
|
+
const packages = await listPackages(auth, options.pluginId, options.productId);
|
|
182
|
+
const pkg = pickUploadedPackage(packages, { changeLog: options.changeLog, submittedAt });
|
|
183
|
+
if (!pkg) {
|
|
184
|
+
throw new Error(`没有找到更新说明为「${options.changeLog}」的新版本,无法开启测试`);
|
|
185
|
+
}
|
|
186
|
+
const acted = await actOnPackage(auth, pkg, options);
|
|
187
|
+
if (acted.done) return acted.result;
|
|
188
|
+
if (Date.now() >= deadline) {
|
|
189
|
+
throw new Error(`版本 ${pkg.version} 等待后仍是${statusText(Number(pkg.status))},未开启测试`);
|
|
190
|
+
}
|
|
191
|
+
console.log("打包尚未完成,30 秒后再次查询");
|
|
192
|
+
await sleep(POLL_MS);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = {
|
|
197
|
+
startTestAfterUpload,
|
|
198
|
+
startTestForModel
|
|
199
|
+
};
|