/
package-manager.ts
121 lines (110 loc) · 3.32 KB
/
package-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { join } from 'path';
/*
* Because we don't want to depend on @nrwl/workspace (to speed up the workspace creation)
* we duplicate the helper functions from @nrwl/workspace in this file.
*/
export const packageManagerList = ['pnpm', 'yarn', 'npm'] as const;
export type PackageManager = typeof packageManagerList[number];
export function detectPackageManager(dir: string = ''): PackageManager {
return existsSync(join(dir, 'yarn.lock'))
? 'yarn'
: existsSync(join(dir, 'pnpm-lock.yaml'))
? 'pnpm'
: 'npm';
}
/**
* Returns commands for the package manager used in the workspace.
* By default, the package manager is derived based on the lock file,
* but it can also be passed in explicitly.
*
* Example:
*
* ```javascript
* execSync(`${getPackageManagerCommand().addDev} my-dev-package`);
* ```
*
*/
export function getPackageManagerCommand(
packageManager: PackageManager = detectPackageManager()
): {
install: string;
add: string;
addDev: string;
rm: string;
exec: string;
list: string;
run: (script: string, args: string) => string;
} {
switch (packageManager) {
case 'yarn':
return {
install: 'yarn',
add: 'yarn add -W',
addDev: 'yarn add -D -W',
rm: 'yarn remove',
exec: 'yarn',
run: (script: string, args: string) => `yarn ${script} ${args}`,
list: 'yarn list',
};
case 'pnpm':
const [major, minor] = getPackageManagerVersion('pnpm').split('.');
let useExec = false;
if (+major >= 6 && +minor >= 13) {
useExec = true;
}
return {
install: 'pnpm install --no-frozen-lockfile', // explicitly disable in case of CI
add: 'pnpm add',
addDev: 'pnpm add -D',
rm: 'pnpm rm',
exec: useExec ? 'pnpm exec' : 'pnpx',
run: (script: string, args: string) => `pnpm run ${script} -- ${args}`,
list: 'pnpm ls --depth 100',
};
case 'npm':
process.env.npm_config_legacy_peer_deps =
process.env.npm_config_legacy_peer_deps ?? 'true';
return {
install: 'npm install',
add: 'npm install',
addDev: 'npm install -D',
rm: 'npm rm',
exec: 'npx',
run: (script: string, args: string) => `npm run ${script} -- ${args}`,
list: 'npm ls',
};
}
}
export function getPackageManagerVersion(
packageManager: PackageManager
): string {
return execSync(`${packageManager} --version`).toString('utf-8').trim();
}
/**
* Detects which package manager was used to invoke create-nx-{plugin|workspace} command
* based on the main Module process that invokes the command
* - npx returns 'npm'
* - pnpx returns 'pnpm'
* - yarn create returns 'yarn'
*
* Default to 'npm'
*/
export function detectInvokedPackageManager(): PackageManager {
let detectedPackageManager: PackageManager = 'npm';
// mainModule is deprecated since Node 14, fallback for older versions
const invoker = require.main || process['mainModule'];
// default to `npm`
if (!invoker) {
return detectedPackageManager;
}
console.log(invoker.path);
for (const pkgManager of packageManagerList) {
if (invoker.path.includes(pkgManager)) {
detectedPackageManager = pkgManager;
break;
}
}
return detectedPackageManager;
}