|
| 1 | +import type Json from '@codemod.com/jssg-types/src/langs/json' |
| 2 | +import type { Edit, SgRoot } from '@codemod.com/jssg-types/src/main' |
| 3 | + |
| 4 | +const PACKAGE_UPDATES = { |
| 5 | + '@types/express': '^5.0.0', |
| 6 | + '@types/express-serve-static-core': '^5.0.0', |
| 7 | + '@types/serve-static': '^2.2.0', |
| 8 | + accepts: '^2.0.0', |
| 9 | + 'body-parser': '^2.2.1', |
| 10 | + 'content-disposition': '^1.0.0', |
| 11 | + 'content-type': '^1.0.5', |
| 12 | + cookie: '^0.7.1', |
| 13 | + 'cookie-signature': '^1.2.1', |
| 14 | + debug: '^4.4.0', |
| 15 | + depd: '^2.0.0', |
| 16 | + encodeurl: '^2.0.0', |
| 17 | + 'escape-html': '^1.0.3', |
| 18 | + etag: '^1.8.1', |
| 19 | + express: '^5.0.0', |
| 20 | + finalhandler: '^2.1.0', |
| 21 | + fresh: '^2.0.0', |
| 22 | + 'http-errors': '^2.0.0', |
| 23 | + 'merge-descriptors': '^2.0.0', |
| 24 | + 'mime-types': '^3.0.0', |
| 25 | + 'on-finished': '^2.4.1', |
| 26 | + once: '^1.4.0', |
| 27 | + parseurl: '^1.3.3', |
| 28 | + 'proxy-addr': '^2.0.7', |
| 29 | + qs: '^6.14.0', |
| 30 | + 'range-parser': '^1.2.1', |
| 31 | + router: '^2.2.0', |
| 32 | + send: '^1.1.0', |
| 33 | + 'serve-static': '^2.2.0', |
| 34 | + statuses: '^2.0.1', |
| 35 | + 'type-is': '^2.0.1', |
| 36 | + vary: '^1.1.2', |
| 37 | +} as const |
| 38 | +const DEPENDENCY_SECTIONS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const |
| 39 | + |
| 40 | +type PackageJson = { |
| 41 | + [key: string]: unknown |
| 42 | +} |
| 43 | + |
| 44 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 45 | + return typeof value === 'object' && value !== null && !Array.isArray(value) |
| 46 | +} |
| 47 | + |
| 48 | +function parseVersion(range: string): [number, number, number] | null { |
| 49 | + // Range operators (^, ~, >=, ...) are ignored and missing parts default to 0, |
| 50 | + // so `>=4` parses as 4.0.0. Unparseable ranges such as `*`, `x`, `workspace:*` |
| 51 | + // or `latest` yield null and are left untouched. |
| 52 | + const match = range.match(/(\d+)(?:\.(\d+))?(?:\.(\d+))?/) |
| 53 | + if (!match) return null |
| 54 | + |
| 55 | + return [Number(match[1]), Number(match[2] ?? 0), Number(match[3] ?? 0)] |
| 56 | +} |
| 57 | + |
| 58 | +function isGreater(a: [number, number, number], b: [number, number, number]): boolean { |
| 59 | + for (let index = 0; index < 3; index++) { |
| 60 | + if (a[index] !== b[index]) return a[index] > b[index] |
| 61 | + } |
| 62 | + |
| 63 | + return false |
| 64 | +} |
| 65 | + |
| 66 | +function updateDependency(dependencies: unknown, packageName: string, version: string): boolean { |
| 67 | + if (!isRecord(dependencies)) { |
| 68 | + return false |
| 69 | + } |
| 70 | + |
| 71 | + const current = dependencies[packageName] |
| 72 | + if (typeof current !== 'string' || current === version) { |
| 73 | + return false |
| 74 | + } |
| 75 | + |
| 76 | + // Never downgrade: skip when the declared version is already newer than the |
| 77 | + // Express 5 target, so re-running the recipe is idempotent and projects ahead |
| 78 | + // of 5.2.1 keep their versions. Equal versions are still normalized to the |
| 79 | + // target range (e.g. `~2.0.0` -> `^2.0.0`). |
| 80 | + const existing = parseVersion(current) |
| 81 | + const target = parseVersion(version) |
| 82 | + if (!existing || !target || isGreater(existing, target)) { |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + dependencies[packageName] = version |
| 87 | + return true |
| 88 | +} |
| 89 | + |
| 90 | +function detectIndent(source: string): string | number { |
| 91 | + const match = source.match(/\n([ \t]+)"/) |
| 92 | + |
| 93 | + return match?.[1] ?? 2 |
| 94 | +} |
| 95 | + |
| 96 | +function detectLineEnding(source: string): string { |
| 97 | + return source.includes('\r\n') ? '\r\n' : '\n' |
| 98 | +} |
| 99 | + |
| 100 | +async function transform(root: SgRoot<Json>): Promise<string | null> { |
| 101 | + const rootNode = root.root() |
| 102 | + const source = rootNode.text() |
| 103 | + let packageJson: PackageJson |
| 104 | + |
| 105 | + try { |
| 106 | + packageJson = JSON.parse(source) as PackageJson |
| 107 | + } catch { |
| 108 | + return null |
| 109 | + } |
| 110 | + |
| 111 | + let changed = false |
| 112 | + |
| 113 | + for (const section of DEPENDENCY_SECTIONS) { |
| 114 | + const dependencies = packageJson[section] |
| 115 | + |
| 116 | + for (const [packageName, version] of Object.entries(PACKAGE_UPDATES)) { |
| 117 | + changed = updateDependency(dependencies, packageName, version) || changed |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (!changed) { |
| 122 | + return null |
| 123 | + } |
| 124 | + |
| 125 | + const lineEnding = detectLineEnding(source) |
| 126 | + const nextSource = `${JSON.stringify(packageJson, null, detectIndent(source)).replace(/\n/g, lineEnding)}${source.endsWith('\n') ? lineEnding : ''}` |
| 127 | + const edits: Edit[] = [rootNode.replace(nextSource)] |
| 128 | + |
| 129 | + return rootNode.commitEdits(edits) |
| 130 | +} |
| 131 | + |
| 132 | +export default transform |
0 commit comments