Skip to content

Commit

Permalink
Merge pull request #20428 from storybookjs/issue-17870-automigrate-pk…
Browse files Browse the repository at this point in the history
…g-mgr

CLI: Use correct package manager for automigrate
  • Loading branch information
IanVS authored Dec 31, 2022
2 parents 038735d + 9c2799c commit 4764461
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
7 changes: 4 additions & 3 deletions code/lib/cli/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ export async function add(
addon: string,
options: { useNpm: boolean; packageManager: PackageManagerName; skipPostinstall: boolean }
) {
const { useNpm, packageManager: pkgMgr } = options;
if (useNpm) {
let { packageManager: pkgMgr } = options;
if (options.useNpm) {
useNpmWarning();
pkgMgr = 'npm';
}
const packageManager = JsPackageManagerFactory.getPackageManager({ useNpm, force: pkgMgr });
const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr });
const packageJson = packageManager.retrievePackageJson();
const [addonName, versionSpecifier] = getVersionSpecifier(addon);

Expand Down
25 changes: 21 additions & 4 deletions code/lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import tempy from 'tempy';
import dedent from 'ts-dedent';

import { join } from 'path';
import { JsPackageManagerFactory, type PackageManagerName } from '../js-package-manager';
import {
JsPackageManagerFactory,
useNpmWarning,
type PackageManagerName,
} from '../js-package-manager';

import type { Fix } from './fixes';
import { fixes as allFixes } from './fixes';
Expand Down Expand Up @@ -47,7 +51,7 @@ interface FixOptions {
yes?: boolean;
dryRun?: boolean;
useNpm?: boolean;
force?: PackageManagerName;
packageManager?: PackageManagerName;
}

enum FixStatus {
Expand All @@ -72,12 +76,25 @@ const logAvailableMigrations = () => {
logger.info(`\nThe following migrations are available: ${availableFixes}`);
};

export const automigrate = async ({ fixId, dryRun, yes, useNpm, force, list }: FixOptions = {}) => {
export const automigrate = async ({
fixId,
dryRun,
yes,
useNpm,
packageManager: pkgMgr,
list,
}: FixOptions = {}) => {
if (list) {
logAvailableMigrations();
return null;
}

if (useNpm) {
useNpmWarning();
// eslint-disable-next-line no-param-reassign
pkgMgr = 'npm';
}

const fixes = fixId ? allFixes.filter((f) => f.id === fixId) : allFixes;

if (fixId && fixes.length === 0) {
Expand All @@ -88,7 +105,7 @@ export const automigrate = async ({ fixId, dryRun, yes, useNpm, force, list }: F

augmentLogsToFile();

const packageManager = JsPackageManagerFactory.getPackageManager({ useNpm, force });
const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr });

logger.info('🔎 checking possible migrations..');
const fixResults = {} as Record<FixId, FixStatus>;
Expand Down
10 changes: 6 additions & 4 deletions code/lib/cli/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,13 @@ const projectTypeInquirer = async (
};

async function doInitiate(options: CommandOptions, pkg: PackageJson): Promise<void> {
const { useNpm, packageManager: pkgMgr } = options;
if (useNpm) {
let { packageManager: pkgMgr } = options;
if (options.useNpm) {
useNpmWarning();

pkgMgr = 'npm';
}
const packageManager = JsPackageManagerFactory.getPackageManager({ useNpm, force: pkgMgr });
const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr });
const welcomeMessage = 'storybook init - the simplest way to add a Storybook to your project.';
logger.log(chalk.inverse(`\n ${welcomeMessage} \n`));

Expand Down Expand Up @@ -327,7 +329,7 @@ async function doInitiate(options: CommandOptions, pkg: PackageJson): Promise<vo
telemetry('init', { projectType });
}

await automigrate({ yes: options.yes || process.env.CI === 'true', useNpm, force: pkgMgr });
await automigrate({ yes: options.yes || process.env.CI === 'true', packageManager: pkgMgr });

logger.log('\nTo run your Storybook, type:\n');
codeLog([packageManager.getRunStorybookCommand()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ findUpSyncMock.mockReturnValue(undefined);
describe('JsPackageManagerFactory', () => {
describe('getPackageManager', () => {
describe('return an NPM proxy', () => {
it('when `useNpm` option is used', () => {
expect(JsPackageManagerFactory.getPackageManager({ useNpm: true })).toBeInstanceOf(
NPMProxy
);
});

it('when `force` option is `npm`', () => {
expect(JsPackageManagerFactory.getPackageManager({ force: 'npm' })).toBeInstanceOf(
NPMProxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { Yarn1Proxy } from './Yarn1Proxy';

export class JsPackageManagerFactory {
public static getPackageManager(
{ force, useNpm }: { force?: PackageManagerName; useNpm?: boolean } = {},
{ force }: { force?: PackageManagerName } = {},
cwd?: string
): JsPackageManager {
if (useNpm || force === 'npm') {
if (force === 'npm') {
return new NPMProxy({ cwd });
}
if (force === 'pnpm') {
Expand Down
6 changes: 4 additions & 2 deletions code/lib/cli/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ export const doUpgrade = async ({
}: UpgradeOptions) => {
if (useNpm) {
useNpmWarning();
// eslint-disable-next-line no-param-reassign
pkgMgr = 'npm';
}
const packageManager = JsPackageManagerFactory.getPackageManager({ useNpm, force: pkgMgr });
const packageManager = JsPackageManagerFactory.getPackageManager({ force: pkgMgr });

const beforeVersion = await getStorybookCoreVersion();

Expand Down Expand Up @@ -206,7 +208,7 @@ export const doUpgrade = async ({
let automigrationResults;
if (!skipCheck) {
checkVersionConsistency();
automigrationResults = await automigrate({ dryRun, yes, useNpm, force: pkgMgr });
automigrationResults = await automigrate({ dryRun, yes, packageManager: pkgMgr });
}

if (!options.disableTelemetry) {
Expand Down

0 comments on commit 4764461

Please sign in to comment.