Skip to content

Commit 67a9b17

Browse files
Merge pull request #31796 from storybookjs/valentin/circumvent-npm-peer-dep-error-during-upgrade
Core: Enhance package manager install methods to support optional force flag (cherry picked from commit c8f2db1)
1 parent a834932 commit 67a9b17

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

code/core/src/common/js-package-manager/BUNProxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ export class BUNProxy extends JsPackageManager {
190190
};
191191
}
192192

193-
protected runInstall() {
193+
protected runInstall(options?: { force?: boolean }) {
194194
return this.executeCommand({
195195
command: 'bun',
196-
args: ['install', ...this.getInstallArgs()],
196+
args: ['install', ...this.getInstallArgs(), ...(options?.force ? ['--force'] : [])],
197197
stdio: 'inherit',
198198
cwd: this.cwd,
199199
});

code/core/src/common/js-package-manager/JsPackageManager.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export abstract class JsPackageManager {
134134
return false;
135135
}
136136

137-
async installDependencies() {
138-
await prompt.executeTask(() => this.runInstall(), {
137+
async installDependencies(options?: { force?: boolean }) {
138+
await prompt.executeTask(() => this.runInstall(options), {
139139
id: 'install-dependencies',
140140
intro: 'Installing dependencies...',
141141
error: 'An error occurred while installing dependencies.',
@@ -146,13 +146,16 @@ export abstract class JsPackageManager {
146146
this.clearInstalledVersionCache();
147147
}
148148

149-
async dedupeDependencies() {
150-
await prompt.executeTask(() => this.runInternalCommand('dedupe', [], this.cwd), {
151-
id: 'dedupe-dependencies',
152-
intro: 'Deduplicating dependencies...',
153-
error: 'An error occurred while deduplicating dependencies.',
154-
success: 'Dependencies deduplicated',
155-
});
149+
async dedupeDependencies(options?: { force?: boolean }) {
150+
await prompt.executeTask(
151+
() => this.runInternalCommand('dedupe', [...(options?.force ? ['--force'] : [])], this.cwd),
152+
{
153+
id: 'dedupe-dependencies',
154+
intro: 'Deduplicating dependencies...',
155+
error: 'An error occurred while deduplicating dependencies.',
156+
success: 'Dependencies deduplicated',
157+
}
158+
);
156159

157160
// Clear installed version cache after installation
158161
this.clearInstalledVersionCache();
@@ -535,7 +538,7 @@ export abstract class JsPackageManager {
535538
const resolutions = this.getResolutions(packageJson, versions);
536539
this.writePackageJson({ ...packageJson, ...resolutions }, operationDir);
537540
}
538-
protected abstract runInstall(): ExecaChildProcess;
541+
protected abstract runInstall(options?: { force?: boolean }): ExecaChildProcess;
539542

540543
protected abstract runAddDeps(
541544
dependencies: string[],

code/core/src/common/js-package-manager/NPMProxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ export class NPMProxy extends JsPackageManager {
186186
};
187187
}
188188

189-
protected runInstall() {
189+
protected runInstall(options?: { force?: boolean }) {
190190
return this.executeCommand({
191191
command: 'npm',
192-
args: ['install', ...this.getInstallArgs()],
192+
args: ['install', ...this.getInstallArgs(), ...(options?.force ? ['--force'] : [])],
193193
cwd: this.cwd,
194194
stdio: prompt.getPreferredStdio(),
195195
});

code/core/src/common/js-package-manager/PNPMProxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ export class PNPMProxy extends JsPackageManager {
195195
};
196196
}
197197

198-
protected runInstall() {
198+
protected runInstall(options?: { force?: boolean }) {
199199
return this.executeCommand({
200200
command: 'pnpm',
201-
args: ['install', ...this.getInstallArgs()],
201+
args: ['install', ...this.getInstallArgs(), ...(options?.force ? ['--force'] : [])],
202202
stdio: prompt.getPreferredStdio(),
203203
cwd: this.cwd,
204204
});

code/core/src/common/js-package-manager/Yarn1Proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ export class Yarn1Proxy extends JsPackageManager {
141141
};
142142
}
143143

144-
protected runInstall() {
144+
protected runInstall(options?: { force?: boolean }) {
145145
return this.executeCommand({
146146
command: 'yarn',
147-
args: ['install', ...this.getInstallArgs()],
147+
args: ['install', ...this.getInstallArgs(), ...(options?.force ? ['--force'] : [])],
148148
stdio: prompt.getPreferredStdio(),
149149
cwd: this.cwd,
150150
});

code/lib/cli-storybook/src/upgrade.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,12 @@ export async function upgrade(options: UpgradeOptions): Promise<void> {
427427
? JsPackageManagerFactory.getPackageManager({ force: options.packageManager })
428428
: storybookProjects[0].packageManager;
429429

430-
await rootPackageManager.installDependencies();
430+
if (rootPackageManager.type === 'npm') {
431+
// see https://github.com/npm/cli/issues/8059 for more details
432+
await rootPackageManager.installDependencies({ force: true });
433+
} else {
434+
await rootPackageManager.installDependencies();
435+
}
431436

432437
if (rootPackageManager.type !== 'yarn1' && rootPackageManager.isStorybookInMonorepo()) {
433438
logger.warn(
@@ -442,7 +447,12 @@ export async function upgrade(options: UpgradeOptions): Promise<void> {
442447
}));
443448

444449
if (dedupe) {
445-
await rootPackageManager.dedupeDependencies();
450+
if (rootPackageManager.type === 'npm') {
451+
// see https://github.com/npm/cli/issues/8059 for more details
452+
await rootPackageManager.dedupeDependencies({ force: true });
453+
} else {
454+
await rootPackageManager.dedupeDependencies();
455+
}
446456
} else {
447457
logger.log(
448458
`If you find any issues running Storybook, you can run ${rootPackageManager.getRunCommand('dedupe')} manually to deduplicate your dependencies and try again.`

0 commit comments

Comments
 (0)