-
-
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
Open
Vinayak1337
wants to merge
1
commit into
expressjs:main
Choose a base branch
from
Vinayak1337:feat/package-json-express-v5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,103 @@ | ||
| 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 updateDependency(dependencies: unknown, packageName: string, version: string): boolean { | ||
| if (!isRecord(dependencies)) { | ||
| return false | ||
| } | ||
|
|
||
| if (Object.hasOwn(dependencies, packageName) && dependencies[packageName] !== version) { | ||
| dependencies[packageName] = version | ||
| return true | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| 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 |
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.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", | ||
| "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" | ||
| } | ||
| } |
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.