| 575 | } |
| 576 | |
| 577 | function readPackageMetadata(root: string): Result<PackageMetadata, string> { |
| 578 | const normalizedRoot = path.resolve(root) |
| 579 | const cached = packageMetadataCache.get(normalizedRoot) |
| 580 | if (cached !== undefined) { |
| 581 | return cached |
| 582 | } |
| 583 | const packageJsonPath = path.join(normalizedRoot, "package.json") |
| 584 | let parsed: unknown |
| 585 | try { |
| 586 | parsed = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) |
| 587 | } catch (error) { |
| 588 | const result = { |
| 589 | _tag: "Failure", |
| 590 | error: `unable to read package.json at ${packageJsonPath}: ${String(error)}` |
| 591 | } as const |
| 592 | packageMetadataCache.set(normalizedRoot, result) |
| 593 | return result |
| 594 | } |
| 595 | if (!isRecord(parsed)) { |
| 596 | const result = { _tag: "Failure", error: `package.json at ${packageJsonPath} must be an object` } as const |
| 597 | packageMetadataCache.set(normalizedRoot, result) |
| 598 | return result |
| 599 | } |
| 600 | if (typeof parsed.name !== "string" || parsed.name.length === 0) { |
| 601 | const result = { |
| 602 | _tag: "Failure", |
| 603 | error: `package.json at ${packageJsonPath} must include a non-empty name` |
| 604 | } as const |
| 605 | packageMetadataCache.set(normalizedRoot, result) |
| 606 | return result |
| 607 | } |
| 608 | if (parsed.exports === undefined) { |
| 609 | const result = { _tag: "Failure", error: `package.json at ${packageJsonPath} must include exports` } as const |
| 610 | packageMetadataCache.set(normalizedRoot, result) |
| 611 | return result |
| 612 | } |
| 613 | const result = { |
| 614 | _tag: "Success", |
| 615 | value: { |
| 616 | root: normalizedRoot, |
| 617 | sourceRoot: path.join(normalizedRoot, "src"), |
| 618 | name: parsed.name, |
| 619 | exports: parsed.exports |
| 620 | } |
| 621 | } as const |
| 622 | packageMetadataCache.set(normalizedRoot, result) |
| 623 | return result |
| 624 | } |
| 625 | |
| 626 | function parseBarrelExports(indexPath: string): Result<ParsedBarrelExports, string> { |
| 627 | const normalizedIndexPath = path.resolve(indexPath) |