-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
151 lines (140 loc) · 6.1 KB
/
Copy pathindex.ts
File metadata and controls
151 lines (140 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { copyFileSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { createVersionResolver } from "../ci/version-file.js";
import { createNodeVersionResolver } from "../ci/node-version-file.js";
import { resolutionContext } from "../ci/resolution.js";
import { installVitePlus } from "../ci/install-viteplus.js";
import { prepareCacheMetadata } from "../ci/cache.js";
import type { CacheMetadata } from "../ci/cache.js";
import { restoreCacheSnapshot } from "../ci/cache-snapshot.js";
import { parseInstalledVpVersion } from "../ci/version.js";
import { packageManagerArgs, parsePackageManager } from "../ci/package-manager.js";
import { getCommandOutput } from "../ci/process.js";
import { nodeManagerOffArgs, parseNodeManager } from "../ci/node-manager.js";
import type { RuntimeEnv } from "../ci/types.js";
import { configureAuth } from "./auth.js";
import { setupSfw } from "./install-sfw.js";
import { parseRunInstall, runInstall } from "./run-install.js";
import { exportShellEnv, run } from "./shell.js";
import { resolveProjectDir } from "./utils.js";
function fail(message: string): never {
console.error(`setup-vp: ${message}`);
process.exit(1);
}
// Switch to the image's Node.js after installation, preserving package-manager management.
export function applyEnvironmentModes(
env: RuntimeEnv = process.env,
runFn: typeof run = run,
): void {
const nodeManager = parseNodeManager(env.SETUP_VP_NODE_MANAGER);
const packageManager = parsePackageManager(env.SETUP_VP_PACKAGE_MANAGER);
const versionOutput =
nodeManager === false || packageManager !== undefined
? getCommandOutput("vp", ["--version"]) || ""
: "";
const packageManagerCommands = packageManagerArgs(packageManager, versionOutput);
if (nodeManager === false) {
runFn("vp", nodeManagerOffArgs(versionOutput));
}
for (const args of packageManagerCommands) {
runFn("vp", args);
}
}
export async function main(phase = "setup"): Promise<void> {
const env = process.env;
const workspaceRoot = env.CI_PROJECT_DIR || process.cwd();
const cacheRoot = path.join(workspaceRoot, ".setup-vp-cache");
const cacheStateFile = path.join(workspaceRoot, ".setup-vp-cache-state.json");
const outputsFile = path.join(workspaceRoot, ".setup-vp-outputs.env");
if (phase === "save-cache") {
const metadata = JSON.parse(readFileSync(cacheStateFile, "utf8")) as CacheMetadata;
// Do not restore the pre-install snapshot over files populated by job scripts.
restoreCacheSnapshot(metadata, cacheRoot, console.warn, false).save();
return;
}
if (phase !== "setup") throw new Error(`Invalid GitLab phase: ${phase}`);
const projectDir = resolveProjectDir(env);
// An opt-out or failed setup must not reuse state from a previous shell-runner job.
rmSync(cacheStateFile, { force: true });
rmSync(outputsFile, { force: true });
const context = resolutionContext(workspaceRoot);
const nodeManager = parseNodeManager(env.SETUP_VP_NODE_MANAGER);
// Validate configuration before invoking the installer.
parsePackageManager(env.SETUP_VP_PACKAGE_MANAGER);
const runInstallEntries = parseRunInstall(env.SETUP_VP_RUN_INSTALL ?? "true");
if (nodeManager === false && (env.SETUP_VP_NODE_VERSION || env.SETUP_VP_NODE_VERSION_FILE)) {
throw new Error("node-version and node-version-file cannot be used with node-manager: false");
}
const nodeVersion =
env.SETUP_VP_NODE_VERSION ||
(env.SETUP_VP_NODE_VERSION_FILE
? createNodeVersionResolver(context).resolveNodeVersionFile(
env.SETUP_VP_NODE_VERSION_FILE,
projectDir,
)
: undefined);
const version = createVersionResolver(context).resolveVitePlusVersion(
{
version: env.SETUP_VP_VERSION,
versionFile: env.SETUP_VP_VERSION_FILE,
cacheDependencyPath: env.SETUP_VP_CACHE_DEPENDENCY_PATH,
},
projectDir,
);
await installVitePlus(version, { env, exportPath: (value) => exportShellEnv("PATH", value) });
applyEnvironmentModes();
if (nodeVersion) run("vp", ["env", "use", nodeVersion], { cwd: projectDir });
configureAuth(env.SETUP_VP_REGISTRY_URL || "", env.SETUP_VP_SCOPE || "", env, projectDir);
const cacheEnabled = env.SETUP_VP_CACHE?.toLowerCase() === "true";
if (cacheEnabled) env.SETUP_VP_SFW_CACHE_DIR = path.join(cacheRoot, "sfw");
else delete env.SETUP_VP_SFW_CACHE_DIR;
const metadata = cacheEnabled
? prepareCacheMetadata({
projectDir,
cacheDependencyPath: env.SETUP_VP_CACHE_DEPENDENCY_PATH,
logWarning: console.warn,
})
: { ready: false };
const cache = restoreCacheSnapshot(metadata, cacheRoot);
const cacheSaveEnabled = env.SETUP_VP_CACHE_SAVE?.toLowerCase() !== "false";
if (metadata.ready && cacheSaveEnabled) {
writeFileSync(cacheStateFile, JSON.stringify(metadata), { mode: 0o600 });
copyFileSync(process.argv[1]!, path.join(workspaceRoot, ".setup-vp-runtime.mjs"));
}
const installCommand = await setupSfw(runInstallEntries, env, version);
await runInstall(runInstallEntries, projectDir, installCommand);
if (cacheSaveEnabled) cache.save();
const output = getCommandOutput("vp", ["--version"], { cwd: projectDir });
if (!output)
throw new Error(
"Failed to verify Vite+ installation: vp --version failed or returned no output.",
);
console.log(output);
const outputs = {
SETUP_VP_INSTALLED_VERSION: parseInstalledVpVersion(output),
SETUP_VP_CACHE_HIT: String(cache.hit),
};
for (const [name, value] of Object.entries(outputs)) {
env[name] = value;
exportShellEnv(name, value);
}
// Only non-secret outputs belong in a GitLab dotenv artifact.
writeFileSync(
outputsFile,
Object.entries(outputs)
.map(([name, value]) => `${name}=${value}\n`)
.join(""),
{ mode: 0o600 },
);
}
export function isEntrypoint(argvPath = process.argv[1], moduleUrl = import.meta.url): boolean {
return Boolean(argvPath && moduleUrl === pathToFileURL(path.resolve(argvPath)).href);
}
if (isEntrypoint()) {
try {
await main(process.argv[2]);
} catch (error) {
fail(error instanceof Error ? error.message : String(error));
}
}