Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"scripts": {
"build": "turbo run build --concurrency=3",
"watch": "pnpm -r run watch",
"play": "tsx scripts/play.ts",
"docs": "pnpm -C docs run docs",
"docs:build": "pnpm -C docs run docs:build",
"docs:serve": "pnpm -C docs run docs:serve",
Expand All @@ -37,10 +38,12 @@
"@antfu/utils": "catalog:inlined",
"@playwright/test": "catalog:testing",
"@types/node": "catalog:types",
"@types/prompts": "catalog:types",
"@types/ws": "catalog:types",
"bumpp": "catalog:tooling",
"eslint": "catalog:tooling",
"nano-staged": "catalog:tooling",
"prompts": "catalog:tooling",
"simple-git-hooks": "catalog:tooling",
"skills-npm": "catalog:tooling",
"tsdown": "catalog:build",
Expand Down
53 changes: 44 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ catalogs:
bumpp: ^11.1.0
eslint: ^10.7.0
nano-staged: ^1.0.2
prompts: ^2.4.2
simple-git-hooks: ^2.13.1
skills-npm: ^1.2.0
typescript: ^6.0.3
types:
'@types/node': ^26.1.1
'@types/prompts': ^2.4.9
'@types/react': ^19.2.17
'@types/react-dom': ^19.2.3
'@types/ws': ^8.18.1
100 changes: 100 additions & 0 deletions scripts/play.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import type { Choice } from 'prompts'
import { spawnSync } from 'node:child_process'
import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import prompts from 'prompts'

/**
* Workspace globs, mirrored from `pnpm-workspace.yaml`'s `packages:` list
* (same mirroring rationale as `scripts/verify-typecheck-coverage.ts`): a
* new example, plugin, or other playground shows up here for free, with no
* change to this script.
*/
const WORKSPACE_PATTERNS = ['packages/*', 'plugins/*', 'examples/*', 'storybook', 'docs']

/**
* Script names that make a workspace package runnable as a "play" — the
* first one present in a package's `scripts` wins.
*/
const RUN_SCRIPTS = ['dev', 'storybook', 'docs', 'start']

const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), '..')

interface Play {
dir: string
pkgName: string
script: string
}

function expandPattern(pattern: string): string[] {
if (!pattern.endsWith('/*'))
return [pattern]
const base = pattern.slice(0, -2)
const baseDir = join(rootDir, base)
if (!existsSync(baseDir))
return []
return readdirSync(baseDir, { withFileTypes: true })
.filter(entry => entry.isDirectory())
.map(entry => `${base}/${entry.name}`)
}

function findPlay(dir: string): Play | undefined {
const pkgPath = join(rootDir, dir, 'package.json')
if (!existsSync(pkgPath))
return undefined
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'))
const script = RUN_SCRIPTS.find(name => pkg.scripts?.[name])
if (!script)
return undefined
return { dir, pkgName: pkg.name, script }
}

function suggest(input: string, choices: Choice[]): Promise<Choice[]> {
const needle = input.toLowerCase()
return Promise.resolve(choices.filter(choice => choice.title.toLowerCase().includes(needle)))
}

async function main(): Promise<void> {
const plays = WORKSPACE_PATTERNS
.flatMap(expandPattern)
.map(findPlay)
.filter((play): play is Play => play !== undefined)
.sort((a, b) => a.dir.localeCompare(b.dir))

if (plays.length === 0) {
console.error(`No playgrounds found — none of ${WORKSPACE_PATTERNS.join(', ')} has a package.json with a ${RUN_SCRIPTS.join('/')} script.`)
process.exitCode = 1
return
}

const { play } = await prompts({
type: 'autocomplete',
name: 'play',
message: 'Select a playground to run',
choices: plays.map(p => ({
title: p.dir,
description: `${p.pkgName} · pnpm run ${p.script}`,
value: p,
})),
suggest,
}) as { play?: Play }

if (!play) {
console.log('No playground selected.')
return
}

console.log(`\n▶ pnpm run ${play.script} (${play.dir})\n`)

const result = spawnSync('pnpm', ['run', play.script], {
cwd: join(rootDir, play.dir),
stdio: 'inherit',
shell: false,
})

process.exitCode = result.status ?? 1
}

main()
Loading