#!/usr/bin/env node import React from 'react'; import { render } from 'ink'; import { spawn } from 'node:child_process'; import { readFile } from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; import path from 'node:path'; import { App } from './components/App.js'; import { loadProject, applyProject } from './package-file.js'; import { resolveToggles, parseWorkspaceOptions } from './flags.js'; import { formatSummary, formatIgnoredConfigNote, isMonorepoProject, manifestPathsOf } from './summary.js'; /** @import { OverrideSpec } from './types.js' */ /** * @typedef {{ type: 'submit', selections: Map, overrides: Record, removals: string[] } * | { type: 'abort' }} TuiResult * How the interactive session ended. */ const e = React.createElement; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const HELP = ` upgrade-interactive (nui) An interactive dependency upgrader for npm projects, inspired by yarn's "upgrade-interactive" (Yarn Berry / Yarn 4). Usage $ npx upgrade-interactive [options] Options --install Run "npm install" after writing changes (default: on) --no-install Update package.json only, skip running "npm install" afterwards --audit Flag vulnerable packages (default: on) --no-audit Skip the vulnerability check (no advisory network calls) --section Group the list into Dependencies / Dev dependencies / Overrides (default: on) --no-section Show one flat list instead -w, --workspace Limit to matching workspace(s); repeatable, matches package name or path --no-workspaces Only the root package.json (ignore any "workspaces" field) -h, --help Show this help message -v, --version Show the version number Install, audit and sectioning are on by default. Persist a preference either way with the NUI_INSTALL / NUI_AUDIT / NUI_SECTION environment variables, or a package.json config block: "upgrade-interactive": { "install": false, "audit": false, "section": true } Precedence: command-line flag > environment variable > package.json config > default (on). In an npm workspaces repo (a root "workspaces" field) the root and each workspace are shown as their own section, and each package's range is written to its own manifest; "overrides" are always written to the root. The config block above is read from the root manifest only — a workspace's own block is ignored, and you get a note when that changes the run. Use -w to focus on particular workspaces, or --no-workspaces for the root manifest alone. $ npx upgrade-interactive -w packages/api # by path $ npx upgrade-interactive -w @acme/web # by package name $ npx upgrade-interactive -w packages/api -w @acme/web A -w value that matches no workspace is an error listing what didn't match, as is combining -w with --no-workspaces. Controls (inside the interactive UI) / select a package / select which version to apply (Current / Range / Latest); columns a package doesn't offer are skipped c / r / l select all packages' Current / Range / Latest column at once; a package missing that column falls back to a lower one o pin a vulnerable package to a safe version (override, or a range bump for a direct dependency) (audit mode) x remove an existing override that's no longer needed (audit mode) apply the selected upgrades (and run npm install) / esc abort without changing anything `; async function main() { const args = process.argv.slice(2); if (args.includes('-h') || args.includes('--help')) { process.stdout.write(HELP + '\n'); return; } if (args.includes('-v') || args.includes('--version')) { const pkgRaw = await readFile(path.join(__dirname, '..', 'package.json'), 'utf8'); process.stdout.write(JSON.parse(pkgRaw).version + '\n'); return; } if (!process.stdin.isTTY) { process.stderr.write('upgrade-interactive requires an interactive terminal (TTY).\n'); process.exitCode = 1; return; } const cwd = process.cwd(); const { workspaces, filter } = parseWorkspaceOptions(args); let project; try { project = await loadProject(cwd, { workspaces, filter }); } catch (err) { process.stderr.write(`${/** @type {Error} */ (err).message}\n`); process.exitCode = 1; return; } const config = project.root.json['upgrade-interactive']; const { install, audit, section } = resolveToggles({ args, env: process.env, config }); // Audit against the project root: npm workspaces share the root lockfile, so // this also makes runs from inside a workspace subdirectory resolve correctly. const rootDir = path.dirname(project.root.filePath); const manifestPaths = manifestPathsOf(project, rootDir); const result = await new Promise(/** @param {(result: TuiResult) => void} resolve */ (resolve) => { const { waitUntilExit } = render( e(App, { descriptors: project.descriptors, audit, section, cwd: rootDir, overrides: project.root.json.overrides || {}, manifestPaths, onSubmit: (selections, overrides, removals) => resolve({ type: 'submit', selections, overrides, removals }), onAbort: () => resolve({ type: 'abort' }), }), { exitOnCtrlC: false } ); waitUntilExit().catch(() => resolve({ type: 'abort' })); }); // After the TUI, never before it: anything written ahead of render() scrolls // above the interface and is missed, and this note's whole job is to be seen. // Every exit path below runs through here, so an ignored setting is reported // whether or not the run ended in changes. const ignoredConfig = formatIgnoredConfigNote(project, { install, audit, section }); if (ignoredConfig) process.stderr.write(`\n${ignoredConfig}`); if (result.type === 'abort') { process.stdout.write('\nAborted. No changes were made.\n'); process.exitCode = 1; return; } const overrideSelections = result.overrides || {}; const overrideRemovals = result.removals || []; if ( result.selections.size === 0 && Object.keys(overrideSelections).length === 0 && overrideRemovals.length === 0 ) { process.stdout.write('\nNo changes selected.\n'); return; } const { applied, overrides, removed, alreadyPresent } = await applyProject( project, result.selections, overrideSelections, overrideRemovals ); process.stdout.write('\n'); process.stdout.write( formatSummary({ applied, overrides, removed, alreadyPresent, isMonorepo: isMonorepoProject(project) }) ); if (applied.length === 0 && overrides.length === 0 && removed.length === 0) { // "No effective changes" is true of package.json and misleading about the // project: an accepted pin that was already there means the installed tree // never picked it up. npm only re-resolves a changed override on the next // install in a *standalone* project — in a workspace it reports "up to date" // and leaves the old version (docs/2026-08-26-spike-reresolve.md), so those // users need to be told what actually clears it. if (alreadyPresent && alreadyPresent.length > 0) { process.stdout.write( '\nThese overrides were already in package.json, so nothing was written —\n' + 'but the installed tree still resolves to a different version.\n' ); if (isMonorepoProject(project)) { process.stdout.write( 'npm will not re-resolve them in a workspace project. To apply them:\n' + ' rm -rf node_modules package-lock.json && npm install\n' ); } else { process.stdout.write('Run npm install to apply them.\n'); } return; } process.stdout.write('No effective changes.\n'); return; } if (!install) { process.stdout.write('\nUpdated package.json. Run npm install to apply.\n'); return; } // Install once, from the project root — npm workspaces share the root // lockfile, so a per-workspace install would be wrong. For a standalone // project the root dir is just cwd. process.stdout.write('\nRunning npm install...\n'); await runNpmInstall(rootDir); } /** * @param {string} cwd * @returns {Promise} */ function runNpmInstall(cwd) { return new Promise(/** @param {() => void} resolve */ (resolve) => { const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'; const child = spawn(npmCmd, ['install'], { cwd, stdio: 'inherit' }); child.on('exit', (code) => { process.exitCode = code ?? 0; resolve(); }); child.on('error', () => { process.stderr.write('Failed to run npm install. Run it manually to finish updating your lockfile.\n'); process.exitCode = 1; resolve(); }); }); } main().catch((err) => { process.stderr.write(`${err?.stack || err}\n`); process.exitCode = 1; });