@app-public/weaverbird-debug 1.0.0 → 1.0.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 +8 -3
- package/index.js +72 -31
- package/npm-packages.js +21 -0
- package/package.json +1 -1
- package/token.js +50 -0
- package/update.js +44 -0
package/README.md
CHANGED
|
@@ -34,13 +34,17 @@ npm install -g @app-public/weaverbird-test # 测试环境
|
|
|
34
34
|
## CLI 命令
|
|
35
35
|
|
|
36
36
|
```bash
|
|
37
|
-
weaverbird
|
|
37
|
+
weaverbird --version # 显示 CLI 版本号(顶层)
|
|
38
|
+
weaverbird --help # 显示顶层帮助
|
|
39
|
+
weaverbird cli login # 浏览器 OAuth 登录
|
|
38
40
|
weaverbird-debug cli login # 本地调试
|
|
39
41
|
weaverbird-test cli login # 测试环境
|
|
40
42
|
weaverbird cli logout # 清除当前环境本地 token
|
|
41
43
|
weaverbird cli status # 查询登录状态及 token 信息
|
|
42
|
-
weaverbird cli --
|
|
43
|
-
weaverbird
|
|
44
|
+
weaverbird cli token set --type figma <token> # 设置 Figma token(本地 + 服务端)
|
|
45
|
+
weaverbird update # 更新当前环境 CLI 到最新版本
|
|
46
|
+
weaverbird cli update # 同上(子命令形式)
|
|
47
|
+
weaverbird cli --help # 显示子命令帮助
|
|
44
48
|
```
|
|
45
49
|
|
|
46
50
|
### login 流程
|
|
@@ -258,6 +262,7 @@ CLI 依赖服务端 OAuth 接口(均为 POST JSON,响应格式与项目统
|
|
|
258
262
|
| `/v1/api/ci/oauth/getAuthorizeSession` | Web 查询授权会话状态(pending/expired/invalid) |
|
|
259
263
|
| `/v1/api/ci/oauth/authorize` | 签发 authorization code(需 session_token + header `token`) |
|
|
260
264
|
| `/v1/api/ci/oauth/token` | 换取/刷新 access_token |
|
|
265
|
+
| `/v1/api/ci/open/setToken` | CLI 设置第三方 token(如 figma),需 `cli_token` header |
|
|
261
266
|
|
|
262
267
|
授权页由前端 `{WEAVERBIRD_URL}/weaverbird/oauth/authorize?session=...` 实现;会话 **5 分钟过期、一次性使用**,过期或已用后 Web 会拦截并提示重新执行 `weaverbird cli login`。
|
|
263
268
|
|
package/index.js
CHANGED
|
@@ -6,6 +6,8 @@ const { startLogin } = require('./login');
|
|
|
6
6
|
const { getToken, clearToken, getTokenPath } = require('./tokenStore');
|
|
7
7
|
const { CI_BASE_URL, ENV } = require('./config');
|
|
8
8
|
const { resolveScriptName } = require('./cli-env');
|
|
9
|
+
const { runUpdate } = require('./update');
|
|
10
|
+
const { setServiceToken } = require('./token');
|
|
9
11
|
const pkg = require('./package.json');
|
|
10
12
|
|
|
11
13
|
const scriptName = resolveScriptName();
|
|
@@ -22,46 +24,85 @@ function printStatus() {
|
|
|
22
24
|
console.log(JSON.stringify(result, null, 2));
|
|
23
25
|
}
|
|
24
26
|
|
|
27
|
+
function registerTokenCommands(yargs) {
|
|
28
|
+
return yargs
|
|
29
|
+
.command(
|
|
30
|
+
'set <token>',
|
|
31
|
+
'设置第三方 token 并同步到服务端',
|
|
32
|
+
(yargs) =>
|
|
33
|
+
yargs.option('type', {
|
|
34
|
+
alias: 't',
|
|
35
|
+
type: 'string',
|
|
36
|
+
choices: ['figma'],
|
|
37
|
+
demandOption: true,
|
|
38
|
+
description: 'token 类型,当前支持 figma',
|
|
39
|
+
}),
|
|
40
|
+
async (argv) => {
|
|
41
|
+
await setServiceToken(argv.type, argv.token);
|
|
42
|
+
}
|
|
43
|
+
)
|
|
44
|
+
.demandCommand(1, '请使用 token set --type <type> <token>')
|
|
45
|
+
.help();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function registerCliSubcommands(yargs) {
|
|
49
|
+
return yargs
|
|
50
|
+
.command(
|
|
51
|
+
'login',
|
|
52
|
+
`浏览器 OAuth 登录,token 保存至 ~/.weaverbird/token.${ENV}.json`,
|
|
53
|
+
() => {},
|
|
54
|
+
async () => {
|
|
55
|
+
await startLogin();
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
.command(
|
|
59
|
+
'logout',
|
|
60
|
+
'清除本地 token',
|
|
61
|
+
() => {},
|
|
62
|
+
() => {
|
|
63
|
+
clearToken();
|
|
64
|
+
console.log('✅ 已退出登录');
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
.command(
|
|
68
|
+
'status',
|
|
69
|
+
'查询登录状态及 token 信息',
|
|
70
|
+
() => {},
|
|
71
|
+
() => {
|
|
72
|
+
printStatus();
|
|
73
|
+
}
|
|
74
|
+
)
|
|
75
|
+
.command(
|
|
76
|
+
'update',
|
|
77
|
+
'从 npm 更新当前环境 CLI 到最新版本',
|
|
78
|
+
() => {},
|
|
79
|
+
() => {
|
|
80
|
+
runUpdate();
|
|
81
|
+
}
|
|
82
|
+
)
|
|
83
|
+
.command('token', '管理第三方 token', (yargs) => registerTokenCommands(yargs));
|
|
84
|
+
}
|
|
85
|
+
|
|
25
86
|
async function main() {
|
|
26
87
|
await yargs(hideBin(process.argv))
|
|
27
88
|
.scriptName(scriptName)
|
|
28
89
|
.usage('$0 <command> [options]')
|
|
29
|
-
.version(
|
|
90
|
+
.version(pkg.version)
|
|
91
|
+
.command(
|
|
92
|
+
'update',
|
|
93
|
+
'从 npm 更新当前环境 CLI 到最新版本',
|
|
94
|
+
() => {},
|
|
95
|
+
() => {
|
|
96
|
+
runUpdate();
|
|
97
|
+
}
|
|
98
|
+
)
|
|
30
99
|
.command(
|
|
31
100
|
'cli',
|
|
32
101
|
'Weaverbird CLI 子命令',
|
|
33
102
|
(yargs) =>
|
|
34
|
-
yargs
|
|
35
|
-
.version(pkg.version)
|
|
36
|
-
.command(
|
|
37
|
-
'login',
|
|
38
|
-
`浏览器 OAuth 登录,token 保存至 ~/.weaverbird/token.${ENV}.json`,
|
|
39
|
-
() => {},
|
|
40
|
-
async () => {
|
|
41
|
-
await startLogin();
|
|
42
|
-
}
|
|
43
|
-
)
|
|
44
|
-
.command(
|
|
45
|
-
'logout',
|
|
46
|
-
'清除本地 token',
|
|
47
|
-
() => {},
|
|
48
|
-
() => {
|
|
49
|
-
clearToken();
|
|
50
|
-
console.log('✅ 已退出登录');
|
|
51
|
-
}
|
|
52
|
-
)
|
|
53
|
-
.command(
|
|
54
|
-
'status',
|
|
55
|
-
'查询登录状态及 token 信息',
|
|
56
|
-
() => {},
|
|
57
|
-
() => {
|
|
58
|
-
printStatus();
|
|
59
|
-
}
|
|
60
|
-
)
|
|
61
|
-
.demandCommand(0)
|
|
62
|
-
.help()
|
|
103
|
+
registerCliSubcommands(yargs.version(pkg.version)).demandCommand(0).help()
|
|
63
104
|
)
|
|
64
|
-
.demandCommand(1, `请使用 ${scriptName}
|
|
105
|
+
.demandCommand(1, `请使用 ${scriptName} <command>,执行 ${scriptName} --help 查看帮助`)
|
|
65
106
|
.help()
|
|
66
107
|
.parseAsync();
|
|
67
108
|
}
|
package/npm-packages.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const NPM_SCOPE = '@app-public';
|
|
2
|
+
|
|
3
|
+
const ENV_NPM_PACKAGES = {
|
|
4
|
+
release: `${NPM_SCOPE}/weaverbird`,
|
|
5
|
+
debug: `${NPM_SCOPE}/weaverbird-debug`,
|
|
6
|
+
test: `${NPM_SCOPE}/weaverbird-test`,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
function getNpmPackageName(env) {
|
|
10
|
+
const name = ENV_NPM_PACKAGES[env];
|
|
11
|
+
if (!name) {
|
|
12
|
+
throw new Error(`未知环境 "${env}",无法确定 npm 包名`);
|
|
13
|
+
}
|
|
14
|
+
return name;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
NPM_SCOPE,
|
|
19
|
+
ENV_NPM_PACKAGES,
|
|
20
|
+
getNpmPackageName,
|
|
21
|
+
};
|
package/package.json
CHANGED
package/token.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { getToken, saveToken } = require('./tokenStore');
|
|
2
|
+
const { request } = require('./api');
|
|
3
|
+
|
|
4
|
+
const SET_TOKEN_PATH = '/v1/api/ci/open/setToken';
|
|
5
|
+
|
|
6
|
+
const SUPPORTED_TYPES = {
|
|
7
|
+
figma: {
|
|
8
|
+
localKey: 'figma_token',
|
|
9
|
+
label: 'Figma',
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
async function setServiceToken(type, tokenValue) {
|
|
14
|
+
const config = SUPPORTED_TYPES[type];
|
|
15
|
+
if (!config) {
|
|
16
|
+
throw new Error(`不支持的 type: ${type},当前支持: ${Object.keys(SUPPORTED_TYPES).join(', ')}`);
|
|
17
|
+
}
|
|
18
|
+
if (!tokenValue || !String(tokenValue).trim()) {
|
|
19
|
+
throw new Error('token 不能为空');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const stored = getToken();
|
|
23
|
+
if (!stored?.access_token) {
|
|
24
|
+
throw new Error('未登录,请先执行 weaverbird cli login');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const value = String(tokenValue).trim();
|
|
28
|
+
saveToken({
|
|
29
|
+
...stored,
|
|
30
|
+
[config.localKey]: value,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const result = await request(SET_TOKEN_PATH, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
type,
|
|
37
|
+
token: value,
|
|
38
|
+
}),
|
|
39
|
+
});
|
|
40
|
+
if (result.code !== 200) {
|
|
41
|
+
throw new Error(result.msg || '同步 token 到服务端失败');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
console.log(`✅ ${config.label} token 已保存到本地并同步至服务端`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = {
|
|
48
|
+
SUPPORTED_TYPES,
|
|
49
|
+
setServiceToken,
|
|
50
|
+
};
|
package/update.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const { spawnSync } = require('child_process');
|
|
2
|
+
const { ENV } = require('./config');
|
|
3
|
+
const { getNpmPackageName } = require('./npm-packages');
|
|
4
|
+
const pkg = require('./package.json');
|
|
5
|
+
|
|
6
|
+
function queryLatestVersion(packageName) {
|
|
7
|
+
const result = spawnSync('npm', ['view', packageName, 'version'], {
|
|
8
|
+
encoding: 'utf8',
|
|
9
|
+
shell: true,
|
|
10
|
+
});
|
|
11
|
+
if (result.status !== 0) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return result.stdout.trim() || null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function runUpdate() {
|
|
18
|
+
const packageName = getNpmPackageName(ENV);
|
|
19
|
+
const latest = queryLatestVersion(packageName);
|
|
20
|
+
|
|
21
|
+
console.log(`📦 包名: ${packageName}`);
|
|
22
|
+
console.log(`📌 当前版本: ${pkg.version}`);
|
|
23
|
+
if (latest) {
|
|
24
|
+
console.log(`🌐 最新版本: ${latest}`);
|
|
25
|
+
if (latest === pkg.version) {
|
|
26
|
+
console.log('✅ 已是最新版本');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
console.log(`⬇️ 正在更新 ${packageName}@latest ...`);
|
|
32
|
+
const result = spawnSync('npm', ['install', '-g', `${packageName}@latest`], {
|
|
33
|
+
stdio: 'inherit',
|
|
34
|
+
shell: true,
|
|
35
|
+
});
|
|
36
|
+
if (result.status !== 0) {
|
|
37
|
+
throw new Error('更新失败,请检查网络或 npm 登录状态');
|
|
38
|
+
}
|
|
39
|
+
console.log('✅ 更新完成');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
runUpdate,
|
|
44
|
+
};
|