|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// Read JSON from stdin |
| 7 | +let workspaceData = ''; |
| 8 | +process.stdin.on('data', chunk => { |
| 9 | + workspaceData += chunk; |
| 10 | +}); |
| 11 | + |
| 12 | +process.stdin.on('end', () => { |
| 13 | + try { |
| 14 | + // Parse each line as a separate JSON object |
| 15 | + const workspaces = workspaceData |
| 16 | + .trim() |
| 17 | + .split('\n') |
| 18 | + .map(line => JSON.parse(line)); |
| 19 | + |
| 20 | + // Read the root package.json to get workspace patterns |
| 21 | + const rootPackageJson = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')); |
| 22 | + const workspacePatterns = rootPackageJson.workspaces || []; |
| 23 | + |
| 24 | + // Create releases object with all packages set to patch |
| 25 | + const releases = workspaces.reduce((acc, workspace) => { |
| 26 | + if (workspace.name) { |
| 27 | + // Only include packages that match the workspace patterns |
| 28 | + const location = workspace.location; |
| 29 | + if (workspacePatterns.some(pattern => { |
| 30 | + // For exact matches (no wildcards), do exact string comparison |
| 31 | + if (!pattern.includes('*')) { |
| 32 | + return location === pattern; |
| 33 | + } |
| 34 | + |
| 35 | + // For patterns with wildcards, convert to regex but exclude dev packages |
| 36 | + const regex = new RegExp('^' + pattern.replace(/\*/g, '[^/]+') + '$'); |
| 37 | + const matches = regex.test(location) && !location.startsWith('packages/dev/'); |
| 38 | + return matches; |
| 39 | + })) { |
| 40 | + acc[workspace.name] = 'patch'; |
| 41 | + } |
| 42 | + } |
| 43 | + return acc; |
| 44 | + }, {}); |
| 45 | + |
| 46 | + // Convert to YAML format |
| 47 | + const yamlContent = `releases: |
| 48 | +${Object.entries(releases).map(([name, version]) => ` "${name}": ${version}`).join('\n')} |
| 49 | +
|
| 50 | +undecided: |
| 51 | + - react-spectrum-monorepo |
| 52 | +`; |
| 53 | + |
| 54 | + // Write to .yarn/versions/version.yml |
| 55 | + const versionsDir = path.join(process.cwd(), '.yarn', 'versions'); |
| 56 | + if (!fs.existsSync(versionsDir)) { |
| 57 | + fs.mkdirSync(versionsDir, {recursive: true}); |
| 58 | + } |
| 59 | + |
| 60 | + fs.writeFileSync(path.join(versionsDir, 'version.yml'), yamlContent); |
| 61 | + console.log('Generated version.yml successfully'); |
| 62 | + } catch (error) { |
| 63 | + console.error('Error generating version.yml:', error); |
| 64 | + process.exit(1); |
| 65 | + } |
| 66 | +}); |
0 commit comments