|
| 1 | +// npm-publish prep. Runs from `prepublishOnly` (after `npm run build`). |
| 2 | +// |
| 3 | +// Two npm-specific concerns the plain `tsc` build doesn't handle: |
| 4 | +// |
| 5 | +// 1. dist/patch-ink.mjs — the postinstall hook runs `node dist/patch-ink.mjs` |
| 6 | +// to patch node_modules/ink at the user's install time (needed for the raw-PTY |
| 7 | +// handoff, e.g. `gh auth login` — see scripts/patch-ink.ts). `tsc -b` only |
| 8 | +// compiles src/**/*, so this file is otherwise absent from the npm tarball and |
| 9 | +// the postinstall guard silently skips it. Compile it here (same command as |
| 10 | +// scripts/pkg.ts uses for the binary build). |
| 11 | +// |
| 12 | +// 2. README.md — npm should show a "migrated and re-purposed" blurb that the |
| 13 | +// git-tracked README must NOT carry. README.npm.md holds ONLY the blurb; we |
| 14 | +// back up the real README and prepend the blurb to it (so the body is never |
| 15 | +// duplicated). `npm run restore-npm` (via postpublish, and defensively on |
| 16 | +// failure) restores it from the backup. |
| 17 | +// |
| 18 | +// The backup file (README.md.orig) is the single source of truth for restoration, |
| 19 | +// so a failed publish never leaves README.md dirty. |
| 20 | + |
| 21 | +import chalk from 'chalk' |
| 22 | +import { execSync } from 'node:child_process' |
| 23 | +import { existsSync } from 'node:fs' |
| 24 | +import fs from 'node:fs/promises' |
| 25 | + |
| 26 | +const README = 'README.md' |
| 27 | +const README_BLURB = 'README.npm.md' |
| 28 | +// The transient backup is intentionally NOT named README* and is a dotfile — npm |
| 29 | +// force-includes any README* file into the tarball regardless of the `files` |
| 30 | +// allowlist, and this backup is just build scratch that shouldn't ship. |
| 31 | +const README_BACKUP = '.npm-readme-backup.md' |
| 32 | + |
| 33 | +// ── 1. Compile patch-ink.ts → dist/patch-ink.mjs ───────────────────────────── |
| 34 | +console.log(chalk.magenta('Compiling patch-ink.ts to dist/patch-ink.mjs')) |
| 35 | +execSync( |
| 36 | + 'tsc --module nodenext --moduleResolution nodenext --target es2022 --outDir dist scripts/patch-ink.ts', |
| 37 | + { shell: 'zsh' }, |
| 38 | +) |
| 39 | +await fs.rename('dist/patch-ink.js', 'dist/patch-ink.mjs') |
| 40 | + |
| 41 | +// ── 2. Prepend the npm blurb to README.md ──────────────────────────────────── |
| 42 | +// README.npm.md holds ONLY the blurb; we prepend it to the real README so the |
| 43 | +// body never has to be duplicated/kept in sync. `npm run restore-npm` puts the |
| 44 | +// original README.md back from the backup. |
| 45 | +if (!existsSync(README_BLURB)) { |
| 46 | + console.error(chalk.red(`ERROR: ${README_BLURB} not found. Cannot build npm README.`)) |
| 47 | + process.exit(1) |
| 48 | +} |
| 49 | + |
| 50 | +// If a stale backup exists (previous publish aborted before restore), the real |
| 51 | +// README was already saved there — use it as the base so we don't prepend twice. |
| 52 | +if (!existsSync(README_BACKUP)) { |
| 53 | + console.log(chalk.magenta(`Backing up ${README} → ${README_BACKUP}`)) |
| 54 | + await fs.copyFile(README, README_BACKUP) |
| 55 | +} else { |
| 56 | + console.log(chalk.yellow(`${README_BACKUP} already exists (stale?); using it as the base`)) |
| 57 | +} |
| 58 | + |
| 59 | +console.log(chalk.magenta(`Prepending ${README_BLURB} to ${README}`)) |
| 60 | +const blurb = await fs.readFile(README_BLURB, 'utf8') |
| 61 | +const body = await fs.readFile(README_BACKUP, 'utf8') |
| 62 | +await fs.writeFile(README, `${blurb.trimEnd()}\n\n${body}`, 'utf8') |
0 commit comments