@miphamai/cli 0.7.9 → 0.7.10
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/bin/mipham.ts +38 -11
- package/package.json +1 -1
- package/src/shared/update.ts +41 -4
package/bin/mipham.ts
CHANGED
|
@@ -208,25 +208,52 @@ async function runUpdate(): Promise<boolean> {
|
|
|
208
208
|
console.log(` Checking npm registry for latest version...`)
|
|
209
209
|
console.log()
|
|
210
210
|
|
|
211
|
-
// Check latest version from npm
|
|
211
|
+
// Check latest version from npm with retry + mirror fallback
|
|
212
212
|
let latestVersion = ''
|
|
213
213
|
try {
|
|
214
214
|
const { execSync } = await import('node:child_process')
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
215
|
+
|
|
216
|
+
// Registry fallback chain: npm default → npmmirror (China mirror)
|
|
217
|
+
const attempts: Array<{ label: string; registry: string; timeout: number }> = [
|
|
218
|
+
{ label: 'npm registry', registry: 'https://registry.npmjs.org/', timeout: 10_000 },
|
|
219
|
+
{ label: 'npm registry (retry)', registry: 'https://registry.npmjs.org/', timeout: 20_000 },
|
|
220
|
+
{
|
|
221
|
+
label: 'npmmirror (China mirror)',
|
|
222
|
+
registry: 'https://registry.npmmirror.com/',
|
|
223
|
+
timeout: 15_000,
|
|
224
|
+
},
|
|
225
|
+
]
|
|
226
|
+
|
|
227
|
+
let lastError: Error | null = null
|
|
228
|
+
|
|
229
|
+
for (const attempt of attempts) {
|
|
230
|
+
try {
|
|
231
|
+
const result = execSync(
|
|
232
|
+
`npm view ${PACKAGE} version --json --registry=${attempt.registry}`,
|
|
233
|
+
{ encoding: 'utf-8', timeout: attempt.timeout, stdio: ['pipe', 'pipe', 'pipe'] },
|
|
234
|
+
).trim()
|
|
235
|
+
latestVersion = result.replace(/"/g, '')
|
|
236
|
+
if (latestVersion) break
|
|
237
|
+
} catch (err) {
|
|
238
|
+
lastError = err instanceof Error ? err : new Error(String(err))
|
|
239
|
+
// Continue to next attempt
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!latestVersion && lastError) throw lastError
|
|
244
|
+
} catch (_err: unknown) {
|
|
245
|
+
console.log(`✗ Could not reach npm registry (network timeout)`)
|
|
246
|
+
console.log()
|
|
247
|
+
console.log(` Manual update:`)
|
|
248
|
+
console.log(` npm install -g ${PACKAGE}@latest`)
|
|
249
|
+
console.log()
|
|
250
|
+
console.log(` Or check: https://www.npmjs.com/package/${PACKAGE}`)
|
|
225
251
|
process.exit(1)
|
|
226
252
|
}
|
|
227
253
|
|
|
228
254
|
if (!latestVersion) {
|
|
229
255
|
console.log('✗ Could not determine latest version.')
|
|
256
|
+
console.log(` Check manually: https://www.npmjs.com/package/${PACKAGE}`)
|
|
230
257
|
process.exit(1)
|
|
231
258
|
}
|
|
232
259
|
|
package/package.json
CHANGED
package/src/shared/update.ts
CHANGED
|
@@ -16,6 +16,12 @@ const HOME = homedir()
|
|
|
16
16
|
const MIPHAM_HOME = join(HOME, '.mipham')
|
|
17
17
|
const CONFIG_PATH = join(MIPHAM_HOME, 'config.yml')
|
|
18
18
|
|
|
19
|
+
// Registry fallback chain: npm default → npmmirror (China mirror)
|
|
20
|
+
const REGISTRIES = [
|
|
21
|
+
{ name: 'npm', url: 'https://registry.npmjs.org/' },
|
|
22
|
+
{ name: 'npmmirror', url: 'https://registry.npmmirror.com/' },
|
|
23
|
+
]
|
|
24
|
+
|
|
19
25
|
export interface UpdateCheck {
|
|
20
26
|
/** Current installed version */
|
|
21
27
|
current: string
|
|
@@ -43,18 +49,49 @@ export function getCurrentVersion(): string {
|
|
|
43
49
|
}
|
|
44
50
|
|
|
45
51
|
/**
|
|
46
|
-
*
|
|
52
|
+
* Run `npm view` against a specific registry with a given timeout.
|
|
47
53
|
* Returns the version string, or throws on failure.
|
|
48
54
|
*/
|
|
49
|
-
function
|
|
50
|
-
const result = execSync(`npm view ${PACKAGE} version --json`, {
|
|
55
|
+
function tryRegistry(registry: string, timeoutMs: number): string {
|
|
56
|
+
const result = execSync(`npm view ${PACKAGE} version --json --registry=${registry}`, {
|
|
51
57
|
encoding: 'utf-8',
|
|
52
|
-
timeout:
|
|
58
|
+
timeout: timeoutMs,
|
|
53
59
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
54
60
|
}).trim()
|
|
55
61
|
return result.replace(/"/g, '')
|
|
56
62
|
}
|
|
57
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Fetch the latest version from the npm registry with retry + mirror fallback.
|
|
66
|
+
*
|
|
67
|
+
* Strategy:
|
|
68
|
+
* 1. Try npm default registry (10s timeout)
|
|
69
|
+
* 2. Retry npm default registry (20s timeout)
|
|
70
|
+
* 3. Fall back to npmmirror.com — China mirror (15s timeout)
|
|
71
|
+
*
|
|
72
|
+
* Returns the version string, or throws on all failures.
|
|
73
|
+
*/
|
|
74
|
+
function fetchLatestVersion(): string {
|
|
75
|
+
const attempts: Array<{ registry: string; timeout: number }> = [
|
|
76
|
+
{ registry: REGISTRIES[0]!.url, timeout: 10_000 },
|
|
77
|
+
{ registry: REGISTRIES[0]!.url, timeout: 20_000 },
|
|
78
|
+
{ registry: REGISTRIES[1]!.url, timeout: 15_000 },
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
let lastError: Error | null = null
|
|
82
|
+
|
|
83
|
+
for (const attempt of attempts) {
|
|
84
|
+
try {
|
|
85
|
+
return tryRegistry(attempt.registry, attempt.timeout)
|
|
86
|
+
} catch (err) {
|
|
87
|
+
lastError = err instanceof Error ? err : new Error(String(err))
|
|
88
|
+
// Continue to next attempt
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
throw lastError ?? new Error('Failed to fetch latest version')
|
|
93
|
+
}
|
|
94
|
+
|
|
58
95
|
/**
|
|
59
96
|
* Check if an update is available by comparing the local version
|
|
60
97
|
* against the npm registry.
|