|
| 1 | +import type { Choice } from 'prompts' |
| 2 | +import { spawnSync } from 'node:child_process' |
| 3 | +import { existsSync, readdirSync, readFileSync } from 'node:fs' |
| 4 | +import { dirname, join, resolve } from 'node:path' |
| 5 | +import process from 'node:process' |
| 6 | +import { fileURLToPath } from 'node:url' |
| 7 | +import prompts from 'prompts' |
| 8 | + |
| 9 | +/** |
| 10 | + * Workspace globs, mirrored from `pnpm-workspace.yaml`'s `packages:` list |
| 11 | + * (same mirroring rationale as `scripts/verify-typecheck-coverage.ts`): a |
| 12 | + * new example, plugin, or other playground shows up here for free, with no |
| 13 | + * change to this script. |
| 14 | + */ |
| 15 | +const WORKSPACE_PATTERNS = ['packages/*', 'plugins/*', 'examples/*', 'storybook', 'docs'] |
| 16 | + |
| 17 | +/** |
| 18 | + * Script names that make a workspace package runnable as a "play" — the |
| 19 | + * first one present in a package's `scripts` wins. |
| 20 | + */ |
| 21 | +const RUN_SCRIPTS = ['dev', 'storybook', 'docs', 'start'] |
| 22 | + |
| 23 | +const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), '..') |
| 24 | + |
| 25 | +interface Play { |
| 26 | + dir: string |
| 27 | + pkgName: string |
| 28 | + script: string |
| 29 | +} |
| 30 | + |
| 31 | +function expandPattern(pattern: string): string[] { |
| 32 | + if (!pattern.endsWith('/*')) |
| 33 | + return [pattern] |
| 34 | + const base = pattern.slice(0, -2) |
| 35 | + const baseDir = join(rootDir, base) |
| 36 | + if (!existsSync(baseDir)) |
| 37 | + return [] |
| 38 | + return readdirSync(baseDir, { withFileTypes: true }) |
| 39 | + .filter(entry => entry.isDirectory()) |
| 40 | + .map(entry => `${base}/${entry.name}`) |
| 41 | +} |
| 42 | + |
| 43 | +function findPlay(dir: string): Play | undefined { |
| 44 | + const pkgPath = join(rootDir, dir, 'package.json') |
| 45 | + if (!existsSync(pkgPath)) |
| 46 | + return undefined |
| 47 | + const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) |
| 48 | + const script = RUN_SCRIPTS.find(name => pkg.scripts?.[name]) |
| 49 | + if (!script) |
| 50 | + return undefined |
| 51 | + return { dir, pkgName: pkg.name, script } |
| 52 | +} |
| 53 | + |
| 54 | +function suggest(input: string, choices: Choice[]): Promise<Choice[]> { |
| 55 | + const needle = input.toLowerCase() |
| 56 | + return Promise.resolve(choices.filter(choice => choice.title.toLowerCase().includes(needle))) |
| 57 | +} |
| 58 | + |
| 59 | +async function main(): Promise<void> { |
| 60 | + const plays = WORKSPACE_PATTERNS |
| 61 | + .flatMap(expandPattern) |
| 62 | + .map(findPlay) |
| 63 | + .filter((play): play is Play => play !== undefined) |
| 64 | + .sort((a, b) => a.dir.localeCompare(b.dir)) |
| 65 | + |
| 66 | + if (plays.length === 0) { |
| 67 | + console.error(`No playgrounds found — none of ${WORKSPACE_PATTERNS.join(', ')} has a package.json with a ${RUN_SCRIPTS.join('/')} script.`) |
| 68 | + process.exitCode = 1 |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + const { play } = await prompts({ |
| 73 | + type: 'autocomplete', |
| 74 | + name: 'play', |
| 75 | + message: 'Select a playground to run', |
| 76 | + choices: plays.map(p => ({ |
| 77 | + title: p.dir, |
| 78 | + description: `${p.pkgName} · pnpm run ${p.script}`, |
| 79 | + value: p, |
| 80 | + })), |
| 81 | + suggest, |
| 82 | + }) as { play?: Play } |
| 83 | + |
| 84 | + if (!play) { |
| 85 | + console.log('No playground selected.') |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + console.log(`\n▶ pnpm run ${play.script} (${play.dir})\n`) |
| 90 | + |
| 91 | + const result = spawnSync('pnpm', ['run', play.script], { |
| 92 | + cwd: join(rootDir, play.dir), |
| 93 | + stdio: 'inherit', |
| 94 | + shell: false, |
| 95 | + }) |
| 96 | + |
| 97 | + process.exitCode = result.status ?? 1 |
| 98 | +} |
| 99 | + |
| 100 | +main() |
0 commit comments