|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `pnpm --filter @objectstack/scenarios gallery-refresh --gallery DIR [flags]` |
| 5 | + * — the gallery refresh in one step (`refresh.ts`): npm's latest stable |
| 6 | + * `@objectstack/cli` against the gallery's `index.json`; when they differ, run |
| 7 | + * the cards on that release, export into the gallery checkout and commit on |
| 8 | + * `export/<version>`. Ruling D2: a daily scheduled Claude Code task on the |
| 9 | + * maintainer's subscription login runs it (the package README holds the |
| 10 | + * task's runbook). |
| 11 | + * |
| 12 | + * --gallery DIR the gallery checkout, the top of its git working tree (required) |
| 13 | + * --cards DIR the card directory (default: the repo's docs/qa/scenarios); |
| 14 | + * any other directory is loaded as the private overlay, |
| 15 | + * where a `generic` card is refused (`libraryOf`) |
| 16 | + * --model M the model every live turn asks for (`run-scenarios --model`; |
| 17 | + * default: the login's own) |
| 18 | + * --debut run and export one card only, the first exportable card |
| 19 | + * by id: the gallery's first app (refused once the index lists one) |
| 20 | + * --work DIR where the run's records, projects and recording go |
| 21 | + * (default: a new temp directory) |
| 22 | + * --chromium PATH the Chromium executable for the screenshots |
| 23 | + * |
| 24 | + * Output: `skip: …` (one line, exit 0) when the gallery is already on the |
| 25 | + * release; otherwise the branch, the commit and the PR body, exit 0. A refusal |
| 26 | + * prints why and exits 1. ⛔ It never pushes and never opens the PR. |
| 27 | + */ |
| 28 | + |
| 29 | +import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs'; |
| 30 | +import { tmpdir } from 'node:os'; |
| 31 | +import { dirname, join, resolve } from 'node:path'; |
| 32 | +import { fileURLToPath } from 'node:url'; |
| 33 | + |
| 34 | +import { formatRefusal, libraryOf, loadScenarios, readCapabilityAreas } from '../loader.js'; |
| 35 | +import { findUp, main as runScenarios } from '../runner/cli.js'; |
| 36 | +import { liveScreenshots, renderSummary } from './cli.js'; |
| 37 | +import { galleryRefresh, RefreshRefusal, runnerArgv, type RefreshDeps, type RefreshOutcome } from './refresh.js'; |
| 38 | + |
| 39 | +export interface RefreshCliArgs { |
| 40 | + gallery: string; |
| 41 | + cards?: string; |
| 42 | + model?: string; |
| 43 | + debut: boolean; |
| 44 | + work?: string; |
| 45 | + chromium?: string; |
| 46 | +} |
| 47 | + |
| 48 | +export function parseRefreshArgs(argv: readonly string[]): RefreshCliArgs { |
| 49 | + const a: Partial<RefreshCliArgs> & { debut: boolean } = { debut: false }; |
| 50 | + for (let i = 0; i < argv.length; i++) { |
| 51 | + const flag = argv[i]!; |
| 52 | + const val = () => { |
| 53 | + const v = argv[++i]; |
| 54 | + if (v === undefined) throw new Error(`${flag} needs a value`); |
| 55 | + return v; |
| 56 | + }; |
| 57 | + if (flag === '--gallery') a.gallery = val(); |
| 58 | + else if (flag === '--cards') a.cards = val(); |
| 59 | + else if (flag === '--model') { |
| 60 | + const raw = val(); |
| 61 | + if (raw.trim() === '' || raw.startsWith('-')) throw new Error(`--model takes a model alias or name (sonnet, opus, claude-sonnet-4-5), not \`${raw}\``); |
| 62 | + a.model = raw; |
| 63 | + } else if (flag === '--debut') a.debut = true; |
| 64 | + else if (flag === '--work') a.work = val(); |
| 65 | + else if (flag === '--chromium') a.chromium = val(); |
| 66 | + else throw new Error(`unknown flag \`${flag}\``); |
| 67 | + } |
| 68 | + if (!a.gallery) throw new Error('--gallery DIR is required: the gallery checkout the refresh exports into and commits in'); |
| 69 | + return a as RefreshCliArgs; |
| 70 | +} |
| 71 | + |
| 72 | +/** What the CLI prints for an outcome, one entry per line. */ |
| 73 | +export function renderOutcome(o: RefreshOutcome): string[] { |
| 74 | + if (o.kind === 'skip') return [o.line]; |
| 75 | + return [ |
| 76 | + ...renderSummary(o.summary), |
| 77 | + `branch: ${o.branch}`, |
| 78 | + `commit: ${o.commit}`, |
| 79 | + 'Not pushed. Push the branch and open the PR with this body:', |
| 80 | + '', |
| 81 | + o.body, |
| 82 | + ]; |
| 83 | +} |
| 84 | + |
| 85 | +/** Test seam: replace the steps that reach outside (npm, the model, the browser). */ |
| 86 | +export type RefreshCliDeps = Partial<RefreshDeps>; |
| 87 | + |
| 88 | +export async function main(argv: readonly string[], deps: RefreshCliDeps = {}, io = { out: (l: string) => console.log(l), err: (l: string) => console.error(l) }): Promise<number> { |
| 89 | + const args = parseRefreshArgs(argv); |
| 90 | + const here = dirname(fileURLToPath(import.meta.url)); |
| 91 | + const repo = findUp(here, (d) => existsSync(join(d, 'pnpm-workspace.yaml'))); |
| 92 | + const cardsDir = resolve(args.cards ?? join(repo, 'docs/qa/scenarios')); |
| 93 | + const { cards, refusals } = loadScenarios(cardsDir, { |
| 94 | + areas: readCapabilityAreas(join(repo, 'docs/qa/platform-checklist/areas')), |
| 95 | + library: libraryOf(cardsDir, repo), |
| 96 | + }); |
| 97 | + for (const r of refusals) io.err(formatRefusal(r)); |
| 98 | + const work = resolve(args.work ?? mkdtempSync(join(tmpdir(), 'gallery-refresh-'))); |
| 99 | + mkdirSync(work, { recursive: true }); |
| 100 | + const run: RefreshDeps['run'] = |
| 101 | + deps.run ?? |
| 102 | + (async (plan) => { |
| 103 | + const code = await runScenarios(runnerArgv(plan)); |
| 104 | + if (code !== 0) throw new RefreshRefusal(`run-scenarios exited ${code}: nothing is exported`); |
| 105 | + }); |
| 106 | + try { |
| 107 | + const outcome = await galleryRefresh( |
| 108 | + { galleryDir: resolve(args.gallery), cards, ...(args.cards ? { cardsDir } : {}), debut: args.debut, ...(args.model ? { model: args.model } : {}), workDir: work }, |
| 109 | + { ...deps, run, screenshots: deps.screenshots ?? liveScreenshots(args.chromium) }, |
| 110 | + ); |
| 111 | + // A skip ran nothing: the temp directory it was given is removed again. |
| 112 | + if (outcome.kind === 'skip' && args.work === undefined) rmSync(work, { recursive: true, force: true }); |
| 113 | + for (const line of renderOutcome(outcome)) io.out(line); |
| 114 | + return 0; |
| 115 | + } catch (e) { |
| 116 | + if (!(e instanceof RefreshRefusal)) throw e; |
| 117 | + io.err(`gallery-refresh: refused — ${e.message}`); |
| 118 | + io.err(`work directory: ${work}`); |
| 119 | + return 1; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 124 | + main(process.argv.slice(2)).then( |
| 125 | + (code) => process.exit(code), |
| 126 | + (e) => { |
| 127 | + console.error(e instanceof Error ? e.message : e); |
| 128 | + process.exit(1); |
| 129 | + }, |
| 130 | + ); |
| 131 | +} |
0 commit comments