upgrade-impact 0.0.0-stage → 0.1.0
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 +59 -2
- package/bin/upgrade-impact.js +28 -0
- package/package.json +29 -4
- package/src/analyze.js +86 -0
- package/src/index.js +34 -0
- package/src/install.js +49 -0
- package/src/report.js +11 -0
package/README.md
CHANGED
|
@@ -1,3 +1,60 @@
|
|
|
1
|
-
#
|
|
1
|
+
# upgrade-impact
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
See exactly which lines of your code a dependency upgrade would break, before you upgrade.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
$ npx upgrade-impact zod@4
|
|
7
|
+
|
|
8
|
+
zod 3.25.76 → 4.6.7
|
|
9
|
+
✖ 3 new type error(s) in 1 file(s)
|
|
10
|
+
|
|
11
|
+
src/index.ts:5:27 TS2554 Expected 2-3 arguments, but got 1.
|
|
12
|
+
src/index.ts:7:31 TS2353 Object literal may only specify known properties, and 'invalid_type_error' does not exist in type '$ZodNumberParams'.
|
|
13
|
+
src/index.ts:10:16 TS2339 Property 'errors' does not exist on type 'ZodError<unknown>'.
|
|
14
|
+
```
|
|
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.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Run it in the folder that holds your `tsconfig.json`:
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
npx upgrade-impact <package>@<version> [--json]
|
|
24
|
+
```
|
|
25
|
+
|
|
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.
|
|
28
|
+
|
|
29
|
+
| Exit code | Meaning |
|
|
30
|
+
| --- | --- |
|
|
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) |
|
|
34
|
+
|
|
35
|
+
## How it works
|
|
36
|
+
|
|
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.
|
|
40
|
+
|
|
41
|
+
If the package ships no type declarations, it uses the matching major version of its `@types/*` package.
|
|
42
|
+
|
|
43
|
+
## Limits
|
|
44
|
+
|
|
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.
|
|
48
|
+
|
|
49
|
+
## Development
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
npm install
|
|
53
|
+
npm test
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The integration tests install `zod` from the npm registry, so they need network access.
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { checkUpgrade } from "../src/index.js";
|
|
4
|
+
import { formatReport } from "../src/report.js";
|
|
5
|
+
|
|
6
|
+
const USAGE = "Usage: upgrade-impact <package>@<version> [--json]";
|
|
7
|
+
const EXIT_BREAKAGES_FOUND = 1;
|
|
8
|
+
const EXIT_ERROR = 2;
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const { values, positionals } = parseArgs({
|
|
12
|
+
allowPositionals: true,
|
|
13
|
+
options: { json: { type: "boolean" }, help: { type: "boolean", short: "h" } },
|
|
14
|
+
});
|
|
15
|
+
if (values.help) {
|
|
16
|
+
console.log(USAGE);
|
|
17
|
+
} else if (positionals.length !== 1) {
|
|
18
|
+
console.error(USAGE);
|
|
19
|
+
process.exitCode = EXIT_ERROR;
|
|
20
|
+
} else {
|
|
21
|
+
const result = checkUpgrade(positionals[0], process.cwd());
|
|
22
|
+
console.log(values.json ? JSON.stringify(result, null, 2) : formatReport(result));
|
|
23
|
+
if (result.breakages.length > 0) process.exitCode = EXIT_BREAKAGES_FOUND;
|
|
24
|
+
}
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error(`upgrade-impact: ${error.message}`);
|
|
27
|
+
process.exitCode = EXIT_ERROR;
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "upgrade-impact",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "See exactly which lines of your code a dependency upgrade would break, before you upgrade.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"upgrade-impact": "bin/upgrade-impact.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test test/*.test.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"upgrade",
|
|
21
|
+
"dependencies",
|
|
22
|
+
"breaking-changes",
|
|
23
|
+
"typescript",
|
|
24
|
+
"semver",
|
|
25
|
+
"cli"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"typescript": "^6.0.3"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/analyze.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
const DECLARATION_EXTENSIONS = new Set([ts.Extension.Dts, ts.Extension.Dmts, ts.Extension.Dcts]);
|
|
5
|
+
|
|
6
|
+
const FORMAT_HOST = {
|
|
7
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
8
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
9
|
+
getNewLine: () => "\n",
|
|
10
|
+
};
|
|
11
|
+
|
|
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}`);
|
|
16
|
+
}
|
|
17
|
+
const host = {
|
|
18
|
+
...ts.sys,
|
|
19
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
20
|
+
throw new Error(ts.formatDiagnostics([diagnostic], FORMAT_HOST));
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
const config = ts.getParsedCommandLineOfConfigFile(configPath, undefined, host);
|
|
24
|
+
if (config.errors.length > 0) {
|
|
25
|
+
throw new Error(ts.formatDiagnostics(config.errors, FORMAT_HOST));
|
|
26
|
+
}
|
|
27
|
+
return config;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** True when the package resolves to type declarations from installDir. */
|
|
31
|
+
export function hasTypes(packageName, installDir, options) {
|
|
32
|
+
const { resolvedModule } = ts.resolveModuleName(packageName, anchorFile(installDir), options, ts.sys);
|
|
33
|
+
return resolvedModule !== undefined && DECLARATION_EXTENSIONS.has(resolvedModule.extension);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 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));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// A file inside installDir, so module resolution searches installDir/node_modules.
|
|
46
|
+
function anchorFile(installDir) {
|
|
47
|
+
return path.join(installDir, "index.ts");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function projectErrors(config, redirect) {
|
|
51
|
+
const host = ts.createCompilerHost(config.options);
|
|
52
|
+
if (redirect) {
|
|
53
|
+
host.resolveModuleNameLiterals = (literals, containingFile, redirectedReference, options, containingSourceFile) =>
|
|
54
|
+
literals.map((literal) => {
|
|
55
|
+
const isTargetImport = literal.text === redirect.packageName || literal.text.startsWith(`${redirect.packageName}/`);
|
|
56
|
+
const resolveFrom = isTargetImport ? anchorFile(redirect.installDir) : containingFile;
|
|
57
|
+
const mode = ts.getModeForUsageLocation(containingSourceFile, literal, options);
|
|
58
|
+
return ts.resolveModuleName(literal.text, resolveFrom, options, host, undefined, redirectedReference, mode);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
const program = ts.createProgram({
|
|
62
|
+
rootNames: config.fileNames,
|
|
63
|
+
options: config.options,
|
|
64
|
+
projectReferences: config.projectReferences,
|
|
65
|
+
host,
|
|
66
|
+
});
|
|
67
|
+
return ts
|
|
68
|
+
.getPreEmitDiagnostics(program)
|
|
69
|
+
.filter(
|
|
70
|
+
(diagnostic) =>
|
|
71
|
+
diagnostic.category === ts.DiagnosticCategory.Error &&
|
|
72
|
+
diagnostic.file !== undefined &&
|
|
73
|
+
!diagnostic.file.fileName.includes("/node_modules/"),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function toBreakage(diagnostic, projectDir) {
|
|
78
|
+
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
79
|
+
return {
|
|
80
|
+
file: path.relative(projectDir, diagnostic.file.fileName),
|
|
81
|
+
line: line + 1,
|
|
82
|
+
column: character + 1,
|
|
83
|
+
code: diagnostic.code,
|
|
84
|
+
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
|
|
85
|
+
};
|
|
86
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { findBreakages, hasTypes, readProjectConfig } from "./analyze.js";
|
|
5
|
+
import { installPackages, parseSpec, readInstalledVersion, typesPackageName } from "./install.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Type-checks the project against another version of one dependency and returns the new errors.
|
|
9
|
+
* The target version is installed into a temporary folder; the project itself is never modified.
|
|
10
|
+
*/
|
|
11
|
+
export function checkUpgrade(spec, projectDir) {
|
|
12
|
+
const { name, version } = parseSpec(spec);
|
|
13
|
+
const from = readInstalledVersion(name, projectDir);
|
|
14
|
+
const config = readProjectConfig(projectDir);
|
|
15
|
+
const installDir = fs.mkdtempSync(path.join(os.tmpdir(), "upgrade-impact-"));
|
|
16
|
+
try {
|
|
17
|
+
installPackages([`${name}@${version}`], installDir);
|
|
18
|
+
const to = readInstalledVersion(name, installDir);
|
|
19
|
+
ensureTypes(name, to, installDir, config.options);
|
|
20
|
+
return { name, from, to, breakages: findBreakages(config, projectDir, name, installDir) };
|
|
21
|
+
} finally {
|
|
22
|
+
fs.rmSync(installDir, { recursive: true, force: true });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Packages without bundled types are checked against the matching major of their @types package.
|
|
27
|
+
function ensureTypes(name, version, installDir, options) {
|
|
28
|
+
if (hasTypes(name, installDir, options)) return;
|
|
29
|
+
const typesSpec = `${typesPackageName(name)}@${version.split(".")[0]}`;
|
|
30
|
+
installPackages([typesSpec], installDir);
|
|
31
|
+
if (!hasTypes(name, installDir, options)) {
|
|
32
|
+
throw new Error(`${name}@${version} has no type declarations, and installing ${typesSpec} did not provide any`);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/install.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
// Only name and semver characters are allowed: on Windows the spec passes through a shell.
|
|
6
|
+
const SPEC_PATTERN = /^((?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*)@([\w.^~*-]+)$/i;
|
|
7
|
+
|
|
8
|
+
/** Splits "name@version" into its parts, rejecting anything that is not a plain package spec. */
|
|
9
|
+
export function parseSpec(spec) {
|
|
10
|
+
const match = SPEC_PATTERN.exec(spec);
|
|
11
|
+
if (!match) {
|
|
12
|
+
throw new Error(`Expected <package>@<version>, for example zod@4 or @scope/pkg@^2.1.0. Got: ${spec}`);
|
|
13
|
+
}
|
|
14
|
+
return { name: match[1], version: match[2] };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Installs packages into installDir without running their install scripts. */
|
|
18
|
+
export function installPackages(specs, installDir) {
|
|
19
|
+
const args = ["install", ...specs, "--prefix", installDir, "--ignore-scripts", "--no-audit", "--no-fund", "--silent"];
|
|
20
|
+
const isWindows = process.platform === "win32";
|
|
21
|
+
// On Windows npm is a .cmd file that only starts through cmd.exe, which splits unquoted
|
|
22
|
+
// arguments at spaces and strips ^ from version ranges.
|
|
23
|
+
const shellSafeArgs = isWindows ? args.map((arg) => `"${arg}"`) : args;
|
|
24
|
+
const result = spawnSync("npm", shellSafeArgs, { encoding: "utf8", env: npmChildEnv(), shell: isWindows });
|
|
25
|
+
if (result.error) throw result.error;
|
|
26
|
+
if (result.status !== 0) {
|
|
27
|
+
throw new Error(`npm install ${specs.join(" ")} failed:\n${result.stderr.trim()}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Under npx or npm run, npm exports its resolved config as lowercase npm_config_* variables. A nested
|
|
32
|
+
// install treats those as command-line flags (npm 11 rejects allow-scripts passed that way), so drop
|
|
33
|
+
// them and let the child read .npmrc files itself. User-set NPM_CONFIG_* variables are kept.
|
|
34
|
+
function npmChildEnv() {
|
|
35
|
+
return Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("npm_config_")));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
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}`);
|
|
42
|
+
}
|
|
43
|
+
return JSON.parse(fs.readFileSync(manifestPath, "utf8")).version;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** "@scope/pkg" -> "@types/scope__pkg", "pkg" -> "@types/pkg" */
|
|
47
|
+
export function typesPackageName(name) {
|
|
48
|
+
return `@types/${name.replace(/^@/, "").replace("/", "__")}`;
|
|
49
|
+
}
|
package/src/report.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function formatReport({ name, from, to, breakages }) {
|
|
2
|
+
const header = `${name} ${from} → ${to}`;
|
|
3
|
+
if (breakages.length === 0) {
|
|
4
|
+
return `${header}\n✔ No new type errors in your code.`;
|
|
5
|
+
}
|
|
6
|
+
const fileCount = new Set(breakages.map((breakage) => breakage.file)).size;
|
|
7
|
+
const details = breakages.map(
|
|
8
|
+
({ file, line, column, code, message }) => ` ${file}:${line}:${column} TS${code} ${message.replaceAll("\n", "\n ")}`,
|
|
9
|
+
);
|
|
10
|
+
return [header, `✖ ${breakages.length} new type error(s) in ${fileCount} file(s)`, "", ...details].join("\n");
|
|
11
|
+
}
|