The Open Standard for Generative UI
import * as fs from "node:fs";
import * as path from "node:path";
import { printLogTail, QUIET_COMMAND_CAPTURE_LIMIT } from "../../../lib/command-output";
import type { PackageManager } from "../../../lib/detect-package-manager";
import { CliCancelledError, cliErrorProperties, throwCommandFailure } from "../../../lib/errors";
import { mutedNpmEnv, runCommand } from "../../../lib/process-runner";
import { withRetry } from "../../../lib/retry";
import { withSpinner } from "../../../lib/spinner";
import type { OverlayName, TemplateName } from "./create-types";
import type { CreateTelemetryClient } from "./telemetry";
export function resolveInstallInvocation(params: {
backendFramework: OverlayName;
packageManager: PackageManager;
targetDir: string;
}): { installCmd: string; installArgs: string[] } {
const { packageManager, targetDir } = params;
// Framework scaffolds without an npm lock must resolve ranges against the
// registry (`npm install`). When a backend overlay ships package-lock.json,
// keep the normal `npm ci` path. --prefer-offline is only safe for `npm ci`,
// where the lockfile pins exact versions and cache hits are content-addressed;
// bare `npm install` can fail with ETARGET on a stale packument cache.
const hasNpmLock = fs.existsSync(path.join(targetDir, "package-lock.json"));
const installCmd =
packageManager.name === "npm" && !hasNpmLock
? "npm install --no-audit --no-fund --progress=false"
: packageManager.name === "pnpm"
? "pnpm install --no-frozen-lockfile"
: packageManager.installCmd;
const installArgs =
packageManager.name === "npm" && !hasNpmLock
? ["install", "--no-audit", "--no-fund", "--progress=false"]
: packageManager.name === "pnpm"
? ["install", "--no-frozen-lockfile"]
: packageManager.installArgs;
return { installCmd, installArgs };
}
export async function installProjectDependencies(params: {
tel: CreateTelemetryClient;
verbose?: boolean;
targetDir: string;
template: TemplateName;
aiSetup: string;
packageManager: PackageManager;
installCmd: string;
installArgs: string[];
installDependencies: boolean;
}): Promise<boolean> {
const {
tel,
verbose,
targetDir,
template,
aiSetup,
packageManager,
installCmd,
installArgs,
installDependencies,
} = params;
if (!installDependencies) {
tel.trackDependencyInstallSkipped({
skip_reason: "no_install_flag",
});
console.info(`Skipping dependency install (--no-install). Run \`${installCmd}\` later.`);
return false;
}
tel.trackDependencyInstallStarted({ template, ai_setup: aiSetup });
const runInstall = () =>
verbose
? runCommand(packageManager.runCmd, installArgs, targetDir)
: runCommand(packageManager.runCmd, installArgs, targetDir, {
echo: false,
stdin: "ignore",
captureLimit: QUIET_COMMAND_CAPTURE_LIMIT,
env: mutedNpmEnv(),
});
if (verbose) {
console.info(`Installing dependencies with: ${installCmd}\n`);
}
const attemptInstall = async () => {
const result = await runInstall();
if (!result.error && result.status === 0) return result;
if (!verbose) {
printLogTail(result.diagnosticTail, "install log (tail)");
}
throwCommandFailure(result, "dependency_install", "dependency install failed", {
error_class: "dependency",
error_code: "NONZERO_EXIT",
});
};
try {
const runWithRetry = () =>
withRetry("Dependency install", attemptInstall, {
onRetry: tel.reportNetworkRetry("dependency_install"),
});
await (verbose ? runWithRetry() : withSpinner("Installing dependencies...", runWithRetry));
} catch (err) {
const properties = {
template,
ai_setup: aiSetup,
dependency_installed: false,
...cliErrorProperties(err),
};
if (err instanceof CliCancelledError) {
tel.trackDependencyInstallCancelled(properties);
} else {
tel.trackDependencyInstallFailed(properties);
}
throw err;
}
if (!verbose) {
console.info("✓ Dependencies installed");
}
tel.trackDependencyInstallSucceeded({
template,
ai_setup: aiSetup,
dependency_installed: true,
});
return true;
}