@app-public/weaverbird-debug 1.0.1 → 1.0.3
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 +10 -1
- package/api.js +2 -2
- package/index.js +11 -2
- package/login.js +10 -2
- package/package.json +1 -1
- package/token.js +6 -1
- package/update.js +6 -4
- package/user.js +47 -0
package/README.md
CHANGED
|
@@ -65,11 +65,18 @@ weaverbird cli --help # 显示子命令帮助
|
|
|
65
65
|
"env": "debug",
|
|
66
66
|
"ci_base_url": "http://10.25.72.141:16644",
|
|
67
67
|
"token_path": "C:\\Users\\xxx\\.weaverbird\\token.debug.json",
|
|
68
|
+
"user_id": "abc123",
|
|
69
|
+
"user_name": "张三",
|
|
68
70
|
"token": {
|
|
69
71
|
"access_token": "eyJxxx",
|
|
70
72
|
"token_type": "Bearer",
|
|
71
73
|
"expires_in": 7200,
|
|
72
|
-
"refresh_token": "xxx"
|
|
74
|
+
"refresh_token": "xxx",
|
|
75
|
+
"user": {
|
|
76
|
+
"id": "abc123",
|
|
77
|
+
"name": "zhangsan",
|
|
78
|
+
"realname": "张三"
|
|
79
|
+
}
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
82
|
```
|
|
@@ -82,6 +89,8 @@ weaverbird cli --help # 显示子命令帮助
|
|
|
82
89
|
"env": "debug",
|
|
83
90
|
"ci_base_url": "http://10.25.72.141:16644",
|
|
84
91
|
"token_path": "C:\\Users\\xxx\\.weaverbird\\token.debug.json",
|
|
92
|
+
"user_id": null,
|
|
93
|
+
"user_name": null,
|
|
85
94
|
"token": null
|
|
86
95
|
}
|
|
87
96
|
```
|
package/api.js
CHANGED
|
@@ -20,7 +20,7 @@ async function refreshAccessToken(refreshToken) {
|
|
|
20
20
|
if (result.code !== 200 || !result.data?.access_token) {
|
|
21
21
|
throw new Error(result.msg || '刷新 token 失败');
|
|
22
22
|
}
|
|
23
|
-
saveToken(result.data);
|
|
23
|
+
saveToken({ ...getToken(), ...result.data });
|
|
24
24
|
return result.data;
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -38,9 +38,9 @@ async function request(path, options = {}) {
|
|
|
38
38
|
const response = await fetch(url, {
|
|
39
39
|
...options,
|
|
40
40
|
headers: {
|
|
41
|
-
cli_token: accessToken,
|
|
42
41
|
'Content-Type': 'application/json',
|
|
43
42
|
...(options.headers || {}),
|
|
43
|
+
cli_token: accessToken,
|
|
44
44
|
},
|
|
45
45
|
});
|
|
46
46
|
const data = await response.json().catch(() => ({}));
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const yargs = require('yargs/yargs');
|
|
|
4
4
|
const { hideBin } = require('yargs/helpers');
|
|
5
5
|
const { startLogin } = require('./login');
|
|
6
6
|
const { getToken, clearToken, getTokenPath } = require('./tokenStore');
|
|
7
|
+
const { getDisplayName } = require('./user');
|
|
7
8
|
const { CI_BASE_URL, ENV } = require('./config');
|
|
8
9
|
const { resolveScriptName } = require('./cli-env');
|
|
9
10
|
const { runUpdate } = require('./update');
|
|
@@ -11,14 +12,18 @@ const { setServiceToken } = require('./token');
|
|
|
11
12
|
const pkg = require('./package.json');
|
|
12
13
|
|
|
13
14
|
const scriptName = resolveScriptName();
|
|
15
|
+
const UPDATE_ALIASES = ['update', 'udpate'];
|
|
14
16
|
|
|
15
17
|
function printStatus() {
|
|
16
18
|
const token = getToken();
|
|
19
|
+
const user = token?.user;
|
|
17
20
|
const result = {
|
|
18
21
|
logged_in: !!(token && token.access_token),
|
|
19
22
|
env: ENV,
|
|
20
23
|
ci_base_url: CI_BASE_URL,
|
|
21
24
|
token_path: getTokenPath(),
|
|
25
|
+
user_id: user?.id || null,
|
|
26
|
+
user_name: getDisplayName(user),
|
|
22
27
|
token: token || null,
|
|
23
28
|
};
|
|
24
29
|
console.log(JSON.stringify(result, null, 2));
|
|
@@ -47,6 +52,8 @@ function registerTokenCommands(yargs) {
|
|
|
47
52
|
|
|
48
53
|
function registerCliSubcommands(yargs) {
|
|
49
54
|
return yargs
|
|
55
|
+
.strictCommands()
|
|
56
|
+
.recommendCommands()
|
|
50
57
|
.command(
|
|
51
58
|
'login',
|
|
52
59
|
`浏览器 OAuth 登录,token 保存至 ~/.weaverbird/token.${ENV}.json`,
|
|
@@ -73,7 +80,7 @@ function registerCliSubcommands(yargs) {
|
|
|
73
80
|
}
|
|
74
81
|
)
|
|
75
82
|
.command(
|
|
76
|
-
|
|
83
|
+
UPDATE_ALIASES,
|
|
77
84
|
'从 npm 更新当前环境 CLI 到最新版本',
|
|
78
85
|
() => {},
|
|
79
86
|
() => {
|
|
@@ -88,8 +95,10 @@ async function main() {
|
|
|
88
95
|
.scriptName(scriptName)
|
|
89
96
|
.usage('$0 <command> [options]')
|
|
90
97
|
.version(pkg.version)
|
|
98
|
+
.strictCommands()
|
|
99
|
+
.recommendCommands()
|
|
91
100
|
.command(
|
|
92
|
-
|
|
101
|
+
UPDATE_ALIASES,
|
|
93
102
|
'从 npm 更新当前环境 CLI 到最新版本',
|
|
94
103
|
() => {},
|
|
95
104
|
() => {
|
package/login.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const express = require('express');
|
|
2
2
|
const open = require('open');
|
|
3
3
|
const fetch = require('cross-fetch');
|
|
4
|
-
const {saveToken} = require('./tokenStore');
|
|
4
|
+
const { saveToken } = require('./tokenStore');
|
|
5
|
+
const { fetchAndSaveCurrentUser, getDisplayName } = require('./user');
|
|
5
6
|
const {
|
|
6
7
|
CLIENT_ID,
|
|
7
8
|
CLIENT_SECRET,
|
|
@@ -219,13 +220,20 @@ async function startLogin() {
|
|
|
219
220
|
try {
|
|
220
221
|
const token = await exchangeCodeForToken(code);
|
|
221
222
|
saveToken(token);
|
|
223
|
+
let userName = null;
|
|
224
|
+
try {
|
|
225
|
+
const user = await fetchAndSaveCurrentUser();
|
|
226
|
+
userName = getDisplayName(user);
|
|
227
|
+
} catch (userErr) {
|
|
228
|
+
console.warn(`⚠️ 获取用户信息失败:${userErr.message}`);
|
|
229
|
+
}
|
|
222
230
|
res.send(renderCallbackPage({
|
|
223
231
|
type: 'success',
|
|
224
232
|
title: '登录成功',
|
|
225
233
|
message: 'CLI 已完成授权,token 已保存到本地。',
|
|
226
234
|
hint: '可以关闭此页面',
|
|
227
235
|
}));
|
|
228
|
-
console.log('✅ 登录成功');
|
|
236
|
+
console.log(userName ? `✅ 登录成功:${userName}` : '✅ 登录成功');
|
|
229
237
|
resolve(token);
|
|
230
238
|
} catch (err) {
|
|
231
239
|
res.status(500).send(renderCallbackPage({
|
package/package.json
CHANGED
package/token.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { getToken, saveToken } = require('./tokenStore');
|
|
2
|
-
const { request } = require('./api');
|
|
2
|
+
const { request, getValidAccessToken } = require('./api');
|
|
3
3
|
|
|
4
4
|
const SET_TOKEN_PATH = '/v1/api/ci/open/setToken';
|
|
5
5
|
|
|
@@ -25,6 +25,7 @@ async function setServiceToken(type, tokenValue) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
const value = String(tokenValue).trim();
|
|
28
|
+
const cliToken = await getValidAccessToken();
|
|
28
29
|
saveToken({
|
|
29
30
|
...stored,
|
|
30
31
|
[config.localKey]: value,
|
|
@@ -32,6 +33,10 @@ async function setServiceToken(type, tokenValue) {
|
|
|
32
33
|
|
|
33
34
|
const result = await request(SET_TOKEN_PATH, {
|
|
34
35
|
method: 'POST',
|
|
36
|
+
headers: {
|
|
37
|
+
'Content-Type': 'application/json',
|
|
38
|
+
'cli_token': cliToken,
|
|
39
|
+
},
|
|
35
40
|
body: JSON.stringify({
|
|
36
41
|
type,
|
|
37
42
|
token: value,
|
package/update.js
CHANGED
|
@@ -3,10 +3,13 @@ const { ENV } = require('./config');
|
|
|
3
3
|
const { getNpmPackageName } = require('./npm-packages');
|
|
4
4
|
const pkg = require('./package.json');
|
|
5
5
|
|
|
6
|
+
function getNpmCommand() {
|
|
7
|
+
return process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
function queryLatestVersion(packageName) {
|
|
7
|
-
const result = spawnSync(
|
|
11
|
+
const result = spawnSync(getNpmCommand(), ['view', packageName, 'version'], {
|
|
8
12
|
encoding: 'utf8',
|
|
9
|
-
shell: true,
|
|
10
13
|
});
|
|
11
14
|
if (result.status !== 0) {
|
|
12
15
|
return null;
|
|
@@ -29,9 +32,8 @@ function runUpdate() {
|
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
console.log(`⬇️ 正在更新 ${packageName}@latest ...`);
|
|
32
|
-
const result = spawnSync(
|
|
35
|
+
const result = spawnSync(getNpmCommand(), ['install', '-g', `${packageName}@latest`], {
|
|
33
36
|
stdio: 'inherit',
|
|
34
|
-
shell: true,
|
|
35
37
|
});
|
|
36
38
|
if (result.status !== 0) {
|
|
37
39
|
throw new Error('更新失败,请检查网络或 npm 登录状态');
|
package/user.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const { getToken, saveToken } = require('./tokenStore');
|
|
2
|
+
const { request, getValidAccessToken } = require('./api');
|
|
3
|
+
const GET_USER_PATH = '/v1/api/ci/open/getUser';
|
|
4
|
+
|
|
5
|
+
function normalizeUser(user) {
|
|
6
|
+
if (!user) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
id: user.id,
|
|
11
|
+
name: user.name,
|
|
12
|
+
realname: user.realname || null,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getDisplayName(user) {
|
|
17
|
+
if (!user) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return user.realname || user.name || null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function fetchAndSaveCurrentUser() {
|
|
24
|
+
const cliToken = await getValidAccessToken();
|
|
25
|
+
const result = await request(GET_USER_PATH, {
|
|
26
|
+
method: 'GET',
|
|
27
|
+
headers: {
|
|
28
|
+
cli_token: cliToken,
|
|
29
|
+
},
|
|
30
|
+
}); if (result.code !== 200 || !result.data?.user) {
|
|
31
|
+
throw new Error(result.msg || '获取用户信息失败');
|
|
32
|
+
}
|
|
33
|
+
const user = normalizeUser(result.data.user);
|
|
34
|
+
const stored = getToken() || {};
|
|
35
|
+
saveToken({
|
|
36
|
+
...stored,
|
|
37
|
+
user,
|
|
38
|
+
});
|
|
39
|
+
return user;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = {
|
|
43
|
+
GET_USER_PATH,
|
|
44
|
+
normalizeUser,
|
|
45
|
+
getDisplayName,
|
|
46
|
+
fetchAndSaveCurrentUser,
|
|
47
|
+
};
|