@zjex/git-workflow 0.3.4 → 0.3.6

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 CHANGED
@@ -12,7 +12,7 @@
12
12
  <a href="https://github.com/iamzjt-front-end/git-workflow"><img src="https://img.shields.io/github/stars/iamzjt-front-end/git-workflow?style=flat&colorA=18181B&colorB=F59E0B" alt="github stars"></a>
13
13
  <a href="https://github.com/iamzjt-front-end/git-workflow/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/@zjex/git-workflow?style=flat&colorA=18181B&colorB=10B981" alt="license"></a>
14
14
  <a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-%3E%3D18-339933?style=flat&logo=node.js&logoColor=white&colorA=18181B" alt="node version"></a>
15
- <a href="https://github.com/iamzjt-front-end/git-workflow/actions"><img src="https://img.shields.io/badge/tests-266%20passed-success?style=flat&colorA=18181B" alt="tests"></a>
15
+ <a href="https://github.com/iamzjt-front-end/git-workflow/actions"><img src="https://img.shields.io/badge/tests-267%20passed-success?style=flat&colorA=18181B" alt="tests"></a>
16
16
  <a href="https://github.com/iamzjt-front-end/git-workflow/issues"><img src="https://img.shields.io/github/issues/iamzjt-front-end/git-workflow?style=flat&colorA=18181B&colorB=EC4899" alt="issues"></a>
17
17
  </p>
18
18
 
package/dist/index.js CHANGED
@@ -100,12 +100,9 @@ async function checkForUpdates(currentVersion, packageName = "@zjex/git-workflow
100
100
  try {
101
101
  const cache = readCache();
102
102
  const now = Date.now();
103
- if (cache?.latestVersion && cache.checkedVersion === currentVersion) {
104
- if (cache.lastDismiss && now - cache.lastDismiss < DISMISS_INTERVAL) {
105
- backgroundCheck(currentVersion, packageName);
106
- return;
107
- }
108
- if (semver.gt(cache.latestVersion, currentVersion)) {
103
+ if (cache?.latestVersion && semver.gt(cache.latestVersion, currentVersion)) {
104
+ const isDismissed = cache.lastDismiss && now - cache.lastDismiss < DISMISS_INTERVAL;
105
+ if (!isDismissed) {
109
106
  if (interactive) {
110
107
  const action = await showUpdateMessage(
111
108
  currentVersion,
@@ -132,16 +129,19 @@ async function checkForUpdates(currentVersion, packageName = "@zjex/git-workflow
132
129
  function backgroundCheck(currentVersion, packageName) {
133
130
  const cache = readCache();
134
131
  const now = Date.now();
135
- if (cache?.lastCheck && now - cache.lastCheck < CHECK_INTERVAL) {
132
+ const isUpToDate = cache?.latestVersion && !semver.gt(cache.latestVersion, currentVersion);
133
+ const recentlyChecked = cache?.lastCheck && now - cache.lastCheck < CHECK_INTERVAL;
134
+ if (isUpToDate && recentlyChecked) {
136
135
  return;
137
136
  }
138
- Promise.resolve().then(async () => {
137
+ setImmediate(async () => {
139
138
  try {
140
139
  const latestVersion = await getLatestVersion(packageName);
141
140
  if (latestVersion) {
141
+ const cache2 = readCache() || {};
142
142
  writeCache({
143
- ...cache,
144
- lastCheck: now,
143
+ ...cache2,
144
+ lastCheck: Date.now(),
145
145
  latestVersion,
146
146
  checkedVersion: currentVersion
147
147
  });
@@ -266,7 +266,7 @@ async function performUpdate(packageName) {
266
266
  borderStyle: "round",
267
267
  borderColor: "green",
268
268
  align: "left",
269
- width: 50
269
+ width: 40
270
270
  }
271
271
  )
272
272
  );
@@ -307,13 +307,13 @@ function writeCache(cache) {
307
307
  } catch {
308
308
  }
309
309
  }
310
- var CHECK_INTERVAL, DISMISS_INTERVAL, CACHE_FILE;
310
+ var DISMISS_INTERVAL, CHECK_INTERVAL, CACHE_FILE;
311
311
  var init_update_notifier = __esm({
312
312
  "src/update-notifier.ts"() {
313
313
  "use strict";
314
314
  init_utils();
315
- CHECK_INTERVAL = 1e3 * 60 * 60 * 4;
316
315
  DISMISS_INTERVAL = 1e3 * 60 * 60 * 24;
316
+ CHECK_INTERVAL = 1e3 * 60 * 60 * 1;
317
317
  CACHE_FILE = ".gw-update-check";
318
318
  }
319
319
  });
@@ -2561,7 +2561,7 @@ async function update(currentVersion) {
2561
2561
  borderStyle: "round",
2562
2562
  borderColor: "green",
2563
2563
  align: "left",
2564
- width: 50
2564
+ width: 40
2565
2565
  }
2566
2566
  )
2567
2567
  );
@@ -2916,7 +2916,7 @@ process.on("SIGTERM", () => {
2916
2916
  console.log("");
2917
2917
  process.exit(0);
2918
2918
  });
2919
- var version = true ? "0.3.4" : "0.0.0-dev";
2919
+ var version = true ? "0.3.6" : "0.0.0-dev";
2920
2920
  async function mainMenu() {
2921
2921
  console.log(
2922
2922
  colors.green(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zjex/git-workflow",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "🚀 极简的 Git 工作流 CLI 工具,让分支管理和版本发布变得轻松愉快",
5
5
  "type": "module",
6
6
  "bin": {
@@ -156,7 +156,7 @@ export async function update(currentVersion: string): Promise<void> {
156
156
  borderStyle: "round",
157
157
  borderColor: "green",
158
158
  align: "left",
159
- width: 50,
159
+ width: 40,
160
160
  }
161
161
  )
162
162
  );
@@ -8,8 +8,8 @@ import ora from "ora";
8
8
  import semver from "semver";
9
9
  import { colors } from "./utils.js";
10
10
 
11
- const CHECK_INTERVAL = 1000 * 60 * 60 * 4; // 4 小时检查一次
12
11
  const DISMISS_INTERVAL = 1000 * 60 * 60 * 24; // 24 小时后再次提示
12
+ const CHECK_INTERVAL = 1000 * 60 * 60 * 1; // 已是最新版本时,1 小时检查一次
13
13
  const CACHE_FILE = ".gw-update-check";
14
14
 
15
15
  interface UpdateCache {
@@ -20,7 +20,8 @@ interface UpdateCache {
20
20
  }
21
21
 
22
22
  /**
23
- * 检查是否有新版本(异步静默检查)
23
+ * 检查是否有新版本
24
+ * 策略:后台异步检查,下次运行时提示
24
25
  * @param currentVersion 当前版本
25
26
  * @param packageName 包名
26
27
  * @param interactive 是否交互式(true: 显示完整提示并可选择更新,false: 只显示简单提示)
@@ -34,19 +35,17 @@ export async function checkForUpdates(
34
35
  const cache = readCache();
35
36
  const now = Date.now();
36
37
 
37
- // 1. 先检查缓存中是否有新版本需要提示
38
- if (cache?.latestVersion && cache.checkedVersion === currentVersion) {
39
- // 如果用户在 24 小时内关闭过提示,跳过
40
- if (cache.lastDismiss && now - cache.lastDismiss < DISMISS_INTERVAL) {
41
- // 继续后台检查(不阻塞)
42
- backgroundCheck(currentVersion, packageName);
43
- return;
44
- }
38
+ // 1. 先用缓存的结果提示用户(如果有新版本)
39
+ if (
40
+ cache?.latestVersion &&
41
+ semver.gt(cache.latestVersion, currentVersion)
42
+ ) {
43
+ // 检查用户是否在 24 小时内关闭过提示
44
+ const isDismissed =
45
+ cache.lastDismiss && now - cache.lastDismiss < DISMISS_INTERVAL;
45
46
 
46
- // 使用 semver 比较版本
47
- if (semver.gt(cache.latestVersion, currentVersion)) {
47
+ if (!isDismissed) {
48
48
  if (interactive) {
49
- // 交互式模式:显示完整提示,可选择更新
50
49
  const action = await showUpdateMessage(
51
50
  currentVersion,
52
51
  cache.latestVersion,
@@ -59,13 +58,12 @@ export async function checkForUpdates(
59
58
  writeCache({ ...cache, lastDismiss: now });
60
59
  }
61
60
  } else {
62
- // 非交互式模式:只显示简单提示
63
61
  showSimpleNotification(currentVersion, cache.latestVersion);
64
62
  }
65
63
  }
66
64
  }
67
65
 
68
- // 2. 后台异步检查更新(不阻塞当前命令)
66
+ // 2. 后台异步检查更新(每次都检查,不阻塞)
69
67
  backgroundCheck(currentVersion, packageName);
70
68
  } catch (error) {
71
69
  // 如果是用户按 Ctrl+C,重新抛出让全局处理
@@ -78,25 +76,33 @@ export async function checkForUpdates(
78
76
 
79
77
  /**
80
78
  * 后台异步检查更新(不阻塞)
79
+ * - 有新版本时:每次都检查
80
+ * - 已是最新版本时:1 小时检查一次
81
81
  */
82
82
  function backgroundCheck(currentVersion: string, packageName: string): void {
83
83
  const cache = readCache();
84
84
  const now = Date.now();
85
85
 
86
- // 如果距离上次检查不到 4 小时,跳过
87
- if (cache?.lastCheck && now - cache.lastCheck < CHECK_INTERVAL) {
86
+ // 如果已是最新版本,且距离上次检查不到 1 小时,跳过
87
+ const isUpToDate =
88
+ cache?.latestVersion && !semver.gt(cache.latestVersion, currentVersion);
89
+ const recentlyChecked =
90
+ cache?.lastCheck && now - cache.lastCheck < CHECK_INTERVAL;
91
+
92
+ if (isUpToDate && recentlyChecked) {
88
93
  return;
89
94
  }
90
95
 
91
- // 使用 Promise 异步执行,不阻塞当前命令
92
- Promise.resolve().then(async () => {
96
+ // 使用 setImmediate 确保不阻塞主流程
97
+ setImmediate(async () => {
93
98
  try {
94
99
  const latestVersion = await getLatestVersion(packageName);
95
100
 
96
101
  if (latestVersion) {
102
+ const cache = readCache() || {};
97
103
  writeCache({
98
104
  ...cache,
99
- lastCheck: now,
105
+ lastCheck: Date.now(),
100
106
  latestVersion,
101
107
  checkedVersion: currentVersion,
102
108
  });
@@ -264,7 +270,7 @@ async function performUpdate(packageName: string): Promise<void> {
264
270
  borderStyle: "round",
265
271
  borderColor: "green",
266
272
  align: "left",
267
- width: 50,
273
+ width: 40,
268
274
  }
269
275
  )
270
276
  );
@@ -153,7 +153,7 @@ describe("Update Notifier 模块测试", () => {
153
153
  consoleSpy.mockRestore();
154
154
  });
155
155
 
156
- it("4小时内不应该重复检查", async () => {
156
+ it("已是最新版本且1小时内不应该重复检查", async () => {
157
157
  const mockCache = {
158
158
  lastCheck: Date.now(),
159
159
  latestVersion: "1.0.0",
@@ -167,14 +167,14 @@ describe("Update Notifier 模块测试", () => {
167
167
  await checkForUpdates("1.0.0");
168
168
  await vi.runAllTimersAsync();
169
169
 
170
- // writeFileSync 不应该被调用(因为在4小时内)
170
+ // writeFileSync 不应该被调用(因为已是最新版本且在1小时内)
171
171
  expect(writeFileSync).not.toHaveBeenCalled();
172
172
  });
173
173
 
174
- it("超过4小时应该重新检查", async () => {
175
- const fourHoursAgo = Date.now() - 5 * 60 * 60 * 1000; // 5小时前
174
+ it("已是最新版本但超过1小时应该重新检查", async () => {
175
+ const oneHourAgo = Date.now() - 2 * 60 * 60 * 1000; // 2小时前
176
176
  const mockCache = {
177
- lastCheck: fourHoursAgo,
177
+ lastCheck: oneHourAgo,
178
178
  latestVersion: "1.0.0",
179
179
  checkedVersion: "1.0.0",
180
180
  };
@@ -189,6 +189,28 @@ describe("Update Notifier 模块测试", () => {
189
189
  expect(writeFileSync).toHaveBeenCalled();
190
190
  });
191
191
 
192
+ it("有新版本时每次都应该后台检查", async () => {
193
+ const mockCache = {
194
+ lastCheck: Date.now(), // 刚刚检查过
195
+ latestVersion: "1.0.1", // 有新版本
196
+ checkedVersion: "1.0.0",
197
+ };
198
+
199
+ vi.mocked(existsSync).mockReturnValue(true);
200
+ vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockCache));
201
+ vi.mocked(execSync).mockReturnValue("1.0.2" as any);
202
+
203
+ const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
204
+
205
+ await checkForUpdates("1.0.0");
206
+ await vi.runAllTimersAsync();
207
+
208
+ // 即使刚检查过,有新版本时也应该继续检查
209
+ expect(writeFileSync).toHaveBeenCalled();
210
+
211
+ consoleSpy.mockRestore();
212
+ });
213
+
192
214
  it("缓存文件损坏时应该静默处理", async () => {
193
215
  vi.mocked(existsSync).mockReturnValue(true);
194
216
  vi.mocked(readFileSync).mockReturnValue("invalid json");