upgrade-impact 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # upgrade-impact
2
2
 
3
- See exactly which lines of your code a dependency upgrade would break, before you upgrade.
3
+ **Find out exactly which lines of your code a dependency upgrade would break, before you upgrade.**
4
4
 
5
5
  ```
6
6
  $ npx upgrade-impact zod@4
@@ -13,38 +13,157 @@ zod 3.25.76 → 4.6.7
13
13
  src/index.ts:10:16 TS2339 Property 'errors' does not exist on type 'ZodError<unknown>'.
14
14
  ```
15
15
 
16
- Reading changelogs and searching your code for every changed API is slow and easy to get wrong. Often none of a major version's breaking changes touch your code at all. `upgrade-impact` tells you which case you're in, in seconds.
16
+ One command, no install, and your project is never touched.
17
17
 
18
- ## Usage
18
+ ## What it does
19
19
 
20
- Run it in the folder that holds your `tsconfig.json`:
20
+ A new major version of a library ships with a list of breaking changes. To know whether *your* code is affected, you'd normally read the changelog, search your code for every API it mentions, upgrade, and see what breaks. That's slow, easy to get wrong, and often turns out to be unnecessary, because many breaking changes don't touch the parts of a library you actually use.
21
+
22
+ `upgrade-impact` answers the question directly. Give it a package and the version you're considering, and it type-checks your real code against that version. It tells you either:
23
+
24
+ - **"✔ No new type errors in your code."** Every API you use still has the same shape, so the upgrade is safe as far as types can tell; or
25
+ - **the exact file, line and column of every place that breaks**, with TypeScript's explanation of why.
26
+
27
+ It does this without upgrading anything. The new version is downloaded into a temporary folder, checked, and deleted. Your `package.json`, lockfile and `node_modules` stay exactly as they were.
28
+
29
+ ## Quick start
30
+
31
+ In the folder that holds your project's `tsconfig.json`:
32
+
33
+ ```
34
+ npx upgrade-impact <package>@<version>
35
+ ```
36
+
37
+ Examples:
38
+
39
+ ```
40
+ npx upgrade-impact react-router@7
41
+ npx upgrade-impact zod@^4.1.0
42
+ npx upgrade-impact next@latest
43
+ npx upgrade-impact @tanstack/react-query@5
44
+ ```
45
+
46
+ There's nothing to install or configure: `npx` downloads the tool, runs it and leaves nothing behind.
47
+
48
+ ## When to use it
49
+
50
+ - **Before a major upgrade**, to see how much work it is: zero changes, a few lines, or a rewrite.
51
+ - **When reviewing a Renovate or Dependabot pull request**, to see what the bump breaks instead of guessing from the version number.
52
+ - **In CI**, to fail a build or flag a pull request when an upgrade would break the code (see below).
53
+ - **When choosing between versions**, by running it once per candidate version and comparing the results.
54
+
55
+ ## Options and exit codes
21
56
 
22
57
  ```
23
58
  npx upgrade-impact <package>@<version> [--json]
24
59
  ```
25
60
 
26
- - `<version>` can be an exact version, a range or a tag: `react-router@7`, `zod@^4.1.0`, `next@latest`.
27
- - `--json` prints machine-readable output, for CI or for commenting on Renovate and Dependabot pull requests.
61
+ | Option | What it does |
62
+ | --- | --- |
63
+ | `<package>@<version>` | The package and the version to check. The version can be exact (`4.1.2`), a range (`^4.1.0`, `~2.3`), a major (`4`) or a tag (`latest`, `next`). |
64
+ | `--json` | Print the result as JSON instead of text, for scripts and CI |
65
+ | `-h`, `--help` | Show usage |
28
66
 
29
67
  | Exit code | Meaning |
30
68
  | --- | --- |
31
- | 0 | No new type errors |
32
- | 1 | The upgrade breaks your code (details printed) |
33
- | 2 | The check could not run (bad arguments, no tsconfig, install failed) |
69
+ | `0` | No new type errors: the upgrade doesn't break your code's types |
70
+ | `1` | The upgrade breaks your code; each problem is listed |
71
+ | `2` | The check couldn't run (bad arguments, no `tsconfig.json`, package not installed, download failed). The reason is printed. |
72
+
73
+ ## Using it in CI
74
+
75
+ Because the exit code is `1` when something breaks, a single step is enough to fail a build:
76
+
77
+ ```yaml
78
+ - run: npx upgrade-impact zod@4
79
+ ```
80
+
81
+ With `--json` you get structured output to post as a pull-request comment or feed into other tools:
82
+
83
+ ```json
84
+ {
85
+ "name": "zod",
86
+ "from": "3.25.76",
87
+ "to": "4.6.7",
88
+ "breakages": [
89
+ {
90
+ "file": "src/index.ts",
91
+ "line": 5,
92
+ "column": 27,
93
+ "code": 2554,
94
+ "message": "Expected 2-3 arguments, but got 1."
95
+ }
96
+ ]
97
+ }
98
+ ```
99
+
100
+ `from` is the version you have installed and `to` is the exact version the requested version resolved to. Each breakage gives the file (relative to where you ran the command, with forward slashes on every OS), the 1-based line and column, the TypeScript error number and its message.
101
+
102
+ ## What it works with
103
+
104
+ - **TypeScript projects** with a `tsconfig.json`.
105
+ - **Packages with or without bundled types.** If a package ships no type declarations, its matching `@types/*` package is used, for example `@types/lodash@4` for `lodash@4`.
106
+ - **Monorepos and workspaces.** The installed version is found in the nearest `node_modules`, including hoisted ones in parent folders.
107
+ - **Solution-style configs.** If `tsconfig.json` only lists `references` (as in Vite's templates), every referenced project is checked.
108
+ - **ESM and CommonJS**, including packages that use `exports` maps and subpath imports such as `zod/mini`.
34
109
 
35
110
  ## How it works
36
111
 
37
- 1. Installs the target version into a temporary folder, with install scripts disabled. Your project, `node_modules` and lockfile are never touched.
38
- 2. Type-checks your project twice with the TypeScript compiler: once as it is now, and once with imports of that package pointed at the new version.
39
- 3. Reports only the errors that appear in the second run, so errors your project already has are not blamed on the upgrade.
112
+ 1. Downloads the requested version into a new temporary folder with `npm install --ignore-scripts`, using your normal npm settings, such as a private registry.
113
+ 2. Type-checks your project with the TypeScript compiler, exactly as it is today.
114
+ 3. Type-checks it again, this time resolving imports of that one package to the downloaded version.
115
+ 4. Reports only the errors that appear in the second check, so errors your project already has aren't blamed on the upgrade.
116
+ 5. Deletes the temporary folder.
117
+
118
+ ## Safety
119
+
120
+ - **Package code never runs.** Install scripts are disabled, and the check only reads type declarations.
121
+ - **Your project is never modified.** Nothing is written outside the temporary folder.
122
+ - **Arguments are strictly validated**, so a package argument can't be used to run shell commands.
123
+
124
+ ## What it can't catch
125
+
126
+ `upgrade-impact` finds what the **types** reveal: removed or renamed exports, changed function signatures, removed options, and changed return types. Some breaking changes don't show up in types:
127
+
128
+ - **Behaviour changes with the same types**, such as a new default value, a different sort order, or an error thrown in a new situation. Still read the release notes and run your tests.
129
+ - **Plain JavaScript projects.** A `tsconfig.json` is required.
130
+ - **Outdated `@types/*` packages.** Community types sometimes lag behind the library they describe, so results for packages without bundled types are less precise.
131
+ - **Global types.** Types loaded through `"types"` in `tsconfig.json` or `/// <reference types>` are still checked against the installed version. Only `import` and `require` are pointed at the new version.
132
+ - **TypeScript 7-only settings.** Checks use TypeScript 6. A `tsconfig.json` that uses options only TypeScript 7 understands is rejected with TypeScript's own error.
133
+
134
+ ## FAQ
135
+
136
+ **Does it change my `package.json`, lockfile or `node_modules`?**
137
+ No. The new version goes into a temporary folder that's deleted afterwards.
138
+
139
+ **Do I need to install it?**
140
+ No. `npx upgrade-impact …` is enough. You can also add it as a dev dependency if you want a fixed version in CI.
141
+
142
+ **Does it need internet access?**
143
+ Yes, to download the version you're checking from your npm registry.
144
+
145
+ **Is it safe to run on packages I don't trust?**
146
+ It never runs their code: install scripts are disabled and only type declarations are read. The download itself trusts your npm registry, the same as `npm install`.
147
+
148
+ **I pressed Ctrl+C and a folder was left behind.**
149
+ If the check is interrupted during the download, a folder named `upgrade-impact-*` may stay in your system temp folder. It's safe to delete.
150
+
151
+ ## Requirements
152
+
153
+ - Node.js 20 or newer, with npm on your `PATH`
154
+ - A TypeScript project with a `tsconfig.json`
155
+ - Network access to your npm registry
40
156
 
41
- If the package ships no type declarations, it uses the matching major version of its `@types/*` package.
157
+ ## Changelog
42
158
 
43
- ## Limits
159
+ **0.1.1**
160
+ - Fixed: projects whose `tsconfig.json` only lists `references` (such as Vite templates) were reported as safe without being checked. Every referenced project is now checked.
161
+ - Fixed: in monorepos, a package installed in a parent folder's `node_modules` was reported as "not installed".
162
+ - `--json` file paths now use forward slashes on every OS.
163
+ - Clearer error messages, and full stack traces for unexpected errors.
44
164
 
45
- - It finds what the **types** reveal: removed exports, renamed options, changed signatures. Behaviour that changed without a type change (a different default, a new exception) does not show up, so still read the release notes and run your tests.
46
- - It needs a TypeScript project (`tsconfig.json`). Plain JavaScript projects are not supported yet.
47
- - `@types/*` packages sometimes lag behind the library they describe, so results for packages without bundled types are less precise.
165
+ **0.1.0**
166
+ - First release.
48
167
 
49
168
  ## Development
50
169
 
@@ -53,7 +172,7 @@ npm install
53
172
  npm test
54
173
  ```
55
174
 
56
- The integration tests install `zod` from the npm registry, so they need network access.
175
+ The tests download `zod` from the npm registry, so they need network access. `npm publish` runs the tests first.
57
176
 
58
177
  ## License
59
178
 
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { parseArgs } from "node:util";
3
+ import { UpgradeImpactError } from "../src/errors.js";
3
4
  import { checkUpgrade } from "../src/index.js";
4
5
  import { formatReport } from "../src/report.js";
5
6
 
@@ -23,6 +24,8 @@ try {
23
24
  if (result.breakages.length > 0) process.exitCode = EXIT_BREAKAGES_FOUND;
24
25
  }
25
26
  } catch (error) {
26
- console.error(`upgrade-impact: ${error.message}`);
27
+ // parseArgs rejects unknown flags with ERR_PARSE_ARGS_*; that is a usage problem, not a bug.
28
+ const isUsageProblem = error instanceof UpgradeImpactError || error.code?.startsWith("ERR_PARSE_ARGS");
29
+ console.error(isUsageProblem ? `upgrade-impact: ${error.message}` : error);
27
30
  process.exitCode = EXIT_ERROR;
28
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "upgrade-impact",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "See exactly which lines of your code a dependency upgrade would break, before you upgrade.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -14,7 +14,8 @@
14
14
  "node": ">=20"
15
15
  },
16
16
  "scripts": {
17
- "test": "node --test test/*.test.js"
17
+ "test": "node --test test/*.test.js",
18
+ "prepublishOnly": "npm test"
18
19
  },
19
20
  "keywords": [
20
21
  "upgrade",
package/src/analyze.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import path from "node:path";
2
2
  import ts from "typescript";
3
+ import { UpgradeImpactError } from "./errors.js";
3
4
 
4
5
  const DECLARATION_EXTENSIONS = new Set([ts.Extension.Dts, ts.Extension.Dmts, ts.Extension.Dcts]);
5
6
 
@@ -9,20 +10,42 @@ const FORMAT_HOST = {
9
10
  getNewLine: () => "\n",
10
11
  };
11
12
 
12
- export function readProjectConfig(projectDir) {
13
- const configPath = path.join(projectDir, "tsconfig.json");
14
- if (!ts.sys.fileExists(configPath)) {
15
- throw new Error(`No tsconfig.json in ${projectDir}`);
13
+ /**
14
+ * Parsed configs to type-check: tsconfig.json itself, or, for a solution-style tsconfig.json
15
+ * that lists no files (as in Vite templates), every project it references.
16
+ */
17
+ export function readProjectConfigs(projectDir) {
18
+ const rootPath = path.join(projectDir, "tsconfig.json");
19
+ if (!ts.sys.fileExists(rootPath)) {
20
+ throw new UpgradeImpactError(`No tsconfig.json in ${projectDir}`);
16
21
  }
22
+ const configs = collectConfigs(rootPath, new Set());
23
+ if (configs.length === 0) {
24
+ throw new UpgradeImpactError(`${rootPath} and the projects it references include no files`);
25
+ }
26
+ return configs;
27
+ }
28
+
29
+ function collectConfigs(configPath, visitedPaths) {
30
+ if (visitedPaths.has(configPath)) return [];
31
+ visitedPaths.add(configPath);
32
+ const config = readConfig(configPath);
33
+ if (config.fileNames.length > 0) return [config];
34
+ return (config.projectReferences ?? []).flatMap((reference) =>
35
+ collectConfigs(ts.resolveProjectReferencePath(reference), visitedPaths),
36
+ );
37
+ }
38
+
39
+ function readConfig(configPath) {
17
40
  const host = {
18
41
  ...ts.sys,
19
42
  onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
20
- throw new Error(ts.formatDiagnostics([diagnostic], FORMAT_HOST));
43
+ throw new UpgradeImpactError(ts.formatDiagnostics([diagnostic], FORMAT_HOST));
21
44
  },
22
45
  };
23
46
  const config = ts.getParsedCommandLineOfConfigFile(configPath, undefined, host);
24
47
  if (config.errors.length > 0) {
25
- throw new Error(ts.formatDiagnostics(config.errors, FORMAT_HOST));
48
+ throw new UpgradeImpactError(ts.formatDiagnostics(config.errors, FORMAT_HOST));
26
49
  }
27
50
  return config;
28
51
  }
@@ -34,12 +57,21 @@ export function hasTypes(packageName, installDir, options) {
34
57
  }
35
58
 
36
59
  /** Type errors in the project's own files that appear only when the package resolves from installDir. */
37
- export function findBreakages(config, projectDir, packageName, installDir) {
38
- const errorKey = (diagnostic) => `${diagnostic.file.fileName}:${diagnostic.start}:${diagnostic.code}`;
39
- const existingErrors = new Set(projectErrors(config).map(errorKey));
40
- return projectErrors(config, { packageName, installDir })
41
- .filter((diagnostic) => !existingErrors.has(errorKey(diagnostic)))
42
- .map((diagnostic) => toBreakage(diagnostic, projectDir));
60
+ export function findBreakages(configs, projectDir, packageName, installDir) {
61
+ // Keyed so a file shared by two referenced projects is reported once.
62
+ const newErrors = new Map();
63
+ for (const config of configs) {
64
+ const existingErrors = new Set(projectErrors(config).map(errorKey));
65
+ for (const diagnostic of projectErrors(config, { packageName, installDir })) {
66
+ const key = errorKey(diagnostic);
67
+ if (!existingErrors.has(key)) newErrors.set(key, diagnostic);
68
+ }
69
+ }
70
+ return [...newErrors.values()].map((diagnostic) => toBreakage(diagnostic, projectDir));
71
+ }
72
+
73
+ function errorKey(diagnostic) {
74
+ return `${diagnostic.file.fileName}:${diagnostic.start}:${diagnostic.code}`;
43
75
  }
44
76
 
45
77
  // A file inside installDir, so module resolution searches installDir/node_modules.
@@ -77,7 +109,8 @@ function projectErrors(config, redirect) {
77
109
  function toBreakage(diagnostic, projectDir) {
78
110
  const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
79
111
  return {
80
- file: path.relative(projectDir, diagnostic.file.fileName),
112
+ // Forward slashes on every OS, so --json output is the same on Windows and in Linux CI.
113
+ file: path.relative(projectDir, diagnostic.file.fileName).split(path.sep).join("/"),
81
114
  line: line + 1,
82
115
  column: character + 1,
83
116
  code: diagnostic.code,
package/src/errors.js ADDED
@@ -0,0 +1,2 @@
1
+ /** A problem the user can fix (bad arguments, missing tsconfig, failed install). Printed without a stack trace. */
2
+ export class UpgradeImpactError extends Error {}
package/src/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
- import { findBreakages, hasTypes, readProjectConfig } from "./analyze.js";
4
+ import { findBreakages, hasTypes, readProjectConfigs } from "./analyze.js";
5
+ import { UpgradeImpactError } from "./errors.js";
5
6
  import { installPackages, parseSpec, readInstalledVersion, typesPackageName } from "./install.js";
6
7
 
7
8
  /**
@@ -10,14 +11,14 @@ import { installPackages, parseSpec, readInstalledVersion, typesPackageName } fr
10
11
  */
11
12
  export function checkUpgrade(spec, projectDir) {
12
13
  const { name, version } = parseSpec(spec);
14
+ const configs = readProjectConfigs(projectDir);
13
15
  const from = readInstalledVersion(name, projectDir);
14
- const config = readProjectConfig(projectDir);
15
16
  const installDir = fs.mkdtempSync(path.join(os.tmpdir(), "upgrade-impact-"));
16
17
  try {
17
18
  installPackages([`${name}@${version}`], installDir);
18
19
  const to = readInstalledVersion(name, installDir);
19
- ensureTypes(name, to, installDir, config.options);
20
- return { name, from, to, breakages: findBreakages(config, projectDir, name, installDir) };
20
+ ensureTypes(name, to, installDir, configs[0].options);
21
+ return { name, from, to, breakages: findBreakages(configs, projectDir, name, installDir) };
21
22
  } finally {
22
23
  fs.rmSync(installDir, { recursive: true, force: true });
23
24
  }
@@ -29,6 +30,6 @@ function ensureTypes(name, version, installDir, options) {
29
30
  const typesSpec = `${typesPackageName(name)}@${version.split(".")[0]}`;
30
31
  installPackages([typesSpec], installDir);
31
32
  if (!hasTypes(name, installDir, options)) {
32
- throw new Error(`${name}@${version} has no type declarations, and installing ${typesSpec} did not provide any`);
33
+ throw new UpgradeImpactError(`${name}@${version} has no type declarations, and installing ${typesSpec} did not provide any`);
33
34
  }
34
35
  }
package/src/install.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { spawnSync } from "node:child_process";
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
+ import { UpgradeImpactError } from "./errors.js";
4
5
 
5
6
  // Only name and semver characters are allowed: on Windows the spec passes through a shell.
6
7
  const SPEC_PATTERN = /^((?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*)@([\w.^~*-]+)$/i;
@@ -9,7 +10,7 @@ const SPEC_PATTERN = /^((?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*)@([\w.^~*-]+)$/i;
9
10
  export function parseSpec(spec) {
10
11
  const match = SPEC_PATTERN.exec(spec);
11
12
  if (!match) {
12
- throw new Error(`Expected <package>@<version>, for example zod@4 or @scope/pkg@^2.1.0. Got: ${spec}`);
13
+ throw new UpgradeImpactError(`Expected <package>@<version>, for example zod@4 or @scope/pkg@^2.1.0. Got: ${spec}`);
13
14
  }
14
15
  return { name: match[1], version: match[2] };
15
16
  }
@@ -22,9 +23,13 @@ export function installPackages(specs, installDir) {
22
23
  // arguments at spaces and strips ^ from version ranges.
23
24
  const shellSafeArgs = isWindows ? args.map((arg) => `"${arg}"`) : args;
24
25
  const result = spawnSync("npm", shellSafeArgs, { encoding: "utf8", env: npmChildEnv(), shell: isWindows });
25
- if (result.error) throw result.error;
26
+ if (result.error) {
27
+ throw new UpgradeImpactError("Could not run npm. Check that Node.js and npm are installed and on PATH.", {
28
+ cause: result.error,
29
+ });
30
+ }
26
31
  if (result.status !== 0) {
27
- throw new Error(`npm install ${specs.join(" ")} failed:\n${result.stderr.trim()}`);
32
+ throw new UpgradeImpactError(`npm install ${specs.join(" ")} failed:\n${result.stderr.trim()}`);
28
33
  }
29
34
  }
30
35
 
@@ -35,12 +40,17 @@ function npmChildEnv() {
35
40
  return Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("npm_config_")));
36
41
  }
37
42
 
43
+ /** Version of the package Node would load from baseDir, searching parent node_modules folders as Node does. */
38
44
  export function readInstalledVersion(name, baseDir) {
39
- const manifestPath = path.join(baseDir, "node_modules", name, "package.json");
40
- if (!fs.existsSync(manifestPath)) {
41
- throw new Error(`${name} is not installed in ${baseDir}`);
45
+ for (let dir = path.resolve(baseDir); ; dir = path.dirname(dir)) {
46
+ const manifestPath = path.join(dir, "node_modules", name, "package.json");
47
+ if (fs.existsSync(manifestPath)) {
48
+ return JSON.parse(fs.readFileSync(manifestPath, "utf8")).version;
49
+ }
50
+ if (path.dirname(dir) === dir) {
51
+ throw new UpgradeImpactError(`${name} is not installed in ${baseDir} or any parent folder`);
52
+ }
42
53
  }
43
- return JSON.parse(fs.readFileSync(manifestPath, "utf8")).version;
44
54
  }
45
55
 
46
56
  /** "@scope/pkg" -> "@types/scope__pkg", "pkg" -> "@types/pkg" */