ReviewOS

also looking at this

stacks/ts-css

Merge @stacksjs/ts-css into this repo as packages/toolkit

#27
Merged glennmichael123 wants to merge merge/ts-css-toolkit into main
128 files +9797 -201
packages/toolkit/bin/cli.tsadded+58-0
Changes to packages/toolkit/bin/cli.ts
@@ -0,0 +1,58 @@
1#!/usr/bin/env bun
2/**
3 * ts-css CLI — minify, parse, or round-trip CSS files.
4 */
5
6import { CLI } from '@stacksjs/clapp'
7import { existsSync, readFileSync } from 'node:fs'
8import { minify, minifyBlock } from '../src/optimize'
9import { generate, parse } from '../src/parse'
10import { version } from '../package.json'
11
12function 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
20const cli = new CLI('ts-css')
21
22cli
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
35cli
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
44cli
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
52cli.command('version', 'Show the version of the CLI').action(() => {
53 console.log(version)
54})
55
56cli.version(version)
57cli.help()
58cli.parse()