Skip to content

Commit d05fd3d

Browse files
authored
Merge pull request #14106 from storybookjs/12983-npm-legacy-peer-deps
CLI: Add `--legacy-peer-deps` for NPM7 install
2 parents 79ed5f2 + dc4ae0c commit d05fd3d

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

lib/cli/src/js-package-manager/NPMProxy.test.ts

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,56 @@ describe('NPM Proxy', () => {
2222
});
2323

2424
describe('installDependencies', () => {
25-
it('should run `npm install`', () => {
26-
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('');
25+
describe('npm6', () => {
26+
it('should run `npm install`', () => {
27+
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('6.0.0');
2728

28-
npmProxy.installDependencies();
29+
npmProxy.installDependencies();
2930

30-
expect(executeCommandSpy).toHaveBeenCalledWith('npm', ['install'], expect.any(String));
31+
expect(executeCommandSpy).toHaveBeenLastCalledWith('npm', ['install'], expect.any(String));
32+
});
33+
});
34+
describe('npm7', () => {
35+
it('should run `npm install --legacy-peer-deps`', () => {
36+
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.1.0');
37+
38+
npmProxy.installDependencies();
39+
40+
expect(executeCommandSpy).toHaveBeenLastCalledWith(
41+
'npm',
42+
['install', '--legacy-peer-deps'],
43+
expect.any(String)
44+
);
45+
});
3146
});
3247
});
3348

3449
describe('addDependencies', () => {
35-
it('with devDep it should run `npm install -D @storybook/addons`', () => {
36-
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('');
37-
38-
npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);
39-
40-
expect(executeCommandSpy).toHaveBeenCalledWith(
41-
'npm',
42-
['install', '-D', '@storybook/addons'],
43-
expect.any(String)
44-
);
50+
describe('npm6', () => {
51+
it('with devDep it should run `npm install -D @storybook/addons`', () => {
52+
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('6.0.0');
53+
54+
npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);
55+
56+
expect(executeCommandSpy).toHaveBeenLastCalledWith(
57+
'npm',
58+
['install', '-D', '@storybook/addons'],
59+
expect.any(String)
60+
);
61+
});
62+
});
63+
describe('npm7', () => {
64+
it('with devDep it should run `npm install -D @storybook/addons`', () => {
65+
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.0.0');
66+
67+
npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);
68+
69+
expect(executeCommandSpy).toHaveBeenLastCalledWith(
70+
'npm',
71+
['install', '--legacy-peer-deps', '-D', '@storybook/addons'],
72+
expect.any(String)
73+
);
74+
});
4575
});
4676
});
4777

lib/cli/src/js-package-manager/NPMProxy.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import semver from '@storybook/semver';
12
import { JsPackageManager } from './JsPackageManager';
23

34
export class NPMProxy extends JsPackageManager {
45
readonly type = 'npm';
56

7+
installArgs: string[] | undefined;
8+
69
initPackageJson() {
710
return this.executeCommand('npm', ['init', '-y']);
811
}
@@ -15,8 +18,18 @@ export class NPMProxy extends JsPackageManager {
1518
return `npm run ${command}`;
1619
}
1720

21+
getInstallArgs(): string[] {
22+
if (!this.installArgs) {
23+
const version = this.executeCommand('npm', ['--version']);
24+
this.installArgs = semver.gte(version, '7.0.0')
25+
? ['install', '--legacy-peer-deps']
26+
: ['install'];
27+
}
28+
return this.installArgs;
29+
}
30+
1831
protected runInstall(): void {
19-
this.executeCommand('npm', ['install'], 'inherit');
32+
this.executeCommand('npm', this.getInstallArgs(), 'inherit');
2033
}
2134

2235
protected runAddDeps(dependencies: string[], installAsDevDependencies: boolean): void {
@@ -26,7 +39,7 @@ export class NPMProxy extends JsPackageManager {
2639
args = ['-D', ...args];
2740
}
2841

29-
this.executeCommand('npm', ['install', ...args], 'inherit');
42+
this.executeCommand('npm', [...this.getInstallArgs(), ...args], 'inherit');
3043
}
3144

3245
protected runGetVersions<T extends boolean>(

0 commit comments

Comments
 (0)