Skip to content

Commit

Permalink
Postinstall: Run command in shell for windows (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun Evening authored Oct 5, 2023
1 parent c40cf15 commit e5201d5
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions src/postinstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,44 @@ import { spawn } from "child_process";

type PackageManagers = "npm" | "pnpm" | "yarn1" | "yarn2";

const PACKAGE_MANAGER_TO_COMMAND: Record<PackageManagers, string> = {
npm: "npx",
pnpm: "pnpm dlx",
yarn1: "npx",
yarn2: "yarn dlx",
const PACKAGE_MANAGER_TO_COMMAND: Record<PackageManagers, string[]> = {
npm: ["npx"],
pnpm: ["pnpm", "dlx"],
yarn1: ["npx"],
yarn2: ["yarn", "dlx"],
};

const selectPackageManagerCommand = (
packageManager: PackageManagers
): string[] => PACKAGE_MANAGER_TO_COMMAND[packageManager];

const spawnPackageManagerScript = async (
packageManager: PackageManagers,
args: string[]
) => {
const [command, ...baseArgs] = selectPackageManagerCommand(packageManager);

await spawn(command, [...baseArgs, ...args], {
stdio: "inherit",
cwd: process.cwd(),
shell: true,
});
};

const automigrate = async ({
packageManager,
packageManager = "npm",
}: {
packageManager: PackageManagers;
}) => {
const command = PACKAGE_MANAGER_TO_COMMAND[packageManager];
await spawn(command, ["@storybook/auto-config", "styling"], {
stdio: "inherit",
cwd: process.cwd(),
});
try {
await spawnPackageManagerScript(packageManager, [
"@storybook/auto-config",
"styling",
]);
} catch (e) {
console.error(e);
process.exit(1);
}
};

export default automigrate;

0 comments on commit e5201d5

Please sign in to comment.