|
| 1 | +#!/usr/bin/env node |
| 2 | +// Normalize internal @mapconductor/* dependency specs to "*" across every |
| 3 | +// workspace package.json so npm always links the local workspace (git |
| 4 | +// submodule) instead of falling back to the npm registry. |
| 5 | +// |
| 6 | +// The submodule packages pin each other to exact versions (e.g. |
| 7 | +// "@mapconductor/js-sdk-core": "0.1.0"). When a workspace has since been |
| 8 | +// bumped (e.g. to 0.1.1) that exact spec no longer matches the local |
| 9 | +// package, so npm tries to download it from registry.npmjs.org and fails |
| 10 | +// with a 404 because these packages are not published yet. |
| 11 | +// |
| 12 | +// Rewriting the internal specs to "*" makes npm satisfy them from the local |
| 13 | +// workspaces regardless of the exact version each package declares. We only |
| 14 | +// touch dependencies that point at packages living in this repo's |
| 15 | +// workspaces, so external deps are left untouched. |
| 16 | +// |
| 17 | +// Run this after `git submodule update --init` and before `npm install`. |
| 18 | +// It edits checked-out files in place; do not commit the submodule changes. |
| 19 | + |
| 20 | +import { readFileSync, writeFileSync, existsSync, readdirSync } from 'node:fs'; |
| 21 | +import { dirname, join, resolve } from 'node:path'; |
| 22 | +import { fileURLToPath } from 'node:url'; |
| 23 | + |
| 24 | +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..'); |
| 25 | + |
| 26 | +function readJson(path) { |
| 27 | + return JSON.parse(readFileSync(path, 'utf8')); |
| 28 | +} |
| 29 | + |
| 30 | +// Resolve the workspaces entries (including simple "dir/*" globs) into |
| 31 | +// concrete directories that contain a package.json. |
| 32 | +const rootPkg = readJson(join(repoRoot, 'package.json')); |
| 33 | +const workspaceDirs = new Set(); |
| 34 | +for (const pattern of rootPkg.workspaces ?? []) { |
| 35 | + const cleaned = pattern.replace(/\/$/, ''); |
| 36 | + if (cleaned.endsWith('/*')) { |
| 37 | + const base = cleaned.slice(0, -2); |
| 38 | + const baseDir = join(repoRoot, base); |
| 39 | + if (!existsSync(baseDir)) continue; |
| 40 | + for (const entry of readdirSync(baseDir, { withFileTypes: true })) { |
| 41 | + const dir = join(base, entry.name); |
| 42 | + if (entry.isDirectory() && existsSync(join(repoRoot, dir, 'package.json'))) { |
| 43 | + workspaceDirs.add(dir); |
| 44 | + } |
| 45 | + } |
| 46 | + } else if (cleaned.includes('*')) { |
| 47 | + throw new Error(`Unsupported workspaces glob pattern: ${pattern}`); |
| 48 | + } else if (existsSync(join(repoRoot, cleaned, 'package.json'))) { |
| 49 | + workspaceDirs.add(cleaned); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Map of workspace package name -> directory, so we know which deps are local. |
| 54 | +const localNames = new Set(); |
| 55 | +const pkgPaths = []; |
| 56 | +for (const dir of workspaceDirs) { |
| 57 | + const pkgPath = join(repoRoot, dir, 'package.json'); |
| 58 | + const pkg = readJson(pkgPath); |
| 59 | + if (pkg.name) localNames.add(pkg.name); |
| 60 | + pkgPaths.push({ dir, pkgPath }); |
| 61 | +} |
| 62 | + |
| 63 | +const DEP_FIELDS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']; |
| 64 | + |
| 65 | +let changedFiles = 0; |
| 66 | +let changedSpecs = 0; |
| 67 | +for (const { dir, pkgPath } of pkgPaths) { |
| 68 | + const pkg = readJson(pkgPath); |
| 69 | + let touched = false; |
| 70 | + for (const field of DEP_FIELDS) { |
| 71 | + const deps = pkg[field]; |
| 72 | + if (!deps) continue; |
| 73 | + for (const name of Object.keys(deps)) { |
| 74 | + if (localNames.has(name) && deps[name] !== '*') { |
| 75 | + deps[name] = '*'; |
| 76 | + touched = true; |
| 77 | + changedSpecs++; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + if (touched) { |
| 82 | + writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n'); |
| 83 | + changedFiles++; |
| 84 | + console.log(` linked ${dir}/package.json`); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +console.log( |
| 89 | + `link-local-workspaces: normalized ${changedSpecs} internal dep spec(s) across ${changedFiles} package(s) to "*".`, |
| 90 | +); |
0 commit comments