Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import {
4 getPackageManagerExecuteCommand,
5 getPackageManagerInstallCommand,
6 getPackageManagerScriptCommand,
7 translateExecuteCommand,
8} from '../src/package-manager.js'
9import { formatCommand } from '../src/utils.js'
10
11describe('getPackageManagerScriptCommand', () => {
12 it('yarn', () => {
13 expect(formatCommand(getPackageManagerScriptCommand('yarn', ['dev']))).toBe(
14 'yarn run dev',
15 )
16 })
17 it('pnpm', () => {
18 expect(formatCommand(getPackageManagerScriptCommand('pnpm', ['dev']))).toBe(
19 'pnpm dev',
20 )
21 })
22 it('bun', () => {
23 expect(formatCommand(getPackageManagerScriptCommand('bun', ['dev']))).toBe(
24 'bun --bun run dev',
25 )
26 })
27 it('deno', () => {
28 expect(formatCommand(getPackageManagerScriptCommand('deno', ['dev']))).toBe(
29 'deno task dev',
30 )
31 })
32 it('npm', () => {
33 expect(formatCommand(getPackageManagerScriptCommand('npm', ['dev']))).toBe(
34 'npm run dev',
35 )
36 })
37})
38
39describe('getPackageManagerExecuteCommand', () => {
40 it('yarn', () => {
41 expect(
42 formatCommand(
43 getPackageManagerExecuteCommand('yarn', 'shadcn', ['add', 'button']),
44 ),
45 ).toBe('yarn dlx shadcn add button')
46 })
47 it('pnpm', () => {
48 expect(
49 formatCommand(
50 getPackageManagerExecuteCommand('pnpm', 'shadcn', ['add', 'button']),
51 ),
52 ).toBe('pnpm dlx shadcn add button')
53 })
54 it('bun', () => {
55 expect(
56 formatCommand(
57 getPackageManagerExecuteCommand('bun', 'shadcn', ['add', 'button']),
58 ),
59 ).toBe('bunx --bun shadcn add button')
60 })
61 it('deno', () => {
62 expect(
63 formatCommand(
64 getPackageManagerExecuteCommand('deno', 'shadcn', ['add', 'button']),
65 ),
66 ).toBe('deno run npm:shadcn add button')
67 })
68 it('npm', () => {
69 expect(
70 formatCommand(
71 getPackageManagerExecuteCommand('npm', 'shadcn', ['add', 'button']),
72 ),
73 ).toBe('npx -y shadcn add button')
74 })
75})
76
77describe('getPackageManagerInstallCommand', () => {
78 it('yarn install', () => {
79 expect(formatCommand(getPackageManagerInstallCommand('yarn'))).toBe(
80 'yarn install',
81 )
82 })
83 it('pnpm install', () => {
84 expect(formatCommand(getPackageManagerInstallCommand('pnpm'))).toBe(
85 'pnpm install',
86 )
87 })
88 it('bun install', () => {
89 expect(formatCommand(getPackageManagerInstallCommand('bun'))).toBe(
90 'bun install',
91 )
92 })
93 it('deno install', () => {
94 expect(formatCommand(getPackageManagerInstallCommand('deno'))).toBe(
95 'deno install',
96 )
97 })
98 it('npm install', () => {
99 expect(formatCommand(getPackageManagerInstallCommand('npm'))).toBe(
100 'npm install',
101 )
102 })
103
104 it('yarn install radix-ui', () => {
105 expect(
106 formatCommand(getPackageManagerInstallCommand('yarn', 'radix-ui')),
107 ).toBe('yarn add radix-ui')
108 })
109 it('pnpm install radix-ui', () => {
110 expect(
111 formatCommand(getPackageManagerInstallCommand('pnpm', 'radix-ui')),
112 ).toBe('pnpm add radix-ui')
113 })
114 it('bun install radix-ui', () => {
115 expect(
116 formatCommand(getPackageManagerInstallCommand('bun', 'radix-ui')),
117 ).toBe('bun install radix-ui')
118 })
119 it('deno install radix-ui', () => {
120 expect(
121 formatCommand(getPackageManagerInstallCommand('deno', 'radix-ui')),
122 ).toBe('deno install radix-ui')
123 })
124 it('npm install radix-ui', () => {
125 expect(
126 formatCommand(getPackageManagerInstallCommand('npm', 'radix-ui')),
127 ).toBe('npm install radix-ui')
128 })
129
130 it('yarn install vitest in dev mode', () => {
131 expect(
132 formatCommand(getPackageManagerInstallCommand('yarn', 'vitest', true)),
133 ).toBe('yarn add vitest --dev')
134 })
135 it('pnpm install vitest in dev mode', () => {
136 expect(
137 formatCommand(getPackageManagerInstallCommand('pnpm', 'vitest', true)),
138 ).toBe('pnpm add vitest --dev')
139 })
140 it('bun install vitest in dev mode', () => {
141 expect(
142 formatCommand(getPackageManagerInstallCommand('bun', 'vitest', true)),
143 ).toBe('bun install vitest -D')
144 })
145 it('deno install vitest in dev mode', () => {
146 expect(
147 formatCommand(getPackageManagerInstallCommand('deno', 'vitest', true)),
148 ).toBe('deno install vitest -D')
149 })
150 it('npm install vitest in dev mode', () => {
151 expect(
152 formatCommand(getPackageManagerInstallCommand('npm', 'vitest', true)),
153 ).toBe('npm install vitest -D')
154 })
155})
156
157describe('translateExecuteCommand', () => {
158 it('should translate npx to bunx for bun', () => {
159 expect(
160 formatCommand(
161 translateExecuteCommand('bun', {
162 command: 'npx',
163 args: ['shadcn', 'add', 'button'],
164 }),
165 ),
166 ).toBe('bunx --bun shadcn add button')
167 })
168 it('should translate npx to pnpm dlx for pnpm', () => {
169 expect(
170 formatCommand(
171 translateExecuteCommand('pnpm', {
172 command: 'npx',
173 args: ['shadcn', 'add', 'button'],
174 }),
175 ),
176 ).toBe('pnpm dlx shadcn add button')
177 })
178 it('should translate npx to yarn dlx for yarn', () => {
179 expect(
180 formatCommand(
181 translateExecuteCommand('yarn', {
182 command: 'npx',
183 args: ['shadcn', 'add', 'button'],
184 }),
185 ),
186 ).toBe('yarn dlx shadcn add button')
187 })
188 it('should translate npx to deno run for deno', () => {
189 expect(
190 formatCommand(
191 translateExecuteCommand('deno', {
192 command: 'npx',
193 args: ['shadcn', 'add', 'button'],
194 }),
195 ),
196 ).toBe('deno run npm:shadcn add button')
197 })
198 it('should keep npx -y for npm', () => {
199 expect(
200 formatCommand(
201 translateExecuteCommand('npm', {
202 command: 'npx',
203 args: ['shadcn', 'add', 'button'],
204 }),
205 ),
206 ).toBe('npx -y shadcn add button')
207 })
208 it('should pass through non-npx commands unchanged', () => {
209 expect(
210 formatCommand(
211 translateExecuteCommand('bun', {
212 command: 'node',
213 args: ['script.js'],
214 }),
215 ),
216 ).toBe('node script.js')
217 })
218 it('should handle missing args gracefully', () => {
219 expect(
220 formatCommand(translateExecuteCommand('bun', { command: 'npx' })),
221 ).toBe('npx')
222 })
223 it('should strip -y flag from npx input', () => {
224 expect(
225 formatCommand(
226 translateExecuteCommand('pnpm', {
227 command: 'npx',
228 args: ['-y', 'shadcn', 'add', 'button'],
229 }),
230 ),
231 ).toBe('pnpm dlx shadcn add button')
232 })
233 it('should translate bunx to target package manager', () => {
234 expect(
235 formatCommand(
236 translateExecuteCommand('pnpm', {
237 command: 'bunx',
238 args: ['shadcn', 'add', 'button'],
239 }),
240 ),
241 ).toBe('pnpm dlx shadcn add button')
242 })
243 it('should strip --bun flag from bunx input', () => {
244 expect(
245 formatCommand(
246 translateExecuteCommand('pnpm', {
247 command: 'bunx',
248 args: ['--bun', 'shadcn', 'add', 'button'],
249 }),
250 ),
251 ).toBe('pnpm dlx shadcn add button')
252 })
253 it('should translate pnpx to target package manager', () => {
254 expect(
255 formatCommand(
256 translateExecuteCommand('yarn', {
257 command: 'pnpx',
258 args: ['shadcn', 'add', 'button'],
259 }),
260 ),
261 ).toBe('yarn dlx shadcn add button')
262 })
263 it('should translate pnpm dlx to target package manager', () => {
264 expect(
265 formatCommand(
266 translateExecuteCommand('bun', {
267 command: 'pnpm',
268 args: ['dlx', 'shadcn', 'add', 'button'],
269 }),
270 ),
271 ).toBe('bunx --bun shadcn add button')
272 })
273 it('should translate yarn dlx to target package manager', () => {
274 expect(
275 formatCommand(
276 translateExecuteCommand('npm', {
277 command: 'yarn',
278 args: ['dlx', 'shadcn', 'add', 'button'],
279 }),
280 ),
281 ).toBe('npx -y shadcn add button')
282 })
283 it('should translate deno run npm: to target package manager', () => {
284 expect(
285 formatCommand(
286 translateExecuteCommand('pnpm', {
287 command: 'deno',
288 args: ['run', 'npm:shadcn', 'add', 'button'],
289 }),
290 ),
291 ).toBe('pnpm dlx shadcn add button')
292 })
293 it('should pass through non-execute pnpm commands unchanged', () => {
294 expect(
295 formatCommand(
296 translateExecuteCommand('bun', {
297 command: 'pnpm',
298 args: ['install'],
299 }),
300 ),
301 ).toBe('pnpm install')
302 })
303 it('should pass through non-execute deno commands unchanged', () => {
304 expect(
305 formatCommand(
306 translateExecuteCommand('npm', {
307 command: 'deno',
308 args: ['task', 'dev'],
309 }),
310 ),
311 ).toBe('deno task dev')
312 })
313})