Open History to see the last commit
1#!/usr/bin/env node
2/**
3 * Codemod runner.
4 *
5 * Resolves user-supplied glob/file arguments against the original
6 * `INIT_CWD` (the directory the user ran `pnpm` from) instead of the
7 * package directory pnpm switches into when `--filter ... exec` is used.
8 *
9 * Usage (from the repo root):
10 * pnpm codemod:ag-ui-compliance "src/**\/*.{ts,tsx}"
11 *
12 * The first argument is the codemod folder name under `codemods/`.
13 * The remainder are jscodeshift's own arguments (paths and flags).
14 */
15import { spawnSync } from 'node:child_process'
16import { dirname, isAbsolute, resolve } from 'node:path'
17import { fileURLToPath } from 'node:url'
18import process from 'node:process'
19
20const here = dirname(fileURLToPath(import.meta.url))
21const [codemodName, ...rest] = process.argv.slice(2)
22
23if (!codemodName) {
24 console.error('Usage: codemod-runner <codemod-name> [jscodeshift args...]')
25 process.exit(2)
26}
27
28const transformPath = resolve(here, codemodName, 'transform.ts')
29// `INIT_CWD` is set by pnpm to the directory the user ran `pnpm` from
30// (preserved across `--filter ... exec`). Fall back to the parent shell's
31// CWD if the runner is invoked outside pnpm.
32const userCwd = process.env['INIT_CWD'] || process.cwd()
33
34// Resolve any positional path-like argument against `userCwd`. Flag
35// arguments (start with `-`) are passed through untouched.
36const args = ['--parser=tsx', '-t', transformPath]
37for (const arg of rest) {
38 if (arg.startsWith('-') || isAbsolute(arg)) {
39 args.push(arg)
40 } else {
41 args.push(resolve(userCwd, arg))
42 }
43}
44
45const result = spawnSync('jscodeshift', args, {
46 cwd: here,
47 stdio: 'inherit',
48 shell: process.platform === 'win32',
49})
50process.exit(result.status ?? 1)