Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { createMemoryEnvironment } from '../src/environment.js'
3
4import {
5 getPackageManagerExecuteCommand,
6 getPackageManagerInstallCommand,
7 getPackageManagerScriptCommand,
8 packageManagerInstall,
9 translateExecuteCommand,
10} from '../src/package-manager.js'
11import { formatCommand } from '../src/utils.js'
12
13describe('installing after package.json changes', () => {
14 it('updates the pnpm lockfile only when requested', async () => {
15 const { environment, output } = createMemoryEnvironment()
16 await packageManagerInstall(environment, '/app', 'pnpm')
17 await packageManagerInstall(environment, '/app', 'pnpm', undefined, {
18 updateLockfile: true,
19 })
20 await packageManagerInstall(environment, '/app', 'npm', undefined, {
21 updateLockfile: true,
22 })
23 expect(output.commands).toEqual([
24 { command: 'pnpm', args: ['install'] },
25 { command: 'pnpm', args: ['install', '--no-frozen-lockfile'] },
26 { command: 'npm', args: ['install'] },
27 ])
28 })
29})
30
31describe('getPackageManagerScriptCommand', () => {
32 it('yarn', () => {
33 expect(formatCommand(getPackageManagerScriptCommand('yarn', ['dev']))).toBe(
34 'yarn run dev',
35 )
36 })
37 it('pnpm', () => {
38 expect(formatCommand(getPackageManagerScriptCommand('pnpm', ['dev']))).toBe(
39 'pnpm dev',
40 )
41 })
42 it('bun', () => {
43 expect(formatCommand(getPackageManagerScriptCommand('bun', ['dev']))).toBe(
44 'bun --bun run dev',
45 )
46 })
47 it('deno', () => {
48 expect(formatCommand(getPackageManagerScriptCommand('deno', ['dev']))).toBe(
49 'deno task dev',
50 )
51 })
52 it('npm', () => {
53 expect(formatCommand(getPackageManagerScriptCommand('npm', ['dev']))).toBe(
54 'npm run dev',
55 )
56 })
57})
58
59describe('getPackageManagerExecuteCommand', () => {
60 it('yarn', () => {
61 expect(
62 formatCommand(
63 getPackageManagerExecuteCommand('yarn', 'shadcn', ['add', 'button']),
64 ),
65 ).toBe('yarn dlx shadcn add button')
66 })
67 it('pnpm', () => {
68 expect(
69 formatCommand(
70 getPackageManagerExecuteCommand('pnpm', 'shadcn', ['add', 'button']),
71 ),
72 ).toBe('pnpm dlx shadcn add button')
73 })
74 it('bun', () => {
75 expect(
76 formatCommand(
77 getPackageManagerExecuteCommand('bun', 'shadcn', ['add', 'button']),
78 ),
79 ).toBe('bunx --bun shadcn add button')
80 })
81 it('deno', () => {
82 expect(
83 formatCommand(
84 getPackageManagerExecuteCommand('deno', 'shadcn', ['add', 'button']),
85 ),
86 ).toBe('deno run npm:shadcn add button')
87 })
88 it('npm', () => {
89 expect(
90 formatCommand(
91 getPackageManagerExecuteCommand('npm', 'shadcn', ['add', 'button']),
92 ),
93 ).toBe('npx -y shadcn add button')
94 })
95})
96
97describe('getPackageManagerInstallCommand', () => {
98 it('yarn install', () => {
99 expect(formatCommand(getPackageManagerInstallCommand('yarn'))).toBe(
100 'yarn install',
101 )
102 })
103 it('pnpm install', () => {
104 expect(formatCommand(getPackageManagerInstallCommand('pnpm'))).toBe(
105 'pnpm install',
106 )
107 })
108 it('bun install', () => {
109 expect(formatCommand(getPackageManagerInstallCommand('bun'))).toBe(
110 'bun install',
111 )
112 })
113 it('deno install', () => {
114 expect(formatCommand(getPackageManagerInstallCommand('deno'))).toBe(
115 'deno install',
116 )
117 })
118 it('npm install', () => {
119 expect(formatCommand(getPackageManagerInstallCommand('npm'))).toBe(
120 'npm install',
121 )
122 })
123
124 it('yarn install radix-ui', () => {
125 expect(
126 formatCommand(getPackageManagerInstallCommand('yarn', 'radix-ui')),
127 ).toBe('yarn add radix-ui')
128 })
129 it('pnpm install radix-ui', () => {
130 expect(
131 formatCommand(getPackageManagerInstallCommand('pnpm', 'radix-ui')),
132 ).toBe('pnpm add radix-ui')
133 })
134 it('bun install radix-ui', () => {
135 expect(
136 formatCommand(getPackageManagerInstallCommand('bun', 'radix-ui')),
137 ).toBe('bun install radix-ui')
138 })
139 it('deno install radix-ui', () => {
140 expect(
141 formatCommand(getPackageManagerInstallCommand('deno', 'radix-ui')),
142 ).toBe('deno install radix-ui')
143 })
144 it('npm install radix-ui', () => {
145 expect(
146 formatCommand(getPackageManagerInstallCommand('npm', 'radix-ui')),
147 ).toBe('npm install radix-ui')
148 })
149
150 it('yarn install vitest in dev mode', () => {
151 expect(
152 formatCommand(getPackageManagerInstallCommand('yarn', 'vitest', true)),
153 ).toBe('yarn add vitest --dev')
154 })
155 it('pnpm install vitest in dev mode', () => {
156 expect(
157 formatCommand(getPackageManagerInstallCommand('pnpm', 'vitest', true)),
158 ).toBe('pnpm add vitest --dev')
159 })
160 it('bun install vitest in dev mode', () => {
161 expect(
162 formatCommand(getPackageManagerInstallCommand('bun', 'vitest', true)),
163 ).toBe('bun install vitest -D')
164 })
165 it('deno install vitest in dev mode', () => {
166 expect(
167 formatCommand(getPackageManagerInstallCommand('deno', 'vitest', true)),
168 ).toBe('deno install vitest -D')
169 })
170 it('npm install vitest in dev mode', () => {
171 expect(
172 formatCommand(getPackageManagerInstallCommand('npm', 'vitest', true)),
173 ).toBe('npm install vitest -D')
174 })
175})
176
177describe('translateExecuteCommand', () => {
178 it('should translate npx to bunx for bun', () => {
179 expect(
180 formatCommand(
181 translateExecuteCommand('bun', {
182 command: 'npx',
183 args: ['shadcn', 'add', 'button'],
184 }),
185 ),
186 ).toBe('bunx --bun shadcn add button')
187 })
188 it('should translate npx to pnpm dlx for pnpm', () => {
189 expect(
190 formatCommand(
191 translateExecuteCommand('pnpm', {
192 command: 'npx',
193 args: ['shadcn', 'add', 'button'],
194 }),
195 ),
196 ).toBe('pnpm dlx shadcn add button')
197 })
198 it('should translate npx to yarn dlx for yarn', () => {
199 expect(
200 formatCommand(
201 translateExecuteCommand('yarn', {
202 command: 'npx',
203 args: ['shadcn', 'add', 'button'],
204 }),
205 ),
206 ).toBe('yarn dlx shadcn add button')
207 })
208 it('should translate npx to deno run for deno', () => {
209 expect(
210 formatCommand(
211 translateExecuteCommand('deno', {
212 command: 'npx',
213 args: ['shadcn', 'add', 'button'],
214 }),
215 ),
216 ).toBe('deno run npm:shadcn add button')
217 })
218 it('should keep npx -y for npm', () => {
219 expect(
220 formatCommand(
221 translateExecuteCommand('npm', {
222 command: 'npx',
223 args: ['shadcn', 'add', 'button'],
224 }),
225 ),
226 ).toBe('npx -y shadcn add button')
227 })
228 it('should pass through non-npx commands unchanged', () => {
229 expect(
230 formatCommand(
231 translateExecuteCommand('bun', {
232 command: 'node',
233 args: ['script.js'],
234 }),
235 ),
236 ).toBe('node script.js')
237 })
238 it('should handle missing args gracefully', () => {
239 expect(
240 formatCommand(translateExecuteCommand('bun', { command: 'npx' })),
241 ).toBe('npx')
242 })
243 it('should strip -y flag from npx input', () => {
244 expect(
245 formatCommand(
246 translateExecuteCommand('pnpm', {
247 command: 'npx',
248 args: ['-y', 'shadcn', 'add', 'button'],
249 }),
250 ),
251 ).toBe('pnpm dlx shadcn add button')
252 })
253 it('should translate bunx to target package manager', () => {
254 expect(
255 formatCommand(
256 translateExecuteCommand('pnpm', {
257 command: 'bunx',
258 args: ['shadcn', 'add', 'button'],
259 }),
260 ),
261 ).toBe('pnpm dlx shadcn add button')
262 })
263 it('should strip --bun flag from bunx input', () => {
264 expect(
265 formatCommand(
266 translateExecuteCommand('pnpm', {
267 command: 'bunx',
268 args: ['--bun', 'shadcn', 'add', 'button'],
269 }),
270 ),
271 ).toBe('pnpm dlx shadcn add button')
272 })
273 it('should translate pnpx to target package manager', () => {
274 expect(
275 formatCommand(
276 translateExecuteCommand('yarn', {
277 command: 'pnpx',
278 args: ['shadcn', 'add', 'button'],
279 }),
280 ),
281 ).toBe('yarn dlx shadcn add button')
282 })
283 it('should translate pnpm dlx to target package manager', () => {
284 expect(
285 formatCommand(
286 translateExecuteCommand('bun', {
287 command: 'pnpm',
288 args: ['dlx', 'shadcn', 'add', 'button'],
289 }),
290 ),
291 ).toBe('bunx --bun shadcn add button')
292 })
293 it('should translate yarn dlx to target package manager', () => {
294 expect(
295 formatCommand(
296 translateExecuteCommand('npm', {
297 command: 'yarn',
298 args: ['dlx', 'shadcn', 'add', 'button'],
299 }),
300 ),
301 ).toBe('npx -y shadcn add button')
302 })
303 it('should translate deno run npm: to target package manager', () => {
304 expect(
305 formatCommand(
306 translateExecuteCommand('pnpm', {
307 command: 'deno',
308 args: ['run', 'npm:shadcn', 'add', 'button'],
309 }),
310 ),
311 ).toBe('pnpm dlx shadcn add button')
312 })
313 it('should pass through non-execute pnpm commands unchanged', () => {
314 expect(
315 formatCommand(
316 translateExecuteCommand('bun', {
317 command: 'pnpm',
318 args: ['install'],
319 }),
320 ),
321 ).toBe('pnpm install')
322 })
323 it('should pass through non-execute deno commands unchanged', () => {
324 expect(
325 formatCommand(
326 translateExecuteCommand('npm', {
327 command: 'deno',
328 args: ['task', 'dev'],
329 }),
330 ),
331 ).toBe('deno task dev')
332 })
333})
334
335describe('allowBuilds', () => {
336 it('should add --allow-build flags before dlx for pnpm', () => {
337 expect(
338 formatCommand(
339 getPackageManagerExecuteCommand(
340 'pnpm',
341 'storybook',
342 ['init', '-y', '--no-dev'],
343 { allowBuilds: ['esbuild'], packageManagerVersion: '10.2.0' },
344 ),
345 ),
346 ).toBe('pnpm --allow-build=esbuild dlx storybook init -y --no-dev')
347 })
348 it('should emit one --allow-build flag per package for pnpm', () => {
349 expect(
350 formatCommand(
351 getPackageManagerExecuteCommand('pnpm', 'storybook', ['init'], {
352 allowBuilds: ['esbuild', 'sharp'],
353 packageManagerVersion: '10.2.0',
354 }),
355 ),
356 ).toBe('pnpm --allow-build=esbuild --allow-build=sharp dlx storybook init')
357 })
358 it('should ignore allowBuilds for non-pnpm package managers', () => {
359 expect(
360 formatCommand(
361 getPackageManagerExecuteCommand('npm', 'storybook', ['init'], {
362 allowBuilds: ['esbuild'],
363 }),
364 ),
365 ).toBe('npx -y storybook init')
366 expect(
367 formatCommand(
368 getPackageManagerExecuteCommand('yarn', 'storybook', ['init'], {
369 allowBuilds: ['esbuild'],
370 }),
371 ),
372 ).toBe('yarn dlx storybook init')
373 expect(
374 formatCommand(
375 getPackageManagerExecuteCommand('bun', 'storybook', ['init'], {
376 allowBuilds: ['esbuild'],
377 }),
378 ),
379 ).toBe('bunx --bun storybook init')
380 expect(
381 formatCommand(
382 getPackageManagerExecuteCommand('deno', 'storybook', ['init'], {
383 allowBuilds: ['esbuild'],
384 }),
385 ),
386 ).toBe('deno run npm:storybook init')
387 })
388 it('should not add flags for pnpm when allowBuilds is empty', () => {
389 expect(
390 formatCommand(
391 getPackageManagerExecuteCommand('pnpm', 'storybook', ['init'], {
392 allowBuilds: [],
393 }),
394 ),
395 ).toBe('pnpm dlx storybook init')
396 })
397 it('should forward allowBuilds through translateExecuteCommand for pnpm', () => {
398 expect(
399 formatCommand(
400 translateExecuteCommand('pnpm', {
401 command: 'npx',
402 args: ['storybook', 'init', '-y', '--no-dev'],
403 allowBuilds: ['esbuild'],
404 }, '10.2.0'),
405 ),
406 ).toBe('pnpm --allow-build=esbuild dlx storybook init -y --no-dev')
407 })
408 it('should drop allowBuilds through translateExecuteCommand for npm', () => {
409 expect(
410 formatCommand(
411 translateExecuteCommand('npm', {
412 command: 'npx',
413 args: ['storybook', 'init', '-y', '--no-dev'],
414 allowBuilds: ['esbuild'],
415 }),
416 ),
417 ).toBe('npx -y storybook init -y --no-dev')
418 })
419})
420
421
422describe('pnpm dlx version compatibility', () => {
423 it.each([undefined, '9.15.5', '10.1.0'])(
424 'retains plain dlx for pnpm %s',
425 (packageManagerVersion) => {
426 expect(getPackageManagerExecuteCommand('pnpm', 'storybook', ['init'], {
427 allowBuilds: ['esbuild'], packageManagerVersion,
428 })).toEqual({ command: 'pnpm', args: ['dlx', 'storybook', 'init'] })
429 },
430 )
431
432 it.each(['10.2.0', '10.34.0', '11.22.0'])(
433 'approves only requested build scripts for pnpm %s',
434 (packageManagerVersion) => {
435 expect(getPackageManagerExecuteCommand('pnpm', 'storybook', ['init'], {
436 allowBuilds: ['esbuild'], packageManagerVersion,
437 })).toEqual({ command: 'pnpm', args: ['--allow-build=esbuild', 'dlx', 'storybook', 'init'] })
438 },
439 )
440
441 it('rejects an invalid detected version before executing approvals', () => {
442 expect(() => getPackageManagerExecuteCommand('pnpm', 'storybook', [], {
443 allowBuilds: ['esbuild'], packageManagerVersion: 'not-a-version',
444 })).toThrow('Invalid pnpm version')
445 })
446})