|
2 | 2 |
|
3 | 3 | import { writeFile } from "node:fs/promises";
|
4 | 4 |
|
| 5 | +import { Command } from "@commander-js/extra-typings"; |
5 | 6 | import { glob } from "glob";
|
6 | 7 |
|
7 | 8 | import { dtsPath, generateDeclaration } from "./logic.js";
|
8 |
| - |
9 |
| -/* globals process -- Node/CLI tool */ |
10 |
| -await main(process.argv[2], process.argv[3] === `--dashes`); |
11 |
| -// See https://github.com/connorjs/css-typed/issues/5 for "proper" CLI arg handling |
12 |
| - |
13 |
| -async function main(pattern, dashesEnabled) { |
14 |
| - if (!pattern) { |
15 |
| - console.error(`Expected glob pattern`); |
16 |
| - process.exit(2); |
17 |
| - } |
18 |
| - const options = dashesEnabled ? { localsConvention: `dashes` } : {}; |
19 |
| - |
20 |
| - const files = await glob(pattern); |
21 |
| - |
22 |
| - const time = new Date().toISOString(); |
23 |
| - const results = await Promise.all( |
24 |
| - files.map((file) => |
25 |
| - generateDeclaration(file, time, options).then((ts) => |
26 |
| - writeDeclarationFile(file, ts), |
| 9 | +import { version } from "./version.js"; |
| 10 | + |
| 11 | +await new Command() |
| 12 | + .name(`css-typed`) |
| 13 | + .description(`TypeScript declaration generator for CSS files.`) |
| 14 | + .version(version) |
| 15 | + .argument(`<pattern>`, `Glob path for CSS files to target.`) |
| 16 | + .option( |
| 17 | + `--dashes`, |
| 18 | + `Transform kebab-case classes (dashed names) to camelCase.`, |
| 19 | + false, |
| 20 | + ) |
| 21 | + .action(async function (pattern, options, program) { |
| 22 | + const declarationOptions = options.dashes |
| 23 | + ? { localsConvention: `dashes` } |
| 24 | + : {}; |
| 25 | + |
| 26 | + const files = await glob(pattern); |
| 27 | + |
| 28 | + const time = new Date().toISOString(); |
| 29 | + const results = await Promise.all( |
| 30 | + files.map((file) => |
| 31 | + generateDeclaration(file, time, declarationOptions).then((ts) => |
| 32 | + writeDeclarationFile(file, ts), |
| 33 | + ), |
27 | 34 | ),
|
28 |
| - ), |
29 |
| - ); |
30 |
| - |
31 |
| - const errors = results.filter(Boolean); |
32 |
| - if (errors.length > 0) { |
33 |
| - console.error(`Errors encountered`, errors); |
34 |
| - process.exit(3); |
35 |
| - } |
36 |
| - |
37 |
| - process.exit(0); |
38 |
| -} |
| 35 | + ); |
| 36 | + |
| 37 | + const errors = results.filter(Boolean); |
| 38 | + if (errors.length > 0) { |
| 39 | + program.error(`Errors encountered: ${errors}`); |
| 40 | + } |
| 41 | + }) |
| 42 | + .parseAsync(); |
39 | 43 |
|
40 | 44 | /**
|
41 | 45 | * Writes the TypeScript declaration content to file. Handles the output path.
|
|
0 commit comments