|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Prepares local packages for symlinking into external projects. |
| 21 | + * |
| 22 | + * Problems this solves: |
| 23 | + * - `catalog:` references in package.json are a pnpm workspace-only protocol. |
| 24 | + * External consumers (even via `file:`) can't resolve them. |
| 25 | + * - `workspace:*` references must become `file:` paths so the external project |
| 26 | + * resolves inter-package dependencies to the local builds too. |
| 27 | + * |
| 28 | + * What it does: |
| 29 | + * 1. Reads the `catalog:` entries from pnpm-workspace.yaml. |
| 30 | + * 2. Builds all packages (pnpm build:packages). |
| 31 | + * 3. Patches every `packages/<*>/package.json`, replacing: |
| 32 | + * `"catalog:"` → the real version string from the catalog |
| 33 | + * `"workspace:*"` → `"file:<absolute-path-to-package>"` |
| 34 | + * 4. Prints ready-to-paste override snippets for pnpm and npm. |
| 35 | + * |
| 36 | + * To restore the source files after you're done: |
| 37 | + * git checkout packages/<*>/package.json |
| 38 | + */ |
| 39 | + |
| 40 | +const fs = require('fs'); |
| 41 | +const path = require('path'); |
| 42 | +const {execSync} = require('child_process'); |
| 43 | + |
| 44 | +const ROOT = path.resolve(__dirname, '..'); |
| 45 | + |
| 46 | +// --------------------------------------------------------------------------- |
| 47 | +// 1. Parse catalog from pnpm-workspace.yaml |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | + |
| 50 | +/** |
| 51 | + * Minimal YAML parser for the flat `catalog:` section in pnpm-workspace.yaml. |
| 52 | + * Handles both quoted and unquoted keys/values, and multi-word values. |
| 53 | + */ |
| 54 | +function parseCatalog() { |
| 55 | + const yamlPath = path.join(ROOT, 'pnpm-workspace.yaml'); |
| 56 | + const yaml = fs.readFileSync(yamlPath, 'utf-8'); |
| 57 | + const catalog = {}; |
| 58 | + let inCatalog = false; |
| 59 | + |
| 60 | + for (const raw of yaml.split('\n')) { |
| 61 | + const line = raw.trimEnd(); |
| 62 | + |
| 63 | + if (/^catalog:\s*$/.test(line)) { |
| 64 | + inCatalog = true; |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if (inCatalog) { |
| 69 | + // A non-indented, non-empty line signals the end of the catalog block. |
| 70 | + if (line.length > 0 && !/^\s/.test(line)) { |
| 71 | + inCatalog = false; |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + // Match ` 'key': value` or ` key: value` |
| 76 | + const match = line.match(/^\s+['"]?([^'":\s][^'":]*?)['"]?\s*:\s*(.+)$/); |
| 77 | + if (match) { |
| 78 | + catalog[match[1].trim()] = match[2].trim().replace(/^['"]|['"]$/g, ''); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return catalog; |
| 84 | +} |
| 85 | + |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | +// 2. Discover all publishable packages (packages/* minus workspace exclusions) |
| 88 | +// --------------------------------------------------------------------------- |
| 89 | + |
| 90 | +const EXCLUDED_PACKAGES = new Set(['nuxt']); // mirrors !packages/nuxt in pnpm-workspace.yaml |
| 91 | + |
| 92 | +function findPackages() { |
| 93 | + const packagesDir = path.join(ROOT, 'packages'); |
| 94 | + |
| 95 | + return fs |
| 96 | + .readdirSync(packagesDir, {withFileTypes: true}) |
| 97 | + .filter(entry => entry.isDirectory() && !EXCLUDED_PACKAGES.has(entry.name)) |
| 98 | + .map(entry => path.join(packagesDir, entry.name)) |
| 99 | + .filter(pkgPath => fs.existsSync(path.join(pkgPath, 'package.json'))); |
| 100 | +} |
| 101 | + |
| 102 | +// --------------------------------------------------------------------------- |
| 103 | +// 3. Build packages |
| 104 | +// --------------------------------------------------------------------------- |
| 105 | + |
| 106 | +function buildPackages() { |
| 107 | + console.log('\nBuilding packages...\n'); |
| 108 | + execSync('pnpm build:packages', {cwd: ROOT, stdio: 'inherit'}); |
| 109 | + console.log('\nBuild complete.\n'); |
| 110 | +} |
| 111 | + |
| 112 | +// --------------------------------------------------------------------------- |
| 113 | +// 4. Patch package.json files – replace catalog: and workspace:* references |
| 114 | +// --------------------------------------------------------------------------- |
| 115 | + |
| 116 | +const DEP_FIELDS = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']; |
| 117 | + |
| 118 | +function patchPackages(pkgPaths, catalog) { |
| 119 | + // Build a name → absolute-path map for workspace packages. |
| 120 | + const workspaceMap = {}; |
| 121 | + for (const pkgPath of pkgPaths) { |
| 122 | + const pkgJson = JSON.parse(fs.readFileSync(path.join(pkgPath, 'package.json'), 'utf-8')); |
| 123 | + if (pkgJson.name) workspaceMap[pkgJson.name] = pkgPath; |
| 124 | + } |
| 125 | + |
| 126 | + for (const pkgPath of pkgPaths) { |
| 127 | + const pkgJsonPath = path.join(pkgPath, 'package.json'); |
| 128 | + const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8')); |
| 129 | + let modified = false; |
| 130 | + |
| 131 | + for (const field of DEP_FIELDS) { |
| 132 | + if (!pkgJson[field]) continue; |
| 133 | + |
| 134 | + for (const [dep, version] of Object.entries(pkgJson[field])) { |
| 135 | + // catalog: (default catalog) or catalog:name (named catalog – treated the same here) |
| 136 | + if (typeof version === 'string' && version.startsWith('catalog:')) { |
| 137 | + const resolved = catalog[dep]; |
| 138 | + if (resolved) { |
| 139 | + pkgJson[field][dep] = resolved; |
| 140 | + modified = true; |
| 141 | + } else { |
| 142 | + console.warn(` [warn] No catalog entry for "${dep}" in ${pkgJson.name}`); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + if (version === 'workspace:*' || version === 'workspace:^' || version === 'workspace:~') { |
| 147 | + const resolved = workspaceMap[dep]; |
| 148 | + if (resolved) { |
| 149 | + pkgJson[field][dep] = `file:${resolved}`; |
| 150 | + modified = true; |
| 151 | + } else { |
| 152 | + console.warn(` [warn] Workspace package "${dep}" not found for ${pkgJson.name}`); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (modified) { |
| 159 | + fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2) + '\n'); |
| 160 | + console.log(` patched ${pkgJson.name}`); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// --------------------------------------------------------------------------- |
| 166 | +// 5. Print override snippets |
| 167 | +// --------------------------------------------------------------------------- |
| 168 | + |
| 169 | +function printSnippets(pkgPaths) { |
| 170 | + const overrides = {}; |
| 171 | + for (const pkgPath of pkgPaths) { |
| 172 | + const pkgJson = JSON.parse(fs.readFileSync(path.join(pkgPath, 'package.json'), 'utf-8')); |
| 173 | + if (pkgJson.name) overrides[pkgJson.name] = `file:${pkgPath}`; |
| 174 | + } |
| 175 | + |
| 176 | + const divider = '─'.repeat(60); |
| 177 | + |
| 178 | + console.log(`\n${divider}`); |
| 179 | + console.log(" pnpm — add to your project's package.json"); |
| 180 | + console.log(divider); |
| 181 | + console.log(JSON.stringify({pnpm: {overrides}}, null, 2)); |
| 182 | + |
| 183 | + console.log(`\n${divider}`); |
| 184 | + console.log(" npm — add to your project's package.json"); |
| 185 | + console.log(divider); |
| 186 | + console.log(JSON.stringify({overrides}, null, 2)); |
| 187 | + |
| 188 | + console.log(`\n${divider}`); |
| 189 | + console.log(" Yarn (Berry) — add to your project's package.json"); |
| 190 | + console.log(divider); |
| 191 | + console.log(JSON.stringify({resolutions: overrides}, null, 2)); |
| 192 | + |
| 193 | + console.log(`\n${divider}`); |
| 194 | + console.log(' To restore source files when done:'); |
| 195 | + console.log(' git checkout packages/*/package.json'); |
| 196 | + console.log(divider + '\n'); |
| 197 | +} |
| 198 | + |
| 199 | +// --------------------------------------------------------------------------- |
| 200 | +// Main |
| 201 | +// --------------------------------------------------------------------------- |
| 202 | + |
| 203 | +console.log('symlink — preparing local packages for external linking\n'); |
| 204 | + |
| 205 | +const catalog = parseCatalog(); |
| 206 | +console.log(`Catalog entries found: ${Object.keys(catalog).length}`); |
| 207 | + |
| 208 | +const pkgPaths = findPackages(); |
| 209 | +console.log(`Packages found: ${pkgPaths.length} (${pkgPaths.map(p => path.basename(p)).join(', ')})`); |
| 210 | + |
| 211 | +buildPackages(); |
| 212 | + |
| 213 | +console.log('Patching package.json files...'); |
| 214 | +patchPackages(pkgPaths, catalog); |
| 215 | + |
| 216 | +printSnippets(pkgPaths); |
0 commit comments