Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Add --legacy-peer-deps for NPM7 install #14106

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if you need some .trim() here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested by hand -- doesn't seem to matter to semver, but good thought

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