|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Root-level script to collect build metadata |
| 5 | + * This wraps the build verification package and can be run from anywhere |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node scripts/collect-build-metadata.js |
| 9 | + * node scripts/collect-build-metadata.js --output build-info.json |
| 10 | + * node scripts/collect-build-metadata.js --embed app/utils/build-info.ts |
| 11 | + */ |
| 12 | + |
| 13 | +const { execSync } = require('child_process'); |
| 14 | +const path = require('path'); |
| 15 | +const fs = require('fs'); |
| 16 | + |
| 17 | +const packageDir = path.join(__dirname, '../packages/codinit-dev'); |
| 18 | +const args = process.argv.slice(2); |
| 19 | + |
| 20 | +// Check if the package is built |
| 21 | +const distPath = path.join(packageDir, 'dist/index.js'); |
| 22 | + |
| 23 | +if (!fs.existsSync(distPath)) { |
| 24 | + console.log('Building verification package...'); |
| 25 | + |
| 26 | + try { |
| 27 | + execSync('pnpm build', { cwd: packageDir, stdio: 'inherit' }); |
| 28 | + } catch (error) { |
| 29 | + console.error('Failed to build verification package'); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Import and run the collector |
| 35 | +const { collectBuildMetadata } = require(distPath); |
| 36 | +const metadata = collectBuildMetadata(); |
| 37 | + |
| 38 | +// Display metadata |
| 39 | +console.log('\n' + '='.repeat(60)); |
| 40 | +console.log('Build Metadata Collected'); |
| 41 | +console.log('='.repeat(60)); |
| 42 | +console.log(`Commit: ${metadata.commit}`); |
| 43 | +console.log(`Branch: ${metadata.branch}`); |
| 44 | +console.log(`Timestamp: ${metadata.timestamp}`); |
| 45 | +console.log(`Builder: ${metadata.builder}`); |
| 46 | +console.log(`Environment: ${metadata.environment}`); |
| 47 | +console.log(`Official: ${metadata.isOfficial ? 'YES' : 'NO'}`); |
| 48 | + |
| 49 | +if (metadata.buildNumber) { |
| 50 | + console.log(`Build #: ${metadata.buildNumber}`); |
| 51 | +} |
| 52 | + |
| 53 | +if (metadata.tags && metadata.tags.length > 0) { |
| 54 | + console.log(`Tags: ${metadata.tags.join(', ')}`); |
| 55 | +} |
| 56 | + |
| 57 | +if (metadata.signature) { |
| 58 | + console.log(`Signature: ${metadata.signature.slice(0, 16)}...`); |
| 59 | +} |
| 60 | + |
| 61 | +console.log('='.repeat(60) + '\n'); |
| 62 | + |
| 63 | +// Handle output flags |
| 64 | +const outputFlag = args.indexOf('--output'); |
| 65 | +const embedFlag = args.indexOf('--embed'); |
| 66 | + |
| 67 | +// Output to JSON file |
| 68 | +if (outputFlag !== -1 && args[outputFlag + 1]) { |
| 69 | + const outputPath = path.resolve(process.cwd(), args[outputFlag + 1]); |
| 70 | + fs.writeFileSync(outputPath, JSON.stringify(metadata, null, 2)); |
| 71 | + console.log(`✓ Metadata written to: ${outputPath}\n`); |
| 72 | +} |
| 73 | + |
| 74 | +// Embed as TypeScript module |
| 75 | +if (embedFlag !== -1 && args[embedFlag + 1]) { |
| 76 | + const embedPath = path.resolve(process.cwd(), args[embedFlag + 1]); |
| 77 | + const tsContent = `/** |
| 78 | + * Auto-generated build metadata |
| 79 | + * Generated at: ${new Date().toISOString()} |
| 80 | + * DO NOT EDIT MANUALLY |
| 81 | + */ |
| 82 | +
|
| 83 | +export const BUILD_METADATA = ${JSON.stringify(metadata, null, 2)} as const; |
| 84 | +
|
| 85 | +export type BuildMetadata = typeof BUILD_METADATA; |
| 86 | +`; |
| 87 | + |
| 88 | + // Ensure directory exists |
| 89 | + fs.mkdirSync(path.dirname(embedPath), { recursive: true }); |
| 90 | + fs.writeFileSync(embedPath, tsContent); |
| 91 | + console.log(`✓ Metadata embedded in: ${embedPath}\n`); |
| 92 | +} |
| 93 | + |
| 94 | +// Exit successfully |
| 95 | +process.exit(0); |
0 commit comments