|
| 1 | +#!/usr/bin/env bun |
| 2 | +import process from 'node:process' |
| 3 | +import { watch } from 'node:fs' |
| 4 | +import { CAC } from 'cac' |
| 5 | +import { buildAndWrite } from '../src/build' |
| 6 | +import { config } from '../src/config' |
| 7 | +import { version } from '../package.json' |
| 8 | + |
| 9 | +const cli = new CAC('headwind') |
| 10 | + |
| 11 | +interface BuildOptions { |
| 12 | + output?: string |
| 13 | + minify?: boolean |
| 14 | + watch?: boolean |
| 15 | +} |
| 16 | + |
| 17 | +cli |
| 18 | + .command('build', 'Build CSS from content files') |
| 19 | + .option('--output <path>', 'Output CSS file path') |
| 20 | + .option('--minify', 'Minify CSS output') |
| 21 | + .option('--watch', 'Watch for file changes') |
| 22 | + .example('headwind build') |
| 23 | + .example('headwind build --output ./dist/styles.css') |
| 24 | + .example('headwind build --minify') |
| 25 | + .example('headwind build --watch') |
| 26 | + .action(async (options?: BuildOptions) => { |
| 27 | + const buildConfig = { |
| 28 | + ...config, |
| 29 | + output: options?.output || config.output, |
| 30 | + minify: options?.minify ?? config.minify, |
| 31 | + } |
| 32 | + |
| 33 | + async function runBuild(): Promise<void> { |
| 34 | + try { |
| 35 | + console.log('🚀 Building CSS...') |
| 36 | + const result = await buildAndWrite(buildConfig) |
| 37 | + console.log(`✅ Built ${result.classes.size} classes in ${result.duration.toFixed(2)}ms`) |
| 38 | + console.log(`📝 Output: ${buildConfig.output}`) |
| 39 | + } |
| 40 | + catch (error) { |
| 41 | + console.error('❌ Build failed:', error) |
| 42 | + process.exit(1) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (options?.watch) { |
| 47 | + console.log('👀 Watching for changes...') |
| 48 | + await runBuild() |
| 49 | + |
| 50 | + // Watch content directories |
| 51 | + const watchDirs = new Set<string>() |
| 52 | + for (const pattern of buildConfig.content) { |
| 53 | + const dir = pattern.split('**')[0] || '.' |
| 54 | + watchDirs.add(dir) |
| 55 | + } |
| 56 | + |
| 57 | + for (const dir of watchDirs) { |
| 58 | + watch(dir, { recursive: true }, async (eventType, filename) => { |
| 59 | + if (filename && /\.(html|js|ts|jsx|tsx)$/.test(filename)) { |
| 60 | + console.log(`\n📝 ${filename} changed, rebuilding...`) |
| 61 | + await runBuild() |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + console.log(`\n👀 Watching: ${Array.from(watchDirs).join(', ')}`) |
| 67 | + } |
| 68 | + else { |
| 69 | + await runBuild() |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | +cli |
| 74 | + .command('watch', 'Build and watch for changes') |
| 75 | + .option('--output <path>', 'Output CSS file path') |
| 76 | + .option('--minify', 'Minify CSS output') |
| 77 | + .example('headwind watch') |
| 78 | + .example('headwind watch --output ./dist/styles.css') |
| 79 | + .action(async (options?: BuildOptions) => { |
| 80 | + const buildConfig = { |
| 81 | + ...config, |
| 82 | + output: options?.output || config.output, |
| 83 | + minify: options?.minify ?? config.minify, |
| 84 | + } |
| 85 | + |
| 86 | + async function runBuild(): Promise<void> { |
| 87 | + try { |
| 88 | + console.log('🚀 Building CSS...') |
| 89 | + const result = await buildAndWrite(buildConfig) |
| 90 | + console.log(`✅ Built ${result.classes.size} classes in ${result.duration.toFixed(2)}ms`) |
| 91 | + console.log(`📝 Output: ${buildConfig.output}`) |
| 92 | + } |
| 93 | + catch (error) { |
| 94 | + console.error('❌ Build failed:', error) |
| 95 | + process.exit(1) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + console.log('👀 Watching for changes...') |
| 100 | + await runBuild() |
| 101 | + |
| 102 | + // Watch content directories |
| 103 | + const watchDirs = new Set<string>() |
| 104 | + for (const pattern of buildConfig.content) { |
| 105 | + const dir = pattern.split('**')[0] || '.' |
| 106 | + watchDirs.add(dir) |
| 107 | + } |
| 108 | + |
| 109 | + for (const dir of watchDirs) { |
| 110 | + watch(dir, { recursive: true }, async (eventType, filename) => { |
| 111 | + if (filename && /\.(html|js|ts|jsx|tsx)$/.test(filename)) { |
| 112 | + console.log(`\n📝 ${filename} changed, rebuilding...`) |
| 113 | + await runBuild() |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + console.log(`\n👀 Watching: ${Array.from(watchDirs).join(', ')}`) |
| 119 | + }) |
| 120 | + |
| 121 | +cli.command('version', 'Show the version of Headwind').action(() => { |
| 122 | + console.log(version) |
| 123 | +}) |
| 124 | + |
| 125 | +cli.version(version) |
| 126 | +cli.help() |
| 127 | +cli.parse() |
0 commit comments