|
| 1 | +import { Command } from 'commander'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs'; |
| 4 | +import { pathToFileURL } from 'url'; |
| 5 | +import chalk from 'chalk'; |
| 6 | +import { bundleRequire } from 'bundle-require'; |
| 7 | +import { ObjectStackDefinitionSchema } from '@objectstack/spec'; |
| 8 | + |
| 9 | +export const compileCommand = new Command('compile') |
| 10 | + .description('Compile ObjectStack configuration to JSON definition') |
| 11 | + .argument('[source]', 'Source configuration file', 'objectstack.config.ts') |
| 12 | + .argument('[output]', 'Output JSON file', 'dist/objectstack.json') |
| 13 | + .action(async (source, output) => { |
| 14 | + const start = Date.now(); |
| 15 | + console.log(chalk.bold(`\n🔹 ObjectStack Compiler v0.1`)); |
| 16 | + console.log(chalk.dim(`------------------------------`)); |
| 17 | + console.log(`📂 Source: ${chalk.blue(source)}`); |
| 18 | + |
| 19 | + const absolutePath = path.resolve(process.cwd(), source); |
| 20 | + if (!fs.existsSync(absolutePath)) { |
| 21 | + console.error(chalk.red(`\n❌ Config file not found: ${absolutePath}`)); |
| 22 | + process.exit(1); |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + // 1. Load Configuration |
| 27 | + console.log(chalk.yellow(`📦 Bundling Configuration...`)); |
| 28 | + const { mod } = await bundleRequire({ |
| 29 | + filepath: absolutePath, |
| 30 | + }); |
| 31 | + |
| 32 | + const config = mod.default || mod; |
| 33 | + |
| 34 | + if (!config) { |
| 35 | + throw new Error(`Default export not found in ${source}`); |
| 36 | + } |
| 37 | + |
| 38 | + // 2. Validate against Protocol |
| 39 | + console.log(chalk.yellow(`🔍 Validating Protocol Compliance...`)); |
| 40 | + const result = ObjectStackDefinitionSchema.safeParse(config); |
| 41 | + |
| 42 | + if (!result.success) { |
| 43 | + console.error(chalk.red(`\n❌ Validation Failed!`)); |
| 44 | + |
| 45 | + result.error.errors.forEach(e => { |
| 46 | + console.error(chalk.red(` - [${e.path.join('.')}] ${e.message}`)); |
| 47 | + }); |
| 48 | + |
| 49 | + process.exit(1); |
| 50 | + } |
| 51 | + |
| 52 | + // 3. Generate Artifact |
| 53 | + const artifactPath = path.resolve(process.cwd(), output); |
| 54 | + const artifactDir = path.dirname(artifactPath); |
| 55 | + |
| 56 | + if (!fs.existsSync(artifactDir)) { |
| 57 | + fs.mkdirSync(artifactDir, { recursive: true }); |
| 58 | + } |
| 59 | + |
| 60 | + const jsonContent = JSON.stringify(result.data, null, 2); |
| 61 | + fs.writeFileSync(artifactPath, jsonContent); |
| 62 | + |
| 63 | + const size = (jsonContent.length / 1024).toFixed(2); |
| 64 | + console.log(chalk.green(`\n✅ Build Success (${Date.now() - start}ms)`)); |
| 65 | + console.log(`📦 Artifact: ${chalk.blue(output)} (${size} KB)`); |
| 66 | + console.log(chalk.magenta(`✨ Ready for Deployment`)); |
| 67 | + |
| 68 | + } catch (error: any) { |
| 69 | + console.error(chalk.red(`\n❌ Compilation Error:`)); |
| 70 | + console.error(error.message || error); |
| 71 | + process.exit(1); |
| 72 | + } |
| 73 | + }); |
0 commit comments