|
1 | 1 | import * as os from 'os';
|
2 | 2 | import * as path from 'path';
|
| 3 | +import { LogLevel } from './types'; |
3 | 4 |
|
4 | 5 | interface PackageManagerProps {
|
5 | 6 | readonly lockFile: string;
|
6 | 7 | readonly installCommand: string[];
|
7 | 8 | readonly runCommand: string[];
|
8 |
| - readonly argsSeparator?: string |
| 9 | + readonly argsSeparator?: string; |
| 10 | +} |
| 11 | + |
| 12 | +export enum LockFile { |
| 13 | + NPM = 'package-lock.json', |
| 14 | + YARN = 'yarn.lock', |
| 15 | + PNPM = 'pnpm-lock.yaml', |
9 | 16 | }
|
10 | 17 |
|
11 | 18 | /**
|
12 | 19 | * A node package manager
|
13 | 20 | */
|
14 | 21 | export class PackageManager {
|
15 |
| - public static NPM = new PackageManager({ |
16 |
| - lockFile: 'package-lock.json', |
17 |
| - installCommand: ['npm', 'ci'], |
18 |
| - runCommand: ['npx', '--no-install'], |
19 |
| - }); |
20 |
| - |
21 |
| - public static YARN = new PackageManager({ |
22 |
| - lockFile: 'yarn.lock', |
23 |
| - installCommand: ['yarn', 'install', '--no-immutable'], |
24 |
| - runCommand: ['yarn', 'run'], |
25 |
| - }); |
26 |
| - |
27 |
| - public static PNPM = new PackageManager({ |
28 |
| - lockFile: 'pnpm-lock.yaml', |
29 |
| - installCommand: ['pnpm', 'install'], |
30 |
| - runCommand: ['pnpm', 'exec'], |
31 |
| - argsSeparator: '--', |
32 |
| - }); |
33 |
| - |
34 |
| - public static fromLockFile(lockFilePath: string): PackageManager { |
| 22 | + /** |
| 23 | + * Use a lock file path to determine the package manager to use. Optionally, specify a log level to |
| 24 | + * control its verbosity. |
| 25 | + * @param lockFilePath Path of the lock file |
| 26 | + * @param logLevel optional log level @default LogLevel.INFO |
| 27 | + * @returns the right PackageManager for that lock file |
| 28 | + */ |
| 29 | + public static fromLockFile(lockFilePath: string, logLevel?: LogLevel): PackageManager { |
35 | 30 | const lockFile = path.basename(lockFilePath);
|
36 | 31 |
|
37 | 32 | switch (lockFile) {
|
38 |
| - case PackageManager.NPM.lockFile: |
39 |
| - return PackageManager.NPM; |
40 |
| - case PackageManager.YARN.lockFile: |
41 |
| - return PackageManager.YARN; |
42 |
| - case PackageManager.PNPM.lockFile: |
43 |
| - return PackageManager.PNPM; |
| 33 | + case LockFile.YARN: |
| 34 | + return new PackageManager({ |
| 35 | + lockFile: LockFile.YARN, |
| 36 | + installCommand: logLevel && logLevel !== LogLevel.INFO ? ['yarn', 'install', '--no-immutable', '--silent'] : ['yarn', 'install', '--no-immutable'], |
| 37 | + runCommand: ['yarn', 'run'], |
| 38 | + }); |
| 39 | + case LockFile.PNPM: |
| 40 | + return new PackageManager({ |
| 41 | + lockFile: LockFile.PNPM, |
| 42 | + installCommand: logLevel && logLevel !== LogLevel.INFO ? ['pnpm', 'install', '--reporter', 'silent'] : ['pnpm', 'install'], |
| 43 | + runCommand: ['pnpm', 'exec'], |
| 44 | + argsSeparator: '--', |
| 45 | + }); |
44 | 46 | default:
|
45 |
| - return PackageManager.NPM; |
| 47 | + return new PackageManager({ |
| 48 | + lockFile: LockFile.NPM, |
| 49 | + installCommand: logLevel ? ['npm', 'ci', '--loglevel', logLevel] : ['npm', 'ci'], |
| 50 | + runCommand: ['npx', '--no-install'], |
| 51 | + }); |
46 | 52 | }
|
47 | 53 | }
|
48 | 54 |
|
|
0 commit comments