|
1
|
import PackageManager from "../package-manager" |
|
2
|
import ProcessManager from "../process-manager" |
|
3
|
import execute from "../execute" |
|
4
|
import { existsSync, readFileSync, rmSync, writeFileSync } from "fs" |
|
5
|
import { parse as parseYaml } from "yaml" |
|
6
|
import logMessage from "../log-message" |
|
7
|
|
|
8
|
// Mock dependencies |
|
9
|
jest.mock("../execute") |
|
10
|
jest.mock("fs") |
|
11
|
jest.mock("yaml") |
|
12
|
jest.mock("../log-message") |
|
13
|
|
|
14
|
const mockExecute = execute as jest.MockedFunction<typeof execute> |
|
15
|
const mockExistsSync = existsSync as jest.MockedFunction<typeof existsSync> |
|
16
|
const mockRmSync = rmSync as jest.MockedFunction<typeof rmSync> |
|
17
|
const mockLogMessage = logMessage as jest.MockedFunction<typeof logMessage> |
|
18
|
const mockReadFileSync = readFileSync as jest.Mock |
|
19
|
const mockWriteFileSync = writeFileSync as jest.Mock |
|
20
|
const mockParseYaml = parseYaml as jest.MockedFunction<typeof parseYaml> |
|
21
|
|
|
22
|
describe("PackageManager", () => { |
|
23
|
let processManager: ProcessManager |
|
24
|
let originalEnv: NodeJS.ProcessEnv |
|
25
|
|
|
26
|
beforeEach(() => { |
|
27
|
processManager = new ProcessManager() |
|
28
|
originalEnv = { ...process.env } |
|
29
|
jest.clearAllMocks() |
|
30
|
// Default setup so transformWorkspaceConfig doesn't throw in unrelated tests |
|
31
|
mockReadFileSync.mockReturnValue(JSON.stringify({ name: "test", scripts: {} })) |
|
32
|
mockParseYaml.mockReturnValue({ packages: [] }) |
|
33
|
}) |
|
34
|
|
|
35
|
afterEach(() => { |
|
36
|
process.env = originalEnv |
|
37
|
}) |
|
38
|
|
|
39
|
describe("constructor", () => { |
|
40
|
it("should initialize with default options", () => { |
|
41
|
const pm = new PackageManager(processManager) |
|
42
|
expect(pm.getPackageManager()).toBeUndefined() |
|
43
|
}) |
|
44
|
|
|
45
|
it("should set npm as chosen package manager when useNpm is true", () => { |
|
46
|
const pm = new PackageManager(processManager, { useNpm: true }) |
|
47
|
expect(pm["chosenPackageManager"]).toBe("npm") |
|
48
|
}) |
|
49
|
|
|
50
|
it("should set yarn as chosen package manager when useYarn is true", () => { |
|
51
|
const pm = new PackageManager(processManager, { useYarn: true }) |
|
52
|
expect(pm["chosenPackageManager"]).toBe("yarn") |
|
53
|
}) |
|
54
|
|
|
55
|
it("should set pnpm as chosen package manager when usePnpm is true", () => { |
|
56
|
const pm = new PackageManager(processManager, { usePnpm: true }) |
|
57
|
expect(pm["chosenPackageManager"]).toBe("pnpm") |
|
58
|
}) |
|
59
|
|
|
60
|
it("should respect verbose option", () => { |
|
61
|
const pm = new PackageManager(processManager, { verbose: true }) |
|
62
|
expect(pm["verbose"]).toBe(true) |
|
63
|
}) |
|
64
|
|
|
65
|
it("should prioritize npm over other options", () => { |
|
66
|
const pm = new PackageManager(processManager, { |
|
67
|
useNpm: true, |
|
68
|
useYarn: true, |
|
69
|
usePnpm: true, |
|
70
|
}) |
|
71
|
expect(pm["chosenPackageManager"]).toBe("npm") |
|
72
|
}) |
|
73
|
}) |
|
74
|
|
|
75
|
describe("detectFromUserAgent", () => { |
|
76
|
it("should detect pnpm from user agent with version", () => { |
|
77
|
process.env.npm_config_user_agent = "pnpm/8.0.0" |
|
78
|
const pm = new PackageManager(processManager) |
|
79
|
const result = pm["detectFromUserAgent"]() |
|
80
|
expect(result.manager).toBe("pnpm") |
|
81
|
expect(result.version).toBe("8.0.0") |
|
82
|
}) |
|
83
|
|
|
84
|
it("should detect pnpm from pnpx with version", () => { |
|
85
|
process.env.npm_config_user_agent = "pnpx/8.0.0" |
|
86
|
const pm = new PackageManager(processManager) |
|
87
|
const result = pm["detectFromUserAgent"]() |
|
88
|
expect(result.manager).toBe("pnpm") |
|
89
|
expect(result.version).toBe("8.0.0") |
|
90
|
}) |
|
91
|
|
|
92
|
it("should detect yarn from user agent with version", () => { |
|
93
|
process.env.npm_config_user_agent = "yarn/1.22.0" |
|
94
|
const pm = new PackageManager(processManager) |
|
95
|
const result = pm["detectFromUserAgent"]() |
|
96
|
expect(result.manager).toBe("yarn") |
|
97
|
expect(result.version).toBe("1.22.0") |
|
98
|
}) |
|
99
|
|
|
100
|
it("should detect nub from user agent with version", () => { |
|
101
|
// nub's user agent also contains "npm" (e.g. `nub/0.4.5 npm/?`), so this |
|
102
|
// confirms nub is matched rather than falling through to npm. |
|
103
|
process.env.npm_config_user_agent = "nub/0.4.5 npm/? node/v24.14.0" |
|
104
|
const pm = new PackageManager(processManager) |
|
105
|
const result = pm["detectFromUserAgent"]() |
|
106
|
expect(result.manager).toBe("nub") |
|
107
|
expect(result.version).toBe("0.4.5") |
|
108
|
}) |
|
109
|
|
|
110
|
it("should detect nub from user agent without version", () => { |
|
111
|
process.env.npm_config_user_agent = "nub" |
|
112
|
const pm = new PackageManager(processManager) |
|
113
|
const result = pm["detectFromUserAgent"]() |
|
114
|
expect(result.manager).toBe("nub") |
|
115
|
expect(result.version).toBeUndefined() |
|
116
|
}) |
|
117
|
|
|
118
|
it("should default to npm for unknown user agent", () => { |
|
119
|
process.env.npm_config_user_agent = "some-unknown-manager/1.0.0" |
|
120
|
const pm = new PackageManager(processManager) |
|
121
|
const result = pm["detectFromUserAgent"]() |
|
122
|
expect(result.manager).toBe("npm") |
|
123
|
expect(result.version).toBeUndefined() |
|
124
|
}) |
|
125
|
|
|
126
|
it("should default to npm when user agent is undefined", () => { |
|
127
|
delete process.env.npm_config_user_agent |
|
128
|
const pm = new PackageManager(processManager) |
|
129
|
const result = pm["detectFromUserAgent"]() |
|
130
|
expect(result.manager).toBe("npm") |
|
131
|
expect(result.version).toBeUndefined() |
|
132
|
}) |
|
133
|
}) |
|
134
|
|
|
135
|
describe("setPackageManager", () => { |
|
136
|
it("should not run if package manager is already set", async () => { |
|
137
|
const pm = new PackageManager(processManager) |
|
138
|
pm["packageManager"] = "yarn" |
|
139
|
|
|
140
|
await pm.setPackageManager({}) |
|
141
|
|
|
142
|
expect(mockExecute).not.toHaveBeenCalled() |
|
143
|
}) |
|
144
|
|
|
145
|
it("should set chosen package manager if available", async () => { |
|
146
|
mockExecute.mockResolvedValue({ stdout: "8.0.0", stderr: "" }) |
|
147
|
const pm = new PackageManager(processManager, { usePnpm: true }) |
|
148
|
|
|
149
|
await pm.setPackageManager({}) |
|
150
|
|
|
151
|
expect(pm.getPackageManager()).toBe("pnpm") |
|
152
|
expect(await pm.getPackageManagerString()).toBe("pnpm@8.0.0") |
|
153
|
expect(mockExecute).toHaveBeenCalledWith( |
|
154
|
["pnpm -v", {}], |
|
155
|
{ verbose: false } |
|
156
|
) |
|
157
|
}) |
|
158
|
|
|
159
|
it("should throw error when chosen package manager is not available", async () => { |
|
160
|
mockExecute.mockRejectedValueOnce(new Error("Command not found")) |
|
161
|
|
|
162
|
const pm = new PackageManager(processManager, { usePnpm: true }) |
|
163
|
|
|
164
|
await pm.setPackageManager({}) |
|
165
|
|
|
166
|
expect(mockLogMessage).toHaveBeenCalledWith({ |
|
167
|
type: "error", |
|
168
|
message: expect.stringContaining('"pnpm" is not available'), |
|
169
|
}) |
|
170
|
}) |
|
171
|
|
|
172
|
it("should detect from user agent when no package manager chosen", async () => { |
|
173
|
process.env.npm_config_user_agent = "yarn/1.22.0" |
|
174
|
|
|
175
|
const pm = new PackageManager(processManager) |
|
176
|
await pm.setPackageManager({}) |
|
177
|
|
|
178
|
expect(pm.getPackageManager()).toBe("yarn") |
|
179
|
expect(await pm.getPackageManagerString()).toBe("yarn@1.22.0") |
|
180
|
// Detection message should not be logged in non-verbose mode |
|
181
|
expect(mockLogMessage).not.toHaveBeenCalledWith({ |
|
182
|
type: "info", |
|
183
|
message: expect.stringContaining('Using detected package manager "yarn"'), |
|
184
|
}) |
|
185
|
// Should not call getVersion since version is in user agent |
|
186
|
expect(mockExecute).not.toHaveBeenCalled() |
|
187
|
}) |
|
188
|
|
|
189
|
it("should use detected package manager even without version in user agent", async () => { |
|
190
|
mockExecute.mockResolvedValue({ stdout: "1.22.0", stderr: "" }) |
|
191
|
process.env.npm_config_user_agent = "yarn" |
|
192
|
|
|
193
|
const pm = new PackageManager(processManager) |
|
194
|
await pm.setPackageManager({}) |
|
195
|
|
|
196
|
expect(pm.getPackageManager()).toBe("yarn") |
|
197
|
expect(await pm.getPackageManagerString()).toBe("yarn@1.22.0") |
|
198
|
// Should call getVersion since version is not in user agent |
|
199
|
expect(mockExecute).toHaveBeenCalledWith(["yarn -v", {}], { |
|
200
|
verbose: false, |
|
201
|
}) |
|
202
|
// Fallback message should not be logged in non-verbose mode |
|
203
|
expect(mockLogMessage).not.toHaveBeenCalledWith({ |
|
204
|
type: "info", |
|
205
|
message: expect.stringContaining("Falling back to"), |
|
206
|
}) |
|
207
|
}) |
|
208
|
|
|
209
|
it("should log detection messages in verbose mode", async () => { |
|
210
|
process.env.npm_config_user_agent = "yarn/1.22.0" |
|
211
|
|
|
212
|
const pm = new PackageManager(processManager, { verbose: true }) |
|
213
|
await pm.setPackageManager({}) |
|
214
|
|
|
215
|
expect(pm.getPackageManager()).toBe("yarn") |
|
216
|
expect(mockLogMessage).toHaveBeenCalledWith({ |
|
217
|
type: "info", |
|
218
|
message: expect.stringContaining('Using detected package manager "yarn"'), |
|
219
|
}) |
|
220
|
// Should not call getVersion since version is in user agent |
|
221
|
expect(mockExecute).not.toHaveBeenCalled() |
|
222
|
}) |
|
223
|
|
|
224
|
it("should log fallback messages in verbose mode when no version in user agent", async () => { |
|
225
|
mockExecute.mockResolvedValue({ stdout: "1.22.0", stderr: "" }) |
|
226
|
process.env.npm_config_user_agent = "yarn" |
|
227
|
|
|
228
|
const pm = new PackageManager(processManager, { verbose: true }) |
|
229
|
await pm.setPackageManager({}) |
|
230
|
|
|
231
|
expect(pm.getPackageManager()).toBe("yarn") |
|
232
|
expect(mockLogMessage).toHaveBeenCalledWith({ |
|
233
|
type: "info", |
|
234
|
message: expect.stringContaining("Falling back to yarn"), |
|
235
|
}) |
|
236
|
// Should call getVersion to get the version |
|
237
|
expect(mockExecute).toHaveBeenCalledWith(["yarn -v", {}], { |
|
238
|
verbose: false, |
|
239
|
}) |
|
240
|
}) |
|
241
|
}) |
|
242
|
|
|
243
|
describe("removeLockFiles", () => { |
|
244
|
it("should not remove files if package manager is not set", async () => { |
|
245
|
const pm = new PackageManager(processManager) |
|
246
|
|
|
247
|
await pm.removeLockFiles("/test/path") |
|
248
|
|
|
249
|
expect(mockExistsSync).not.toHaveBeenCalled() |
|
250
|
expect(mockRmSync).not.toHaveBeenCalled() |
|
251
|
}) |
|
252
|
|
|
253
|
it("should remove yarn.lock, pnpm-lock.yaml, and .yarn when using npm", async () => { |
|
254
|
const pm = new PackageManager(processManager) |
|
255
|
pm["packageManager"] = "npm" |
|
256
|
mockExistsSync.mockReturnValue(true) |
|
257
|
|
|
258
|
await pm.removeLockFiles("/test/path") |
|
259
|
|
|
260
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/yarn.lock") |
|
261
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/pnpm-lock.yaml") |
|
262
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/.yarn") |
|
263
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/yarn.lock", { |
|
264
|
force: true, |
|
265
|
recursive: true, |
|
266
|
}) |
|
267
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/pnpm-lock.yaml", { |
|
268
|
force: true, |
|
269
|
recursive: true, |
|
270
|
}) |
|
271
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/.yarn", { |
|
272
|
force: true, |
|
273
|
recursive: true, |
|
274
|
}) |
|
275
|
}) |
|
276
|
|
|
277
|
it("should remove package-lock.json and pnpm-lock.yaml when using yarn", async () => { |
|
278
|
const pm = new PackageManager(processManager) |
|
279
|
pm["packageManager"] = "yarn" |
|
280
|
mockExistsSync.mockReturnValue(true) |
|
281
|
|
|
282
|
await pm.removeLockFiles("/test/path") |
|
283
|
|
|
284
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/package-lock.json") |
|
285
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/pnpm-lock.yaml") |
|
286
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/package-lock.json", { |
|
287
|
force: true, |
|
288
|
recursive: true, |
|
289
|
}) |
|
290
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/pnpm-lock.yaml", { |
|
291
|
force: true, |
|
292
|
recursive: true, |
|
293
|
}) |
|
294
|
}) |
|
295
|
|
|
296
|
it("should remove yarn.lock, package-lock.json, and .yarn when using pnpm", async () => { |
|
297
|
const pm = new PackageManager(processManager) |
|
298
|
pm["packageManager"] = "pnpm" |
|
299
|
mockExistsSync.mockReturnValue(true) |
|
300
|
|
|
301
|
await pm.removeLockFiles("/test/path") |
|
302
|
|
|
303
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/yarn.lock") |
|
304
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/package-lock.json") |
|
305
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/.yarn") |
|
306
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/yarn.lock", { |
|
307
|
force: true, |
|
308
|
recursive: true, |
|
309
|
}) |
|
310
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/package-lock.json", { |
|
311
|
force: true, |
|
312
|
recursive: true, |
|
313
|
}) |
|
314
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/.yarn", { |
|
315
|
force: true, |
|
316
|
recursive: true, |
|
317
|
}) |
|
318
|
}) |
|
319
|
|
|
320
|
it("should remove yarn.lock, package-lock.json, pnpm-lock.yaml, and .yarn when using nub", async () => { |
|
321
|
const pm = new PackageManager(processManager) |
|
322
|
pm["packageManager"] = "nub" |
|
323
|
mockExistsSync.mockReturnValue(true) |
|
324
|
|
|
325
|
await pm.removeLockFiles("/test/path") |
|
326
|
|
|
327
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/yarn.lock") |
|
328
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/package-lock.json") |
|
329
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/pnpm-lock.yaml") |
|
330
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/.yarn") |
|
331
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/yarn.lock", { |
|
332
|
force: true, |
|
333
|
recursive: true, |
|
334
|
}) |
|
335
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/package-lock.json", { |
|
336
|
force: true, |
|
337
|
recursive: true, |
|
338
|
}) |
|
339
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/pnpm-lock.yaml", { |
|
340
|
force: true, |
|
341
|
recursive: true, |
|
342
|
}) |
|
343
|
expect(mockRmSync).toHaveBeenCalledWith("/test/path/.yarn", { |
|
344
|
force: true, |
|
345
|
recursive: true, |
|
346
|
}) |
|
347
|
}) |
|
348
|
|
|
349
|
it("should not remove files that don't exist", async () => { |
|
350
|
const pm = new PackageManager(processManager) |
|
351
|
pm["packageManager"] = "npm" |
|
352
|
mockExistsSync.mockReturnValue(false) |
|
353
|
|
|
354
|
await pm.removeLockFiles("/test/path") |
|
355
|
|
|
356
|
expect(mockExistsSync).toHaveBeenCalled() |
|
357
|
expect(mockRmSync).not.toHaveBeenCalled() |
|
358
|
}) |
|
359
|
}) |
|
360
|
|
|
361
|
describe("transformWorkspaceConfig", () => { |
|
362
|
const YAML_CONTENT = "packages:\n - 'apps/*'\n - 'apps/storefront'" |
|
363
|
const makePackageJson = (extra: Record<string, unknown> = {}) => |
|
364
|
JSON.stringify({ |
|
365
|
name: "test", |
|
366
|
scripts: { dev: "pnpm -r dev", build: "pnpm -r build", start: "pnpm start" }, |
|
367
|
...extra, |
|
368
|
}) |
|
369
|
|
|
370
|
const getWrittenPackageJson = () => { |
|
371
|
const call = mockWriteFileSync.mock.calls.find( |
|
372
|
([filePath]: [string]) => filePath === "/test/path/package.json" |
|
373
|
) |
|
374
|
return call ? JSON.parse(call[1] as string) : null |
|
375
|
} |
|
376
|
|
|
377
|
beforeEach(() => { |
|
378
|
mockExistsSync.mockReturnValue(true) |
|
379
|
mockReadFileSync |
|
380
|
.mockReturnValueOnce(YAML_CONTENT) |
|
381
|
.mockReturnValueOnce(makePackageJson()) |
|
382
|
mockParseYaml.mockReturnValue({ packages: ["apps/*", "apps/storefront"] }) |
|
383
|
}) |
|
384
|
|
|
385
|
it("should do nothing when package manager is pnpm", async () => { |
|
386
|
const pm = new PackageManager(processManager) |
|
387
|
pm["packageManager"] = "pnpm" |
|
388
|
|
|
389
|
await pm.transformWorkspaceConfig("/test/path") |
|
390
|
|
|
391
|
expect(mockExistsSync).not.toHaveBeenCalled() |
|
392
|
expect(mockReadFileSync).not.toHaveBeenCalled() |
|
393
|
expect(mockWriteFileSync).not.toHaveBeenCalled() |
|
394
|
}) |
|
395
|
|
|
396
|
it("should do nothing when pnpm-workspace.yaml does not exist", async () => { |
|
397
|
mockExistsSync.mockReturnValue(false) |
|
398
|
|
|
399
|
const pm = new PackageManager(processManager) |
|
400
|
pm["packageManager"] = "yarn" |
|
401
|
|
|
402
|
await pm.transformWorkspaceConfig("/test/path") |
|
403
|
|
|
404
|
expect(mockReadFileSync).not.toHaveBeenCalled() |
|
405
|
expect(mockWriteFileSync).not.toHaveBeenCalled() |
|
406
|
}) |
|
407
|
|
|
408
|
it("should add workspaces from pnpm-workspace.yaml to package.json", async () => { |
|
409
|
const pm = new PackageManager(processManager) |
|
410
|
pm["packageManager"] = "yarn" |
|
411
|
|
|
412
|
await pm.transformWorkspaceConfig("/test/path") |
|
413
|
|
|
414
|
const written = getWrittenPackageJson() |
|
415
|
expect(written.workspaces).toEqual(["apps/*", "apps/storefront"]) |
|
416
|
}) |
|
417
|
|
|
418
|
it("should replace pnpm -r scripts with turbo", async () => { |
|
419
|
const pm = new PackageManager(processManager) |
|
420
|
pm["packageManager"] = "yarn" |
|
421
|
|
|
422
|
await pm.transformWorkspaceConfig("/test/path") |
|
423
|
|
|
424
|
const written = getWrittenPackageJson() |
|
425
|
expect(written.scripts.dev).toBe("turbo dev") |
|
426
|
expect(written.scripts.build).toBe("turbo build") |
|
427
|
}) |
|
428
|
|
|
429
|
it("should replace non-recursive pnpm scripts with the package manager", async () => { |
|
430
|
const pm = new PackageManager(processManager) |
|
431
|
pm["packageManager"] = "yarn" |
|
432
|
|
|
433
|
await pm.transformWorkspaceConfig("/test/path") |
|
434
|
|
|
435
|
const written = getWrittenPackageJson() |
|
436
|
expect(written.scripts.start).toBe("yarn start") |
|
437
|
}) |
|
438
|
|
|
439
|
it("should replace pnpm -r scripts with turbo when using npm", async () => { |
|
440
|
const pm = new PackageManager(processManager) |
|
441
|
pm["packageManager"] = "npm" |
|
442
|
|
|
443
|
await pm.transformWorkspaceConfig("/test/path") |
|
444
|
|
|
445
|
const written = getWrittenPackageJson() |
|
446
|
expect(written.scripts.dev).toBe("turbo dev") |
|
447
|
expect(written.scripts.build).toBe("turbo build") |
|
448
|
}) |
|
449
|
|
|
450
|
it("should replace non-recursive pnpm scripts with npm when using npm", async () => { |
|
451
|
const pm = new PackageManager(processManager) |
|
452
|
pm["packageManager"] = "npm" |
|
453
|
|
|
454
|
await pm.transformWorkspaceConfig("/test/path") |
|
455
|
|
|
456
|
const written = getWrittenPackageJson() |
|
457
|
expect(written.scripts.start).toBe("npm start") |
|
458
|
}) |
|
459
|
|
|
460
|
it("should handle mixed pnpm -r and plain pnpm in the same script", async () => { |
|
461
|
mockReadFileSync |
|
462
|
.mockReset() |
|
463
|
.mockReturnValueOnce(YAML_CONTENT) |
|
464
|
.mockReturnValueOnce( |
|
465
|
JSON.stringify({ |
|
466
|
name: "test", |
|
467
|
scripts: { all: "pnpm -r build && pnpm start" }, |
|
468
|
}) |
|
469
|
) |
|
470
|
|
|
471
|
const pm = new PackageManager(processManager) |
|
472
|
pm["packageManager"] = "yarn" |
|
473
|
|
|
474
|
await pm.transformWorkspaceConfig("/test/path") |
|
475
|
|
|
476
|
const written = getWrittenPackageJson() |
|
477
|
expect(written.scripts.all).toBe("turbo build && yarn start") |
|
478
|
}) |
|
479
|
|
|
480
|
it("should delete pnpm-workspace.yaml", async () => { |
|
481
|
const pm = new PackageManager(processManager) |
|
482
|
pm["packageManager"] = "yarn" |
|
483
|
|
|
484
|
await pm.transformWorkspaceConfig("/test/path") |
|
485
|
|
|
486
|
expect(mockRmSync).toHaveBeenCalledWith( |
|
487
|
"/test/path/pnpm-workspace.yaml", |
|
488
|
{ force: true } |
|
489
|
) |
|
490
|
}) |
|
491
|
|
|
492
|
describe("yarn-specific changes", () => { |
|
493
|
it("should create an empty yarn.lock", async () => { |
|
494
|
const pm = new PackageManager(processManager) |
|
495
|
pm["packageManager"] = "yarn" |
|
496
|
|
|
497
|
await pm.transformWorkspaceConfig("/test/path") |
|
498
|
|
|
499
|
expect(mockWriteFileSync).toHaveBeenCalledWith( |
|
500
|
"/test/path/yarn.lock", |
|
501
|
"" |
|
502
|
) |
|
503
|
}) |
|
504
|
|
|
505
|
it("should create .yarnrc.yml with nodeLinker: node-modules", async () => { |
|
506
|
const pm = new PackageManager(processManager) |
|
507
|
pm["packageManager"] = "yarn" |
|
508
|
|
|
509
|
await pm.transformWorkspaceConfig("/test/path") |
|
510
|
|
|
511
|
expect(mockWriteFileSync).toHaveBeenCalledWith( |
|
512
|
"/test/path/.yarnrc.yml", |
|
513
|
"nodeLinker: node-modules\n" |
|
514
|
) |
|
515
|
}) |
|
516
|
|
|
517
|
it("should not add npm overrides", async () => { |
|
518
|
const pm = new PackageManager(processManager) |
|
519
|
pm["packageManager"] = "yarn" |
|
520
|
|
|
521
|
await pm.transformWorkspaceConfig("/test/path") |
|
522
|
|
|
523
|
const written = getWrittenPackageJson() |
|
524
|
expect(written.overrides).toBeUndefined() |
|
525
|
}) |
|
526
|
}) |
|
527
|
|
|
528
|
describe("npm-specific changes", () => { |
|
529
|
it("should pin ajv v8 at the root and scope v6 to ESLint", async () => { |
|
530
|
const pm = new PackageManager(processManager) |
|
531
|
pm["packageManager"] = "npm" |
|
532
|
|
|
533
|
await pm.transformWorkspaceConfig("/test/path") |
|
534
|
|
|
535
|
const written = getWrittenPackageJson() |
|
536
|
expect(written.devDependencies).toEqual( |
|
537
|
expect.objectContaining({ ajv: "^8.13.0" }) |
|
538
|
) |
|
539
|
expect(written.overrides).toEqual( |
|
540
|
expect.objectContaining({ |
|
541
|
eslint: { ajv: "^6.12.4" }, |
|
542
|
"@eslint/eslintrc": { ajv: "^6.12.4" }, |
|
543
|
}) |
|
544
|
) |
|
545
|
}) |
|
546
|
|
|
547
|
it("should preserve existing overrides and devDependencies when adding ajv", async () => { |
|
548
|
mockReadFileSync |
|
549
|
.mockReset() |
|
550
|
.mockReturnValueOnce(YAML_CONTENT) |
|
551
|
.mockReturnValueOnce( |
|
552
|
makePackageJson({ |
|
553
|
devDependencies: { typescript: "^5.0.0" }, |
|
554
|
overrides: { someOtherPackage: "^1.0.0" }, |
|
555
|
}) |
|
556
|
) |
|
557
|
|
|
558
|
const pm = new PackageManager(processManager) |
|
559
|
pm["packageManager"] = "npm" |
|
560
|
|
|
561
|
await pm.transformWorkspaceConfig("/test/path") |
|
562
|
|
|
563
|
const written = getWrittenPackageJson() |
|
564
|
expect(written.devDependencies).toEqual({ |
|
565
|
typescript: "^5.0.0", |
|
566
|
ajv: "^8.13.0", |
|
567
|
}) |
|
568
|
expect(written.overrides).toEqual({ |
|
569
|
someOtherPackage: "^1.0.0", |
|
570
|
eslint: { ajv: "^6.12.4" }, |
|
571
|
"@eslint/eslintrc": { ajv: "^6.12.4" }, |
|
572
|
}) |
|
573
|
}) |
|
574
|
|
|
575
|
it("should not create yarn.lock or .yarnrc.yml", async () => { |
|
576
|
const pm = new PackageManager(processManager) |
|
577
|
pm["packageManager"] = "npm" |
|
578
|
|
|
579
|
await pm.transformWorkspaceConfig("/test/path") |
|
580
|
|
|
581
|
expect(mockWriteFileSync).not.toHaveBeenCalledWith( |
|
582
|
"/test/path/yarn.lock", |
|
583
|
expect.anything() |
|
584
|
) |
|
585
|
expect(mockWriteFileSync).not.toHaveBeenCalledWith( |
|
586
|
"/test/path/.yarnrc.yml", |
|
587
|
expect.anything() |
|
588
|
) |
|
589
|
}) |
|
590
|
}) |
|
591
|
}) |
|
592
|
|
|
593
|
describe("installDependencies", () => { |
|
594
|
it("should set package manager before installing if not set", async () => { |
|
595
|
process.env.npm_config_user_agent = "yarn/1.22.0" |
|
596
|
|
|
597
|
const pm = new PackageManager(processManager) |
|
598
|
await pm.installDependencies({ cwd: "/test/path" }) |
|
599
|
|
|
600
|
expect(pm.getPackageManager()).toBe("yarn") |
|
601
|
}) |
|
602
|
|
|
603
|
it("should run transformWorkspaceConfig and removeLockFiles before installing", async () => { |
|
604
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
605
|
mockExistsSync.mockReturnValue(true) |
|
606
|
|
|
607
|
const pm = new PackageManager(processManager, { useNpm: true }) |
|
608
|
pm["packageManager"] = "npm" |
|
609
|
|
|
610
|
await pm.installDependencies({ cwd: "/test/path" }) |
|
611
|
|
|
612
|
// transformWorkspaceConfig ran (checked pnpm-workspace.yaml) |
|
613
|
expect(mockExistsSync).toHaveBeenCalledWith("/test/path/pnpm-workspace.yaml") |
|
614
|
// removeLockFiles ran |
|
615
|
expect(mockRmSync).toHaveBeenCalled() |
|
616
|
}) |
|
617
|
|
|
618
|
it("should execute yarn command when using yarn", async () => { |
|
619
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
620
|
|
|
621
|
const pm = new PackageManager(processManager) |
|
622
|
pm["packageManager"] = "yarn" |
|
623
|
|
|
624
|
await pm.installDependencies({ cwd: "/test/path" }) |
|
625
|
|
|
626
|
expect(mockExecute).toHaveBeenCalledWith( |
|
627
|
["yarn", { cwd: "/test/path" }], |
|
628
|
{ verbose: false } |
|
629
|
) |
|
630
|
}) |
|
631
|
|
|
632
|
it("should execute pnpm install when using pnpm", async () => { |
|
633
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
634
|
|
|
635
|
const pm = new PackageManager(processManager) |
|
636
|
pm["packageManager"] = "pnpm" |
|
637
|
|
|
638
|
await pm.installDependencies({ cwd: "/test/path" }) |
|
639
|
|
|
640
|
expect(mockExecute).toHaveBeenCalledWith( |
|
641
|
["pnpm install", { cwd: "/test/path" }], |
|
642
|
{ verbose: false } |
|
643
|
) |
|
644
|
}) |
|
645
|
|
|
646
|
it("should execute npm install then npm ci when using npm", async () => { |
|
647
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
648
|
|
|
649
|
const pm = new PackageManager(processManager) |
|
650
|
pm["packageManager"] = "npm" |
|
651
|
|
|
652
|
await pm.installDependencies({ cwd: "/test/path" }) |
|
653
|
|
|
654
|
expect(mockExecute).toHaveBeenCalledWith( |
|
655
|
["npm install", { cwd: "/test/path" }], |
|
656
|
{ verbose: false } |
|
657
|
) |
|
658
|
expect(mockExecute).toHaveBeenCalledWith( |
|
659
|
["npm ci", { cwd: "/test/path" }], |
|
660
|
{ verbose: false } |
|
661
|
) |
|
662
|
expect(mockExecute).toHaveBeenCalledTimes(2) |
|
663
|
}) |
|
664
|
|
|
665
|
it("should append --legacy-peer-deps to npm install when installLegacyPeerDeps is true", async () => { |
|
666
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
667
|
|
|
668
|
const pm = new PackageManager(processManager) |
|
669
|
pm["packageManager"] = "npm" |
|
670
|
|
|
671
|
await pm.installDependencies( |
|
672
|
{ cwd: "/test/path" }, |
|
673
|
{ installLegacyPeerDeps: true } |
|
674
|
) |
|
675
|
|
|
676
|
expect(mockExecute).toHaveBeenCalledWith( |
|
677
|
["npm install --legacy-peer-deps", { cwd: "/test/path" }], |
|
678
|
{ verbose: false } |
|
679
|
) |
|
680
|
}) |
|
681
|
|
|
682
|
it("should re-run npm install if npm ci fails", async () => { |
|
683
|
mockExecute |
|
684
|
.mockResolvedValueOnce({ stdout: "", stderr: "" }) // npm install succeeds |
|
685
|
.mockRejectedValueOnce(new Error("npm ci failed")) // npm ci fails |
|
686
|
.mockResolvedValueOnce({ stdout: "", stderr: "" }) // npm install retry succeeds |
|
687
|
|
|
688
|
const pm = new PackageManager(processManager) |
|
689
|
pm["packageManager"] = "npm" |
|
690
|
|
|
691
|
await pm.installDependencies({ cwd: "/test/path" }) |
|
692
|
|
|
693
|
expect(mockExecute).toHaveBeenCalledWith( |
|
694
|
["npm install", { cwd: "/test/path" }], |
|
695
|
{ verbose: false } |
|
696
|
) |
|
697
|
expect(mockExecute).toHaveBeenCalledWith( |
|
698
|
["npm ci", { cwd: "/test/path" }], |
|
699
|
{ verbose: false } |
|
700
|
) |
|
701
|
expect(mockExecute).toHaveBeenCalledTimes(3) |
|
702
|
}) |
|
703
|
|
|
704
|
it("should respect verbose option", async () => { |
|
705
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
706
|
|
|
707
|
const pm = new PackageManager(processManager, { verbose: true }) |
|
708
|
pm["packageManager"] = "yarn" |
|
709
|
|
|
710
|
await pm.installDependencies({ cwd: "/test/path" }) |
|
711
|
|
|
712
|
expect(mockExecute).toHaveBeenCalledWith( |
|
713
|
expect.anything(), |
|
714
|
{ verbose: true } |
|
715
|
) |
|
716
|
}) |
|
717
|
}) |
|
718
|
|
|
719
|
describe("getCommandStr", () => { |
|
720
|
it("should throw error if package manager is not set", () => { |
|
721
|
const pm = new PackageManager(processManager) |
|
722
|
|
|
723
|
expect(() => pm.getCommandStr("dev")).toThrow("Package manager not set") |
|
724
|
}) |
|
725
|
|
|
726
|
it("should return yarn command format", () => { |
|
727
|
const pm = new PackageManager(processManager) |
|
728
|
pm["packageManager"] = "yarn" |
|
729
|
|
|
730
|
expect(pm.getCommandStr("dev")).toBe("yarn dev") |
|
731
|
}) |
|
732
|
|
|
733
|
it("should return pnpm command format", () => { |
|
734
|
const pm = new PackageManager(processManager) |
|
735
|
pm["packageManager"] = "pnpm" |
|
736
|
|
|
737
|
expect(pm.getCommandStr("build")).toBe("pnpm build") |
|
738
|
}) |
|
739
|
|
|
740
|
it("should return npm command format with 'run'", () => { |
|
741
|
const pm = new PackageManager(processManager) |
|
742
|
pm["packageManager"] = "npm" |
|
743
|
|
|
744
|
expect(pm.getCommandStr("test")).toBe("npm run test") |
|
745
|
}) |
|
746
|
}) |
|
747
|
|
|
748
|
describe("runCommand", () => { |
|
749
|
it("should set package manager before running if not set", async () => { |
|
750
|
process.env.npm_config_user_agent = "yarn/1.22.0" |
|
751
|
|
|
752
|
const pm = new PackageManager(processManager) |
|
753
|
await pm.runCommand("dev", { cwd: "/test/path" }) |
|
754
|
|
|
755
|
expect(pm.getPackageManager()).toBe("yarn") |
|
756
|
}) |
|
757
|
|
|
758
|
it("should execute command with correct format for yarn", async () => { |
|
759
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
760
|
|
|
761
|
const pm = new PackageManager(processManager) |
|
762
|
pm["packageManager"] = "yarn" |
|
763
|
|
|
764
|
await pm.runCommand("dev", { cwd: "/test/path" }) |
|
765
|
|
|
766
|
expect(mockExecute).toHaveBeenCalledWith( |
|
767
|
["yarn dev", { cwd: "/test/path" }], |
|
768
|
{ verbose: false } |
|
769
|
) |
|
770
|
}) |
|
771
|
|
|
772
|
it("should execute command with correct format for npm", async () => { |
|
773
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
774
|
|
|
775
|
const pm = new PackageManager(processManager) |
|
776
|
pm["packageManager"] = "npm" |
|
777
|
|
|
778
|
await pm.runCommand("build", { cwd: "/test/path" }) |
|
779
|
|
|
780
|
expect(mockExecute).toHaveBeenCalledWith( |
|
781
|
["npm run build", { cwd: "/test/path" }], |
|
782
|
{ verbose: false } |
|
783
|
) |
|
784
|
}) |
|
785
|
|
|
786
|
it("should pass through verbose options", async () => { |
|
787
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
788
|
|
|
789
|
const pm = new PackageManager(processManager, { verbose: true }) |
|
790
|
pm["packageManager"] = "yarn" |
|
791
|
|
|
792
|
await pm.runCommand("dev", { cwd: "/test/path" }, { needOutput: true }) |
|
793
|
|
|
794
|
expect(mockExecute).toHaveBeenCalledWith( |
|
795
|
expect.anything(), |
|
796
|
{ verbose: true, needOutput: true } |
|
797
|
) |
|
798
|
}) |
|
799
|
|
|
800
|
it("should return execution result", async () => { |
|
801
|
const result = { stdout: "success", stderr: "" } |
|
802
|
mockExecute.mockResolvedValue(result) |
|
803
|
|
|
804
|
const pm = new PackageManager(processManager) |
|
805
|
pm["packageManager"] = "yarn" |
|
806
|
|
|
807
|
const output = await pm.runCommand("dev", {}) |
|
808
|
|
|
809
|
expect(output).toEqual(result) |
|
810
|
}) |
|
811
|
}) |
|
812
|
|
|
813
|
describe("runMedusaCommand", () => { |
|
814
|
it("should set package manager before running if not set", async () => { |
|
815
|
process.env.npm_config_user_agent = "yarn/1.22.0" |
|
816
|
|
|
817
|
const pm = new PackageManager(processManager) |
|
818
|
await pm.runMedusaCommand("migrate", { cwd: "/test/path" }) |
|
819
|
|
|
820
|
expect(pm.getPackageManager()).toBe("yarn") |
|
821
|
}) |
|
822
|
|
|
823
|
it("should execute yarn medusa command for yarn", async () => { |
|
824
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
825
|
|
|
826
|
const pm = new PackageManager(processManager) |
|
827
|
pm["packageManager"] = "yarn" |
|
828
|
|
|
829
|
await pm.runMedusaCommand("migrate", { cwd: "/test/path" }) |
|
830
|
|
|
831
|
expect(mockExecute).toHaveBeenCalledWith( |
|
832
|
["yarn medusa migrate", { cwd: "/test/path" }], |
|
833
|
{ verbose: false } |
|
834
|
) |
|
835
|
}) |
|
836
|
|
|
837
|
it("should execute pnpm medusa command for pnpm", async () => { |
|
838
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
839
|
|
|
840
|
const pm = new PackageManager(processManager) |
|
841
|
pm["packageManager"] = "pnpm" |
|
842
|
|
|
843
|
await pm.runMedusaCommand("seed", { cwd: "/test/path" }) |
|
844
|
|
|
845
|
expect(mockExecute).toHaveBeenCalledWith( |
|
846
|
["pnpm medusa seed", { cwd: "/test/path" }], |
|
847
|
{ verbose: false } |
|
848
|
) |
|
849
|
}) |
|
850
|
|
|
851
|
it("should execute npx medusa command for npm", async () => { |
|
852
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
853
|
|
|
854
|
const pm = new PackageManager(processManager) |
|
855
|
pm["packageManager"] = "npm" |
|
856
|
|
|
857
|
await pm.runMedusaCommand("start", { cwd: "/test/path" }) |
|
858
|
|
|
859
|
expect(mockExecute).toHaveBeenCalledWith( |
|
860
|
["npx medusa start", { cwd: "/test/path" }], |
|
861
|
{ verbose: false } |
|
862
|
) |
|
863
|
}) |
|
864
|
|
|
865
|
it("should pass through verbose options", async () => { |
|
866
|
mockExecute.mockResolvedValue({ stdout: "", stderr: "" }) |
|
867
|
|
|
868
|
const pm = new PackageManager(processManager, { verbose: true }) |
|
869
|
pm["packageManager"] = "yarn" |
|
870
|
|
|
871
|
await pm.runMedusaCommand("migrate", { cwd: "/test/path" }, { needOutput: true }) |
|
872
|
|
|
873
|
expect(mockExecute).toHaveBeenCalledWith( |
|
874
|
expect.anything(), |
|
875
|
{ verbose: true, needOutput: true } |
|
876
|
) |
|
877
|
}) |
|
878
|
|
|
879
|
it("should return execution result", async () => { |
|
880
|
const result = { stdout: "migration complete", stderr: "" } |
|
881
|
mockExecute.mockResolvedValue(result) |
|
882
|
|
|
883
|
const pm = new PackageManager(processManager) |
|
884
|
pm["packageManager"] = "yarn" |
|
885
|
|
|
886
|
const output = await pm.runMedusaCommand("migrate", {}) |
|
887
|
|
|
888
|
expect(output).toEqual(result) |
|
889
|
}) |
|
890
|
}) |
|
891
|
|
|
892
|
describe("getPackageManager", () => { |
|
893
|
it("should return undefined when not set", () => { |
|
894
|
const pm = new PackageManager(processManager) |
|
895
|
expect(pm.getPackageManager()).toBeUndefined() |
|
896
|
}) |
|
897
|
|
|
898
|
it("should return the current package manager", () => { |
|
899
|
const pm = new PackageManager(processManager) |
|
900
|
pm["packageManager"] = "yarn" |
|
901
|
expect(pm.getPackageManager()).toBe("yarn") |
|
902
|
}) |
|
903
|
}) |
|
904
|
|
|
905
|
describe("getPackageManagerString", () => { |
|
906
|
it("should return undefined when version is not set", async () => { |
|
907
|
const pm = new PackageManager(processManager) |
|
908
|
pm["packageManager"] = "yarn" |
|
909
|
expect(await pm.getPackageManagerString()).toBeUndefined() |
|
910
|
}) |
|
911
|
|
|
912
|
it("should return packageManager@version format", async () => { |
|
913
|
const pm = new PackageManager(processManager) |
|
914
|
pm["packageManager"] = "yarn" |
|
915
|
pm["packageManagerVersion"] = "4.9.0" |
|
916
|
expect(await pm.getPackageManagerString()).toBe("yarn@4.9.0") |
|
917
|
}) |
|
918
|
|
|
919
|
it("should work with pnpm", async () => { |
|
920
|
const pm = new PackageManager(processManager) |
|
921
|
pm["packageManager"] = "pnpm" |
|
922
|
pm["packageManagerVersion"] = "8.15.0" |
|
923
|
expect(await pm.getPackageManagerString()).toBe("pnpm@8.15.0") |
|
924
|
}) |
|
925
|
|
|
926
|
it("should work with npm", async () => { |
|
927
|
const pm = new PackageManager(processManager) |
|
928
|
pm["packageManager"] = "npm" |
|
929
|
pm["packageManagerVersion"] = "10.0.0" |
|
930
|
expect(await pm.getPackageManagerString()).toBe("npm@10.0.0") |
|
931
|
}) |
|
932
|
|
|
933
|
it("should call setPackageManager if package manager is not set", async () => { |
|
934
|
process.env.npm_config_user_agent = "yarn/1.22.0" |
|
935
|
|
|
936
|
const pm = new PackageManager(processManager) |
|
937
|
const result = await pm.getPackageManagerString() |
|
938
|
|
|
939
|
expect(pm.getPackageManager()).toBe("yarn") |
|
940
|
expect(result).toBe("yarn@1.22.0") |
|
941
|
}) |
|
942
|
}) |
|
943
|
}) |