Skip to content

Commit

Permalink
Merge pull request #14106 from storybookjs/12983-npm-legacy-peer-deps
Browse files Browse the repository at this point in the history
CLI: Add `--legacy-peer-deps` for NPM7 install
  • Loading branch information
shilman committed Mar 3, 2021
1 parent 3b6baa2 commit 6f50b52
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
58 changes: 44 additions & 14 deletions lib/cli/src/js-package-manager/NPMProxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,56 @@ describe('NPM Proxy', () => {
});

describe('installDependencies', () => {
it('should run `npm install`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('');
describe('npm6', () => {
it('should run `npm install`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('6.0.0');

npmProxy.installDependencies();
npmProxy.installDependencies();

expect(executeCommandSpy).toHaveBeenCalledWith('npm', ['install'], expect.any(String));
expect(executeCommandSpy).toHaveBeenLastCalledWith('npm', ['install'], expect.any(String));
});
});
describe('npm7', () => {
it('should run `npm install --legacy-peer-deps`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.1.0');

npmProxy.installDependencies();

expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['install', '--legacy-peer-deps'],
expect.any(String)
);
});
});
});

describe('addDependencies', () => {
it('with devDep it should run `npm install -D @storybook/addons`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('');

npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);

expect(executeCommandSpy).toHaveBeenCalledWith(
'npm',
['install', '-D', '@storybook/addons'],
expect.any(String)
);
describe('npm6', () => {
it('with devDep it should run `npm install -D @storybook/addons`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('6.0.0');

npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);

expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['install', '-D', '@storybook/addons'],
expect.any(String)
);
});
});
describe('npm7', () => {
it('with devDep it should run `npm install -D @storybook/addons`', () => {
const executeCommandSpy = jest.spyOn(npmProxy, 'executeCommand').mockReturnValue('7.0.0');

npmProxy.addDependencies({ installAsDevDependencies: true }, ['@storybook/addons']);

expect(executeCommandSpy).toHaveBeenLastCalledWith(
'npm',
['install', '--legacy-peer-deps', '-D', '@storybook/addons'],
expect.any(String)
);
});
});
});

Expand Down
17 changes: 15 additions & 2 deletions lib/cli/src/js-package-manager/NPMProxy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import semver from '@storybook/semver';
import { JsPackageManager } from './JsPackageManager';

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

installArgs: string[] | undefined;

initPackageJson() {
return this.executeCommand('npm', ['init', '-y']);
}
Expand All @@ -15,8 +18,18 @@ export class NPMProxy extends JsPackageManager {
return `npm run ${command}`;
}

getInstallArgs(): string[] {
if (!this.installArgs) {
const version = this.executeCommand('npm', ['--version']);
this.installArgs = semver.gte(version, '7.0.0')
? ['install', '--legacy-peer-deps']
: ['install'];
}
return this.installArgs;
}

protected runInstall(): void {
this.executeCommand('npm', ['install'], 'inherit');
this.executeCommand('npm', this.getInstallArgs(), 'inherit');
}

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

this.executeCommand('npm', ['install', ...args], 'inherit');
this.executeCommand('npm', [...this.getInstallArgs(), ...args], 'inherit');
}

protected runGetVersions<T extends boolean>(
Expand Down

0 comments on commit 6f50b52

Please sign in to comment.