|
| 1 | +import fs from 'node:fs'; |
| 2 | +import { glob } from 'node:fs/promises'; |
| 3 | +import { dirname, join, relative } from 'node:path'; |
| 4 | + |
| 5 | +import yaml from 'js-yaml'; |
| 6 | + |
| 7 | +import repos from './repo.json' with { type: 'json' }; |
| 8 | + |
| 9 | +const projectDir = import.meta.dirname; |
| 10 | +const rootDir = join(projectDir, '..'); |
| 11 | +const tgzPath = rootDir; |
| 12 | + |
| 13 | +const projects = Object.keys(repos); |
| 14 | + |
| 15 | +const project = process.argv[2]; |
| 16 | + |
| 17 | +if (!projects.includes(project)) { |
| 18 | + console.error(`Project ${project} is not defined in repo.json. Valid projects: ${projects.join(', ')}`); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +// Read pnpm-workspace.yaml to get workspace patterns |
| 23 | +const workspaceConfig = yaml.load(fs.readFileSync(join(rootDir, 'pnpm-workspace.yaml'), 'utf8')) as { |
| 24 | + packages: string[]; |
| 25 | +}; |
| 26 | + |
| 27 | +// Use glob to find all package directories dynamically |
| 28 | +async function discoverPackages(): Promise<[string, string][]> { |
| 29 | + const packages: [string, string][] = []; |
| 30 | + |
| 31 | + for (const pattern of workspaceConfig.packages) { |
| 32 | + // Convert pnpm patterns (e.g., 'packages/*') to glob patterns for package.json |
| 33 | + const globPattern = `${pattern}/package.json`; |
| 34 | + |
| 35 | + for await (const entry of glob(globPattern, { cwd: rootDir })) { |
| 36 | + const pkgJsonPath = join(rootDir, entry); |
| 37 | + try { |
| 38 | + const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8')); |
| 39 | + |
| 40 | + // Skip private packages and packages without names |
| 41 | + if (pkgJson.private || !pkgJson.name) continue; |
| 42 | + |
| 43 | + const relativePath = dirname(relative(rootDir, pkgJsonPath)); |
| 44 | + packages.push([pkgJson.name, relativePath]); |
| 45 | + } catch { |
| 46 | + // Skip if package.json is invalid or cannot be read |
| 47 | + console.warn(`Warning: Could not read ${pkgJsonPath}`); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return packages; |
| 53 | +} |
| 54 | + |
| 55 | +async function buildOverrides(): Promise<Record<string, string>> { |
| 56 | + const packages = await discoverPackages(); |
| 57 | + const overrides: Record<string, string> = {}; |
| 58 | + |
| 59 | + for (const [name, path] of packages) { |
| 60 | + const version = JSON.parse(fs.readFileSync(join(tgzPath, path, 'package.json'), 'utf8')).version; |
| 61 | + const filename = `${name.replace('@', '').replace('/', '-')}-${version}.tgz`; |
| 62 | + overrides[name] = `file:${tgzPath}/${filename}`; |
| 63 | + } |
| 64 | + |
| 65 | + return overrides; |
| 66 | +} |
| 67 | + |
| 68 | +async function patchPackageJSON(filePath: string, overrides: Record<string, string>): Promise<void> { |
| 69 | + const packageJson = JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 70 | + // Add overrides with tgz files |
| 71 | + packageJson.overrides = { |
| 72 | + ...packageJson.overrides, |
| 73 | + ...overrides, |
| 74 | + }; |
| 75 | + |
| 76 | + for (const name in packageJson.dependencies) { |
| 77 | + const override = overrides[name]; |
| 78 | + if (override) { |
| 79 | + packageJson.dependencies[name] = override; |
| 80 | + } |
| 81 | + } |
| 82 | + for (const name in packageJson.devDependencies) { |
| 83 | + const override = overrides[name]; |
| 84 | + if (override) { |
| 85 | + packageJson.devDependencies[name] = override; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const packageJsonString = JSON.stringify(packageJson, null, 2) + '\n'; |
| 90 | + console.log(packageJsonString); |
| 91 | + fs.writeFileSync(filePath, packageJsonString); |
| 92 | +} |
| 93 | + |
| 94 | +async function patchCnpmcore(overrides: Record<string, string>): Promise<void> { |
| 95 | + const packageJsonPath = join(projectDir, 'cnpmcore', 'package.json'); |
| 96 | + await patchPackageJSON(packageJsonPath, overrides); |
| 97 | +} |
| 98 | + |
| 99 | +async function patchExamples(overrides: Record<string, string>): Promise<void> { |
| 100 | + // https://github.com/eggjs/examples/tree/master/hello-tegg |
| 101 | + let packageJsonPath = join(projectDir, 'examples', 'hello-tegg', 'package.json'); |
| 102 | + await patchPackageJSON(packageJsonPath, overrides); |
| 103 | + |
| 104 | + // https://github.com/eggjs/examples/blob/master/helloworld/package.json |
| 105 | + packageJsonPath = join(projectDir, 'examples', 'helloworld', 'package.json'); |
| 106 | + await patchPackageJSON(packageJsonPath, overrides); |
| 107 | +} |
| 108 | + |
| 109 | +async function main(): Promise<void> { |
| 110 | + const overrides = await buildOverrides(); |
| 111 | + |
| 112 | + switch (project) { |
| 113 | + case 'cnpmcore': |
| 114 | + await patchCnpmcore(overrides); |
| 115 | + break; |
| 116 | + case 'examples': |
| 117 | + await patchExamples(overrides); |
| 118 | + break; |
| 119 | + default: |
| 120 | + console.error(`Project ${project} is not supported`); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +main(); |
0 commit comments