Skip to content

Commit a045b57

Browse files
authored
fix(cli): delegate global commands to project-local CLI (#55)
1 parent 79ee9f8 commit a045b57

4 files changed

Lines changed: 299 additions & 4 deletions

File tree

‎packages/cli/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"warbler": "./src/bin.ts"
88
},
99
"exports": {
10-
".": "./src/index.ts"
10+
".": "./src/index.ts",
11+
"./bin": "./src/bin.ts"
1112
},
1213
"files": [
1314
"src",

‎packages/cli/src/bin.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#!/usr/bin/env bun
2-
import { runCLI } from "./cli";
32

4-
const exitCode = await runCLI(process.argv.slice(2));
5-
process.exitCode = exitCode;
3+
import {
4+
delegateToProjectCLI,
5+
} from "./local-cli-launcher";
6+
7+
const argv = process.argv.slice(2);
8+
9+
const delegatedExitCode = await delegateToProjectCLI(
10+
argv,
11+
import.meta.path,
12+
);
13+
14+
if (delegatedExitCode !== undefined) {
15+
process.exitCode = delegatedExitCode;
16+
} else {
17+
const { runCLI } = await import("./cli");
18+
19+
process.exitCode = await runCLI(argv);
20+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
const LOCAL_CLI_MARKER = "WARBLER_LOCAL_CLI";
2+
3+
export async function delegateToProjectCLI(
4+
argv: readonly string[],
5+
currentEntry: string,
6+
cwd = process.cwd(),
7+
): Promise<number | undefined> {
8+
if (process.env[LOCAL_CLI_MARKER] === "1") {
9+
return undefined;
10+
}
11+
12+
const resolutionRoot = resolveDelegationRoot(argv, cwd);
13+
14+
let localEntry: string;
15+
16+
try {
17+
localEntry = Bun.resolveSync(
18+
"@warblerjs/cli/bin",
19+
resolutionRoot,
20+
);
21+
} catch {
22+
return undefined;
23+
}
24+
25+
if (localEntry === currentEntry) {
26+
return undefined;
27+
}
28+
29+
const environment = Object.assign(
30+
{},
31+
process.env,
32+
{
33+
[LOCAL_CLI_MARKER]: "1",
34+
},
35+
);
36+
37+
const command = new Array<string>(
38+
argv.length + 2,
39+
);
40+
41+
command[0] = process.execPath;
42+
command[1] = localEntry;
43+
44+
for (
45+
let index = 0, length = argv.length;
46+
index < length;
47+
index += 1
48+
) {
49+
const argument = argv[index]!;
50+
51+
if (
52+
index > 0 &&
53+
argv[index - 1] === "--project"
54+
) {
55+
command[index + 2] = resolutionRoot;
56+
continue;
57+
}
58+
59+
if (argument.startsWith("--project=")) {
60+
command[index + 2] =
61+
`--project=${resolutionRoot}`;
62+
63+
continue;
64+
}
65+
66+
command[index + 2] = argument;
67+
}
68+
69+
const child = Bun.spawn(
70+
command,
71+
{
72+
cwd: resolutionRoot,
73+
env: environment,
74+
stdin: "inherit",
75+
stdout: "inherit",
76+
stderr: "inherit",
77+
},
78+
);
79+
80+
return await child.exited;
81+
}
82+
83+
export function resolveDelegationRoot(
84+
argv: readonly string[],
85+
cwd: string,
86+
): string {
87+
for (
88+
let index = 0, length = argv.length;
89+
index < length;
90+
index += 1
91+
) {
92+
const argument = argv[index];
93+
94+
if (argument === "--project") {
95+
const project = argv[index + 1];
96+
97+
if (project !== undefined && project.length > 0) {
98+
return absoluteFrom(cwd, project);
99+
}
100+
101+
return cwd;
102+
}
103+
104+
if (argument?.startsWith("--project=") === true) {
105+
const project = argument.slice("--project=".length);
106+
107+
return project.length > 0
108+
? absoluteFrom(cwd, project)
109+
: cwd;
110+
}
111+
}
112+
113+
return cwd;
114+
}
115+
116+
function absoluteFrom(
117+
cwd: string,
118+
value: string,
119+
): string {
120+
if (value.startsWith("/")) {
121+
return value;
122+
}
123+
124+
return cwd.endsWith("/")
125+
? `${cwd}${value}`
126+
: `${cwd}/${value}`;
127+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import {
2+
afterEach,
3+
describe,
4+
expect,
5+
test,
6+
} from "bun:test";
7+
8+
import {
9+
mkdir,
10+
mkdtemp,
11+
rm,
12+
} from "node:fs/promises";
13+
14+
import {
15+
tmpdir,
16+
} from "node:os";
17+
18+
import {
19+
join,
20+
} from "node:path";
21+
22+
import {
23+
delegateToProjectCLI,
24+
resolveDelegationRoot,
25+
} from "../src/local-cli-launcher";
26+
27+
const cleanupRoots: string[] = [];
28+
29+
afterEach(async () => {
30+
for (
31+
let index = 0, length = cleanupRoots.length;
32+
index < length;
33+
index += 1
34+
) {
35+
await rm(cleanupRoots[index]!, {
36+
recursive: true,
37+
force: true,
38+
});
39+
}
40+
41+
cleanupRoots.length = 0;
42+
});
43+
44+
describe("project-local CLI delegation", () => {
45+
test("resolves explicit project roots", () => {
46+
expect(
47+
resolveDelegationRoot(
48+
["dev", "--project", "apps/example"],
49+
"/workspace",
50+
),
51+
).toBe("/workspace/apps/example");
52+
53+
expect(
54+
resolveDelegationRoot(
55+
["dev", "--project=/srv/application"],
56+
"/workspace",
57+
),
58+
).toBe("/srv/application");
59+
60+
expect(
61+
resolveDelegationRoot(
62+
["dev"],
63+
"/workspace",
64+
),
65+
).toBe("/workspace");
66+
});
67+
68+
test("delegates to the project-local CLI and preserves its exit code", async () => {
69+
const root = await mkdtemp(
70+
join(tmpdir(), "warbler-local-cli-"),
71+
);
72+
73+
cleanupRoots.push(root);
74+
75+
const packageRoot = join(
76+
root,
77+
"node_modules",
78+
"@warblerjs",
79+
"cli",
80+
);
81+
82+
await mkdir(packageRoot, {
83+
recursive: true,
84+
});
85+
86+
await Bun.write(
87+
join(packageRoot, "package.json"),
88+
JSON.stringify({
89+
name: "@warblerjs/cli",
90+
version: "9.9.9",
91+
type: "module",
92+
exports: {
93+
"./bin": "./bin.ts",
94+
},
95+
}),
96+
);
97+
98+
await Bun.write(
99+
join(packageRoot, "bin.ts"),
100+
`
101+
await Bun.write(
102+
process.cwd() + "/delegated.json",
103+
JSON.stringify({
104+
argv: process.argv.slice(2),
105+
cwd: process.cwd(),
106+
marker: process.env.WARBLER_LOCAL_CLI,
107+
}),
108+
);
109+
110+
process.exit(7);
111+
`,
112+
);
113+
114+
const exitCode = await delegateToProjectCLI(
115+
["dev", "--no-color"],
116+
"/global/warbler/bin.ts",
117+
root,
118+
);
119+
120+
expect(exitCode).toBe(7);
121+
122+
const result = await Bun.file(
123+
join(root, "delegated.json"),
124+
).json() as {
125+
readonly argv: readonly string[];
126+
readonly cwd: string;
127+
readonly marker?: string;
128+
};
129+
130+
expect(result).toEqual({
131+
argv: ["dev", "--no-color"],
132+
cwd: root,
133+
marker: "1",
134+
});
135+
});
136+
137+
test("returns undefined when no project-local CLI exists", async () => {
138+
const root = await mkdtemp(
139+
join(tmpdir(), "warbler-no-local-cli-"),
140+
);
141+
142+
cleanupRoots.push(root);
143+
144+
expect(
145+
await delegateToProjectCLI(
146+
["dev"],
147+
"/global/warbler/bin.ts",
148+
root,
149+
),
150+
).toBeUndefined();
151+
});
152+
});

0 commit comments

Comments
 (0)