@taole/deploy-helper 1.0.0 → 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 +8 -8
- package/index.mjs +557 -557
- package/lib/login.mjs +167 -167
- package/lib/offlinePkg.mjs +333 -314
- package/lib/pipelineApi.mjs +364 -364
- package/lib/project.mjs +345 -345
- package/lib/upload.js +49 -49
- package/lib/util.mjs +101 -101
- package/package.json +36 -36
package/lib/login.mjs
CHANGED
|
@@ -1,167 +1,167 @@
|
|
|
1
|
-
import { log } from "./util.mjs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import os from "os";
|
|
4
|
-
import fs from "fs";
|
|
5
|
-
import { exec } from "child_process";
|
|
6
|
-
import { promisify } from "util";
|
|
7
|
-
|
|
8
|
-
const execAsync = promisify(exec);
|
|
9
|
-
const isDev = false;
|
|
10
|
-
/**
|
|
11
|
-
* 保存本地用户登录状态的缓存文件
|
|
12
|
-
*/
|
|
13
|
-
const cacheFilePath = path.join(os.homedir(), ".tw-cli-userinfo-cache.json");
|
|
14
|
-
let host =
|
|
15
|
-
"https://fapi.tuwan.com";
|
|
16
|
-
if (isDev) {
|
|
17
|
-
host = "https://local-h5-plat.tuwan.com";
|
|
18
|
-
}
|
|
19
|
-
const devHost = "http://localhost:9000";
|
|
20
|
-
let CREATE_SESSION_URL = `${host}/api/login/createLoginSession`;
|
|
21
|
-
|
|
22
|
-
export async function getCache() {
|
|
23
|
-
try {
|
|
24
|
-
if (fs.existsSync(cacheFilePath)) {
|
|
25
|
-
const cacheStr = fs.readFileSync(cacheFilePath, "utf8");
|
|
26
|
-
const cache = JSON.parse(cacheStr);
|
|
27
|
-
const Tuwan_Passport = cache.Tuwan_Passport;
|
|
28
|
-
if (Tuwan_Passport) {
|
|
29
|
-
// 检查Tuwan_Passport是否还有效
|
|
30
|
-
const res = await fetch(
|
|
31
|
-
"https://yapi.tuwan.com/User/getUid?format=json",
|
|
32
|
-
{
|
|
33
|
-
headers: {
|
|
34
|
-
Cookie: `Tuwan_Passport=${Tuwan_Passport}`,
|
|
35
|
-
},
|
|
36
|
-
}
|
|
37
|
-
);
|
|
38
|
-
//log("getCache res", res);
|
|
39
|
-
const uidResp = await res.json();
|
|
40
|
-
//log("getCache uidResp", uidResp);
|
|
41
|
-
if (
|
|
42
|
-
uidResp &&
|
|
43
|
-
uidResp.error === 0 &&
|
|
44
|
-
uidResp.data &&
|
|
45
|
-
uidResp.data.uid === cache.userInfo.uid
|
|
46
|
-
) {
|
|
47
|
-
return cache;
|
|
48
|
-
} else {
|
|
49
|
-
log("验证本地缓存失败, 已清理本地缓存");
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
clearCache();
|
|
54
|
-
return null;
|
|
55
|
-
} catch (error) {
|
|
56
|
-
console.error("获取缓存失败:", error);
|
|
57
|
-
clearCache();
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function setCache(data) {
|
|
63
|
-
fs.writeFileSync(cacheFilePath, JSON.stringify(data));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function clearCache() {
|
|
67
|
-
if (fs.existsSync(cacheFilePath)) {
|
|
68
|
-
fs.unlinkSync(cacheFilePath);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function openBrowser(url) {
|
|
73
|
-
const platform = process.platform;
|
|
74
|
-
if (platform === "win32") {
|
|
75
|
-
await execAsync(`start "" "${url}"`);
|
|
76
|
-
} else if (platform === "darwin") {
|
|
77
|
-
await execAsync(`open "${url}"`);
|
|
78
|
-
} else {
|
|
79
|
-
await execAsync(`xdg-open "${url}"`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function createLoginSession() {
|
|
84
|
-
let url = CREATE_SESSION_URL;
|
|
85
|
-
if (isDev) {
|
|
86
|
-
url = url.replace(host, devHost);
|
|
87
|
-
}
|
|
88
|
-
const res = await fetch(url, {
|
|
89
|
-
method: "POST",
|
|
90
|
-
headers: { "Content-Type": "application/json" },
|
|
91
|
-
});
|
|
92
|
-
if (!res.ok) {
|
|
93
|
-
throw new Error(`创建登录会话失败: ${res.status} ${res.statusText}`);
|
|
94
|
-
}
|
|
95
|
-
return res.json();
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function queryLoginResult(
|
|
99
|
-
doneUrl,
|
|
100
|
-
intervalMs = 2000,
|
|
101
|
-
timeoutMs = 120000
|
|
102
|
-
) {
|
|
103
|
-
const start = Date.now();
|
|
104
|
-
if (isDev) {
|
|
105
|
-
doneUrl = doneUrl.replace(host, devHost);
|
|
106
|
-
}
|
|
107
|
-
while (Date.now() - start < timeoutMs) {
|
|
108
|
-
const res = await fetch(doneUrl, {
|
|
109
|
-
method: "POST",
|
|
110
|
-
headers: { "Content-Type": "application/json" },
|
|
111
|
-
});
|
|
112
|
-
//log("queryLoginResult res", res);
|
|
113
|
-
if (!res.ok) {
|
|
114
|
-
throw new Error(`轮询登录结果失败: ${res.status} ${res.statusText}`);
|
|
115
|
-
}
|
|
116
|
-
const data = await res.json();
|
|
117
|
-
//log("queryLoginResult data", JSON.stringify(data, null, 2));
|
|
118
|
-
let loginResult = data.loginResult;
|
|
119
|
-
if (loginResult && data.status === "success") {
|
|
120
|
-
return JSON.parse(loginResult);
|
|
121
|
-
}
|
|
122
|
-
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
123
|
-
}
|
|
124
|
-
throw new Error("登录超时,请重试");
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export async function cmdLogin() {
|
|
128
|
-
const cache = await getCache();
|
|
129
|
-
if (cache && cache.userInfo && cache.userInfo.uid) {
|
|
130
|
-
log(
|
|
131
|
-
`已登录账号: ${cache.userInfo.nickname}(${cache.userInfo.uid}), 若需登录其他账号请先登出`
|
|
132
|
-
);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
log("正在创建登录会话...");
|
|
136
|
-
const { openUrl, doneUrl } = await createLoginSession();
|
|
137
|
-
log("请在浏览器中完成登录:", openUrl);
|
|
138
|
-
|
|
139
|
-
log("正在打开浏览器...");
|
|
140
|
-
await openBrowser(openUrl);
|
|
141
|
-
|
|
142
|
-
log("等待登录结果...");
|
|
143
|
-
const result = await queryLoginResult(doneUrl);
|
|
144
|
-
log(`${result.userInfo.nickname}(${result.userInfo.uid})登录成功`);
|
|
145
|
-
setCache(result);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export async function cmdWhoami() {
|
|
149
|
-
const cache = await getCache();
|
|
150
|
-
const userInfo = cache?.userInfo;
|
|
151
|
-
if (!userInfo || !userInfo.uid) {
|
|
152
|
-
log("当前未登录");
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
log(`当前登录账号: ${userInfo.nickname}(${userInfo.uid})`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export async function cmdLogout() {
|
|
159
|
-
const cache = await getCache();
|
|
160
|
-
const userInfo = cache?.userInfo;
|
|
161
|
-
if (!userInfo || !userInfo.uid) {
|
|
162
|
-
log("当前未登录");
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
clearCache();
|
|
166
|
-
log("已退出登录");
|
|
167
|
-
}
|
|
1
|
+
import { log } from "./util.mjs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import { exec } from "child_process";
|
|
6
|
+
import { promisify } from "util";
|
|
7
|
+
|
|
8
|
+
const execAsync = promisify(exec);
|
|
9
|
+
const isDev = false;
|
|
10
|
+
/**
|
|
11
|
+
* 保存本地用户登录状态的缓存文件
|
|
12
|
+
*/
|
|
13
|
+
const cacheFilePath = path.join(os.homedir(), ".tw-cli-userinfo-cache.json");
|
|
14
|
+
let host =
|
|
15
|
+
"https://fapi.tuwan.com";
|
|
16
|
+
if (isDev) {
|
|
17
|
+
host = "https://local-h5-plat.tuwan.com";
|
|
18
|
+
}
|
|
19
|
+
const devHost = "http://localhost:9000";
|
|
20
|
+
let CREATE_SESSION_URL = `${host}/api/login/createLoginSession`;
|
|
21
|
+
|
|
22
|
+
export async function getCache() {
|
|
23
|
+
try {
|
|
24
|
+
if (fs.existsSync(cacheFilePath)) {
|
|
25
|
+
const cacheStr = fs.readFileSync(cacheFilePath, "utf8");
|
|
26
|
+
const cache = JSON.parse(cacheStr);
|
|
27
|
+
const Tuwan_Passport = cache.Tuwan_Passport;
|
|
28
|
+
if (Tuwan_Passport) {
|
|
29
|
+
// 检查Tuwan_Passport是否还有效
|
|
30
|
+
const res = await fetch(
|
|
31
|
+
"https://yapi.tuwan.com/User/getUid?format=json",
|
|
32
|
+
{
|
|
33
|
+
headers: {
|
|
34
|
+
Cookie: `Tuwan_Passport=${Tuwan_Passport}`,
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
//log("getCache res", res);
|
|
39
|
+
const uidResp = await res.json();
|
|
40
|
+
//log("getCache uidResp", uidResp);
|
|
41
|
+
if (
|
|
42
|
+
uidResp &&
|
|
43
|
+
uidResp.error === 0 &&
|
|
44
|
+
uidResp.data &&
|
|
45
|
+
uidResp.data.uid === cache.userInfo.uid
|
|
46
|
+
) {
|
|
47
|
+
return cache;
|
|
48
|
+
} else {
|
|
49
|
+
log("验证本地缓存失败, 已清理本地缓存");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
clearCache();
|
|
54
|
+
return null;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error("获取缓存失败:", error);
|
|
57
|
+
clearCache();
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function setCache(data) {
|
|
63
|
+
fs.writeFileSync(cacheFilePath, JSON.stringify(data));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function clearCache() {
|
|
67
|
+
if (fs.existsSync(cacheFilePath)) {
|
|
68
|
+
fs.unlinkSync(cacheFilePath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function openBrowser(url) {
|
|
73
|
+
const platform = process.platform;
|
|
74
|
+
if (platform === "win32") {
|
|
75
|
+
await execAsync(`start "" "${url}"`);
|
|
76
|
+
} else if (platform === "darwin") {
|
|
77
|
+
await execAsync(`open "${url}"`);
|
|
78
|
+
} else {
|
|
79
|
+
await execAsync(`xdg-open "${url}"`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function createLoginSession() {
|
|
84
|
+
let url = CREATE_SESSION_URL;
|
|
85
|
+
if (isDev) {
|
|
86
|
+
url = url.replace(host, devHost);
|
|
87
|
+
}
|
|
88
|
+
const res = await fetch(url, {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers: { "Content-Type": "application/json" },
|
|
91
|
+
});
|
|
92
|
+
if (!res.ok) {
|
|
93
|
+
throw new Error(`创建登录会话失败: ${res.status} ${res.statusText}`);
|
|
94
|
+
}
|
|
95
|
+
return res.json();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function queryLoginResult(
|
|
99
|
+
doneUrl,
|
|
100
|
+
intervalMs = 2000,
|
|
101
|
+
timeoutMs = 120000
|
|
102
|
+
) {
|
|
103
|
+
const start = Date.now();
|
|
104
|
+
if (isDev) {
|
|
105
|
+
doneUrl = doneUrl.replace(host, devHost);
|
|
106
|
+
}
|
|
107
|
+
while (Date.now() - start < timeoutMs) {
|
|
108
|
+
const res = await fetch(doneUrl, {
|
|
109
|
+
method: "POST",
|
|
110
|
+
headers: { "Content-Type": "application/json" },
|
|
111
|
+
});
|
|
112
|
+
//log("queryLoginResult res", res);
|
|
113
|
+
if (!res.ok) {
|
|
114
|
+
throw new Error(`轮询登录结果失败: ${res.status} ${res.statusText}`);
|
|
115
|
+
}
|
|
116
|
+
const data = await res.json();
|
|
117
|
+
//log("queryLoginResult data", JSON.stringify(data, null, 2));
|
|
118
|
+
let loginResult = data.loginResult;
|
|
119
|
+
if (loginResult && data.status === "success") {
|
|
120
|
+
return JSON.parse(loginResult);
|
|
121
|
+
}
|
|
122
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
123
|
+
}
|
|
124
|
+
throw new Error("登录超时,请重试");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function cmdLogin() {
|
|
128
|
+
const cache = await getCache();
|
|
129
|
+
if (cache && cache.userInfo && cache.userInfo.uid) {
|
|
130
|
+
log(
|
|
131
|
+
`已登录账号: ${cache.userInfo.nickname}(${cache.userInfo.uid}), 若需登录其他账号请先登出`
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
log("正在创建登录会话...");
|
|
136
|
+
const { openUrl, doneUrl } = await createLoginSession();
|
|
137
|
+
log("请在浏览器中完成登录:", openUrl);
|
|
138
|
+
|
|
139
|
+
log("正在打开浏览器...");
|
|
140
|
+
await openBrowser(openUrl);
|
|
141
|
+
|
|
142
|
+
log("等待登录结果...");
|
|
143
|
+
const result = await queryLoginResult(doneUrl);
|
|
144
|
+
log(`${result.userInfo.nickname}(${result.userInfo.uid})登录成功`);
|
|
145
|
+
setCache(result);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function cmdWhoami() {
|
|
149
|
+
const cache = await getCache();
|
|
150
|
+
const userInfo = cache?.userInfo;
|
|
151
|
+
if (!userInfo || !userInfo.uid) {
|
|
152
|
+
log("当前未登录");
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
log(`当前登录账号: ${userInfo.nickname}(${userInfo.uid})`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function cmdLogout() {
|
|
159
|
+
const cache = await getCache();
|
|
160
|
+
const userInfo = cache?.userInfo;
|
|
161
|
+
if (!userInfo || !userInfo.uid) {
|
|
162
|
+
log("当前未登录");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
clearCache();
|
|
166
|
+
log("已退出登录");
|
|
167
|
+
}
|