@ampcode/sdk 0.0.0-placeholder → 0.1.0-20260513070248-g548495c
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/LICENSE.md +1 -0
- package/README.md +1121 -2
- package/bin/amp-sdk.js +13 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +4665 -0
- package/dist/install.d.ts +26 -0
- package/dist/install.js +520 -0
- package/dist/types.d.ts +575 -0
- package/dist/version-compatibility.d.ts +20 -0
- package/package.json +66 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface InstallCommandOptions {
|
|
2
|
+
forceInstall: boolean;
|
|
3
|
+
}
|
|
4
|
+
interface ParsedInstallCommandOptions {
|
|
5
|
+
options: InstallCommandOptions;
|
|
6
|
+
showHelp: boolean;
|
|
7
|
+
errorMessage: string | null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Runs the @ampcode/sdk installer command.
|
|
11
|
+
*/
|
|
12
|
+
export declare function runInstallCommand(args: string[]): Promise<number>;
|
|
13
|
+
declare function parseInstallCommandOptions(args: string[]): ParsedInstallCommandOptions;
|
|
14
|
+
declare function shouldUseExistingCLI(options: InstallCommandOptions): boolean;
|
|
15
|
+
declare function shouldFallbackToLatestVersion(error: unknown, requiredVersion: string): boolean;
|
|
16
|
+
declare function verifyInstalledVersion(installedVersion: string | null | undefined, requiredVersion: string, binaryPath: string): void;
|
|
17
|
+
declare function isRelaxedVersionCheckEnabled(): boolean;
|
|
18
|
+
export declare const __testing: {
|
|
19
|
+
parseInstallCommandOptions: typeof parseInstallCommandOptions;
|
|
20
|
+
isRelaxedVersionCheckEnabled: typeof isRelaxedVersionCheckEnabled;
|
|
21
|
+
shouldFallbackToLatestVersion: typeof shouldFallbackToLatestVersion;
|
|
22
|
+
shouldUseExistingCLI: typeof shouldUseExistingCLI;
|
|
23
|
+
verifyInstalledVersion: typeof verifyInstalledVersion;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=install.d.ts.map
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __returnValue = (v) => v;
|
|
3
|
+
function __exportSetter(name, newValue) {
|
|
4
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
+
}
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
set: __exportSetter.bind(all, name)
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/install.ts
|
|
17
|
+
import { execSync, spawnSync } from "node:child_process";
|
|
18
|
+
import { createHash } from "node:crypto";
|
|
19
|
+
import {
|
|
20
|
+
chmodSync,
|
|
21
|
+
createReadStream,
|
|
22
|
+
createWriteStream,
|
|
23
|
+
existsSync,
|
|
24
|
+
mkdirSync,
|
|
25
|
+
readFileSync,
|
|
26
|
+
renameSync,
|
|
27
|
+
unlinkSync
|
|
28
|
+
} from "node:fs";
|
|
29
|
+
import { createRequire as createRequire2 } from "node:module";
|
|
30
|
+
import { homedir } from "node:os";
|
|
31
|
+
import { dirname, join } from "node:path";
|
|
32
|
+
import { Readable } from "node:stream";
|
|
33
|
+
import { pipeline } from "node:stream/promises";
|
|
34
|
+
|
|
35
|
+
// src/version-compatibility.ts
|
|
36
|
+
import fs from "node:fs";
|
|
37
|
+
import { createRequire } from "node:module";
|
|
38
|
+
var AMP_VERSION_PATTERN = /0\.0\.\d+(?:-[A-Za-z0-9.-]+)?/;
|
|
39
|
+
var cachedMinimumAmpVersion;
|
|
40
|
+
function parseAmpVersionFromOutput(output) {
|
|
41
|
+
const match = output.match(AMP_VERSION_PATTERN);
|
|
42
|
+
return match?.[0] ?? null;
|
|
43
|
+
}
|
|
44
|
+
function extractAmpVersionTimestamp(version) {
|
|
45
|
+
const match = version.match(/^0\.0\.(\d+)/);
|
|
46
|
+
if (!match?.[1]) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
return Number.parseInt(match[1], 10);
|
|
50
|
+
}
|
|
51
|
+
function isAmpVersionAtLeast(version, minimumVersion) {
|
|
52
|
+
const versionTimestamp = extractAmpVersionTimestamp(version);
|
|
53
|
+
const minimumTimestamp = extractAmpVersionTimestamp(minimumVersion);
|
|
54
|
+
if (versionTimestamp === null || minimumTimestamp === null) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (versionTimestamp === 0) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
return versionTimestamp >= minimumTimestamp;
|
|
61
|
+
}
|
|
62
|
+
function getMinimumCompatibleAmpVersion() {
|
|
63
|
+
if (cachedMinimumAmpVersion !== undefined) {
|
|
64
|
+
return cachedMinimumAmpVersion;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const require2 = createRequire(import.meta.url);
|
|
68
|
+
const sdkPackageJsonPath = require2.resolve("../package.json");
|
|
69
|
+
const sdkPackageJson = JSON.parse(fs.readFileSync(sdkPackageJsonPath, "utf8"));
|
|
70
|
+
const configuredVersion = sdkPackageJson.amp?.minimumCliVersion;
|
|
71
|
+
if (!configuredVersion || extractAmpVersionTimestamp(configuredVersion) === null) {
|
|
72
|
+
cachedMinimumAmpVersion = null;
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
cachedMinimumAmpVersion = configuredVersion;
|
|
76
|
+
return configuredVersion;
|
|
77
|
+
} catch {
|
|
78
|
+
cachedMinimumAmpVersion = null;
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/install.ts
|
|
84
|
+
var STORAGE_BASE = (process.env.AMP_SDK_CLI_BASE_URL || "https://static.ampcode.com/cli").replace(/\/+$/, "");
|
|
85
|
+
var VERSION_COMMAND_TIMEOUT_MS = 1e4;
|
|
86
|
+
var DOWNLOAD_TIMEOUT_MS = 300000;
|
|
87
|
+
var RELAX_VERSION_CHECK_ENV_VAR = "AMP_SDK_RELAX_VERSION_CHECK";
|
|
88
|
+
async function runInstallCommand(args) {
|
|
89
|
+
const command = args[0];
|
|
90
|
+
if (!command || command === "--help" || command === "-h" || command === "help") {
|
|
91
|
+
printUsage();
|
|
92
|
+
return 0;
|
|
93
|
+
}
|
|
94
|
+
if (command !== "install") {
|
|
95
|
+
console.error(`Unknown command: ${command}`);
|
|
96
|
+
printUsage();
|
|
97
|
+
return 1;
|
|
98
|
+
}
|
|
99
|
+
const parsedOptions = parseInstallCommandOptions(args.slice(1));
|
|
100
|
+
if (parsedOptions.errorMessage) {
|
|
101
|
+
console.error(parsedOptions.errorMessage);
|
|
102
|
+
printUsage();
|
|
103
|
+
return 1;
|
|
104
|
+
}
|
|
105
|
+
if (parsedOptions.showHelp) {
|
|
106
|
+
printUsage();
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
109
|
+
await installCLIBinary(parsedOptions.options);
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
async function installCLIBinary(options) {
|
|
113
|
+
const requiredVersion = await resolveRequiredCliVersion();
|
|
114
|
+
let downloadedVersion = requiredVersion;
|
|
115
|
+
if (shouldUseExistingCLI(options)) {
|
|
116
|
+
const installedCli = findInstalledCli(requiredVersion);
|
|
117
|
+
if (installedCli) {
|
|
118
|
+
if (installedCli.version) {
|
|
119
|
+
console.log(`Amp CLI ${installedCli.version} already satisfies minimum ${requiredVersion} ` + `(${installedCli.command.source}).`);
|
|
120
|
+
} else {
|
|
121
|
+
console.log(`Amp CLI found at ${installedCli.command.source}, but version output could not be parsed. ` + `Assuming compatibility with minimum ${requiredVersion}.`);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
console.log("Forcing SDK-managed install; skipping existing CLI detection.");
|
|
127
|
+
}
|
|
128
|
+
console.log(`Installing Amp CLI ${requiredVersion} to ${resolveSDKManagedBinDir()} ` + "(required by this SDK version).");
|
|
129
|
+
let binaryPath;
|
|
130
|
+
try {
|
|
131
|
+
const installedBinary = await installBinary(requiredVersion);
|
|
132
|
+
binaryPath = installedBinary.binaryPath;
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (!shouldFallbackToLatestVersion(error, requiredVersion)) {
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
const latestVersion = await fetchLatestCliVersion();
|
|
138
|
+
if (latestVersion === requiredVersion) {
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
downloadedVersion = latestVersion;
|
|
142
|
+
console.warn(`Amp CLI ${requiredVersion} is unavailable in ${STORAGE_BASE}. ` + `Retrying with latest version ${latestVersion}.`);
|
|
143
|
+
const installedBinary = await installBinary(latestVersion);
|
|
144
|
+
binaryPath = installedBinary.binaryPath;
|
|
145
|
+
}
|
|
146
|
+
const installedVersion = getAmpVersion({
|
|
147
|
+
command: binaryPath,
|
|
148
|
+
args: [],
|
|
149
|
+
source: "SDK_MANAGED"
|
|
150
|
+
});
|
|
151
|
+
if (downloadedVersion !== requiredVersion) {
|
|
152
|
+
console.log(`Downloaded Amp CLI ${downloadedVersion} to satisfy minimum required version ${requiredVersion}.`);
|
|
153
|
+
}
|
|
154
|
+
verifyInstalledVersion(installedVersion, requiredVersion, binaryPath);
|
|
155
|
+
}
|
|
156
|
+
function printUsage() {
|
|
157
|
+
console.log("Usage: npx -y @ampcode/sdk install [--force]");
|
|
158
|
+
}
|
|
159
|
+
function parseInstallCommandOptions(args) {
|
|
160
|
+
const options = {
|
|
161
|
+
forceInstall: false
|
|
162
|
+
};
|
|
163
|
+
for (const arg of args) {
|
|
164
|
+
if (arg === "--help" || arg === "-h") {
|
|
165
|
+
return {
|
|
166
|
+
options,
|
|
167
|
+
showHelp: true,
|
|
168
|
+
errorMessage: null
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (arg === "--force") {
|
|
172
|
+
options.forceInstall = true;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
options,
|
|
177
|
+
showHelp: false,
|
|
178
|
+
errorMessage: `Unknown option for install: ${arg}`
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
options,
|
|
183
|
+
showHelp: false,
|
|
184
|
+
errorMessage: null
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function shouldUseExistingCLI(options) {
|
|
188
|
+
return !options.forceInstall;
|
|
189
|
+
}
|
|
190
|
+
function shouldFallbackToLatestVersion(error, requiredVersion) {
|
|
191
|
+
const configuredVersion = getMinimumCompatibleAmpVersion();
|
|
192
|
+
if (!configuredVersion || configuredVersion !== requiredVersion) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
if (!(error instanceof Error)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
const isArtifactRequestError = error.message.startsWith("Failed to fetch checksum") || error.message.startsWith("Failed to download binary");
|
|
199
|
+
const isNotFoundStatus = error.message.includes("(404)");
|
|
200
|
+
return isArtifactRequestError && isNotFoundStatus;
|
|
201
|
+
}
|
|
202
|
+
function findInstalledCli(requiredVersion) {
|
|
203
|
+
const checkedCommands = new Set;
|
|
204
|
+
for (const command of getCliCandidates()) {
|
|
205
|
+
const commandKey = formatCommand(command);
|
|
206
|
+
if (checkedCommands.has(commandKey)) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
checkedCommands.add(commandKey);
|
|
210
|
+
const version = getAmpVersion(command);
|
|
211
|
+
if (version === undefined) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (version === null || isAmpVersionAtLeast(version, requiredVersion)) {
|
|
215
|
+
return { command, version };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
function getCliCandidates() {
|
|
221
|
+
const candidates = [
|
|
222
|
+
resolveCliFromEnvironment(),
|
|
223
|
+
resolveLocalAmpPackageCommand(),
|
|
224
|
+
resolveSDKManagedCli(),
|
|
225
|
+
resolveAmpHomeCli(),
|
|
226
|
+
resolveCliFromPath()
|
|
227
|
+
];
|
|
228
|
+
return candidates.filter((candidate) => candidate !== null);
|
|
229
|
+
}
|
|
230
|
+
function resolveCliFromEnvironment() {
|
|
231
|
+
const cliPath = process.env.AMP_CLI_PATH;
|
|
232
|
+
if (!cliPath || !existsSync(cliPath)) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
if (isNodeScriptPath(cliPath)) {
|
|
236
|
+
return {
|
|
237
|
+
command: "node",
|
|
238
|
+
args: [cliPath],
|
|
239
|
+
source: "AMP_CLI_PATH"
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
command: cliPath,
|
|
244
|
+
args: [],
|
|
245
|
+
source: "AMP_CLI_PATH"
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function resolveLocalAmpPackageCommand() {
|
|
249
|
+
const require2 = createRequire2(import.meta.url);
|
|
250
|
+
for (const packageName of ["@ampcode/cli", "@sourcegraph/amp"]) {
|
|
251
|
+
try {
|
|
252
|
+
const packageJsonPath = require2.resolve(`${packageName}/package.json`);
|
|
253
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
254
|
+
if (!packageJson.bin?.amp) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const ampPath = join(dirname(packageJsonPath), packageJson.bin.amp);
|
|
258
|
+
if (!existsSync(ampPath)) {
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
command: "node",
|
|
263
|
+
args: [ampPath],
|
|
264
|
+
source: "LOCAL_NPM"
|
|
265
|
+
};
|
|
266
|
+
} catch {}
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
function resolveSDKManagedCli() {
|
|
271
|
+
const ampPath = join(resolveSDKManagedBinDir(), process.platform === "win32" ? "amp.exe" : "amp");
|
|
272
|
+
if (!existsSync(ampPath)) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
command: ampPath,
|
|
277
|
+
args: [],
|
|
278
|
+
source: "SDK_MANAGED"
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
function resolveAmpHomeCli() {
|
|
282
|
+
const ampPath = join(resolveAmpHome(), "bin", process.platform === "win32" ? "amp.exe" : "amp");
|
|
283
|
+
if (!existsSync(ampPath)) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
command: ampPath,
|
|
288
|
+
args: [],
|
|
289
|
+
source: "AMP_HOME"
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function resolveCliFromPath() {
|
|
293
|
+
try {
|
|
294
|
+
execSync(process.platform === "win32" ? "where amp" : "command -v amp", {
|
|
295
|
+
stdio: "ignore"
|
|
296
|
+
});
|
|
297
|
+
return {
|
|
298
|
+
command: "amp",
|
|
299
|
+
args: [],
|
|
300
|
+
source: "PATH"
|
|
301
|
+
};
|
|
302
|
+
} catch {
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function getAmpVersion(command) {
|
|
307
|
+
const result = spawnSync(command.command, [...command.args, "--version"], {
|
|
308
|
+
encoding: "utf8",
|
|
309
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
310
|
+
timeout: VERSION_COMMAND_TIMEOUT_MS
|
|
311
|
+
});
|
|
312
|
+
if (result.error) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const output = `${result.stdout ?? ""}
|
|
316
|
+
${result.stderr ?? ""}`;
|
|
317
|
+
return parseAmpVersionFromOutput(output);
|
|
318
|
+
}
|
|
319
|
+
function formatCommand(command) {
|
|
320
|
+
return [command.command, ...command.args].join(" ");
|
|
321
|
+
}
|
|
322
|
+
async function resolveRequiredCliVersion() {
|
|
323
|
+
const configuredVersion = getMinimumCompatibleAmpVersion();
|
|
324
|
+
if (configuredVersion) {
|
|
325
|
+
return configuredVersion;
|
|
326
|
+
}
|
|
327
|
+
return fetchLatestCliVersion();
|
|
328
|
+
}
|
|
329
|
+
async function fetchLatestCliVersion() {
|
|
330
|
+
const response = await fetchWithTimeout(`${STORAGE_BASE}/cli-version.txt`);
|
|
331
|
+
if (!response.ok) {
|
|
332
|
+
throw new Error(`Failed to fetch latest CLI version (${response.status})`);
|
|
333
|
+
}
|
|
334
|
+
const version = (await response.text()).trim();
|
|
335
|
+
if (!parseAmpVersionFromOutput(version)) {
|
|
336
|
+
throw new Error(`Invalid latest CLI version format: ${version}`);
|
|
337
|
+
}
|
|
338
|
+
return version;
|
|
339
|
+
}
|
|
340
|
+
function detectPlatform() {
|
|
341
|
+
if (process.platform === "darwin") {
|
|
342
|
+
if (process.arch === "arm64") {
|
|
343
|
+
return "darwin-arm64";
|
|
344
|
+
}
|
|
345
|
+
return "darwin-x64";
|
|
346
|
+
}
|
|
347
|
+
if (process.platform === "win32") {
|
|
348
|
+
return "windows-x64";
|
|
349
|
+
}
|
|
350
|
+
if (process.platform === "linux") {
|
|
351
|
+
if (process.arch === "arm64") {
|
|
352
|
+
return "linux-arm64";
|
|
353
|
+
}
|
|
354
|
+
return hasAVX2Support() ? "linux-x64" : "linux-x64-baseline";
|
|
355
|
+
}
|
|
356
|
+
return "linux-x64";
|
|
357
|
+
}
|
|
358
|
+
function hasAVX2Support() {
|
|
359
|
+
if (process.platform === "linux") {
|
|
360
|
+
try {
|
|
361
|
+
const cpuInfo = readFileSync("/proc/cpuinfo", "utf8");
|
|
362
|
+
return cpuInfo.includes("avx2");
|
|
363
|
+
} catch {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (process.platform === "darwin") {
|
|
368
|
+
try {
|
|
369
|
+
const sysctl = execSync("sysctl -a 2>/dev/null", { encoding: "utf8" });
|
|
370
|
+
return sysctl.includes("AVX2");
|
|
371
|
+
} catch {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
async function installBinary(targetVersion) {
|
|
378
|
+
const platform = detectPlatform();
|
|
379
|
+
const binDir = resolveSDKManagedBinDir();
|
|
380
|
+
ensurePrivateDirectory(binDir);
|
|
381
|
+
const binaryName = getBinaryName(platform);
|
|
382
|
+
const checksumName = `${platform}-amp.sha256`;
|
|
383
|
+
const binaryURL = `${STORAGE_BASE}/${targetVersion}/${binaryName}`;
|
|
384
|
+
const checksumURL = `${STORAGE_BASE}/${targetVersion}/${checksumName}`;
|
|
385
|
+
const binaryPath = join(binDir, process.platform === "win32" ? "amp.exe" : "amp");
|
|
386
|
+
const tempPath = join(binDir, process.platform === "win32" ? "amp.download.tmp.exe" : "amp.download.tmp");
|
|
387
|
+
try {
|
|
388
|
+
const checksumResponse = await fetchWithTimeout(checksumURL);
|
|
389
|
+
if (!checksumResponse.ok) {
|
|
390
|
+
throw new Error(`Failed to fetch checksum (${checksumResponse.status})`);
|
|
391
|
+
}
|
|
392
|
+
const expectedChecksum = (await checksumResponse.text()).trim();
|
|
393
|
+
const binaryResponse = await fetchWithTimeout(binaryURL);
|
|
394
|
+
if (!binaryResponse.ok) {
|
|
395
|
+
throw new Error(`Failed to download binary (${binaryResponse.status})`);
|
|
396
|
+
}
|
|
397
|
+
if (!binaryResponse.body) {
|
|
398
|
+
throw new Error("Empty response body when downloading binary");
|
|
399
|
+
}
|
|
400
|
+
await pipeline(Readable.fromWeb(binaryResponse.body), createWriteStream(tempPath));
|
|
401
|
+
await verifyChecksum(tempPath, expectedChecksum);
|
|
402
|
+
if (process.platform !== "win32") {
|
|
403
|
+
chmodSync(tempPath, 493);
|
|
404
|
+
}
|
|
405
|
+
if (process.platform === "win32" && existsSync(binaryPath)) {
|
|
406
|
+
try {
|
|
407
|
+
unlinkSync(binaryPath);
|
|
408
|
+
} catch {}
|
|
409
|
+
}
|
|
410
|
+
renameSync(tempPath, binaryPath);
|
|
411
|
+
return { binaryPath };
|
|
412
|
+
} catch (error) {
|
|
413
|
+
if (existsSync(tempPath)) {
|
|
414
|
+
try {
|
|
415
|
+
unlinkSync(tempPath);
|
|
416
|
+
} catch {}
|
|
417
|
+
}
|
|
418
|
+
if (process.platform === "win32" && isFileLockError(error)) {
|
|
419
|
+
throw new Error("Cannot replace amp binary because it is in use. Close running amp processes and retry.");
|
|
420
|
+
}
|
|
421
|
+
throw error;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
function getBinaryName(platform) {
|
|
425
|
+
return platform.startsWith("windows") ? `amp-${platform}.exe` : `amp-${platform}`;
|
|
426
|
+
}
|
|
427
|
+
async function verifyChecksum(filePath, expectedChecksum) {
|
|
428
|
+
const hash = createHash("sha256");
|
|
429
|
+
for await (const chunk of createReadStream(filePath)) {
|
|
430
|
+
hash.update(chunk);
|
|
431
|
+
}
|
|
432
|
+
const actualChecksum = hash.digest("hex");
|
|
433
|
+
if (actualChecksum !== expectedChecksum) {
|
|
434
|
+
throw new Error(`Checksum mismatch for ${filePath}`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function verifyInstalledVersion(installedVersion, requiredVersion, binaryPath) {
|
|
438
|
+
if (typeof installedVersion !== "string") {
|
|
439
|
+
const message = `Amp CLI installed at ${binaryPath}, but could not read its version. ` + "Binary checksum verified successfully.";
|
|
440
|
+
if (isRelaxedVersionCheckEnabled()) {
|
|
441
|
+
console.warn(`${message} Continuing because ${RELAX_VERSION_CHECK_ENV_VAR}=1 is set.`);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
throw new Error(`${message} Set ${RELAX_VERSION_CHECK_ENV_VAR}=1 to continue in constrained environments.`);
|
|
445
|
+
}
|
|
446
|
+
if (!isAmpVersionAtLeast(installedVersion, requiredVersion)) {
|
|
447
|
+
const message = `Installed CLI ${installedVersion} does not satisfy minimum ${requiredVersion}. ` + "Binary checksum verified successfully.";
|
|
448
|
+
if (isRelaxedVersionCheckEnabled()) {
|
|
449
|
+
console.warn(`${message} Continuing because ${RELAX_VERSION_CHECK_ENV_VAR}=1 is set.`);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
throw new Error(`${message} Set ${RELAX_VERSION_CHECK_ENV_VAR}=1 to continue in constrained environments.`);
|
|
453
|
+
}
|
|
454
|
+
console.log(`Amp CLI ${installedVersion} installed at ${binaryPath}.`);
|
|
455
|
+
}
|
|
456
|
+
function isRelaxedVersionCheckEnabled() {
|
|
457
|
+
const rawValue = process.env[RELAX_VERSION_CHECK_ENV_VAR];
|
|
458
|
+
if (!rawValue) {
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
const normalizedValue = rawValue.trim().toLowerCase();
|
|
462
|
+
return normalizedValue === "1" || normalizedValue === "true";
|
|
463
|
+
}
|
|
464
|
+
function resolveAmpHome() {
|
|
465
|
+
if (process.env.AMP_HOME) {
|
|
466
|
+
return process.env.AMP_HOME;
|
|
467
|
+
}
|
|
468
|
+
const home = homedir();
|
|
469
|
+
if (!home) {
|
|
470
|
+
throw new Error("Cannot determine home directory for AMP_HOME");
|
|
471
|
+
}
|
|
472
|
+
return join(home, ".amp");
|
|
473
|
+
}
|
|
474
|
+
function resolveSDKManagedBinDir() {
|
|
475
|
+
return join(resolveAmpHome(), "sdk", "bin");
|
|
476
|
+
}
|
|
477
|
+
function ensurePrivateDirectory(directoryPath) {
|
|
478
|
+
mkdirSync(directoryPath, { recursive: true, mode: 448 });
|
|
479
|
+
if (process.platform !== "win32") {
|
|
480
|
+
chmodSync(directoryPath, 448);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
async function fetchWithTimeout(url, timeoutMs = DOWNLOAD_TIMEOUT_MS) {
|
|
484
|
+
const controller = new AbortController;
|
|
485
|
+
const timeoutID = setTimeout(() => controller.abort(), timeoutMs);
|
|
486
|
+
try {
|
|
487
|
+
const response = await fetch(url, {
|
|
488
|
+
signal: controller.signal
|
|
489
|
+
});
|
|
490
|
+
clearTimeout(timeoutID);
|
|
491
|
+
return response;
|
|
492
|
+
} catch (error) {
|
|
493
|
+
clearTimeout(timeoutID);
|
|
494
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
495
|
+
throw new Error(`Network timeout after ${timeoutMs}ms while fetching ${url}`);
|
|
496
|
+
}
|
|
497
|
+
throw error;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
function isNodeScriptPath(filePath) {
|
|
501
|
+
return filePath.endsWith(".js") || filePath.endsWith(".mjs") || filePath.endsWith(".cjs");
|
|
502
|
+
}
|
|
503
|
+
function isFileLockError(error) {
|
|
504
|
+
const code = error.code ?? "";
|
|
505
|
+
const message = (error instanceof Error ? error.message : "").toLowerCase();
|
|
506
|
+
return code === "EBUSY" || code === "EPERM" || code === "EACCES" || message.includes("being used by another process") || message.includes("locked");
|
|
507
|
+
}
|
|
508
|
+
var __testing = {
|
|
509
|
+
parseInstallCommandOptions,
|
|
510
|
+
isRelaxedVersionCheckEnabled,
|
|
511
|
+
shouldFallbackToLatestVersion,
|
|
512
|
+
shouldUseExistingCLI,
|
|
513
|
+
verifyInstalledVersion
|
|
514
|
+
};
|
|
515
|
+
export {
|
|
516
|
+
runInstallCommand,
|
|
517
|
+
__testing
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
//# debugId=DEEF54F77A46F67264756E2164756E21
|