Last active
January 18, 2026 10:06
-
-
Save imaman/83cf9a98050194ffbcfe0c11573023a6 to your computer and use it in GitHub Desktop.
opus fails to detect DRY violation
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
| import * as fs from 'node:fs' | |
| import * as path from 'node:path' | |
| import type { DependencyGraph, MonorepoPackage, PackageLocation, PackageMap } from './types.js' | |
| function getDistDir(main: string | undefined): string { | |
| const mainPath = main ?? 'dist/index.js' | |
| const dir = path.dirname(mainPath) | |
| return dir || 'dist' | |
| } | |
| function getEntryPoint(main: string | undefined): string { | |
| return main ?? 'dist/index.js' | |
| } | |
| interface PackageLocationInput { | |
| pkg: MonorepoPackage | |
| monorepoRoot: string | |
| isPackageToBundle: boolean | |
| } | |
| function createPackageLocation(input: PackageLocationInput): PackageLocation { | |
| const { pkg, monorepoRoot, isPackageToBundle } = input | |
| const distDir = getDistDir(pkg.packageJson.main) | |
| const entryPoint = getEntryPoint(pkg.packageJson.main) | |
| const monorepoRelativePath = path.relative(monorepoRoot, pkg.path) | |
| const sourceDistDir = path.resolve(pkg.path, distDir) | |
| const outputDistDir = isPackageToBundle ? distDir : path.join('deps', monorepoRelativePath, distDir) | |
| const outputEntryPoint = isPackageToBundle ? entryPoint : path.join('deps', monorepoRelativePath, entryPoint) | |
| return { | |
| name: pkg.name, | |
| sourceDistDir, | |
| outputDistDir, | |
| outputEntryPoint, | |
| resolveSubpath(subpath: string): string { | |
| return isPackageToBundle ? path.join(distDir, subpath) : path.join('deps', monorepoRelativePath, distDir, subpath) | |
| }, | |
| } | |
| } | |
| function validatePackageLocation(location: PackageLocation): void { | |
| if (!fs.existsSync(location.sourceDistDir)) { | |
| throw new Error( | |
| `dist directory not found at ${location.sourceDistDir}. Did you run the build for ${location.name}?` | |
| ) | |
| } | |
| } | |
| export function buildPackageMap(graph: DependencyGraph, monorepoRoot: string): PackageMap { | |
| const packageMap: PackageMap = new Map() | |
| const mainLocation = createPackageLocation({ | |
| pkg: graph.packageToBundle, | |
| monorepoRoot, | |
| isPackageToBundle: true, | |
| }) | |
| packageMap.set(graph.packageToBundle.name, mainLocation) | |
| validatePackageLocation(mainLocation) | |
| for (const dep of graph.inRepoDeps) { | |
| const depLocation = createPackageLocation({ | |
| pkg: dep, | |
| monorepoRoot, | |
| isPackageToBundle: false, | |
| }) | |
| packageMap.set(dep.name, depLocation) | |
| validatePackageLocation(depLocation) | |
| } | |
| return packageMap | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment