easy-claude 0.2.6 → 0.2.7
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/CHANGELOG.md +10 -0
- package/README.md +11 -4
- package/native/Overlay.swift +90 -25
- package/native/build/EasyClaudeOverlay.app/Contents/Info.plist +1 -1
- package/native/build/EasyClaudeOverlay.app/Contents/MacOS/EasyClaudeOverlay +0 -0
- package/package.json +3 -2
- package/src/overlay-update.js +5 -3
- package/src/update-lock.js +22 -0
- package/src/update.js +7 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# 更新记录
|
|
2
2
|
|
|
3
|
+
## v0.2.7(2026-10-21)
|
|
4
|
+
|
|
5
|
+
- 修复旧浮窗退出后日志清理失效;检查失败保留上次成功的新版提示并显示检查失败状态。
|
|
6
|
+
|
|
7
|
+
- 新增菜单“自动更新”开关,默认关闭并保存选择;开启后每日检查到新版自动安装并重开,失败只在菜单提示。更新锁防止同一安装前缀的多个Easy更新进程并发安装。
|
|
8
|
+
|
|
9
|
+
- 额度菜单重置时间跟随系统12/24小时制及AM/PM显示,保持原时区、日期和倒计时规则。
|
|
10
|
+
|
|
11
|
+
- 更新前在线判断版本,已最新时提示“已经是最新版本”,不安装也不重开;有新版并安装成功才自动重开。
|
|
12
|
+
|
|
3
13
|
## v0.2.6(2026-10-19)
|
|
4
14
|
|
|
5
15
|
- 菜单显示“更新版本·当前版本号”,每天检查一次官方 npm,有新版时右侧显示绿色圆点;检查记录跨重启保存,只提示、不自动安装。
|
package/README.md
CHANGED
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
需要 Node.js 22+ 与 Claude Code 订阅登录;不支持把 API Key 账单余额当作订阅额度。
|
|
6
6
|
|
|
7
|
-
## v0.2.
|
|
7
|
+
## v0.2.7 更新说明
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
9
|
+
- 新增“自动更新”开关,默认关闭;开启后每天检查,有新版才安装并自动重开,保留设置。
|
|
10
|
+
- 已是最新版本时仅提示,不重复安装或重开;同一安装目录的更新互斥执行。
|
|
11
|
+
- 额度菜单重置时间跟随系统12/24小时制及AM/PM。
|
|
12
|
+
- 修复更新重开后临时日志残留;检查失败保留已知新版绿点并显示失败提示。
|
|
12
13
|
|
|
13
14
|
历史版本更新记录见 [CHANGELOG](CHANGELOG.md)。
|
|
14
15
|
|
|
@@ -73,3 +74,9 @@ npm run build:overlay
|
|
|
73
74
|
命令更新:`easy-claude update`,成功后会自动退出旧浮窗并打开新版。旧版本尚未提供 update 时,可先执行 `npm i -g easy-claude@latest`。更新过程中需要正常网络及原安装目录写入权限,不会自动使用 sudo。
|
|
74
75
|
|
|
75
76
|
菜单中的“更新版本”会后台检查并更新,成功后自动重开浮窗;失败时保留当前浮窗并提示原因。更新期间不能重复点击,已保存设置保持不变。
|
|
77
|
+
|
|
78
|
+
更新前会在线核对版本;已经是最新版本时只提示,不重复安装或重开浮窗。有更高正式版本并安装成功后,才自动重开。
|
|
79
|
+
|
|
80
|
+
额度菜单的重置时刻跟随系统的12/24小时制和AM/PM格式,时区仍使用电脑系统时区。
|
|
81
|
+
|
|
82
|
+
菜单“自动更新”默认关闭,开启后在浮窗运行期间每天检查一次,有新正式版本时自动安装并重开,保留设置;关闭后仍每天检查并显示新版提示,但不自动安装。开启时如已发现新版,会立即更新。自动更新失败仅在菜单提示,可点击“更新版本”查看原因或重试。
|
package/native/Overlay.swift
CHANGED
|
@@ -158,15 +158,19 @@ func versionCheckDue(lastAttempt: Double, checkedVersion: String?, currentVersio
|
|
|
158
158
|
checkedVersion != currentVersion || now - lastAttempt >= 86400
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
func resetDescription(_ resetsAt: Double?, weekly: Bool, now: Date = Date(), zone: TimeZone = .current
|
|
161
|
+
func resetDescription(_ resetsAt: Double?, weekly: Bool, now: Date = Date(), zone: TimeZone = .current,
|
|
162
|
+
locale: Locale = .autoupdatingCurrent) -> String {
|
|
162
163
|
guard let resetsAt, resetsAt.isFinite, abs(resetsAt) < 8.64e12 else { return "重置时间未知" }
|
|
163
164
|
let remaining = resetsAt - now.timeIntervalSince1970
|
|
164
165
|
guard remaining > 0 else { return "已到重置时间,等待更新" }
|
|
165
166
|
let formatter = DateFormatter()
|
|
166
|
-
formatter.locale =
|
|
167
|
+
formatter.locale = locale
|
|
167
168
|
formatter.timeZone = zone
|
|
168
|
-
formatter.
|
|
169
|
-
let
|
|
169
|
+
formatter.setLocalizedDateFormatFromTemplate("jm")
|
|
170
|
+
let date = Date(timeIntervalSince1970: resetsAt)
|
|
171
|
+
let clock = formatter.string(from: date)
|
|
172
|
+
formatter.dateFormat = "d'日'"
|
|
173
|
+
let time = weekly ? "\(formatter.string(from: date)) \(clock)" : clock
|
|
170
174
|
let duration: String
|
|
171
175
|
if weekly {
|
|
172
176
|
if remaining >= 86400 { duration = "\(Int(remaining / 86400))天" }
|
|
@@ -233,6 +237,8 @@ final class Overlay: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|
|
233
237
|
var updater: Process?
|
|
234
238
|
var versionCheck: Process?
|
|
235
239
|
var updateAvailable = false
|
|
240
|
+
var automaticUpdateError: String?
|
|
241
|
+
var versionCheckError: String?
|
|
236
242
|
let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "未知"
|
|
237
243
|
var snapshot: Snapshot?
|
|
238
244
|
var error: String?
|
|
@@ -433,6 +439,11 @@ final class Overlay: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|
|
433
439
|
updateItem.setAccessibilityHelp("有新版本可更新")
|
|
434
440
|
}
|
|
435
441
|
menu.addItem(updateItem)
|
|
442
|
+
let automatic = item("自动更新", #selector(toggleAutomaticUpdates))
|
|
443
|
+
automatic.state = UserDefaults.standard.bool(forKey: "automaticUpdatesEnabled") ? .on : .off
|
|
444
|
+
menu.addItem(automatic)
|
|
445
|
+
if let automaticUpdateError { menu.addItem(item(automaticUpdateError, nil)) }
|
|
446
|
+
if let versionCheckError { menu.addItem(item(versionCheckError, nil)) }
|
|
436
447
|
menu.addItem(item("退出浮窗", #selector(quit)))
|
|
437
448
|
return menu
|
|
438
449
|
}
|
|
@@ -499,10 +510,13 @@ final class Overlay: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|
|
499
510
|
guard versionCheck == nil, !script.isEmpty,
|
|
500
511
|
versionCheckDue(lastAttempt: last, checkedVersion: prefs.string(forKey: "updateCheckedVersion"),
|
|
501
512
|
currentVersion: appVersion) else { return }
|
|
513
|
+
if prefs.string(forKey: "updateCheckedVersion") != appVersion {
|
|
514
|
+
prefs.set(false, forKey: "updateAvailable")
|
|
515
|
+
updateAvailable = false
|
|
516
|
+
}
|
|
502
517
|
prefs.set(appVersion, forKey: "updateCheckedVersion")
|
|
503
518
|
prefs.set(Date().timeIntervalSince1970, forKey: "updateLastAttempt")
|
|
504
|
-
|
|
505
|
-
updateAvailable = false
|
|
519
|
+
versionCheckError = nil
|
|
506
520
|
let process = Process(), pipe = Pipe()
|
|
507
521
|
process.executableURL = URL(fileURLWithPath: node)
|
|
508
522
|
process.arguments = [URL(fileURLWithPath: script).deletingLastPathComponent()
|
|
@@ -515,20 +529,36 @@ final class Overlay: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|
|
515
529
|
process.terminationHandler = { [weak self] process in
|
|
516
530
|
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
|
517
531
|
let value = try? JSONSerialization.jsonObject(with: data) as? [String: Any]
|
|
518
|
-
let available = process.terminationStatus == 0
|
|
532
|
+
let available = process.terminationStatus == 0 ? value?["available"] as? Bool : nil
|
|
519
533
|
Task { @MainActor in
|
|
520
534
|
guard let self else { return }
|
|
521
535
|
self.versionCheck = nil
|
|
536
|
+
guard let available else {
|
|
537
|
+
self.versionCheckError = "版本检查失败,保留上次结果"
|
|
538
|
+
return
|
|
539
|
+
}
|
|
540
|
+
self.versionCheckError = nil
|
|
522
541
|
self.updateAvailable = available
|
|
523
542
|
UserDefaults.standard.set(available, forKey: "updateAvailable")
|
|
543
|
+
if available && UserDefaults.standard.bool(forKey: "automaticUpdatesEnabled") {
|
|
544
|
+
self.startUpdate(automatic: true)
|
|
545
|
+
}
|
|
524
546
|
}
|
|
525
547
|
}
|
|
526
548
|
versionCheck = process
|
|
527
549
|
do { try process.run() }
|
|
528
|
-
catch { versionCheck = nil }
|
|
550
|
+
catch { versionCheck = nil; versionCheckError = "版本检查失败,保留上次结果" }
|
|
551
|
+
}
|
|
552
|
+
@objc func toggleAutomaticUpdates() {
|
|
553
|
+
let prefs = UserDefaults.standard
|
|
554
|
+
let enabled = !prefs.bool(forKey: "automaticUpdatesEnabled")
|
|
555
|
+
prefs.set(enabled, forKey: "automaticUpdatesEnabled")
|
|
556
|
+
if enabled && updateAvailable { startUpdate(automatic: true) }
|
|
529
557
|
}
|
|
530
|
-
@objc func updateVersion() {
|
|
558
|
+
@objc func updateVersion() { startUpdate(automatic: false) }
|
|
559
|
+
func startUpdate(automatic: Bool) {
|
|
531
560
|
guard updater == nil else { return }
|
|
561
|
+
automaticUpdateError = nil
|
|
532
562
|
let cli = URL(fileURLWithPath: script).deletingLastPathComponent()
|
|
533
563
|
.appendingPathComponent("overlay-update.js")
|
|
534
564
|
let process = Process()
|
|
@@ -540,22 +570,35 @@ final class Overlay: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|
|
540
570
|
// npm 输出可能超过管道容量,写临时文件以免等待退出时死锁。
|
|
541
571
|
let output = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
542
572
|
guard FileManager.default.createFile(atPath: output.path, contents: nil,
|
|
543
|
-
attributes: [.posixPermissions: 0o600]), let handle = try? FileHandle(
|
|
544
|
-
|
|
573
|
+
attributes: [.posixPermissions: 0o600]), let handle = try? FileHandle(forUpdating: output) else {
|
|
574
|
+
if automatic { automaticUpdateError = "自动更新失败:无法创建更新日志" }
|
|
575
|
+
else { showUpdateResult("无法创建更新日志,请检查临时目录权限。") }
|
|
576
|
+
return
|
|
577
|
+
}
|
|
578
|
+
// 先取消文件名,描述符仍可读写;旧浮窗退出后也不会遗留日志。
|
|
579
|
+
do { try FileManager.default.removeItem(at: output) }
|
|
580
|
+
catch {
|
|
581
|
+
try? handle.close()
|
|
582
|
+
if automatic { automaticUpdateError = "自动更新失败:无法清理临时日志" }
|
|
583
|
+
else { showUpdateResult("无法清理临时更新日志,请检查临时目录权限。") }
|
|
545
584
|
return
|
|
546
585
|
}
|
|
547
586
|
process.standardOutput = handle
|
|
548
587
|
process.standardError = handle
|
|
549
588
|
process.terminationHandler = { [weak self] process in
|
|
589
|
+
try? handle.seek(toOffset: 0)
|
|
590
|
+
let data = (try? handle.readToEnd()) ?? Data()
|
|
550
591
|
try? handle.close()
|
|
551
|
-
let data = (try? Data(contentsOf: output)) ?? Data()
|
|
552
|
-
try? FileManager.default.removeItem(at: output)
|
|
553
592
|
let message = String(decoding: data.suffix(4096), as: UTF8.self)
|
|
554
593
|
Task { @MainActor in
|
|
555
594
|
guard let self else { return }
|
|
556
595
|
self.updater = nil
|
|
557
|
-
|
|
558
|
-
|
|
596
|
+
if automatic {
|
|
597
|
+
if process.terminationStatus != 0 { self.automaticUpdateError = "自动更新失败,请点击更新版本查看原因" }
|
|
598
|
+
} else {
|
|
599
|
+
self.showUpdateResult(process.terminationStatus == 0
|
|
600
|
+
? message : "更新失败,请检查网络或安装目录权限。\n" + message)
|
|
601
|
+
}
|
|
559
602
|
}
|
|
560
603
|
}
|
|
561
604
|
updater = process
|
|
@@ -563,8 +606,8 @@ final class Overlay: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|
|
563
606
|
catch {
|
|
564
607
|
updater = nil
|
|
565
608
|
try? handle.close()
|
|
566
|
-
|
|
567
|
-
showUpdateResult("无法启动更新,请重新运行 easy-claude show。")
|
|
609
|
+
if automatic { automaticUpdateError = "自动更新失败:无法启动更新" }
|
|
610
|
+
else { showUpdateResult("无法启动更新,请重新运行 easy-claude show。") }
|
|
568
611
|
}
|
|
569
612
|
}
|
|
570
613
|
func showUpdateResult(_ message: String) {
|
|
@@ -588,6 +631,14 @@ final class Overlay: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
|
|
588
631
|
struct Main {
|
|
589
632
|
@MainActor static func main() {
|
|
590
633
|
if CommandLine.arguments.contains("--self-test") {
|
|
634
|
+
let preferenceSuite = "easy-auto-update-self-test-" + UUID().uuidString
|
|
635
|
+
let preferences = UserDefaults(suiteName: preferenceSuite)!
|
|
636
|
+
precondition(!preferences.bool(forKey: "automaticUpdatesEnabled"))
|
|
637
|
+
preferences.set(true, forKey: "automaticUpdatesEnabled")
|
|
638
|
+
precondition(UserDefaults(suiteName: preferenceSuite)!.bool(forKey: "automaticUpdatesEnabled"))
|
|
639
|
+
preferences.set(false, forKey: "automaticUpdatesEnabled")
|
|
640
|
+
precondition(!preferences.bool(forKey: "automaticUpdatesEnabled"))
|
|
641
|
+
preferences.removePersistentDomain(forName: preferenceSuite)
|
|
591
642
|
precondition(!versionCheckDue(lastAttempt: 100, checkedVersion: "1.0.0", currentVersion: "1.0.0", now: 86499))
|
|
592
643
|
precondition(versionCheckDue(lastAttempt: 100, checkedVersion: "1.0.0", currentVersion: "1.0.0", now: 86500))
|
|
593
644
|
precondition(versionCheckDue(lastAttempt: 100, checkedVersion: nil, currentVersion: "1.0.0", now: 100))
|
|
@@ -596,17 +647,31 @@ struct Main {
|
|
|
596
647
|
precondition(ClockCity.presets.allSatisfy { TimeZone(identifier: $0.identifier) != nil })
|
|
597
648
|
let countdownStart = ISO8601DateFormatter().date(from: "2026-10-15T04:57:00Z")!
|
|
598
649
|
let countdownUTC = TimeZone(secondsFromGMT: 0)!
|
|
599
|
-
|
|
650
|
+
let afternoon = ISO8601DateFormatter().date(from: "2026-10-21T14:30:00Z")!
|
|
651
|
+
let morning = ISO8601DateFormatter().date(from: "2026-10-21T02:30:00Z")!
|
|
652
|
+
for weekly in [false, true] {
|
|
653
|
+
let us = resetDescription(afternoon.timeIntervalSince1970, weekly: weekly,
|
|
654
|
+
now: morning, zone: countdownUTC, locale: Locale(identifier: "en_US"))
|
|
655
|
+
precondition(us.contains("2:30") && us.contains("PM") && !us.contains("14:30"))
|
|
656
|
+
precondition(!weekly || us.contains("21日"))
|
|
657
|
+
let gb = resetDescription(afternoon.timeIntervalSince1970, weekly: weekly,
|
|
658
|
+
now: morning, zone: countdownUTC, locale: Locale(identifier: "en_GB"))
|
|
659
|
+
precondition(gb.contains("14:30") && !gb.contains("PM"))
|
|
660
|
+
let am = resetDescription(morning.timeIntervalSince1970, weekly: weekly,
|
|
661
|
+
now: morning.addingTimeInterval(-60), zone: countdownUTC, locale: Locale(identifier: "en_US"))
|
|
662
|
+
precondition(am.contains("2:30") && am.contains("AM"))
|
|
663
|
+
}
|
|
664
|
+
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 10920, weekly: false, now: countdownStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "3小时2分后 07:59 重置")
|
|
600
665
|
let weekStart = ISO8601DateFormatter().date(from: "2026-10-14T08:03:00Z")!
|
|
601
|
-
precondition(resetDescription(weekStart.timeIntervalSince1970 + 345360, weekly: true, now: weekStart, zone: countdownUTC) == "3天后 18日 07:59 重置")
|
|
666
|
+
precondition(resetDescription(weekStart.timeIntervalSince1970 + 345360, weekly: true, now: weekStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "3天后 18日 07:59 重置")
|
|
602
667
|
precondition(resetDescription(nil, weekly: true) == "重置时间未知")
|
|
603
668
|
precondition(resetDescription(countdownStart.timeIntervalSince1970, weekly: false, now: countdownStart) == "已到重置时间,等待更新")
|
|
604
|
-
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 1, weekly: false, now: countdownStart, zone: countdownUTC) == "1分后 04:57 重置")
|
|
605
|
-
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 86160, weekly: true, now: countdownStart, zone: countdownUTC) == "23小时后 16日 04:53 重置")
|
|
606
|
-
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 120, weekly: true, now: countdownStart, zone: countdownUTC) == "2分钟后 15日 04:59 重置")
|
|
607
|
-
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 86400, weekly: true, now: countdownStart, zone: countdownUTC) == "1天后 16日 04:57 重置")
|
|
608
|
-
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 86399, weekly: true, now: countdownStart, zone: countdownUTC) == "23小时后 16日 04:56 重置")
|
|
609
|
-
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 3599, weekly: false, now: countdownStart, zone: countdownUTC) == "1小时后 05:56 重置")
|
|
669
|
+
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 1, weekly: false, now: countdownStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "1分后 04:57 重置")
|
|
670
|
+
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 86160, weekly: true, now: countdownStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "23小时后 16日 04:53 重置")
|
|
671
|
+
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 120, weekly: true, now: countdownStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "2分钟后 15日 04:59 重置")
|
|
672
|
+
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 86400, weekly: true, now: countdownStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "1天后 16日 04:57 重置")
|
|
673
|
+
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 86399, weekly: true, now: countdownStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "23小时后 16日 04:56 重置")
|
|
674
|
+
precondition(resetDescription(countdownStart.timeIntervalSince1970 + 3599, weekly: false, now: countdownStart, zone: countdownUTC, locale: Locale(identifier: "en_GB")) == "1小时后 05:56 重置")
|
|
610
675
|
let testRing = RingView()
|
|
611
676
|
testRing.showFiveHourRing = false
|
|
612
677
|
precondition(testRing.displaysWeeklyPercentage)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<key>CFBundleIdentifier</key><string>com.unionschool.easy-claude.overlay</string>
|
|
6
6
|
<key>CFBundleName</key><string>Easy Claude Overlay</string>
|
|
7
7
|
<key>CFBundlePackageType</key><string>APPL</string>
|
|
8
|
-
<key>CFBundleShortVersionString</key><string>0.2.
|
|
8
|
+
<key>CFBundleShortVersionString</key><string>0.2.7</string>
|
|
9
9
|
<key>LSMinimumSystemVersion</key><string>14.0</string>
|
|
10
10
|
<key>LSUIElement</key><true/>
|
|
11
11
|
<key>NSHighResolutionCapable</key><true/>
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-claude",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Claude Code quota rings and transparent world clock for Claude desktop",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"scripts": {
|
|
22
22
|
"test": "node --test",
|
|
23
23
|
"start": "node bin/easy-claude.js",
|
|
24
|
-
"build:overlay": "node scripts/build-overlay.mjs"
|
|
24
|
+
"build:overlay": "node scripts/build-overlay.mjs",
|
|
25
|
+
"test:native": "python3 scripts/test-native-update.py"
|
|
25
26
|
},
|
|
26
27
|
"license": "MIT",
|
|
27
28
|
"author": "UnionSchool",
|
package/src/overlay-update.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { pathToFileURL } from 'node:url';
|
|
2
2
|
import { updatePackage } from './update.js';
|
|
3
|
+
import { withUpdateLock } from './update-lock.js';
|
|
3
4
|
|
|
4
5
|
export async function updateAndShow({ update = updatePackage,
|
|
5
|
-
show = async () => (await import('./overlay.js')).launchOverlay() } = {}) {
|
|
6
|
-
await
|
|
7
|
-
|
|
6
|
+
show = async () => (await import('./overlay.js')).launchOverlay(), lock = withUpdateLock } = {}) {
|
|
7
|
+
await lock(async () => {
|
|
8
|
+
if (await update('easy-claude')) await show();
|
|
9
|
+
});
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { mkdir, rmdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { installedPrefix } from './update.js';
|
|
7
|
+
|
|
8
|
+
const root = fileURLToPath(new URL('../', import.meta.url));
|
|
9
|
+
const prefix = installedPrefix(root) ?? root;
|
|
10
|
+
const key = createHash('sha256').update(prefix).digest('hex').slice(0, 24);
|
|
11
|
+
const defaultLock = join(tmpdir(), `easy-tools-update-${key}.lock`);
|
|
12
|
+
|
|
13
|
+
export async function withUpdateLock(action, { lock = defaultLock } = {}) {
|
|
14
|
+
try { await mkdir(lock, { mode: 0o700 }); }
|
|
15
|
+
catch (error) {
|
|
16
|
+
// shortcut: 异常强退可能留下锁;为避免误删在用锁,不自动按时间清除。
|
|
17
|
+
if (error.code === 'EEXIST') throw new Error(`另一个 Easy 工具正在更新。若确认无更新进程,可删除遗留锁目录后重试:${lock}`);
|
|
18
|
+
throw new Error('无法建立更新锁,请检查临时目录权限。');
|
|
19
|
+
}
|
|
20
|
+
try { return await action(); }
|
|
21
|
+
finally { await rmdir(lock); }
|
|
22
|
+
}
|
package/src/update.js
CHANGED
|
@@ -54,14 +54,17 @@ function runNpm(command, args) {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
export async function updatePackage(name, { root = fileURLToPath(new URL('../', import.meta.url)),
|
|
57
|
-
getNpmCli = findNpmCli, getLatest = readLatest,
|
|
57
|
+
getNpmCli = findNpmCli, getLatest = readLatest,
|
|
58
|
+
currentVersion = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version,
|
|
58
59
|
run = runNpm, platform = process.platform } = {}) {
|
|
59
60
|
if (!['easy-codex', 'easy-claude'].includes(name)) throw new Error('不支持的更新包名。');
|
|
60
61
|
const npmCli = await getNpmCli();
|
|
61
62
|
const latest = await getLatest(npmCli, name);
|
|
62
63
|
if (!isNewer(latest, currentVersion)) {
|
|
63
|
-
console.log(
|
|
64
|
-
|
|
64
|
+
console.log(currentVersion === latest
|
|
65
|
+
? `${name} 已经是最新版本(${currentVersion})。`
|
|
66
|
+
: `${name} 当前版本 ${currentVersion} 高于 npm 最新版本 ${latest},无需更新。`);
|
|
67
|
+
return false;
|
|
65
68
|
}
|
|
66
69
|
const prefix = installedPrefix(root, platform);
|
|
67
70
|
// 先在线检查再锁定版本,避免安装阶段复用旧 latest 缓存而降级。
|
|
@@ -69,6 +72,7 @@ export async function updatePackage(name, { root = fileURLToPath(new URL('../',
|
|
|
69
72
|
'--prefer-online', '--registry=https://registry.npmjs.org', `${name}@${latest}`];
|
|
70
73
|
await run(process.execPath, args);
|
|
71
74
|
console.log(`${name} 已更新到 ${latest}。`);
|
|
75
|
+
return true;
|
|
72
76
|
}
|
|
73
77
|
|
|
74
78
|
export async function checkUpdate(name, { getNpmCli = findNpmCli, getLatest = readLatest,
|