@zjex/git-workflow 0.2.1 → 0.2.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/dist/index.js +1584 -38
- package/package.json +1 -1
- package/src/update-notifier.ts +18 -25
- package/src/utils.ts +6 -0
- package/test-update-flow.mjs +98 -0
- package/tsup.config.ts +13 -3
package/package.json
CHANGED
package/src/update-notifier.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { execSync } from "child_process";
|
|
2
|
+
import { readFileSync, writeFileSync, existsSync } from "fs";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import { join } from "path";
|
|
2
5
|
import boxen from "boxen";
|
|
3
6
|
import { select } from "@inquirer/prompts";
|
|
4
7
|
import ora from "ora";
|
|
@@ -80,9 +83,9 @@ async function showUpdateMessage(
|
|
|
80
83
|
packageName: string
|
|
81
84
|
): Promise<"update" | "continue" | "dismiss"> {
|
|
82
85
|
const message = [
|
|
83
|
-
|
|
86
|
+
colors.bold("� 发现新版新本可用!"),
|
|
84
87
|
"",
|
|
85
|
-
`${colors.dim
|
|
88
|
+
`${colors.dim(current)} → ${colors.green(colors.bold(latest))}`,
|
|
86
89
|
].join("\n");
|
|
87
90
|
|
|
88
91
|
console.log("");
|
|
@@ -155,26 +158,22 @@ async function performUpdate(packageName: string): Promise<void> {
|
|
|
155
158
|
stdio: ["pipe", "pipe", "pipe"],
|
|
156
159
|
});
|
|
157
160
|
|
|
158
|
-
spinner.succeed(colors.green
|
|
161
|
+
spinner.succeed(colors.green("更新成功!"));
|
|
159
162
|
console.log("");
|
|
160
|
-
console.log(
|
|
161
|
-
colors.cyan + " 提示: 请重新运行命令以使用新版本" + colors.reset
|
|
162
|
-
);
|
|
163
|
+
console.log(colors.cyan(" 提示: 请重新运行命令以使用新版本"));
|
|
163
164
|
console.log("");
|
|
164
165
|
|
|
165
166
|
// 更新成功后退出,让用户重新运行
|
|
166
167
|
process.exit(0);
|
|
167
168
|
} catch (error) {
|
|
168
|
-
spinner.fail(colors.red
|
|
169
|
+
spinner.fail(colors.red("更新失败"));
|
|
169
170
|
console.log("");
|
|
170
|
-
console.log(colors.dim
|
|
171
|
-
console.log(
|
|
172
|
-
|
|
173
|
-
);
|
|
174
|
-
console.log(colors.cyan + " npm uninstall -g git-workflow" + colors.reset);
|
|
171
|
+
console.log(colors.dim(" 你可以手动运行以下命令更新:"));
|
|
172
|
+
console.log(colors.yellow(" # 如果之前安装过旧版本,先卸载:"));
|
|
173
|
+
console.log(colors.cyan(" npm uninstall -g git-workflow"));
|
|
175
174
|
console.log("");
|
|
176
|
-
console.log(colors.yellow
|
|
177
|
-
console.log(colors.cyan
|
|
175
|
+
console.log(colors.yellow(" # 然后安装新版本:"));
|
|
176
|
+
console.log(colors.cyan(` npm install -g ${packageName}`));
|
|
178
177
|
console.log("");
|
|
179
178
|
}
|
|
180
179
|
}
|
|
@@ -184,16 +183,13 @@ async function performUpdate(packageName: string): Promise<void> {
|
|
|
184
183
|
*/
|
|
185
184
|
function readCache(): UpdateCache | null {
|
|
186
185
|
try {
|
|
187
|
-
const
|
|
188
|
-
const os = require("os");
|
|
189
|
-
const path = require("path");
|
|
190
|
-
const cacheFile = path.join(os.homedir(), CACHE_FILE);
|
|
186
|
+
const cacheFile = join(homedir(), CACHE_FILE);
|
|
191
187
|
|
|
192
|
-
if (!
|
|
188
|
+
if (!existsSync(cacheFile)) {
|
|
193
189
|
return null;
|
|
194
190
|
}
|
|
195
191
|
|
|
196
|
-
const content =
|
|
192
|
+
const content = readFileSync(cacheFile, "utf-8");
|
|
197
193
|
return JSON.parse(content);
|
|
198
194
|
} catch {
|
|
199
195
|
return null;
|
|
@@ -205,12 +201,9 @@ function readCache(): UpdateCache | null {
|
|
|
205
201
|
*/
|
|
206
202
|
function writeCache(cache: UpdateCache): void {
|
|
207
203
|
try {
|
|
208
|
-
const
|
|
209
|
-
const os = require("os");
|
|
210
|
-
const path = require("path");
|
|
211
|
-
const cacheFile = path.join(os.homedir(), CACHE_FILE);
|
|
204
|
+
const cacheFile = join(homedir(), CACHE_FILE);
|
|
212
205
|
|
|
213
|
-
|
|
206
|
+
writeFileSync(cacheFile, JSON.stringify(cache), "utf-8");
|
|
214
207
|
} catch {
|
|
215
208
|
// 静默失败
|
|
216
209
|
}
|
package/src/utils.ts
CHANGED
|
@@ -4,14 +4,20 @@ export interface Colors {
|
|
|
4
4
|
red: (s: string) => string;
|
|
5
5
|
green: (s: string) => string;
|
|
6
6
|
yellow: (s: string) => string;
|
|
7
|
+
cyan: (s: string) => string;
|
|
7
8
|
dim: (s: string) => string;
|
|
9
|
+
bold: (s: string) => string;
|
|
10
|
+
reset: string;
|
|
8
11
|
}
|
|
9
12
|
|
|
10
13
|
export const colors: Colors = {
|
|
11
14
|
red: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
12
15
|
green: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
13
16
|
yellow: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
17
|
+
cyan: (s) => `\x1b[36m${s}\x1b[0m`,
|
|
14
18
|
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
19
|
+
bold: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
20
|
+
reset: "\x1b[0m",
|
|
15
21
|
};
|
|
16
22
|
|
|
17
23
|
export const TODAY: string = new Date()
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import boxen from "boxen";
|
|
4
|
+
import { select } from "@inquirer/prompts";
|
|
5
|
+
import ora from "ora";
|
|
6
|
+
|
|
7
|
+
const colors = {
|
|
8
|
+
red: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
9
|
+
green: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
10
|
+
yellow: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
11
|
+
cyan: (s) => `\x1b[36m${s}\x1b[0m`,
|
|
12
|
+
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
13
|
+
bold: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
14
|
+
reset: "\x1b[0m",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
async function testUpdateFlow() {
|
|
18
|
+
const current = "0.1.0";
|
|
19
|
+
const latest = "0.2.0";
|
|
20
|
+
const packageName = "@zjex/git-workflow";
|
|
21
|
+
|
|
22
|
+
// 1. 显示更新提示框
|
|
23
|
+
const message = [
|
|
24
|
+
colors.bold("🎉 发现新版本可用!"),
|
|
25
|
+
"",
|
|
26
|
+
`${colors.dim(current)} → ${colors.green(colors.bold(latest))}`,
|
|
27
|
+
].join("\n");
|
|
28
|
+
|
|
29
|
+
console.log("");
|
|
30
|
+
console.log(
|
|
31
|
+
boxen(message, {
|
|
32
|
+
padding: 1,
|
|
33
|
+
margin: 1,
|
|
34
|
+
borderStyle: "round",
|
|
35
|
+
borderColor: "yellow",
|
|
36
|
+
align: "left",
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// 2. 交互式选择
|
|
41
|
+
try {
|
|
42
|
+
const action = await select({
|
|
43
|
+
message: "你想做什么?",
|
|
44
|
+
choices: [
|
|
45
|
+
{
|
|
46
|
+
name: "🚀 立即更新",
|
|
47
|
+
value: "update",
|
|
48
|
+
description: `运行 npm install -g ${packageName}`,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: "⏭️ 稍后更新,继续使用",
|
|
52
|
+
value: "continue",
|
|
53
|
+
description: "下次启动时会再次提示",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: "🙈 跳过此版本 (24h 内不再提示)",
|
|
57
|
+
value: "dismiss",
|
|
58
|
+
description: "24 小时内不会再提示此版本",
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
console.log("");
|
|
64
|
+
|
|
65
|
+
// 3. 根据选择执行操作
|
|
66
|
+
if (action === "update") {
|
|
67
|
+
// 模拟更新过程
|
|
68
|
+
const spinner = ora({
|
|
69
|
+
text: "正在更新...",
|
|
70
|
+
spinner: "dots",
|
|
71
|
+
}).start();
|
|
72
|
+
|
|
73
|
+
// 模拟卸载旧版本
|
|
74
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
75
|
+
spinner.text = "已卸载旧版本,正在安装新版本...";
|
|
76
|
+
|
|
77
|
+
// 模拟安装新版本
|
|
78
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
79
|
+
|
|
80
|
+
spinner.succeed(colors.green("更新成功!"));
|
|
81
|
+
console.log("");
|
|
82
|
+
console.log(colors.cyan(" 提示: 请重新运行命令以使用新版本"));
|
|
83
|
+
console.log("");
|
|
84
|
+
} else if (action === "continue") {
|
|
85
|
+
console.log(colors.cyan("继续使用当前版本..."));
|
|
86
|
+
console.log("");
|
|
87
|
+
} else if (action === "dismiss") {
|
|
88
|
+
console.log(colors.dim("已跳过此版本,24 小时内不再提示"));
|
|
89
|
+
console.log("");
|
|
90
|
+
}
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.log("");
|
|
93
|
+
console.log(colors.dim("已取消"));
|
|
94
|
+
console.log("");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
testUpdateFlow();
|
package/tsup.config.ts
CHANGED
|
@@ -7,10 +7,20 @@ export default defineConfig({
|
|
|
7
7
|
entry: ["src/index.ts"],
|
|
8
8
|
format: ["esm"],
|
|
9
9
|
clean: true,
|
|
10
|
-
shims:
|
|
11
|
-
minify:
|
|
10
|
+
shims: false,
|
|
11
|
+
minify: false,
|
|
12
|
+
splitting: false,
|
|
13
|
+
treeshake: false,
|
|
14
|
+
external: [
|
|
15
|
+
// 不打包这些依赖,让 node 自己解析
|
|
16
|
+
"boxen",
|
|
17
|
+
"ora",
|
|
18
|
+
"@inquirer/prompts",
|
|
19
|
+
"@inquirer/core",
|
|
20
|
+
"cac",
|
|
21
|
+
],
|
|
12
22
|
banner: {
|
|
13
|
-
js: "#!/usr/bin/env node
|
|
23
|
+
js: "#!/usr/bin/env node",
|
|
14
24
|
},
|
|
15
25
|
define: {
|
|
16
26
|
__VERSION__: JSON.stringify(pkg.version),
|