-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: update package.json in v5 migration recipe #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bjohansebas
merged 5 commits into
expressjs:main
from
Vinayak1337:feat/package-json-express-v5
Jun 23, 2026
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbd9b75
feat: add package.json Express v5 codemod
Vinayak1337 484ec6c
Merge branch 'main' of github.com:expressjs/codemod into feat/package…
bjohansebas e0c3ec6
feat: add transformations for static dotfiles, static mime, and sendf…
bjohansebas b1b33f3
feat: add version parsing and update logic for dependencies in Expres…
bjohansebas 0c61d2c
feat: update version to 1.1.0 in codemod.yaml and package.json
bjohansebas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import type Json from '@codemod.com/jssg-types/src/langs/json' | ||
| import type { Edit, SgRoot } from '@codemod.com/jssg-types/src/main' | ||
|
|
||
| const PACKAGE_UPDATES = { | ||
| '@types/express': '^5.0.0', | ||
| '@types/express-serve-static-core': '^5.0.0', | ||
| '@types/serve-static': '^2.2.0', | ||
| accepts: '^2.0.0', | ||
| 'body-parser': '^2.2.1', | ||
| 'content-disposition': '^1.0.0', | ||
| 'content-type': '^1.0.5', | ||
| cookie: '^0.7.1', | ||
| 'cookie-signature': '^1.2.1', | ||
| debug: '^4.4.0', | ||
| depd: '^2.0.0', | ||
| encodeurl: '^2.0.0', | ||
| 'escape-html': '^1.0.3', | ||
| etag: '^1.8.1', | ||
| express: '^5.0.0', | ||
| finalhandler: '^2.1.0', | ||
| fresh: '^2.0.0', | ||
| 'http-errors': '^2.0.0', | ||
| 'merge-descriptors': '^2.0.0', | ||
| 'mime-types': '^3.0.0', | ||
| 'on-finished': '^2.4.1', | ||
| once: '^1.4.0', | ||
| parseurl: '^1.3.3', | ||
| 'proxy-addr': '^2.0.7', | ||
| qs: '^6.14.0', | ||
| 'range-parser': '^1.2.1', | ||
| router: '^2.2.0', | ||
| send: '^1.1.0', | ||
| 'serve-static': '^2.2.0', | ||
| statuses: '^2.0.1', | ||
| 'type-is': '^2.0.1', | ||
| vary: '^1.1.2', | ||
| } as const | ||
| const DEPENDENCY_SECTIONS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const | ||
|
|
||
| type PackageJson = { | ||
| [key: string]: unknown | ||
| } | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === 'object' && value !== null && !Array.isArray(value) | ||
| } | ||
|
|
||
| function parseVersion(range: string): [number, number, number] | null { | ||
| // Range operators (^, ~, >=, ...) are ignored and missing parts default to 0, | ||
| // so `>=4` parses as 4.0.0. Unparseable ranges such as `*`, `x`, `workspace:*` | ||
| // or `latest` yield null and are left untouched. | ||
| const match = range.match(/(\d+)(?:\.(\d+))?(?:\.(\d+))?/) | ||
| if (!match) return null | ||
|
|
||
| return [Number(match[1]), Number(match[2] ?? 0), Number(match[3] ?? 0)] | ||
| } | ||
|
|
||
| function isGreater(a: [number, number, number], b: [number, number, number]): boolean { | ||
| for (let index = 0; index < 3; index++) { | ||
| if (a[index] !== b[index]) return a[index] > b[index] | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| function updateDependency(dependencies: unknown, packageName: string, version: string): boolean { | ||
| if (!isRecord(dependencies)) { | ||
| return false | ||
| } | ||
|
|
||
| const current = dependencies[packageName] | ||
| if (typeof current !== 'string' || current === version) { | ||
| return false | ||
| } | ||
|
|
||
| // Never downgrade: skip when the declared version is already newer than the | ||
| // Express 5 target, so re-running the recipe is idempotent and projects ahead | ||
| // of 5.2.1 keep their versions. Equal versions are still normalized to the | ||
| // target range (e.g. `~2.0.0` -> `^2.0.0`). | ||
| const existing = parseVersion(current) | ||
| const target = parseVersion(version) | ||
| if (!existing || !target || isGreater(existing, target)) { | ||
| return false | ||
| } | ||
|
|
||
| dependencies[packageName] = version | ||
| return true | ||
| } | ||
|
|
||
| function detectIndent(source: string): string | number { | ||
| const match = source.match(/\n([ \t]+)"/) | ||
|
|
||
| return match?.[1] ?? 2 | ||
| } | ||
|
|
||
| function detectLineEnding(source: string): string { | ||
| return source.includes('\r\n') ? '\r\n' : '\n' | ||
| } | ||
|
|
||
| async function transform(root: SgRoot<Json>): Promise<string | null> { | ||
| const rootNode = root.root() | ||
| const source = rootNode.text() | ||
| let packageJson: PackageJson | ||
|
|
||
| try { | ||
| packageJson = JSON.parse(source) as PackageJson | ||
| } catch { | ||
| return null | ||
| } | ||
|
|
||
| let changed = false | ||
|
|
||
| for (const section of DEPENDENCY_SECTIONS) { | ||
| const dependencies = packageJson[section] | ||
|
|
||
| for (const [packageName, version] of Object.entries(PACKAGE_UPDATES)) { | ||
| changed = updateDependency(dependencies, packageName, version) || changed | ||
| } | ||
| } | ||
|
|
||
| if (!changed) { | ||
| return null | ||
| } | ||
|
|
||
| const lineEnding = detectLineEnding(source) | ||
| const nextSource = `${JSON.stringify(packageJson, null, detectIndent(source)).replace(/\n/g, lineEnding)}${source.endsWith('\n') ? lineEnding : ''}` | ||
| const edits: Edit[] = [rootNode.replace(nextSource)] | ||
|
|
||
| return rootNode.commitEdits(edits) | ||
| } | ||
|
|
||
| export default transform |
9 changes: 9 additions & 0 deletions
9
codemods/v5-migration-recipe/tests/expected/already-on-v5.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "already-on-v5", | ||
| "dependencies": { | ||
| "express": "^5.2.1", | ||
| "qs": "^6.15.0", | ||
| "send": "^1.2.0", | ||
| "body-parser": "^2.2.1" | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
codemods/v5-migration-recipe/tests/expected/dependencies.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "name": "express-app", | ||
| "dependencies": { | ||
| "body-parser": "^2.2.1", | ||
| "express": "^5.0.0", | ||
| "serve-static": "^2.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/express": "^5.0.0", | ||
| "typescript": "^5.7.2" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
codemods/v5-migration-recipe/tests/expected/dev-dependency-only.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "name": "dev-only", | ||
| "devDependencies": { | ||
| "express": "^5.0.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "no-express", | ||
| "dependencies": { | ||
| "koa": "^2.15.3" | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "^5.7.2" | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
codemods/v5-migration-recipe/tests/expected/peer-and-optional.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "name": "express-plugin", | ||
| "peerDependencies": { | ||
| "body-parser": "^2.2.1", | ||
| "express": "^5.0.0" | ||
| }, | ||
| "optionalDependencies": { | ||
| "@types/express": "^5.0.0", | ||
| "serve-static": "^2.2.0" | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
codemods/v5-migration-recipe/tests/expected/sub-dependencies.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| { | ||
| "name": "express-sub-dependencies", | ||
| "dependencies": { | ||
| "accepts": "^2.0.0", | ||
| "array-flatten": "1.1.1", | ||
| "body-parser": "^2.2.1", | ||
| "content-disposition": "^1.0.0", | ||
| "content-type": "^1.0.5", | ||
| "cookie": "^0.7.1", | ||
| "cookie-signature": "^1.2.1", | ||
| "debug": "^4.4.0", | ||
| "depd": "^2.0.0", | ||
| "encodeurl": "^2.0.0", | ||
| "escape-html": "^1.0.3", | ||
| "etag": "^1.8.1", | ||
| "express": "^5.0.0", | ||
| "finalhandler": "^2.1.0", | ||
| "fresh": "^2.0.0", | ||
| "http-errors": "^2.0.0", | ||
| "merge-descriptors": "^2.0.0", | ||
| "mime-types": "^3.0.0", | ||
| "on-finished": "^2.4.1", | ||
| "once": "^1.4.0", | ||
| "parseurl": "^1.3.3", | ||
| "path-to-regexp": "~0.1.12", | ||
| "proxy-addr": "^2.0.7", | ||
| "qs": "~6.15.1", | ||
| "range-parser": "^1.2.1", | ||
| "router": "^2.2.0", | ||
| "send": "^1.1.0", | ||
| "serve-static": "^2.2.0", | ||
| "statuses": "^2.0.1", | ||
| "type-is": "^2.0.1", | ||
| "utils-merge": "1.0.1", | ||
| "vary": "^1.1.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/express": "^5.0.0", | ||
| "@types/express-serve-static-core": "^5.0.0", | ||
| "@types/serve-static": "^2.2.0", | ||
| "typescript": "^5.7.2" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "already-on-v5", | ||
| "dependencies": { | ||
| "express": "^5.2.1", | ||
| "qs": "^6.15.0", | ||
| "send": "^1.2.0", | ||
| "body-parser": "^2.2.1" | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
codemods/v5-migration-recipe/tests/input/dependencies.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "name": "express-app", | ||
| "dependencies": { | ||
| "body-parser": "^1.20.3", | ||
| "express": "^4.18.2", | ||
| "serve-static": "^1.16.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/express": "^4.17.21", | ||
| "typescript": "^5.7.2" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
codemods/v5-migration-recipe/tests/input/dev-dependency-only.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "name": "dev-only", | ||
| "devDependencies": { | ||
| "express": "~4.21.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "no-express", | ||
| "dependencies": { | ||
| "koa": "^2.15.3" | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "^5.7.2" | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
codemods/v5-migration-recipe/tests/input/peer-and-optional.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "name": "express-plugin", | ||
| "peerDependencies": { | ||
| "body-parser": "^1.20.2", | ||
| "express": ">=4" | ||
| }, | ||
| "optionalDependencies": { | ||
| "@types/express": "^4.17.0", | ||
| "serve-static": "^1.15.0" | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
codemods/v5-migration-recipe/tests/input/sub-dependencies.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| { | ||
| "name": "express-sub-dependencies", | ||
| "dependencies": { | ||
| "accepts": "~1.3.8", | ||
| "array-flatten": "1.1.1", | ||
| "body-parser": "~1.20.5", | ||
| "content-disposition": "~0.5.4", | ||
| "content-type": "~1.0.4", | ||
| "cookie": "~0.7.1", | ||
| "cookie-signature": "~1.0.6", | ||
| "debug": "2.6.9", | ||
| "depd": "2.0.0", | ||
| "encodeurl": "~2.0.0", | ||
| "escape-html": "~1.0.3", | ||
| "etag": "~1.8.1", | ||
| "express": "^4.18.2", | ||
| "finalhandler": "~1.3.1", | ||
| "fresh": "~0.5.2", | ||
| "http-errors": "~2.0.0", | ||
| "merge-descriptors": "1.0.3", | ||
| "mime-types": "^2.1.35", | ||
| "on-finished": "~2.4.1", | ||
| "once": "^1.3.3", | ||
| "parseurl": "~1.3.3", | ||
| "path-to-regexp": "~0.1.12", | ||
| "proxy-addr": "~2.0.7", | ||
| "qs": "~6.15.1", | ||
| "range-parser": "~1.2.1", | ||
| "router": "^1.3.8", | ||
| "send": "~0.19.0", | ||
| "serve-static": "~1.16.2", | ||
| "statuses": "~2.0.1", | ||
| "type-is": "~1.6.18", | ||
| "utils-merge": "1.0.1", | ||
| "vary": "~1.1.2" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/express": "^4.17.21", | ||
| "@types/express-serve-static-core": "^4.19.6", | ||
| "@types/serve-static": "^1.15.7", | ||
| "typescript": "^5.7.2" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.