also looking at this
Merge @stacksjs/ts-css into this repo as packages/toolkit
#27
128 files
+9797
-201
| @@ -0,0 +1,58 @@ | ||
| 1 | #!/usr/bin/env bun | |
| 2 | /** | |
| 3 | * ts-css CLI — minify, parse, or round-trip CSS files. | |
| 4 | */ | |
| 5 | ||
| 6 | import { CLI } from '@stacksjs/clapp' | |
| 7 | import { existsSync, readFileSync } from 'node:fs' | |
| 8 | import { minify, minifyBlock } from '../src/optimize' | |
| 9 | import { generate, parse } from '../src/parse' | |
| 10 | import { version } from '../package.json' | |
| 11 | ||
| 12 | function readSourceOrExit(file: string): string { | |
| 13 | if (!existsSync(file)) { | |
| 14 | process.stderr.write(`ts-css: file not found: ${file}\n`) | |
| 15 | process.exit(1) | |
| 16 | } | |
| 17 | return readFileSync(file, 'utf8') | |
| 18 | } | |
| 19 | ||
| 20 | const cli = new CLI('ts-css') | |
| 21 | ||
| 22 | cli | |
| 23 | .command('minify <file>', 'Minify a CSS file') | |
| 24 | .option('--no-comments', 'Strip /*!*/ comments too') | |
| 25 | .option('--block', 'Treat input as a declarationList (style="…" body)') | |
| 26 | .action(async (file: string, opts: { comments: boolean, block: boolean }) => { | |
| 27 | const css = readSourceOrExit(file) | |
| 28 | const minOptions = { comments: opts.comments === false ? false : ('exclamation' as const) } | |
| 29 | const result = opts.block | |
| 30 | ? minifyBlock(css, minOptions) | |
| 31 | : minify(css, minOptions) | |
| 32 | process.stdout.write(result.css) | |
| 33 | }) | |
| 34 | ||
| 35 | cli | |
| 36 | .command('parse <file>', 'Parse a CSS file and print its AST as JSON') | |
| 37 | .option('--positions', 'Include source locations on every node') | |
| 38 | .action(async (file: string, opts: { positions: boolean }) => { | |
| 39 | const css = readSourceOrExit(file) | |
| 40 | const ast = parse(css, { positions: opts.positions }) | |
| 41 | process.stdout.write(JSON.stringify(ast, null, 2)) | |
| 42 | }) | |
| 43 | ||
| 44 | cli | |
| 45 | .command('format <file>', 'Round-trip CSS through parser/generator (deterministic single-line output)') | |
| 46 | .action(async (file: string) => { | |
| 47 | const css = readSourceOrExit(file) | |
| 48 | const ast = parse(css) | |
| 49 | process.stdout.write(generate(ast)) | |
| 50 | }) | |
| 51 | ||
| 52 | cli.command('version', 'Show the version of the CLI').action(() => { | |
| 53 | console.log(version) | |
| 54 | }) | |
| 55 | ||
| 56 | cli.version(version) | |
| 57 | cli.help() | |
| 58 | cli.parse() | |