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