/** * Package Command - Create distributable artifacts for C and JS outputs */ import { Command } from 'commander'; import chalk from 'chalk'; import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs'; import { join } from 'path'; import { execa } from 'execa'; /** * Create the package command */ export function createPackageCommand(): Command { const pkgCommand = new Command('package'); pkgCommand .description('Package C and JS artifacts into distributable archives') .action(async () => { if (!existsSync('grammar.js')) { console.error(chalk.red('❌ No grammar.js found. Are you in a DSL project directory?')); process.exit(1); } console.log(chalk.blue('📦 Packaging artifacts...')); // Ensure build is up to date await execa('dsk', ['build'], { stdio: 'inherit' }); const distDir = 'dist'; mkdirSync(distDir, { recursive: true }); // Zip C outputs (simple tar.gz using system tar to avoid extra deps) const cDir = 'generated/c'; if (existsSync(cDir)) { const cArchive = join(distDir, 'c-artifacts.tar.gz'); await execa('tar', ['-czf', cArchive, '-C', 'generated', 'c'], { stdio: 'inherit' }); console.log(chalk.green(`✅ C artifacts: ${cArchive}`)); } // Pack JS package const jsDir = 'generated/js'; if (existsSync(jsDir)) { // Prefer bun pack, fallback to npm pack let pkgPath = ''; try { const { stdout } = await execa('bun', ['pack'], { cwd: jsDir }); pkgPath = stdout.trim(); } catch { const { stdout } = await execa('npm', ['pack'], { cwd: jsDir }); pkgPath = stdout.trim(); } const fileName = pkgPath.split(/\s|\n/).pop() as string; const srcPath = join(jsDir, fileName); const destPath = join(distDir, fileName); await execa('bash', ['-lc', `cp ${JSON.stringify(srcPath)} ${JSON.stringify(destPath)}`]); console.log(chalk.green(`✅ JS package: ${destPath}`)); } console.log(chalk.blue('🎉 Packaging complete.')); }); return pkgCommand; }