@miphamai/cli 0.31.0 → 0.31.1
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 +8 -3
- package/package.json +1 -1
- package/src/shared/update.ts +14 -3
package/bin/mipham.ts
CHANGED
|
@@ -208,8 +208,10 @@ 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 with retry + mirror fallback
|
|
211
|
+
// Check latest version from npm with retry + mirror fallback.
|
|
212
|
+
// Track which registry responded so we can use it for the install step too.
|
|
212
213
|
let latestVersion = ''
|
|
214
|
+
let workingRegistry = 'https://registry.npmjs.org/' // default
|
|
213
215
|
try {
|
|
214
216
|
const { execSync } = await import('node:child_process')
|
|
215
217
|
|
|
@@ -233,7 +235,10 @@ async function runUpdate(): Promise<boolean> {
|
|
|
233
235
|
{ encoding: 'utf-8', timeout: attempt.timeout, stdio: ['pipe', 'pipe', 'pipe'] },
|
|
234
236
|
).trim()
|
|
235
237
|
latestVersion = result.replace(/"/g, '')
|
|
236
|
-
if (latestVersion)
|
|
238
|
+
if (latestVersion) {
|
|
239
|
+
workingRegistry = attempt.registry
|
|
240
|
+
break
|
|
241
|
+
}
|
|
237
242
|
} catch (err) {
|
|
238
243
|
lastError = err instanceof Error ? err : new Error(String(err))
|
|
239
244
|
// Continue to next attempt
|
|
@@ -278,7 +283,7 @@ async function runUpdate(): Promise<boolean> {
|
|
|
278
283
|
console.log()
|
|
279
284
|
console.log(`Updating ${PACKAGE} to v${latestVersion}...`)
|
|
280
285
|
|
|
281
|
-
const ok = performUpdate(latestVersion)
|
|
286
|
+
const ok = performUpdate(latestVersion, workingRegistry)
|
|
282
287
|
if (!ok) {
|
|
283
288
|
console.log()
|
|
284
289
|
console.log(`✗ Update failed.`)
|
package/package.json
CHANGED
package/src/shared/update.ts
CHANGED
|
@@ -139,19 +139,30 @@ function isValidSemver(v: string): boolean {
|
|
|
139
139
|
/**
|
|
140
140
|
* Perform the actual update via npm install -g.
|
|
141
141
|
* Validates the version string before shell execution (prevents command injection).
|
|
142
|
+
*
|
|
143
|
+
* @param version Target semver version (must pass isValidSemver).
|
|
144
|
+
* @param registry Optional npm registry URL — when provided the install uses it
|
|
145
|
+
* directly (useful when the primary registry is slow, e.g. China).
|
|
146
|
+
* Falls back to the npm default if omitted.
|
|
142
147
|
* Returns true on success.
|
|
143
148
|
*/
|
|
144
|
-
export function performUpdate(version: string): boolean {
|
|
149
|
+
export function performUpdate(version: string, registry?: string): boolean {
|
|
145
150
|
if (!isValidSemver(version)) {
|
|
146
151
|
process.stderr.write(`⚠ Refusing to install invalid version: "${version}"\n`)
|
|
147
152
|
return false
|
|
148
153
|
}
|
|
149
154
|
|
|
155
|
+
// Sanitize registry — only allow known URLs to prevent command injection
|
|
156
|
+
const allowedRegistries = REGISTRIES.map((r) => r.url)
|
|
157
|
+
const safeRegistry = registry && allowedRegistries.includes(registry) ? registry : undefined
|
|
158
|
+
|
|
159
|
+
const registryFlag = safeRegistry ? ` --registry=${safeRegistry}` : ''
|
|
160
|
+
|
|
150
161
|
try {
|
|
151
|
-
execSync(`npm install -g ${PACKAGE}@${version}`, {
|
|
162
|
+
execSync(`npm install -g ${PACKAGE}@${version}${registryFlag}`, {
|
|
152
163
|
encoding: 'utf-8',
|
|
153
164
|
stdio: 'inherit',
|
|
154
|
-
timeout:
|
|
165
|
+
timeout: 600_000, // 10 min — slow networks (e.g. China → npmjs) may need 7+ min
|
|
155
166
|
})
|
|
156
167
|
return true
|
|
157
168
|
} catch {
|