|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * @file Move a pinned tool's native snapshots from its old version to the |
| 4 | + * pinned one by editing the version text, for automation that bumps the pin |
| 5 | + * but cannot build the native CLI. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node scripts/test/swap-snapshot-version.mts --dep coana --from 15.15.0 |
| 9 | + */ |
| 10 | + |
| 11 | +import { promises as fs } from 'node:fs' |
| 12 | +import path from 'node:path' |
| 13 | +import process from 'node:process' |
| 14 | +import { parseArgs } from 'node:util' |
| 15 | + |
| 16 | +import { |
| 17 | + SNAPSHOT_DEPENDENCIES, |
| 18 | + findStaleSnapshotDependencies, |
| 19 | + normalizeSnapshotDependencyName, |
| 20 | + readPinnedVersion, |
| 21 | + snapshotDependencyUpdateHint, |
| 22 | +} from './snapshot-dependencies.mts' |
| 23 | +import { isMainModule } from '../lib/is-main-module.mts' |
| 24 | +import { runMain } from '../lib/run-main.mts' |
| 25 | + |
| 26 | +import type { SnapshotDependency } from './snapshot-dependencies.mts' |
| 27 | +import type { ScriptMeta } from '../lib/run-main.mts' |
| 28 | + |
| 29 | +const ROOT = path.resolve(import.meta.dirname, '../..') |
| 30 | + |
| 31 | +export interface SwapArgs { |
| 32 | + readonly dependencyName: string |
| 33 | + readonly from: string |
| 34 | +} |
| 35 | + |
| 36 | +export function parseSwapArgs(argv: readonly string[]): SwapArgs { |
| 37 | + const { values } = parseArgs({ |
| 38 | + args: [...argv], |
| 39 | + options: { dep: { type: 'string' }, from: { type: 'string' } }, |
| 40 | + strict: true, |
| 41 | + }) |
| 42 | + if (!values.dep || !values.from) { |
| 43 | + throw new Error('Pass both --dep <name> and --from <old version>') |
| 44 | + } |
| 45 | + return { dependencyName: values.dep, from: values.from } |
| 46 | +} |
| 47 | + |
| 48 | +function escapeRegExp(text: string): string { |
| 49 | + return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Replace `from` with `to` in a terminal snapshot, taking or giving the |
| 54 | + * difference from the spaces that pad the row, so every row keeps its column |
| 55 | + * count and the rendered pixels match a real capture. |
| 56 | + */ |
| 57 | +export function swapVersionInSnapshot( |
| 58 | + contents: string, |
| 59 | + from: string, |
| 60 | + to: string, |
| 61 | +): string { |
| 62 | + const pattern = new RegExp( |
| 63 | + `(?<![\\w.])${escapeRegExp(from)}(?!\\w|\\.\\d)(\\S*)( *)`, |
| 64 | + 'g', |
| 65 | + ) |
| 66 | + let found = false |
| 67 | + const swapped = contents.replace( |
| 68 | + pattern, |
| 69 | + (_match, tail: string, padding: string) => { |
| 70 | + found = true |
| 71 | + const width = from.length + padding.length |
| 72 | + if (to.length > width) { |
| 73 | + throw new Error(`No room in the row to replace ${from} with ${to}`) |
| 74 | + } |
| 75 | + return `${to}${tail}${' '.repeat(width - to.length)}` |
| 76 | + }, |
| 77 | + ) |
| 78 | + if (!found) { |
| 79 | + throw new Error(`The snapshot does not mention ${from}`) |
| 80 | + } |
| 81 | + return swapped |
| 82 | +} |
| 83 | + |
| 84 | +export async function main(): Promise<void> { |
| 85 | + const { dependencyName, from } = parseSwapArgs(process.argv.slice(2)) |
| 86 | + const name = normalizeSnapshotDependencyName(dependencyName) |
| 87 | + if (!name) { |
| 88 | + throw new Error(`Unknown snapshot dependency: ${dependencyName}`) |
| 89 | + } |
| 90 | + const dependency: SnapshotDependency = SNAPSHOT_DEPENDENCIES[name] |
| 91 | + const to = await readPinnedVersion(dependency) |
| 92 | + const snapshots = dependency.versionSnapshots ?? dependency.snapshots |
| 93 | + // Swap every file in memory first, so a failure leaves none of them edited. |
| 94 | + const swapped = await Promise.all( |
| 95 | + snapshots.map(async snapshot => { |
| 96 | + const file = path.join(ROOT, snapshot) |
| 97 | + try { |
| 98 | + const contents = await fs.readFile(file, 'utf8') |
| 99 | + return { file, contents: swapVersionInSnapshot(contents, from, to) } |
| 100 | + } catch (e) { |
| 101 | + throw new Error(`${snapshot}: ${(e as Error).message}`) |
| 102 | + } |
| 103 | + }), |
| 104 | + ) |
| 105 | + await Promise.all( |
| 106 | + swapped.map(({ contents, file }) => fs.writeFile(file, contents)), |
| 107 | + ) |
| 108 | + const stale = (await findStaleSnapshotDependencies()).filter( |
| 109 | + finding => normalizeSnapshotDependencyName(finding.dependencyName) === name, |
| 110 | + ) |
| 111 | + if (stale.length) { |
| 112 | + throw new Error(snapshotDependencyUpdateHint(stale)) |
| 113 | + } |
| 114 | + process.stdout.write( |
| 115 | + `Moved ${snapshots.length} ${dependency.packageName} snapshots from ${from} to ${to}\n`, |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +const SCRIPT_META: ScriptMeta = { |
| 120 | + describe: |
| 121 | + "moves a pinned tool's native snapshots to its pinned version without a native build", |
| 122 | + help: `Usage: node scripts/test/swap-snapshot-version.mts --dep <name> --from <old version> |
| 123 | +
|
| 124 | + --dep coana, socket-patch or patch |
| 125 | + --from the version the snapshots show now |
| 126 | +
|
| 127 | + Run it after bumping the pin. It fails without editing anything when a |
| 128 | + snapshot does not show the old version or a row has no room for the new one. |
| 129 | + Fall back to pnpm snapshots:update:dep <name> in that case.`, |
| 130 | +} |
| 131 | + |
| 132 | +if (isMainModule(import.meta.url)) { |
| 133 | + runMain(main, SCRIPT_META) |
| 134 | +} |
0 commit comments