|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import { readdirSync, existsSync, readFileSync } from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +/** Note: this script intentionally doesn't run during regular `pnpm install` from the project root because it's not something we expect to need to do all the time and integrating it into the project install flow is excessive */ |
| 6 | + |
| 7 | +const examplesDir = path.resolve(__dirname); |
| 8 | + |
| 9 | +/** Get all directories in the examples folder */ |
| 10 | +const exampleDirs = readdirSync(examplesDir).filter((dir) => |
| 11 | + existsSync(path.join(examplesDir, dir, "package.json")) |
| 12 | +); |
| 13 | + |
| 14 | +exampleDirs.forEach((dir) => { |
| 15 | + const packageJsonPath = path.join(examplesDir, dir, "package.json"); |
| 16 | + |
| 17 | + try { |
| 18 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")); |
| 19 | + |
| 20 | + // Check the packageManager field and run the correct install command |
| 21 | + const packageManager: string = packageJson.packageManager; |
| 22 | + if (!packageManager) { |
| 23 | + throw new Error(`Missing packageManager field in ${packageJsonPath}`); |
| 24 | + } |
| 25 | + |
| 26 | + let installCmd: string; |
| 27 | + |
| 28 | + if (packageManager.startsWith("pnpm")) { |
| 29 | + installCmd = "pnpm install"; |
| 30 | + } else if (packageManager.startsWith("yarn")) { |
| 31 | + installCmd = "yarn install"; |
| 32 | + } else if (packageManager.startsWith("npm")) { |
| 33 | + installCmd = "npm install"; |
| 34 | + } else { |
| 35 | + throw new Error(`Unknown package manager "${packageManager}" in ${dir}`); |
| 36 | + } |
| 37 | + |
| 38 | + console.log(`Running ${installCmd} in ${dir}...`); |
| 39 | + execSync(installCmd, { |
| 40 | + stdio: "inherit", |
| 41 | + cwd: path.join(examplesDir, dir), |
| 42 | + }); |
| 43 | + } catch (error) { |
| 44 | + throw new Error(`Failed to process ${packageJsonPath}: ${error}`); |
| 45 | + } |
| 46 | +}); |
0 commit comments