create-esmx 3.0.0-rc.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +8 -0
- package/dist/index.mjs +282 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.mjs +123 -0
- package/dist/integration.test.d.ts +1 -0
- package/dist/integration.test.mjs +165 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.mjs +7 -0
- package/dist/utils/package-manager.d.ts +10 -0
- package/dist/utils/package-manager.mjs +49 -0
- package/dist/utils/package-manager.test.d.ts +4 -0
- package/dist/utils/package-manager.test.mjs +275 -0
- package/dist/utils/project-name.d.ts +30 -0
- package/dist/utils/project-name.mjs +17 -0
- package/dist/utils/project-name.test.d.ts +4 -0
- package/dist/utils/project-name.test.mjs +186 -0
- package/dist/utils/template.d.ts +19 -0
- package/dist/utils/template.mjs +8 -0
- package/dist/utils/template.test.d.ts +4 -0
- package/dist/utils/template.test.mjs +150 -0
- package/package.json +71 -0
- package/src/index.test.ts +159 -0
- package/src/index.ts +391 -0
- package/src/integration.test.ts +226 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/package-manager.test.ts +540 -0
- package/src/utils/package-manager.ts +92 -0
- package/src/utils/project-name.test.ts +345 -0
- package/src/utils/project-name.ts +55 -0
- package/src/utils/template.test.ts +234 -0
- package/src/utils/template.ts +34 -0
- package/template/vue2/README.md +80 -0
- package/template/vue2/package.json +27 -0
- package/template/vue2/src/app.vue +127 -0
- package/template/vue2/src/components/hello-world.vue +79 -0
- package/template/vue2/src/create-app.ts +11 -0
- package/template/vue2/src/entry.client.ts +5 -0
- package/template/vue2/src/entry.node.ts +29 -0
- package/template/vue2/src/entry.server.ts +37 -0
- package/template/vue2/tsconfig.json +26 -0
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for package-manager utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
6
|
+
import { getCommand } from './package-manager';
|
|
7
|
+
import type { CommandType } from './package-manager';
|
|
8
|
+
|
|
9
|
+
describe('package-manager utilities', () => {
|
|
10
|
+
// Store original environment variable
|
|
11
|
+
let originalUserAgent: string | undefined;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
// Save original environment
|
|
15
|
+
originalUserAgent = process.env.npm_config_user_agent;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
// Restore original environment
|
|
20
|
+
if (originalUserAgent !== undefined) {
|
|
21
|
+
process.env.npm_config_user_agent = originalUserAgent;
|
|
22
|
+
} else {
|
|
23
|
+
process.env.npm_config_user_agent = undefined;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('getCommand', () => {
|
|
28
|
+
describe('when using npm', () => {
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
process.env.npm_config_user_agent =
|
|
31
|
+
'npm/8.19.2 node/v18.12.0 darwin x64 workspaces/false';
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should return npm install command', () => {
|
|
35
|
+
// Arrange
|
|
36
|
+
const commandType: CommandType = 'install';
|
|
37
|
+
|
|
38
|
+
// Act
|
|
39
|
+
const result = getCommand(commandType);
|
|
40
|
+
|
|
41
|
+
// Assert
|
|
42
|
+
expect(result).toBe('npm install');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should return npm dev command', () => {
|
|
46
|
+
// Arrange
|
|
47
|
+
const commandType: CommandType = 'dev';
|
|
48
|
+
|
|
49
|
+
// Act
|
|
50
|
+
const result = getCommand(commandType);
|
|
51
|
+
|
|
52
|
+
// Assert
|
|
53
|
+
expect(result).toBe('npm run dev');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should return npm build command', () => {
|
|
57
|
+
// Arrange
|
|
58
|
+
const commandType: CommandType = 'build';
|
|
59
|
+
|
|
60
|
+
// Act
|
|
61
|
+
const result = getCommand(commandType);
|
|
62
|
+
|
|
63
|
+
// Assert
|
|
64
|
+
expect(result).toBe('npm run build');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should return npm start command', () => {
|
|
68
|
+
// Arrange
|
|
69
|
+
const commandType: CommandType = 'start';
|
|
70
|
+
|
|
71
|
+
// Act
|
|
72
|
+
const result = getCommand(commandType);
|
|
73
|
+
|
|
74
|
+
// Assert
|
|
75
|
+
expect(result).toBe('npm start');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should return npm create command', () => {
|
|
79
|
+
// Arrange
|
|
80
|
+
const commandType: CommandType = 'create';
|
|
81
|
+
|
|
82
|
+
// Act
|
|
83
|
+
const result = getCommand(commandType);
|
|
84
|
+
|
|
85
|
+
// Assert
|
|
86
|
+
expect(result).toBe('npm create esmx@latest');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should return npm build:type command', () => {
|
|
90
|
+
// Arrange
|
|
91
|
+
const commandType: CommandType = 'build:type';
|
|
92
|
+
|
|
93
|
+
// Act
|
|
94
|
+
const result = getCommand(commandType);
|
|
95
|
+
|
|
96
|
+
// Assert
|
|
97
|
+
expect(result).toBe('npm run build:type');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should return npm lint:type command', () => {
|
|
101
|
+
// Arrange
|
|
102
|
+
const commandType: CommandType = 'lint:type';
|
|
103
|
+
|
|
104
|
+
// Act
|
|
105
|
+
const result = getCommand(commandType);
|
|
106
|
+
|
|
107
|
+
// Assert
|
|
108
|
+
expect(result).toBe('npm run lint:type');
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('when using yarn', () => {
|
|
113
|
+
beforeEach(() => {
|
|
114
|
+
process.env.npm_config_user_agent =
|
|
115
|
+
'yarn/1.22.19 npm/? node/v18.12.0 darwin x64';
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should return yarn install command', () => {
|
|
119
|
+
// Arrange
|
|
120
|
+
const commandType: CommandType = 'install';
|
|
121
|
+
|
|
122
|
+
// Act
|
|
123
|
+
const result = getCommand(commandType);
|
|
124
|
+
|
|
125
|
+
// Assert
|
|
126
|
+
expect(result).toBe('yarn install');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should return yarn dev command', () => {
|
|
130
|
+
// Arrange
|
|
131
|
+
const commandType: CommandType = 'dev';
|
|
132
|
+
|
|
133
|
+
// Act
|
|
134
|
+
const result = getCommand(commandType);
|
|
135
|
+
|
|
136
|
+
// Assert
|
|
137
|
+
expect(result).toBe('yarn dev');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should return yarn build command', () => {
|
|
141
|
+
// Arrange
|
|
142
|
+
const commandType: CommandType = 'build';
|
|
143
|
+
|
|
144
|
+
// Act
|
|
145
|
+
const result = getCommand(commandType);
|
|
146
|
+
|
|
147
|
+
// Assert
|
|
148
|
+
expect(result).toBe('yarn build');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should return yarn start command', () => {
|
|
152
|
+
// Arrange
|
|
153
|
+
const commandType: CommandType = 'start';
|
|
154
|
+
|
|
155
|
+
// Act
|
|
156
|
+
const result = getCommand(commandType);
|
|
157
|
+
|
|
158
|
+
// Assert
|
|
159
|
+
expect(result).toBe('yarn start');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should return yarn create command', () => {
|
|
163
|
+
// Arrange
|
|
164
|
+
const commandType: CommandType = 'create';
|
|
165
|
+
|
|
166
|
+
// Act
|
|
167
|
+
const result = getCommand(commandType);
|
|
168
|
+
|
|
169
|
+
// Assert
|
|
170
|
+
expect(result).toBe('yarn create esmx');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should return yarn build:type command', () => {
|
|
174
|
+
// Arrange
|
|
175
|
+
const commandType: CommandType = 'build:type';
|
|
176
|
+
|
|
177
|
+
// Act
|
|
178
|
+
const result = getCommand(commandType);
|
|
179
|
+
|
|
180
|
+
// Assert
|
|
181
|
+
expect(result).toBe('yarn build:type');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should return yarn lint:type command', () => {
|
|
185
|
+
// Arrange
|
|
186
|
+
const commandType: CommandType = 'lint:type';
|
|
187
|
+
|
|
188
|
+
// Act
|
|
189
|
+
const result = getCommand(commandType);
|
|
190
|
+
|
|
191
|
+
// Assert
|
|
192
|
+
expect(result).toBe('yarn lint:type');
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('when using pnpm', () => {
|
|
197
|
+
beforeEach(() => {
|
|
198
|
+
process.env.npm_config_user_agent =
|
|
199
|
+
'pnpm/7.14.0 npm/? node/v18.12.0 darwin x64';
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should return pnpm install command', () => {
|
|
203
|
+
// Arrange
|
|
204
|
+
const commandType: CommandType = 'install';
|
|
205
|
+
|
|
206
|
+
// Act
|
|
207
|
+
const result = getCommand(commandType);
|
|
208
|
+
|
|
209
|
+
// Assert
|
|
210
|
+
expect(result).toBe('pnpm install');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('should return pnpm dev command', () => {
|
|
214
|
+
// Arrange
|
|
215
|
+
const commandType: CommandType = 'dev';
|
|
216
|
+
|
|
217
|
+
// Act
|
|
218
|
+
const result = getCommand(commandType);
|
|
219
|
+
|
|
220
|
+
// Assert
|
|
221
|
+
expect(result).toBe('pnpm dev');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should return pnpm build command', () => {
|
|
225
|
+
// Arrange
|
|
226
|
+
const commandType: CommandType = 'build';
|
|
227
|
+
|
|
228
|
+
// Act
|
|
229
|
+
const result = getCommand(commandType);
|
|
230
|
+
|
|
231
|
+
// Assert
|
|
232
|
+
expect(result).toBe('pnpm build');
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should return pnpm start command', () => {
|
|
236
|
+
// Arrange
|
|
237
|
+
const commandType: CommandType = 'start';
|
|
238
|
+
|
|
239
|
+
// Act
|
|
240
|
+
const result = getCommand(commandType);
|
|
241
|
+
|
|
242
|
+
// Assert
|
|
243
|
+
expect(result).toBe('pnpm start');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('should return pnpm create command', () => {
|
|
247
|
+
// Arrange
|
|
248
|
+
const commandType: CommandType = 'create';
|
|
249
|
+
|
|
250
|
+
// Act
|
|
251
|
+
const result = getCommand(commandType);
|
|
252
|
+
|
|
253
|
+
// Assert
|
|
254
|
+
expect(result).toBe('pnpm create esmx');
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('should return pnpm build:type command', () => {
|
|
258
|
+
// Arrange
|
|
259
|
+
const commandType: CommandType = 'build:type';
|
|
260
|
+
|
|
261
|
+
// Act
|
|
262
|
+
const result = getCommand(commandType);
|
|
263
|
+
|
|
264
|
+
// Assert
|
|
265
|
+
expect(result).toBe('pnpm build:type');
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('should return pnpm lint:type command', () => {
|
|
269
|
+
// Arrange
|
|
270
|
+
const commandType: CommandType = 'lint:type';
|
|
271
|
+
|
|
272
|
+
// Act
|
|
273
|
+
const result = getCommand(commandType);
|
|
274
|
+
|
|
275
|
+
// Assert
|
|
276
|
+
expect(result).toBe('pnpm lint:type');
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe('when using bun', () => {
|
|
281
|
+
beforeEach(() => {
|
|
282
|
+
process.env.npm_config_user_agent =
|
|
283
|
+
'bun/0.6.0 bun/0.6.0 darwin x64';
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should return bun install command', () => {
|
|
287
|
+
// Arrange
|
|
288
|
+
const commandType: CommandType = 'install';
|
|
289
|
+
|
|
290
|
+
// Act
|
|
291
|
+
const result = getCommand(commandType);
|
|
292
|
+
|
|
293
|
+
// Assert
|
|
294
|
+
expect(result).toBe('bun install');
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should return bun dev command', () => {
|
|
298
|
+
// Arrange
|
|
299
|
+
const commandType: CommandType = 'dev';
|
|
300
|
+
|
|
301
|
+
// Act
|
|
302
|
+
const result = getCommand(commandType);
|
|
303
|
+
|
|
304
|
+
// Assert
|
|
305
|
+
expect(result).toBe('bun dev');
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('should return bun build command', () => {
|
|
309
|
+
// Arrange
|
|
310
|
+
const commandType: CommandType = 'build';
|
|
311
|
+
|
|
312
|
+
// Act
|
|
313
|
+
const result = getCommand(commandType);
|
|
314
|
+
|
|
315
|
+
// Assert
|
|
316
|
+
expect(result).toBe('bun run build');
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('should return bun start command', () => {
|
|
320
|
+
// Arrange
|
|
321
|
+
const commandType: CommandType = 'start';
|
|
322
|
+
|
|
323
|
+
// Act
|
|
324
|
+
const result = getCommand(commandType);
|
|
325
|
+
|
|
326
|
+
// Assert
|
|
327
|
+
expect(result).toBe('bun start');
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it('should return bun create command', () => {
|
|
331
|
+
// Arrange
|
|
332
|
+
const commandType: CommandType = 'create';
|
|
333
|
+
|
|
334
|
+
// Act
|
|
335
|
+
const result = getCommand(commandType);
|
|
336
|
+
|
|
337
|
+
// Assert
|
|
338
|
+
expect(result).toBe('bun create esmx');
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('should return bun build:type command', () => {
|
|
342
|
+
// Arrange
|
|
343
|
+
const commandType: CommandType = 'build:type';
|
|
344
|
+
|
|
345
|
+
// Act
|
|
346
|
+
const result = getCommand(commandType);
|
|
347
|
+
|
|
348
|
+
// Assert
|
|
349
|
+
expect(result).toBe('bun run build:type');
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it('should return bun lint:type command', () => {
|
|
353
|
+
// Arrange
|
|
354
|
+
const commandType: CommandType = 'lint:type';
|
|
355
|
+
|
|
356
|
+
// Act
|
|
357
|
+
const result = getCommand(commandType);
|
|
358
|
+
|
|
359
|
+
// Assert
|
|
360
|
+
expect(result).toBe('bun run lint:type');
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
describe('when user agent is not set', () => {
|
|
365
|
+
beforeEach(() => {
|
|
366
|
+
process.env.npm_config_user_agent = undefined;
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('should default to npm commands', () => {
|
|
370
|
+
// Arrange
|
|
371
|
+
const commandType: CommandType = 'install';
|
|
372
|
+
|
|
373
|
+
// Act
|
|
374
|
+
const result = getCommand(commandType);
|
|
375
|
+
|
|
376
|
+
// Assert
|
|
377
|
+
expect(result).toBe('npm install');
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
describe('when user agent is empty string', () => {
|
|
382
|
+
beforeEach(() => {
|
|
383
|
+
process.env.npm_config_user_agent = '';
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it('should default to npm commands', () => {
|
|
387
|
+
// Arrange
|
|
388
|
+
const commandType: CommandType = 'dev';
|
|
389
|
+
|
|
390
|
+
// Act
|
|
391
|
+
const result = getCommand(commandType);
|
|
392
|
+
|
|
393
|
+
// Assert
|
|
394
|
+
expect(result).toBe('npm run dev');
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
describe('when user agent contains unknown package manager', () => {
|
|
399
|
+
beforeEach(() => {
|
|
400
|
+
process.env.npm_config_user_agent =
|
|
401
|
+
'unknown-pm/1.0.0 node/v18.12.0';
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it('should default to npm commands', () => {
|
|
405
|
+
// Arrange
|
|
406
|
+
const commandType: CommandType = 'build';
|
|
407
|
+
|
|
408
|
+
// Act
|
|
409
|
+
const result = getCommand(commandType);
|
|
410
|
+
|
|
411
|
+
// Assert
|
|
412
|
+
expect(result).toBe('npm run build');
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
describe('getCommand with userAgent parameter', () => {
|
|
418
|
+
beforeEach(() => {
|
|
419
|
+
// Clear environment variable to ensure parameter takes precedence
|
|
420
|
+
process.env.npm_config_user_agent = undefined;
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it('should use provided userAgent over environment variable', () => {
|
|
424
|
+
// Arrange
|
|
425
|
+
process.env.npm_config_user_agent = 'npm/8.19.2 node/v18.12.0';
|
|
426
|
+
const customUserAgent =
|
|
427
|
+
'pnpm/7.14.0 npm/? node/v18.12.0 darwin x64';
|
|
428
|
+
|
|
429
|
+
// Act
|
|
430
|
+
const result = getCommand('install', customUserAgent);
|
|
431
|
+
|
|
432
|
+
// Assert
|
|
433
|
+
expect(result).toBe('pnpm install');
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('should detect pnpm from custom userAgent', () => {
|
|
437
|
+
// Arrange
|
|
438
|
+
const userAgent = 'pnpm/7.14.0 npm/? node/v18.12.0 darwin x64';
|
|
439
|
+
|
|
440
|
+
// Act
|
|
441
|
+
const installResult = getCommand('install', userAgent);
|
|
442
|
+
const devResult = getCommand('dev', userAgent);
|
|
443
|
+
const buildResult = getCommand('build', userAgent);
|
|
444
|
+
|
|
445
|
+
// Assert
|
|
446
|
+
expect(installResult).toBe('pnpm install');
|
|
447
|
+
expect(devResult).toBe('pnpm dev');
|
|
448
|
+
expect(buildResult).toBe('pnpm build');
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it('should detect yarn from custom userAgent', () => {
|
|
452
|
+
// Arrange
|
|
453
|
+
const userAgent = 'yarn/1.22.19 npm/? node/v18.12.0 darwin x64';
|
|
454
|
+
|
|
455
|
+
// Act
|
|
456
|
+
const installResult = getCommand('install', userAgent);
|
|
457
|
+
const devResult = getCommand('dev', userAgent);
|
|
458
|
+
const buildResult = getCommand('build', userAgent);
|
|
459
|
+
|
|
460
|
+
// Assert
|
|
461
|
+
expect(installResult).toBe('yarn install');
|
|
462
|
+
expect(devResult).toBe('yarn dev');
|
|
463
|
+
expect(buildResult).toBe('yarn build');
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it('should detect bun from custom userAgent', () => {
|
|
467
|
+
// Arrange
|
|
468
|
+
const userAgent = 'bun/0.6.0 bun/0.6.0 darwin x64';
|
|
469
|
+
|
|
470
|
+
// Act
|
|
471
|
+
const installResult = getCommand('install', userAgent);
|
|
472
|
+
const buildResult = getCommand('build', userAgent);
|
|
473
|
+
const buildTypeResult = getCommand('build:type', userAgent);
|
|
474
|
+
|
|
475
|
+
// Assert
|
|
476
|
+
expect(installResult).toBe('bun install');
|
|
477
|
+
expect(buildResult).toBe('bun run build');
|
|
478
|
+
expect(buildTypeResult).toBe('bun run build:type');
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it('should default to npm when userAgent does not match known patterns', () => {
|
|
482
|
+
// Arrange
|
|
483
|
+
const userAgent = 'unknown-package-manager/1.0.0';
|
|
484
|
+
|
|
485
|
+
// Act
|
|
486
|
+
const result = getCommand('install', userAgent);
|
|
487
|
+
|
|
488
|
+
// Assert
|
|
489
|
+
expect(result).toBe('npm install');
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it('should handle empty userAgent string', () => {
|
|
493
|
+
// Arrange
|
|
494
|
+
const userAgent = '';
|
|
495
|
+
|
|
496
|
+
// Act
|
|
497
|
+
const result = getCommand('install', userAgent);
|
|
498
|
+
|
|
499
|
+
// Assert
|
|
500
|
+
expect(result).toBe('npm install');
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('should fallback to environment variable when userAgent is undefined', () => {
|
|
504
|
+
// Arrange
|
|
505
|
+
process.env.npm_config_user_agent =
|
|
506
|
+
'yarn/1.22.19 npm/? node/v18.12.0';
|
|
507
|
+
|
|
508
|
+
// Act
|
|
509
|
+
const result = getCommand('install', undefined);
|
|
510
|
+
|
|
511
|
+
// Assert
|
|
512
|
+
expect(result).toBe('yarn install');
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
it('should handle case-sensitive package manager detection', () => {
|
|
516
|
+
// Arrange
|
|
517
|
+
const userAgent = 'PNPM/7.14.0 npm/? node/v18.12.0'; // uppercase
|
|
518
|
+
|
|
519
|
+
// Act
|
|
520
|
+
const result = getCommand('install', userAgent);
|
|
521
|
+
|
|
522
|
+
// Assert
|
|
523
|
+
expect(result).toBe('npm install'); // Should default to npm as it's case-sensitive
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it('should test all command types with custom userAgent', () => {
|
|
527
|
+
// Arrange
|
|
528
|
+
const userAgent = 'pnpm/7.14.0 npm/? node/v18.12.0';
|
|
529
|
+
|
|
530
|
+
// Act & Assert
|
|
531
|
+
expect(getCommand('install', userAgent)).toBe('pnpm install');
|
|
532
|
+
expect(getCommand('dev', userAgent)).toBe('pnpm dev');
|
|
533
|
+
expect(getCommand('build', userAgent)).toBe('pnpm build');
|
|
534
|
+
expect(getCommand('start', userAgent)).toBe('pnpm start');
|
|
535
|
+
expect(getCommand('create', userAgent)).toBe('pnpm create esmx');
|
|
536
|
+
expect(getCommand('build:type', userAgent)).toBe('pnpm build:type');
|
|
537
|
+
expect(getCommand('lint:type', userAgent)).toBe('pnpm lint:type');
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package manager detection and command generation utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun';
|
|
6
|
+
|
|
7
|
+
export type CommandType =
|
|
8
|
+
| 'install'
|
|
9
|
+
| 'dev'
|
|
10
|
+
| 'build'
|
|
11
|
+
| 'start'
|
|
12
|
+
| 'create'
|
|
13
|
+
| 'build:type'
|
|
14
|
+
| 'lint:type';
|
|
15
|
+
|
|
16
|
+
interface PackageManagerCommands {
|
|
17
|
+
install: string;
|
|
18
|
+
dev: string;
|
|
19
|
+
build: string;
|
|
20
|
+
start: string;
|
|
21
|
+
create: string;
|
|
22
|
+
'build:type': string;
|
|
23
|
+
'lint:type': string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Configuration for different package managers and their commands
|
|
28
|
+
*/
|
|
29
|
+
const PACKAGE_MANAGER_CONFIG: Record<PackageManager, PackageManagerCommands> = {
|
|
30
|
+
npm: {
|
|
31
|
+
install: 'npm install',
|
|
32
|
+
dev: 'npm run dev',
|
|
33
|
+
build: 'npm run build',
|
|
34
|
+
start: 'npm start',
|
|
35
|
+
create: 'npm create esmx@latest',
|
|
36
|
+
'build:type': 'npm run build:type',
|
|
37
|
+
'lint:type': 'npm run lint:type'
|
|
38
|
+
},
|
|
39
|
+
yarn: {
|
|
40
|
+
install: 'yarn install',
|
|
41
|
+
dev: 'yarn dev',
|
|
42
|
+
build: 'yarn build',
|
|
43
|
+
start: 'yarn start',
|
|
44
|
+
create: 'yarn create esmx',
|
|
45
|
+
'build:type': 'yarn build:type',
|
|
46
|
+
'lint:type': 'yarn lint:type'
|
|
47
|
+
},
|
|
48
|
+
pnpm: {
|
|
49
|
+
install: 'pnpm install',
|
|
50
|
+
dev: 'pnpm dev',
|
|
51
|
+
build: 'pnpm build',
|
|
52
|
+
start: 'pnpm start',
|
|
53
|
+
create: 'pnpm create esmx',
|
|
54
|
+
'build:type': 'pnpm build:type',
|
|
55
|
+
'lint:type': 'pnpm lint:type'
|
|
56
|
+
},
|
|
57
|
+
bun: {
|
|
58
|
+
install: 'bun install',
|
|
59
|
+
dev: 'bun dev',
|
|
60
|
+
build: 'bun run build',
|
|
61
|
+
start: 'bun start',
|
|
62
|
+
create: 'bun create esmx',
|
|
63
|
+
'build:type': 'bun run build:type',
|
|
64
|
+
'lint:type': 'bun run lint:type'
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Detect the package manager being used based on user agent
|
|
70
|
+
*/
|
|
71
|
+
function detectPackageManager(userAgent?: string): PackageManager {
|
|
72
|
+
const agent = userAgent || process.env.npm_config_user_agent || '';
|
|
73
|
+
|
|
74
|
+
if (agent.includes('pnpm')) return 'pnpm';
|
|
75
|
+
if (agent.includes('yarn')) return 'yarn';
|
|
76
|
+
if (agent.includes('bun')) return 'bun';
|
|
77
|
+
|
|
78
|
+
// Default to npm
|
|
79
|
+
return 'npm';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get a specific command for the detected package manager
|
|
84
|
+
* Business logic only needs to specify what command to execute
|
|
85
|
+
*/
|
|
86
|
+
export function getCommand(
|
|
87
|
+
commandType: CommandType,
|
|
88
|
+
userAgent?: string
|
|
89
|
+
): string {
|
|
90
|
+
const packageManager = detectPackageManager(userAgent);
|
|
91
|
+
return PACKAGE_MANAGER_CONFIG[packageManager][commandType];
|
|
92
|
+
}
|