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: Pass package manager to postinstall #23913

Merged
merged 9 commits into from
Aug 25, 2023
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
3 changes: 2 additions & 1 deletion code/addons/themes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"require": "./dist/preview.js",
"import": "./dist/preview.mjs"
},
"./package.json": "./package.json"
"./package.json": "./package.json",
"./postinstall": "./postinstall.js"
},
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
17 changes: 17 additions & 0 deletions code/addons/themes/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { spawn } = require('child_process');

const PACKAGE_MANAGER_TO_COMMAND = {
npm: 'npx',
yarn1: 'yarn dlx',
yarn2: 'yarn dlx',
pnpm: 'pnpm dlx',
};

module.exports = function postinstall(options) {
const command = PACKAGE_MANAGER_TO_COMMAND[options.packageManager];

spawn(command, ['@storybook/auto-config', 'themes'], {
stdio: 'inherit',
cwd: process.cwd(),
});
};
15 changes: 11 additions & 4 deletions code/lib/cli/src/add.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getStorybookInfo } from '@storybook/core-common';
import { readConfig, writeConfig } from '@storybook/csf-tools';
import SemVer from 'semver';

import {
JsPackageManagerFactory,
Expand All @@ -10,15 +11,19 @@ import { getStorybookVersion } from './utils';

const logger = console;

const postinstallAddon = async (addonName: string) => {
interface PostinstallOptions {
packageManager: PackageManagerName;
}

const postinstallAddon = async (addonName: string, options: PostinstallOptions) => {
try {
const modulePath = require.resolve(`${addonName}/postinstall`, { paths: [process.cwd()] });
// eslint-disable-next-line import/no-dynamic-require, global-require
const postinstall = require(modulePath);

try {
logger.log(`Running postinstall script for ${addonName}`);
await postinstall();
await postinstall(options);
} catch (e) {
logger.error(`Error running postinstall script for ${addonName}`);
logger.error(e);
Expand Down Expand Up @@ -73,7 +78,9 @@ export async function add(
const isStorybookAddon = addonName.startsWith('@storybook/');
const storybookVersion = await getStorybookVersion(packageManager);
const version = versionSpecifier || (isStorybookAddon ? storybookVersion : latestVersion);
const addonWithVersion = `${addonName}@^${version}`;
const addonWithVersion = SemVer.valid(version)
? `${addonName}@^${version}`
: `${addonName}@${version}`;
logger.log(`Installing ${addonWithVersion}`);
await packageManager.addDependencies({ installAsDevDependencies: true }, [addonWithVersion]);

Expand All @@ -83,6 +90,6 @@ export async function add(
await writeConfig(main);

if (!options.skipPostinstall && isStorybookAddon) {
await postinstallAddon(addonName);
await postinstallAddon(addonName, { packageManager: pkgMgr });
}
}