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 5 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
18 changes: 18 additions & 0 deletions code/addons/themes/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { spawn } = require('child_process');

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

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

spawn(command, ['@storybook/auto-config', 'themes'], {
stdio: "inherit",
cwd: process.cwd(),
});

}
14 changes: 10 additions & 4 deletions code/lib/cli/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,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 All @@ -33,6 +37,8 @@ const getVersionSpecifier = (addon: string) => {
return groups ? [groups[1], groups[2]] : [addon, undefined];
};

const isSemVer = (version: string) => /^\d+\.\d+\.\d+(-.*)?$/.test(version);
ShaunEvening marked this conversation as resolved.
Show resolved Hide resolved

/**
* Install the given addon package and add it to main.js
*
Expand Down Expand Up @@ -73,7 +79,7 @@ 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 = isSemVer(version) ? `${addonName}@^${version}` : `${addonName}@${version}`;
logger.log(`Installing ${addonWithVersion}`);
await packageManager.addDependencies({ installAsDevDependencies: true }, [addonWithVersion]);

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

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