|
| 1 | +/* eslint-disable sonarjs/cognitive-complexity */ |
| 2 | + |
| 3 | +import { readWantedLockfile } from '@pnpm/lockfile-file' |
| 4 | +import { LockfileFile } from '@pnpm/lockfile-file/lib/write' |
| 5 | +import { ProjectManifest } from '@pnpm/types' |
| 6 | +import { existsSync } from 'fs' |
| 7 | +import normalizePath from 'normalize-path' |
| 8 | +import path from 'path' |
| 9 | + |
| 10 | +type UpdateFunc = ( |
| 11 | + data: Record<string, unknown>, |
| 12 | + dir: string, |
| 13 | + manifest: ProjectManifest, |
| 14 | +) => Record<string, unknown> | Promise<Record<string, unknown> | null> | null |
| 15 | + |
| 16 | +export default async ( |
| 17 | + workspaceDir: string, |
| 18 | +): Promise<Record<string, UpdateFunc>> => { |
| 19 | + const lockfile = await readWantedLockfile(workspaceDir, { |
| 20 | + ignoreIncompatible: false, |
| 21 | + }) |
| 22 | + |
| 23 | + if (lockfile == null) { |
| 24 | + throw new Error('no lockfile found') |
| 25 | + } |
| 26 | + |
| 27 | + return { |
| 28 | + '.releaserc.json': (releaseRc, _dir: string, manifest) => { |
| 29 | + if (!manifest.name) { |
| 30 | + return {} |
| 31 | + } |
| 32 | + |
| 33 | + return { tagFormat: `${manifest.name}@v\${version}`, ...releaseRc } |
| 34 | + }, |
| 35 | + 'package.json': updatePackageJson(workspaceDir, lockfile), |
| 36 | + 'tsconfig.json': updateTsConfig(workspaceDir, lockfile), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function updatePackageJson(_workspaceDir: string, _lockfile: LockfileFile) { |
| 41 | + return (manifest: ProjectManifest, _dir: string) => { |
| 42 | + return { |
| 43 | + ...manifest, |
| 44 | + author: 'Philipp Busse', |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function updateTsConfig(workspaceDir: string, lockfile: LockfileFile) { |
| 50 | + return ( |
| 51 | + tsConfig: Record<string, unknown>, |
| 52 | + dir: string, |
| 53 | + manifest: ProjectManifest, |
| 54 | + ) => { |
| 55 | + if (tsConfig == null || manifest.name?.includes('/tsconfig')) { |
| 56 | + return tsConfig |
| 57 | + } |
| 58 | + |
| 59 | + const relative = normalizePath(path.relative(workspaceDir, dir)) |
| 60 | + |
| 61 | + const importer = lockfile.importers?.[relative] |
| 62 | + |
| 63 | + if (!importer) { |
| 64 | + return tsConfig |
| 65 | + } |
| 66 | + |
| 67 | + const deps = { |
| 68 | + ...importer.dependencies, |
| 69 | + ...importer.devDependencies, |
| 70 | + } |
| 71 | + |
| 72 | + const references = Object.values(deps) |
| 73 | + .filter((dep) => dep.startsWith('link:')) |
| 74 | + .map((dep) => dep.slice('link:'.length)) |
| 75 | + .filter((relativePath) => |
| 76 | + existsSync(path.join(dir, relativePath, 'tsconfig.json')), |
| 77 | + ) |
| 78 | + .map((path) => ({ path })) |
| 79 | + |
| 80 | + console.log(`Updating tsconfig for ${dir}: ${JSON.stringify(references)}`) |
| 81 | + |
| 82 | + return { |
| 83 | + ...tsConfig, |
| 84 | + ...(references && { |
| 85 | + references: references.sort((r1, r2) => r1.path.localeCompare(r2.path)), |
| 86 | + }), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments