Skip to content

Commit b614e54

Browse files
committed
fix(dev): support ci local cli binary lookup
1 parent 6e338de commit b614e54

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

packages/tools/src/local-cli.ts

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawnSync } from 'node:child_process';
2-
import { copyFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
2+
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync } from 'node:fs';
33
import { createRequire } from 'node:module';
44
import path from 'node:path';
55
import { fileURLToPath } from 'node:url';
@@ -9,8 +9,8 @@ const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../
99
const cliDistDir = path.join(repoRoot, 'packages', 'cli', 'dist');
1010
const cliBinPath = path.join(cliDistDir, 'bin.js');
1111
const testCliPath = path.join(repoRoot, 'packages', 'test', 'dist', 'cli.js');
12-
const localVpPath = path.join(repoRoot, 'target', 'debug', isWindows ? 'vp.exe' : 'vp');
13-
const localVpBinDir = path.dirname(localVpPath);
12+
const localVpBinaryName = isWindows ? 'vp.exe' : 'vp';
13+
const defaultLocalVpPath = path.join(repoRoot, 'target', 'debug', localVpBinaryName);
1414
const viteRepoDir = path.join(repoRoot, 'vite');
1515
const legacyViteRepoDir = path.join(repoRoot, 'rolldown-vite');
1616
const rolldownRepoDir = path.join(repoRoot, 'rolldown');
@@ -32,22 +32,71 @@ type CommandOptions = {
3232
hint?: string;
3333
};
3434

35+
type LocalCliArtifacts = {
36+
vpPath: string;
37+
vpBinDir: string;
38+
};
39+
3540
function failMissing(pathname: string, description: string): never {
3641
console.error(`Missing ${description}: ${pathname}`);
3742
console.error(`Run "${bootstrapHint}" from a fresh clone, or "${buildHint}" after setup.`);
3843
process.exit(1);
3944
}
4045

41-
function ensureLocalCliReady(options?: { needsTestCli?: boolean }) {
46+
function getTargetDirs(): string[] {
47+
return [
48+
...new Set(
49+
[process.env.CARGO_TARGET_DIR, path.join(repoRoot, 'target')].filter(
50+
(targetDir): targetDir is string => Boolean(targetDir),
51+
),
52+
),
53+
];
54+
}
55+
56+
function findLocalVpBinary(): string | null {
57+
const profiles = ['debug', 'release'];
58+
59+
for (const targetDir of getTargetDirs()) {
60+
for (const profile of profiles) {
61+
const directPath = path.join(targetDir, profile, localVpBinaryName);
62+
if (existsSync(directPath)) {
63+
return directPath;
64+
}
65+
}
66+
67+
try {
68+
for (const entry of readdirSync(targetDir).toSorted()) {
69+
for (const profile of profiles) {
70+
const nestedPath = path.join(targetDir, entry, profile, localVpBinaryName);
71+
if (existsSync(nestedPath)) {
72+
return nestedPath;
73+
}
74+
}
75+
}
76+
} catch {
77+
continue;
78+
}
79+
}
80+
81+
return null;
82+
}
83+
84+
function ensureLocalCliReady(options?: { needsTestCli?: boolean }): LocalCliArtifacts {
4285
if (!existsSync(cliBinPath)) {
4386
failMissing(cliBinPath, 'local CLI bundle');
4487
}
45-
if (!existsSync(localVpPath)) {
46-
failMissing(localVpPath, 'local debug vp binary');
88+
const vpPath = findLocalVpBinary();
89+
if (!vpPath) {
90+
failMissing(defaultLocalVpPath, 'local vp binary');
4791
}
4892
if (options?.needsTestCli && !existsSync(testCliPath)) {
4993
failMissing(testCliPath, 'local test CLI bundle');
5094
}
95+
96+
return {
97+
vpPath,
98+
vpBinDir: path.dirname(vpPath),
99+
};
51100
}
52101

53102
function localCliEnv(): NodeJS.ProcessEnv {
@@ -262,9 +311,9 @@ function exitWith(result: ReturnType<typeof spawnSync>): never {
262311
}
263312

264313
export function runLocalCli(args: string[]) {
265-
ensureLocalCliReady({ needsTestCli: args[0] === 'test' });
314+
const { vpPath } = ensureLocalCliReady({ needsTestCli: args[0] === 'test' });
266315

267-
const result = spawnSync(localVpPath, args, {
316+
const result = spawnSync(vpPath, args, {
268317
cwd: process.cwd(),
269318
env: localCliEnv(),
270319
stdio: 'inherit',
@@ -273,7 +322,7 @@ export function runLocalCli(args: string[]) {
273322
}
274323

275324
export function runLocalGlobalSnapTest(args: string[]) {
276-
ensureLocalCliReady();
325+
const { vpBinDir } = ensureLocalCliReady();
277326

278327
const result = spawnSync(
279328
process.execPath,
@@ -283,7 +332,7 @@ export function runLocalGlobalSnapTest(args: string[]) {
283332
'--dir',
284333
'snap-tests-global',
285334
'--local-vp-bin-dir',
286-
localVpBinDir,
335+
vpBinDir,
287336
...args,
288337
],
289338
{

0 commit comments

Comments
 (0)