Files
supabase/packages/maybe-turbo/bin/maybe-turbo.mjs
Ivan VasilovandClaude Opus 5.5 11a71d795a chore(docs): replace pnpm lifecycle hooks with turbo task dependencies (#52556)
## Problem

`apps/docs` ordered its build, dev and codegen steps with pnpm
`pre`/`post` hooks and `&&` chains. Turbo couldn't see those steps, so
it couldn't cache or parallelize them, and running the build through
turbo ran the codegen twice. Part of
[FE-4948](https://linear.app/supabase/issue/FE-4948/migrate-the-pnpm-scripts-to-use-turbo).

## Solution

The ordering now lives in `apps/docs/turbo.jsonc` as `dependsOn`, and
every pre/post hook is gone. The new `maybe-turbo` workspace bin runs a
script through `turbo run` unless it's already inside turbo, so `pnpm
build`, `pnpm dev`, `pnpm codegen:references` and the other entry points
keep running their prerequisites.

Turbo is bumped to 2.11.10 for its `jit` input mode. Tasks downstream of
the uncached remote fetches use it to hash the gitignored files those
fetches write after they finish, so a cache hit can't restore output
built from older content. Strict-mode env vars are declared.

Other changes:
- The R2 upload moves from `postbuild` to the `vercel.json` build
command, so it still runs when the build is a cache hit.
- `codegen:references:new:ensure` re-downloads when any JavaScript
TypeDoc dump is missing or empty.
- The guides manifest is now sorted, so its output is deterministic.
- A new `turbo-scripts` agent skill (listed in `AGENTS.md`) documents
these conventions for every app. Its audit script, `node
.agents/skills/turbo-scripts/scripts/audit.mjs apps/<name>`, flags
scripts that need `maybe-turbo`, leftover `pre`/`post` hooks, script
chains, and wrapped scripts without a turbo task.

## Review instructions

1. Read `packages/maybe-turbo/README.md`, `apps/docs/turbo.jsonc` and
`.agents/skills/turbo-scripts/SKILL.md`.
2. From `apps/docs`, set `GH_TOKEN=$(gh auth token)` for the federated
fetch and run `pnpm run build:markdown` a few times. Once warm, every
task except `build:federated-content` and
`codegen:references:new:ensure` should hit the cache.
3. Run `pnpm build` from `apps/docs` and check that the docs Vercel
preview deploys and renders normally.

## Checklist

Check all before review:

- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-09 16:33:28 +01:00

122 lines
4.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
// Runs a package script as a turbo task, so `pnpm run <script>` gets the task's `dependsOn`
// prerequisites and caching without pnpm pre/post hooks.
//
// "build": "maybe-turbo next build --webpack"
//
// Outside turbo, it re-invokes the script as `turbo run <script> --filter=<package>`. Inside
// turbo (TURBO_HASH is set), it runs the wrapped command directly. Args appended to the script
// (`pnpm run embeddings --refresh`) are forwarded either way.
//
// Compound commands go in a single quoted argument:
//
// "typecheck": "maybe-turbo \"next typegen && tsc --noEmit\""
//
// With no command, the script does nothing inside turbo, which suits tasks that only group
// dependencies.
import { spawn } from 'node:child_process'
import { readFileSync } from 'node:fs'
const SELF = 'maybe-turbo'
const {
npm_lifecycle_event: task,
npm_package_json: packageJsonPath,
npm_package_name: packageName,
} = process.env
if (!task || !packageJsonPath || !packageName) {
fail('run it from a package.json script, for example `pnpm run build`.')
}
const script = JSON.parse(readFileSync(packageJsonPath, 'utf8')).scripts?.[task]
if (typeof script !== 'string') fail(`couldn't find the "${task}" script in ${packageJsonPath}.`)
// pnpm appends extra CLI args to the script, so the args this script declares are the leading
// ones and anything after them came from the caller.
const ownArgCount = countOwnArgs(script)
const args = process.argv.slice(2)
const command = args.slice(0, ownArgCount)
const extraArgs = args.slice(ownArgCount)
if (process.env.TURBO_HASH) {
if (command.length === 0) process.exit(0)
if (command.length === 1 && /\s/.test(command[0])) {
run([command[0], ...extraArgs.map(quote)].join(' '), [], { shell: true })
} else {
run(command[0], [...command.slice(1), ...extraArgs], { shell: process.platform === 'win32' })
}
} else {
const passThrough = extraArgs.length > 0 ? ['--', ...extraArgs] : []
run('turbo', ['run', task, `--filter=${packageName}`, ...passThrough], {
shell: process.platform === 'win32',
})
}
function run(file, fileArgs, options) {
const child = spawn(file, fileArgs, { stdio: 'inherit', ...options })
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
process.on(signal, () => child.kill(signal))
}
child.on('error', (error) => fail(error.message))
child.on('exit', (code, signal) => {
if (signal) process.kill(process.pid, signal)
else process.exit(code ?? 1)
})
}
function countOwnArgs(scriptText) {
const tokens = tokenize(scriptText)
const selfIndex = tokens.findIndex((token) => /(^|\/)maybe-turbo(\.mjs)?$/.test(token.value))
if (selfIndex === -1) fail(`the "${task}" script doesn't call ${SELF}.`)
const own = tokens.slice(selfIndex + 1)
const operator = [...tokens.slice(0, selfIndex), ...own].find((token) => token.operator)
if (operator) {
fail(
`the "${task}" script has "${operator.value}" outside the wrapped command. Put compound commands in one quoted argument.`
)
}
return own.length
}
// Minimal POSIX-style word splitting: whitespace separates words, quotes group them, and
// unquoted &&, ||, ;, | are reported as operators.
function tokenize(text) {
const tokens = []
let current = null
let quote = null
for (let i = 0; i < text.length; i++) {
const char = text[i]
if (quote) {
if (char === quote) quote = null
else if (char === '\\' && quote === '"' && i + 1 < text.length) current.value += text[++i]
else current.value += char
} else if (/\s/.test(char)) {
if (current) tokens.push(current)
current = null
} else if (/[;&|]/.test(char)) {
if (current) tokens.push(current)
const value = /[&|]/.test(char) && text[i + 1] === char ? char + text[++i] : char
tokens.push({ value, operator: true })
current = null
} else {
current ??= { value: '' }
if (char === '"' || char === "'") quote = char
else if (char === '\\' && i + 1 < text.length) current.value += text[++i]
else current.value += char
}
}
if (current) tokens.push(current)
return tokens
}
function quote(arg) {
return /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replaceAll("'", `'\\''`)}'`
}
function fail(message) {
console.error(`${SELF}: ${message}`)
process.exit(1)
}