|
1 | 1 | #!/usr/bin/env node
|
2 | 2 |
|
3 |
| -import { existsSync } from "node:fs"; |
4 |
| -import { mkdir, writeFile } from "node:fs/promises"; |
5 |
| -import path from "node:path"; |
| 3 | +import { createCssTypedCommand } from "./cli/command-utils.ts"; |
| 4 | +import { generate } from "./commands/generate.ts"; |
| 5 | +import { version } from "./version.ts"; |
6 | 6 |
|
7 |
| -import { Command, Option } from "@commander-js/extra-typings"; |
8 |
| -import { glob } from "glob"; |
9 |
| - |
10 |
| -import { loadFileConfig } from "./config.ts"; |
11 |
| -import { dtsPath, generateDeclaration } from "./logic.js"; |
12 |
| -import type { Options } from "./options.ts"; |
13 |
| -import { localsConventionChoices } from "./options.ts"; |
14 |
| - |
15 |
| -declare let VERSION: string; // Defined by esbuild |
16 |
| -const version = VERSION; |
17 |
| - |
18 |
| -await new Command() |
| 7 | +await createCssTypedCommand() |
19 | 8 | .name(`css-typed`)
|
20 |
| - .description(`TypeScript declaration generator for CSS files.`) |
21 |
| - .version(version) |
22 |
| - .argument(`[pattern]`, `Glob path for CSS files to target.`) |
23 |
| - .option(`-c, --config <file>`, `Custom path to the configuration file.`) |
24 |
| - .addOption( |
25 |
| - new Option( |
26 |
| - `--localsConvention <value>`, |
27 |
| - `Style of exported classnames. See https://github.com/connorjs/css-typed/tree/v${version}#localsConvention`, |
28 |
| - ) |
29 |
| - .choices(localsConventionChoices) |
30 |
| - .default(`dashesOnly` as const), |
| 9 | + .description( |
| 10 | + `TypeScript declaration generator for CSS files (and other stylesheets).`, |
31 | 11 | )
|
32 |
| - .option( |
33 |
| - `-o, --outdir <outDirectory>`, |
34 |
| - `Root directory for generated CSS declaration files.`, |
35 |
| - ) |
36 |
| - .action(async function ( |
37 |
| - cliPattern, |
38 |
| - { config: cliConfigPath, ...cliOptions }, |
39 |
| - program, |
40 |
| - ) { |
41 |
| - // Load file configuration first |
42 |
| - const configResult = await loadFileConfig(cliConfigPath); |
43 |
| - if (configResult?.filepath) { |
44 |
| - // We loaded the file |
45 |
| - console.debug( |
46 |
| - `[debug] Reading configuration from ${configResult.filepath}.`, |
47 |
| - ); |
48 |
| - } else if (cliConfigPath) { |
49 |
| - // We did not load the file, but we expected to with `-c/--config`, so error |
50 |
| - return program.error(`[error] Failed to parse ${cliConfigPath}.`); |
51 |
| - } |
52 |
| - |
53 |
| - // Remove pattern argument from file config, if present. |
54 |
| - const { pattern: filePattern, ...fileConfig } = configResult?.config ?? {}; |
55 |
| - |
56 |
| - // Resolve options from file config and CLI. CLI overrides file config. |
57 |
| - const options: Options = { ...fileConfig, ...cliOptions }; |
58 |
| - |
59 |
| - // Pattern is required. CLI overrides file config. |
60 |
| - const pattern = cliPattern ?? filePattern; |
61 |
| - if (!pattern) { |
62 |
| - // Match commander error message |
63 |
| - return program.error(`[error] Missing required argument 'pattern'`); |
64 |
| - } |
65 |
| - |
66 |
| - // Find the files and process each. |
67 |
| - const files = await glob(pattern); |
68 |
| - |
69 |
| - const time = new Date().toISOString(); |
70 |
| - await Promise.all( |
71 |
| - files.map((file) => |
72 |
| - generateDeclaration(file, time, options).then((ts) => |
73 |
| - writeDeclarationFile(file, options.outdir, ts), |
74 |
| - ), |
75 |
| - ), |
76 |
| - ); |
77 |
| - }) |
| 12 | + .version(version) |
| 13 | + .cssTypedAction(generate) |
78 | 14 | .parseAsync();
|
79 |
| - |
80 |
| -/** |
81 |
| - * Writes the TypeScript declaration content to file. Handles the output path. |
82 |
| - * |
83 |
| - * @param file - Path to the original stylesheet file. NOT the path to write. |
84 |
| - * @param outdir - Output directory to which to write. |
85 |
| - * @param ts - The TypeScript declaration content to write. |
86 |
| - * @returns Empty promise indicating when writing has completed. |
87 |
| - */ |
88 |
| -async function writeDeclarationFile( |
89 |
| - file: string, |
90 |
| - outdir: string | undefined, |
91 |
| - ts: string | undefined, |
92 |
| -) { |
93 |
| - if (!ts) { |
94 |
| - return undefined; |
95 |
| - } |
96 |
| - |
97 |
| - const [directoryToWrite, fileToWrite] = dtsPath(file, outdir); |
98 |
| - if (!existsSync(directoryToWrite)) { |
99 |
| - await mkdir(directoryToWrite, { recursive: true }); |
100 |
| - } |
101 |
| - |
102 |
| - const pathToWrite = path.join(directoryToWrite, fileToWrite); |
103 |
| - await writeFile(pathToWrite, ts, { encoding: `utf8` }); |
104 |
| -} |
0 commit comments