From e5201d5a37643774e2fe2f9f90c8597be034ff6e Mon Sep 17 00:00:00 2001 From: Shaun Evening Date: Thu, 5 Oct 2023 09:47:17 -0400 Subject: [PATCH] Postinstall: Run command in shell for windows (#8) --- src/postinstall.ts | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/postinstall.ts b/src/postinstall.ts index 87e2398..58feec9 100644 --- a/src/postinstall.ts +++ b/src/postinstall.ts @@ -2,23 +2,44 @@ import { spawn } from "child_process"; type PackageManagers = "npm" | "pnpm" | "yarn1" | "yarn2"; -const PACKAGE_MANAGER_TO_COMMAND: Record = { - npm: "npx", - pnpm: "pnpm dlx", - yarn1: "npx", - yarn2: "yarn dlx", +const PACKAGE_MANAGER_TO_COMMAND: Record = { + 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;