@myapihq/cli 1.0.72 → 1.0.74
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/commands/update.js +49 -16
- package/package.json +1 -1
package/dist/commands/update.js
CHANGED
|
@@ -1,11 +1,48 @@
|
|
|
1
1
|
import { execSync } from 'child_process';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
2
3
|
import { loadConfig } from '../config.js';
|
|
3
4
|
import { info, success } from '../output.js';
|
|
4
5
|
import { installSkills } from './setup.js';
|
|
5
6
|
const REGISTRY_URL = 'https://registry.npmjs.org/@myapihq/cli/latest';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const PACKUMENT_URL = 'https://registry.npmjs.org/@myapihq/cli';
|
|
8
|
+
// Verify the version is listed in the full packument — the same data source
|
|
9
|
+
// `npm install` consults. The /latest endpoint and the tarball URL are on
|
|
10
|
+
// different CDN caches and can update before the packument does, causing
|
|
11
|
+
// `npm install` to fail with ETARGET. This check eliminates that race.
|
|
12
|
+
async function isVersionInstallable(version) {
|
|
13
|
+
try {
|
|
14
|
+
const res = await fetch(PACKUMENT_URL, {
|
|
15
|
+
headers: { Accept: 'application/vnd.npm.install-v1+json' },
|
|
16
|
+
signal: AbortSignal.timeout(5000),
|
|
17
|
+
});
|
|
18
|
+
if (!res.ok)
|
|
19
|
+
return false;
|
|
20
|
+
const data = await res.json();
|
|
21
|
+
return Boolean(data.versions && data.versions[version]);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Resolve the npm binary and global prefix to use for self-update.
|
|
28
|
+
// We install to the same prefix where the current myapi binary lives,
|
|
29
|
+
// so the update actually replaces the running binary.
|
|
30
|
+
// `--prefer-online` forces npm to revalidate its local packument cache
|
|
31
|
+
// against the registry — without it, a stale local cache can also produce
|
|
32
|
+
// ETARGET even when the registry itself has the version.
|
|
33
|
+
function npmInstallArgs(version) {
|
|
34
|
+
const npmCandidate = process.execPath.replace(/[/\\]node(\.exe)?$/, '/npm');
|
|
35
|
+
const bin = existsSync(npmCandidate) ? npmCandidate : 'npm';
|
|
36
|
+
const baseArgs = ['install', '-g', '--prefer-online', `@myapihq/cli@${version}`];
|
|
37
|
+
try {
|
|
38
|
+
const self = process.argv[1];
|
|
39
|
+
const binDir = self.replace(/[/\\][^/\\]+$/, '');
|
|
40
|
+
const prefix = binDir.replace(/[/\\]bin$/, '');
|
|
41
|
+
return { bin, args: ['install', '-g', '--prefer-online', '--prefix', prefix, `@myapihq/cli@${version}`] };
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return { bin, args: baseArgs };
|
|
45
|
+
}
|
|
9
46
|
}
|
|
10
47
|
// checkForUpdate runs silently in the background on every command.
|
|
11
48
|
// Auto-installs if a newer version is available.
|
|
@@ -17,20 +54,15 @@ export async function checkForUpdate(currentVersion) {
|
|
|
17
54
|
const data = await res.json();
|
|
18
55
|
const latest = data.version;
|
|
19
56
|
if (latest && isNewer(latest, currentVersion)) {
|
|
20
|
-
// Verify the
|
|
21
|
-
// the
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const check = await fetch(tarballUrl, { method: 'HEAD', signal: AbortSignal.timeout(3000) });
|
|
25
|
-
if (!check.ok)
|
|
26
|
-
return; // Tarball not ready yet — silently skip, retry next command.
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
57
|
+
// Verify the version is actually installable before attempting install —
|
|
58
|
+
// the /latest endpoint can report a new version before the packument
|
|
59
|
+
// (which `npm install` consults) has propagated through the CDN.
|
|
60
|
+
if (!(await isVersionInstallable(latest)))
|
|
29
61
|
return;
|
|
30
|
-
}
|
|
31
62
|
info(`\n› New version available (${currentVersion} → ${latest}) — installing…`);
|
|
32
63
|
try {
|
|
33
|
-
|
|
64
|
+
const { bin, args } = npmInstallArgs(latest);
|
|
65
|
+
execSync(`"${bin}" ${args.map(a => `"${a}"`).join(' ')}`, { stdio: 'pipe' });
|
|
34
66
|
const config = loadConfig();
|
|
35
67
|
if (config?.skills_installed) {
|
|
36
68
|
await installSkills();
|
|
@@ -67,8 +99,9 @@ export async function update(flags = {}) {
|
|
|
67
99
|
}
|
|
68
100
|
catch { /* proceed anyway */ }
|
|
69
101
|
try {
|
|
70
|
-
const
|
|
71
|
-
|
|
102
|
+
const ver = latest || 'latest';
|
|
103
|
+
const { bin, args } = npmInstallArgs(ver);
|
|
104
|
+
execSync(`"${bin}" ${args.map((a) => `"${a}"`).join(' ')}`, { stdio: 'inherit' });
|
|
72
105
|
}
|
|
73
106
|
catch {
|
|
74
107
|
process.exit(1);
|