|
| 1 | +import { Glob } from 'bun' |
| 2 | +import fs from 'fs' |
| 3 | + |
| 4 | +// Find all package.json files (excluding node_modules) |
| 5 | +const packageFilesUnfiltered = new Glob('./**/package.json').scanSync() |
| 6 | + |
| 7 | +const packageFiles = new Array(...packageFilesUnfiltered).filter((filePath) => !filePath.includes('node_modules')) |
| 8 | + |
| 9 | +// Create a map of package names to their versions |
| 10 | +const packageVersions: Record<string, string> = {} |
| 11 | +packageFiles.forEach((filePath) => { |
| 12 | + const pkg = JSON.parse(fs.readFileSync(filePath, 'utf8')) |
| 13 | + if (pkg.name && pkg.version) { |
| 14 | + packageVersions[pkg.name] = pkg.version |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +// Process each package.json |
| 19 | +packageFiles.forEach((filePath) => { |
| 20 | + const pkg = JSON.parse(fs.readFileSync(filePath, 'utf8')) |
| 21 | + let modified = false |
| 22 | + |
| 23 | + // Helper function to process dependencies |
| 24 | + const processDeps = (deps: Record<string, string>) => { |
| 25 | + if (!deps) return deps |
| 26 | + const newDeps = { ...deps } |
| 27 | + |
| 28 | + Object.entries(deps).forEach(([name, version]) => { |
| 29 | + if (version.startsWith('workspace:')) { |
| 30 | + const actualVersion = version === 'workspace:*' |
| 31 | + ? packageVersions[name] |
| 32 | + : version.replace('workspace:', '') |
| 33 | + |
| 34 | + if (actualVersion) { |
| 35 | + newDeps[name] = actualVersion |
| 36 | + modified = true |
| 37 | + } |
| 38 | + } |
| 39 | + }) |
| 40 | + return newDeps |
| 41 | + } |
| 42 | + |
| 43 | + // Process all dependency types |
| 44 | + pkg.dependencies = processDeps(pkg.dependencies) |
| 45 | + pkg.devDependencies = processDeps(pkg.devDependencies) |
| 46 | + pkg.peerDependencies = processDeps(pkg.peerDependencies) |
| 47 | + |
| 48 | + // Save if modified |
| 49 | + if (modified) { |
| 50 | + fs.writeFileSync(filePath, `${JSON.stringify(pkg, null, 2)}\n`) |
| 51 | + } |
| 52 | +}) |
0 commit comments