diff --git a/code/addons/themes/package.json b/code/addons/themes/package.json index 1d5a3857a9f8..5e690f76608e 100644 --- a/code/addons/themes/package.json +++ b/code/addons/themes/package.json @@ -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", diff --git a/code/addons/themes/postinstall.js b/code/addons/themes/postinstall.js new file mode 100644 index 000000000000..c84a4e88e4b4 --- /dev/null +++ b/code/addons/themes/postinstall.js @@ -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(), + }); +}; diff --git a/code/lib/cli/src/add.ts b/code/lib/cli/src/add.ts index 2b8565085dab..c5ddd1b1c995 100644 --- a/code/lib/cli/src/add.ts +++ b/code/lib/cli/src/add.ts @@ -1,5 +1,6 @@ import { getStorybookInfo } from '@storybook/core-common'; import { readConfig, writeConfig } from '@storybook/csf-tools'; +import SemVer from 'semver'; import { JsPackageManagerFactory, @@ -10,7 +11,11 @@ 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 @@ -18,7 +23,7 @@ const postinstallAddon = async (addonName: string) => { 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); @@ -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]); @@ -83,6 +90,6 @@ export async function add( await writeConfig(main); if (!options.skipPostinstall && isStorybookAddon) { - await postinstallAddon(addonName); + await postinstallAddon(addonName, { packageManager: pkgMgr }); } }