|
1 |
| -import path from 'node:path'; |
| 1 | +import path, { dirname } from 'node:path'; |
2 | 2 | import createdLogger from 'debug';
|
3 | 3 | import preferredPm from 'preferred-pm';
|
| 4 | +import { execa } from 'execa'; |
4 | 5 |
|
5 | 6 | const debug = createdLogger('yeoman:environment:package-manager');
|
6 | 7 |
|
7 |
| -const packageManagerMixin = cls => |
8 |
| - class extends cls { |
9 |
| - /** |
10 |
| - * @private |
11 |
| - * Get the destination package.json file. |
12 |
| - * @return {Vinyl | undefined} a Vinyl file. |
13 |
| - */ |
14 |
| - getDestinationPackageJson() { |
15 |
| - return this.sharedFs.get(path.resolve(this.cwd, 'package.json')); |
16 |
| - } |
17 |
| - |
18 |
| - /** |
19 |
| - * @private |
20 |
| - * Get the destination package.json commit status. |
21 |
| - * @return {boolean} package.json commit status. |
22 |
| - */ |
23 |
| - isDestinationPackageJsonCommitted() { |
24 |
| - const file = this.getDestinationPackageJson(); |
25 |
| - return file && file.committed; |
26 |
| - } |
27 |
| - |
28 |
| - /** |
29 |
| - * @private |
30 |
| - * Detect the package manager based on files or use the passed one. |
31 |
| - * @return {string} package manager. |
32 |
| - */ |
33 |
| - async detectPackageManager() { |
34 |
| - if (this.options.nodePackageManager) { |
35 |
| - return this.options.nodePackageManager; |
36 |
| - } |
37 |
| - |
38 |
| - const pm = await preferredPm(this.cwd); |
39 |
| - return pm && pm.name; |
40 |
| - } |
41 |
| - |
42 |
| - /** |
43 |
| - * Executes package manager install. |
44 |
| - * - checks if package.json was committed. |
45 |
| - * - uses a preferred package manager or try to detect. |
46 |
| - * @return {Promise<boolean>} Promise true if the install execution suceeded. |
47 |
| - */ |
48 |
| - async packageManagerInstallTask() { |
49 |
| - if (!this.getDestinationPackageJson()) { |
50 |
| - return false; |
51 |
| - } |
52 |
| - |
53 |
| - if (this.compatibilityMode === 'v4') { |
54 |
| - debug('Running in generator < 5 compatibility. Package manager install is done by the generator.'); |
55 |
| - return false; |
56 |
| - } |
57 |
| - |
58 |
| - const { customInstallTask } = this.composedStore; |
59 |
| - if (customInstallTask && typeof customInstallTask !== 'function') { |
60 |
| - debug('Install disabled by customInstallTask'); |
61 |
| - return false; |
62 |
| - } |
63 |
| - |
64 |
| - if (!this.isDestinationPackageJsonCommitted()) { |
65 |
| - this.adapter.log(` |
| 8 | +/** |
| 9 | + * Executes package manager install. |
| 10 | + * - checks if package.json was committed. |
| 11 | + * - uses a preferred package manager or try to detect. |
| 12 | + * @return {Promise<boolean>} Promise true if the install execution suceeded. |
| 13 | + */ |
| 14 | +/* |
| 15 | + const { customInstallTask } = this.composedStore; |
| 16 | + packageJsonFile: join(this.cwd, 'package.json'); |
| 17 | +
|
| 18 | +*/ |
| 19 | +export async function packageManagerInstallTask({ memFs, packageJsonFile, customInstallTask, adapter, nodePackageManager, skipInstall }) { |
| 20 | + /** |
| 21 | + * @private |
| 22 | + * Get the destination package.json file. |
| 23 | + * @return {Vinyl | undefined} a Vinyl file. |
| 24 | + */ |
| 25 | + function getDestinationPackageJson() { |
| 26 | + return memFs.get(path.resolve(packageJsonFile)); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @private |
| 31 | + * Get the destination package.json commit status. |
| 32 | + * @return {boolean} package.json commit status. |
| 33 | + */ |
| 34 | + function isDestinationPackageJsonCommitted() { |
| 35 | + const file = getDestinationPackageJson(); |
| 36 | + return file.committed; |
| 37 | + } |
| 38 | + |
| 39 | + if (!getDestinationPackageJson()) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + if (customInstallTask && typeof customInstallTask !== 'function') { |
| 44 | + debug('Install disabled by customInstallTask'); |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + if (!isDestinationPackageJsonCommitted()) { |
| 49 | + adapter.log(` |
66 | 50 | No change to package.json was detected. No package manager install will be executed.`);
|
67 |
| - return false; |
68 |
| - } |
| 51 | + return false; |
| 52 | + } |
69 | 53 |
|
70 |
| - this.adapter.log(` |
| 54 | + adapter.log(` |
71 | 55 | Changes to package.json were detected.`);
|
72 | 56 |
|
73 |
| - if (this.options.skipInstall) { |
74 |
| - this.adapter.log(`Skipping package manager install. |
| 57 | + if (skipInstall) { |
| 58 | + adapter.log(`Skipping package manager install. |
75 | 59 | `);
|
76 |
| - return false; |
77 |
| - } |
| 60 | + return false; |
| 61 | + } |
78 | 62 |
|
79 |
| - let packageManagerName = await this.detectPackageManager(); |
| 63 | + // eslint-disable-next-line unicorn/no-await-expression-member |
| 64 | + let packageManagerName = nodePackageManager ?? (await preferredPm(dirname(packageJsonFile)))?.name; |
80 | 65 |
|
81 |
| - const execPackageManager = async () => { |
82 |
| - if (!packageManagerName) { |
83 |
| - packageManagerName = 'npm'; |
84 |
| - this.adapter.log('Error detecting the package manager. Falling back to npm.'); |
85 |
| - } |
| 66 | + const execPackageManager = async () => { |
| 67 | + if (!packageManagerName) { |
| 68 | + packageManagerName = 'npm'; |
| 69 | + adapter.log('Error detecting the package manager. Falling back to npm.'); |
| 70 | + } |
86 | 71 |
|
87 |
| - if (!['npm', 'yarn', 'pnpm'].includes(packageManagerName)) { |
88 |
| - this.adapter.log(`${packageManagerName} is not a supported package manager. Run it by yourself.`); |
89 |
| - return false; |
90 |
| - } |
| 72 | + if (!['npm', 'yarn', 'pnpm'].includes(packageManagerName)) { |
| 73 | + adapter.log(`${packageManagerName} is not a supported package manager. Run it by yourself.`); |
| 74 | + return false; |
| 75 | + } |
91 | 76 |
|
92 |
| - this.adapter.log(` |
| 77 | + adapter.log(` |
93 | 78 | Running ${packageManagerName} install for you to install the required dependencies.`);
|
94 |
| - await this.spawnCommand(packageManagerName, ['install']); |
95 |
| - return true; |
96 |
| - }; |
| 79 | + await execa(packageManagerName, ['install'], { stdio: 'inherit', cwd: dirname(packageJsonFile) }); |
| 80 | + return true; |
| 81 | + }; |
97 | 82 |
|
98 |
| - if (customInstallTask) { |
99 |
| - const result = customInstallTask(packageManagerName, execPackageManager); |
100 |
| - if (!result || !result.then) { |
101 |
| - return true; |
102 |
| - } |
| 83 | + if (customInstallTask) { |
| 84 | + const result = customInstallTask(packageManagerName, execPackageManager); |
| 85 | + if (!result || !result.then) { |
| 86 | + return true; |
| 87 | + } |
103 | 88 |
|
104 |
| - return result; |
105 |
| - } |
| 89 | + return result; |
| 90 | + } |
106 | 91 |
|
107 |
| - return execPackageManager(); |
108 |
| - } |
109 |
| - }; |
110 |
| -export default packageManagerMixin; |
| 92 | + return execPackageManager(); |
| 93 | +} |
0 commit comments