@xessa/cli 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -2
  3. package/dist/args.d.ts +43 -0
  4. package/dist/args.d.ts.map +1 -0
  5. package/dist/args.js +202 -0
  6. package/dist/args.js.map +1 -0
  7. package/dist/commands/build.d.ts +44 -0
  8. package/dist/commands/build.d.ts.map +1 -0
  9. package/dist/commands/build.js +533 -0
  10. package/dist/commands/build.js.map +1 -0
  11. package/dist/commands/check.d.ts +11 -0
  12. package/dist/commands/check.d.ts.map +1 -0
  13. package/dist/commands/check.js +35 -0
  14. package/dist/commands/check.js.map +1 -0
  15. package/dist/commands/clean.d.ts +12 -0
  16. package/dist/commands/clean.d.ts.map +1 -0
  17. package/dist/commands/clean.js +140 -0
  18. package/dist/commands/clean.js.map +1 -0
  19. package/dist/commands/format.d.ts +41 -0
  20. package/dist/commands/format.d.ts.map +1 -0
  21. package/dist/commands/format.js +127 -0
  22. package/dist/commands/format.js.map +1 -0
  23. package/dist/commands/watch.d.ts +38 -0
  24. package/dist/commands/watch.d.ts.map +1 -0
  25. package/dist/commands/watch.js +338 -0
  26. package/dist/commands/watch.js.map +1 -0
  27. package/dist/compiler.d.ts +20 -0
  28. package/dist/compiler.d.ts.map +1 -0
  29. package/dist/compiler.js +57 -0
  30. package/dist/compiler.js.map +1 -0
  31. package/dist/config.d.ts +93 -0
  32. package/dist/config.d.ts.map +1 -0
  33. package/dist/config.js +331 -0
  34. package/dist/config.js.map +1 -0
  35. package/dist/formatter.d.ts +21 -0
  36. package/dist/formatter.d.ts.map +1 -0
  37. package/dist/formatter.js +38 -0
  38. package/dist/formatter.js.map +1 -0
  39. package/dist/glob.d.ts +52 -0
  40. package/dist/glob.d.ts.map +1 -0
  41. package/dist/glob.js +214 -0
  42. package/dist/glob.js.map +1 -0
  43. package/dist/index.d.ts +34 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +114 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/manifest.d.ts +56 -0
  48. package/dist/manifest.d.ts.map +1 -0
  49. package/dist/manifest.js +138 -0
  50. package/dist/manifest.js.map +1 -0
  51. package/dist/paths.d.ts +24 -0
  52. package/dist/paths.d.ts.map +1 -0
  53. package/dist/paths.js +42 -0
  54. package/dist/paths.js.map +1 -0
  55. package/dist/project.d.ts +44 -0
  56. package/dist/project.d.ts.map +1 -0
  57. package/dist/project.js +92 -0
  58. package/dist/project.js.map +1 -0
  59. package/dist/report.d.ts +128 -0
  60. package/dist/report.d.ts.map +1 -0
  61. package/dist/report.js +128 -0
  62. package/dist/report.js.map +1 -0
  63. package/dist/setup.d.ts +58 -0
  64. package/dist/setup.d.ts.map +1 -0
  65. package/dist/setup.js +181 -0
  66. package/dist/setup.js.map +1 -0
  67. package/dist/writer.d.ts +32 -0
  68. package/dist/writer.d.ts.map +1 -0
  69. package/dist/writer.js +93 -0
  70. package/dist/writer.js.map +1 -0
  71. package/package.json +40 -4
  72. package/src/args.ts +246 -0
  73. package/src/commands/build.ts +682 -0
  74. package/src/commands/check.ts +45 -0
  75. package/src/commands/clean.ts +168 -0
  76. package/src/commands/format.ts +169 -0
  77. package/src/commands/watch.ts +366 -0
  78. package/src/compiler-api.d.ts +261 -0
  79. package/src/compiler.ts +65 -0
  80. package/src/config.ts +443 -0
  81. package/src/formatter.ts +56 -0
  82. package/src/glob.ts +230 -0
  83. package/src/index.ts +137 -0
  84. package/src/manifest.ts +176 -0
  85. package/src/paths.ts +50 -0
  86. package/src/project.ts +122 -0
  87. package/src/report.ts +230 -0
  88. package/src/setup.ts +218 -0
  89. package/src/writer.ts +102 -0
package/src/config.ts ADDED
@@ -0,0 +1,443 @@
1
+ /**
2
+ * `xessa.config.json` discovery, loading and schema validation.
3
+ *
4
+ * The document format is frozen by SPECIFICATION.md §10. Keys are camelCase in
5
+ * JSON while source `@setup` keys stay kebab-case:
6
+ *
7
+ * | `@setup` key | JSON key | Where |
8
+ * |---|---|---|
9
+ * | `prefix` | `classes.prefix` | `classes` |
10
+ * | `css-dir` | `output.cssDir` | `output` |
11
+ * | `css-name` | `output.cssName` | `output` |
12
+ * | `map-dir` | `output.mapDir` | `output` |
13
+ * | `map-name` | `output.mapName` | `output` |
14
+ * | `emit-css` | `output.emitCss` | `output` |
15
+ * | `emit-map` | `output.emitMap` | `output` |
16
+ * | `map-types` | `output.mapTypes` | `output` |
17
+ * | `emit-types` | `output.emitTypes` | `output` |
18
+ * | `source-map` | `output.sourceMap` | `output` |
19
+ * | `minify` | `output.minify` | `output` |
20
+ * | `map-inherit` | `output.mapInherit` | `output` |
21
+ * | `import-css` | `output.importCss` | `output` |
22
+ * | `media-grouping` | `compiler.mediaGrouping` | `compiler` |
23
+ *
24
+ * `output.prefix` is deliberately rejected: class naming lives under `classes`
25
+ * so strategy and prefix stay together.
26
+ *
27
+ * Unknown keys are rejected (exit 2) by default; an explicit
28
+ * `compiler.strict: false` downgrades them to warnings. Known keys are always
29
+ * type-checked.
30
+ *
31
+ * `include` defaults to {@link DEFAULT_INCLUDE} (`**\/*.cssx`), so a configuration
32
+ * file is only needed to change a default or scope discovery. `exclude` stays
33
+ * empty by default: imported modules declare themselves with
34
+ * `@setup { module: true; }` rather than appearing in a project-wide list.
35
+ */
36
+
37
+ import { readFile, stat } from "node:fs/promises";
38
+ import type {
39
+ CompilerConfig,
40
+ MediaGroupingMode,
41
+ NamingOptions,
42
+ NamingStrategy,
43
+ OutputOptions,
44
+ } from "@xessa/compiler";
45
+ import { absolutePath, dirnamePosix, toPosix } from "./paths.js";
46
+
47
+ /** Entry globs used when a project does not configure `include`. */
48
+ export const DEFAULT_INCLUDE: readonly string[] = Object.freeze(["**/*.cssx"]);
49
+
50
+ /** Validated project configuration with its resolved location. */
51
+ export interface CliConfig {
52
+ /** Absolute POSIX path of the configuration file. */
53
+ readonly file: string;
54
+ /** Absolute POSIX workspace root: the directory containing the configuration file. */
55
+ readonly root: string;
56
+ /** Include globs used for project entry discovery; defaults to {@link DEFAULT_INCLUDE}. */
57
+ readonly include: readonly string[];
58
+ /** Exclude globs and directory names removed from discovery. Default `[]`. */
59
+ readonly exclude: readonly string[];
60
+ /** Project-level output settings, resolved against the workspace root. */
61
+ readonly output?: OutputOptions;
62
+ /** Project-level class naming strategy and prefix. */
63
+ readonly classes?: NamingOptions;
64
+ /** Project-level media alias overrides merged after built-in defaults. */
65
+ readonly media?: Readonly<Record<string, string>>;
66
+ /** Compiler behaviour settings such as strict mode and media grouping. */
67
+ readonly compiler?: CompilerConfig;
68
+ }
69
+
70
+ /** Request for {@link loadConfig}. */
71
+ export interface LoadConfigRequest {
72
+ /** Working directory used to resolve an explicit relative `--config` path. */
73
+ readonly cwd: string;
74
+ /** Directory to start the nearest-config walk from. */
75
+ readonly startDir: string;
76
+ /** Explicit `--config` path, absolute or relative to `cwd`. */
77
+ readonly explicitPath?: string;
78
+ }
79
+
80
+ /** Outcome of loading a configuration file. */
81
+ export type ConfigLoadOutcome =
82
+ | {
83
+ readonly ok: true;
84
+ /** Parsed configuration, or `null` when no configuration file was found. */
85
+ readonly config: CliConfig | null;
86
+ /** Non-fatal validation notes (ignored unknown keys outside strict mode). */
87
+ readonly warnings: readonly string[];
88
+ }
89
+ | {
90
+ readonly ok: false;
91
+ /** User-facing explanation; callers report it and exit 2. */
92
+ readonly message: string;
93
+ };
94
+
95
+ interface ConfigDocument {
96
+ readonly config?: CliConfig;
97
+ readonly errors: readonly string[];
98
+ readonly warnings: readonly string[];
99
+ }
100
+
101
+ const TOP_LEVEL_KEYS: ReadonlySet<string> = new Set([
102
+ "$schema",
103
+ "include",
104
+ "exclude",
105
+ "output",
106
+ "classes",
107
+ "media",
108
+ "compiler",
109
+ ]);
110
+
111
+ const OUTPUT_KEYS: ReadonlySet<string> = new Set([
112
+ "cssDir",
113
+ "cssName",
114
+ "mapDir",
115
+ "mapName",
116
+ "emitCss",
117
+ "emitMap",
118
+ "mapTypes",
119
+ "emitTypes",
120
+ "sourceMap",
121
+ "minify",
122
+ "mapInherit",
123
+ "importCss",
124
+ ]);
125
+
126
+ const CLASSES_KEYS: ReadonlySet<string> = new Set(["strategy", "prefix"]);
127
+ const COMPILER_KEYS: ReadonlySet<string> = new Set(["strict", "mediaGrouping"]);
128
+
129
+ /** Walk up from `startDir` to the filesystem root for the nearest config file. */
130
+ export async function findConfigPath(startDir: string): Promise<string | null> {
131
+ let current = toPosix(startDir);
132
+ for (;;) {
133
+ const candidate = `${current === "/" ? "" : current}/xessa.config.json`;
134
+ if (await isFile(candidate)) return candidate;
135
+ const parent = dirnamePosix(current);
136
+ if (parent === current) return null;
137
+ current = parent;
138
+ }
139
+ }
140
+
141
+ /** Walk up for the nearest `xessa.config.json`, else `package.json`, else `startDir`. */
142
+ export async function findWorkspaceRoot(startDir: string): Promise<string> {
143
+ const config = await findConfigPath(startDir);
144
+ if (config !== null) return dirnamePosix(config);
145
+
146
+ let current = toPosix(startDir);
147
+ for (;;) {
148
+ if (await isFile(`${current === "/" ? "" : current}/package.json`)) return current;
149
+ const parent = dirnamePosix(current);
150
+ if (parent === current) return toPosix(startDir);
151
+ current = parent;
152
+ }
153
+ }
154
+
155
+ /** Load and validate `xessa.config.json`, resolving the nearest file when not explicit. */
156
+ export async function loadConfig(request: LoadConfigRequest): Promise<ConfigLoadOutcome> {
157
+ const explicit =
158
+ request.explicitPath === undefined
159
+ ? undefined
160
+ : absolutePath(request.cwd, request.explicitPath);
161
+ const configPath = explicit ?? (await findConfigPath(request.startDir));
162
+
163
+ if (configPath === null) return { ok: true, config: null, warnings: [] };
164
+
165
+ if (!(await isFile(configPath))) {
166
+ return {
167
+ ok: false,
168
+ message:
169
+ explicit === undefined
170
+ ? `Configuration file could not be read: ${configPath}`
171
+ : `Configuration file not found: ${request.explicitPath ?? configPath}`,
172
+ };
173
+ }
174
+
175
+ let text: string;
176
+ try {
177
+ text = await readFile(configPath, "utf8");
178
+ } catch (error) {
179
+ return { ok: false, message: `Could not read ${configPath}: ${describeError(error)}` };
180
+ }
181
+
182
+ const document = parseConfigDocument(text, configPath);
183
+ if (document.config === undefined || document.errors.length > 0) {
184
+ return { ok: false, message: document.errors.join("\n") };
185
+ }
186
+
187
+ return { ok: true, config: document.config, warnings: document.warnings };
188
+ }
189
+
190
+ /** Parse and validate one configuration document. */
191
+ export function parseConfigDocument(text: string, file: string): ConfigDocument {
192
+ let value: unknown;
193
+ try {
194
+ value = JSON.parse(text);
195
+ } catch (error) {
196
+ return { errors: [`${file}: invalid JSON: ${describeError(error)}`], warnings: [] };
197
+ }
198
+
199
+ if (!isRecord(value)) {
200
+ return { errors: [`${file}: expected a JSON object at the top level.`], warnings: [] };
201
+ }
202
+
203
+ const errors: string[] = [];
204
+ const warnings: string[] = [];
205
+ const compilerValue = value.compiler;
206
+ // Strict is the default; only an explicit `false` downgrades unknown keys.
207
+ const strict = !(isRecord(compilerValue) && compilerValue.strict === false);
208
+
209
+ reportUnknownKeys(value, TOP_LEVEL_KEYS, "", strict, errors, warnings);
210
+
211
+ const include = readStringArray(value.include, "include", errors) ?? DEFAULT_INCLUDE;
212
+ const exclude = readStringArray(value.exclude, "exclude", errors) ?? [];
213
+ const output = readOutput(value.output, strict, errors, warnings);
214
+ const classes = readClasses(value.classes, strict, errors, warnings);
215
+ const media = readMedia(value.media, errors);
216
+ const compiler = readCompiler(value.compiler, strict, errors, warnings) ?? {
217
+ strict: true,
218
+ mediaGrouping: "safe",
219
+ };
220
+
221
+ if (errors.length > 0) return { errors, warnings };
222
+
223
+ return {
224
+ config: {
225
+ file,
226
+ root: dirnamePosix(file),
227
+ include,
228
+ exclude,
229
+ ...(output === undefined ? {} : { output }),
230
+ ...(classes === undefined ? {} : { classes }),
231
+ ...(media === undefined ? {} : { media }),
232
+ compiler,
233
+ },
234
+ errors,
235
+ warnings,
236
+ };
237
+ }
238
+
239
+ function readOutput(
240
+ value: unknown,
241
+ strict: boolean,
242
+ errors: string[],
243
+ warnings: string[],
244
+ ): OutputOptions | undefined {
245
+ if (value === undefined) return undefined;
246
+ if (!isRecord(value)) {
247
+ errors.push(`"output" must be an object.`);
248
+ return undefined;
249
+ }
250
+ reportUnknownKeys(value, OUTPUT_KEYS, "output", strict, errors, warnings);
251
+ const output: Record<string, string | boolean> = {};
252
+ assignString(output, value, "cssDir", "output", errors);
253
+ assignString(output, value, "cssName", "output", errors);
254
+ assignString(output, value, "mapDir", "output", errors);
255
+ assignString(output, value, "mapName", "output", errors);
256
+ assignBoolean(output, value, "emitCss", "output", errors);
257
+ assignBoolean(output, value, "emitMap", "output", errors);
258
+ assignBoolean(output, value, "mapTypes", "output", errors);
259
+ assignBoolean(output, value, "emitTypes", "output", errors);
260
+ assignBoolean(output, value, "sourceMap", "output", errors);
261
+ assignBoolean(output, value, "minify", "output", errors);
262
+ assignBoolean(output, value, "mapInherit", "output", errors);
263
+ assignBoolean(output, value, "importCss", "output", errors);
264
+ return output as OutputOptions;
265
+ }
266
+
267
+ function readClasses(
268
+ value: unknown,
269
+ strict: boolean,
270
+ errors: string[],
271
+ warnings: string[],
272
+ ): NamingOptions | undefined {
273
+ if (value === undefined) return undefined;
274
+ if (!isRecord(value)) {
275
+ errors.push(`"classes" must be an object.`);
276
+ return undefined;
277
+ }
278
+ reportUnknownKeys(value, CLASSES_KEYS, "classes", strict, errors, warnings);
279
+
280
+ const strategyValue = value.strategy;
281
+ let strategy: NamingStrategy | undefined;
282
+ if (strategyValue === undefined) {
283
+ strategy = "prefix";
284
+ } else if (strategyValue === "none" || strategyValue === "prefix") {
285
+ strategy = strategyValue;
286
+ } else {
287
+ errors.push(`"classes.strategy" must be "prefix" or "none".`);
288
+ }
289
+
290
+ const prefixValue = value.prefix;
291
+ if (prefixValue !== undefined && typeof prefixValue !== "string") {
292
+ errors.push(`"classes.prefix" must be a string.`);
293
+ }
294
+
295
+ if (strategy === undefined) return undefined;
296
+ return {
297
+ strategy,
298
+ ...(typeof prefixValue === "string" ? { prefix: prefixValue } : {}),
299
+ };
300
+ }
301
+
302
+ function readMedia(value: unknown, errors: string[]): Readonly<Record<string, string>> | undefined {
303
+ if (value === undefined) return undefined;
304
+ if (!isRecord(value)) {
305
+ errors.push(`"media" must be an object mapping alias names to media queries.`);
306
+ return undefined;
307
+ }
308
+ const media: Record<string, string> = {};
309
+ for (const [key, query] of Object.entries(value)) {
310
+ if (key.trim() === "") {
311
+ errors.push(`"media" keys must be non-empty alias names.`);
312
+ continue;
313
+ }
314
+ if (typeof query !== "string" || query.trim() === "") {
315
+ errors.push(`"media.${key}" must be a non-empty media query string.`);
316
+ continue;
317
+ }
318
+ media[key] = query;
319
+ }
320
+ return media;
321
+ }
322
+
323
+ function readCompiler(
324
+ value: unknown,
325
+ strict: boolean,
326
+ errors: string[],
327
+ warnings: string[],
328
+ ): CompilerConfig | undefined {
329
+ if (value === undefined) return undefined;
330
+ if (!isRecord(value)) {
331
+ errors.push(`"compiler" must be an object.`);
332
+ return undefined;
333
+ }
334
+ reportUnknownKeys(value, COMPILER_KEYS, "compiler", strict, errors, warnings);
335
+
336
+ const strictValue = value.strict;
337
+ if (strictValue !== undefined && typeof strictValue !== "boolean") {
338
+ errors.push(`"compiler.strict" must be a boolean.`);
339
+ }
340
+
341
+ const groupingValue = value.mediaGrouping;
342
+ let mediaGrouping: MediaGroupingMode = "safe";
343
+ if (groupingValue !== undefined) {
344
+ if (groupingValue === "safe" || groupingValue === "none" || groupingValue === "merge") {
345
+ mediaGrouping = groupingValue;
346
+ } else {
347
+ errors.push(`"compiler.mediaGrouping" must be "safe", "none" or "merge".`);
348
+ }
349
+ }
350
+
351
+ return {
352
+ strict: typeof strictValue === "boolean" ? strictValue : true,
353
+ mediaGrouping,
354
+ };
355
+ }
356
+
357
+ function reportUnknownKeys(
358
+ value: Record<string, unknown>,
359
+ allowed: ReadonlySet<string>,
360
+ path: string,
361
+ strict: boolean,
362
+ errors: string[],
363
+ warnings: string[],
364
+ ): void {
365
+ for (const key of Object.keys(value)) {
366
+ if (allowed.has(key)) continue;
367
+ const location = path === "" ? key : `${path}.${key}`;
368
+ if (strict) {
369
+ errors.push(`Unknown configuration key "${location}" (strict mode).`);
370
+ } else {
371
+ warnings.push(`Ignoring unknown configuration key "${location}".`);
372
+ }
373
+ }
374
+ }
375
+
376
+ function readStringArray(
377
+ value: unknown,
378
+ key: string,
379
+ errors: string[],
380
+ ): readonly string[] | undefined {
381
+ if (value === undefined) return undefined;
382
+ if (!Array.isArray(value)) {
383
+ errors.push(`"${key}" must be an array of glob strings.`);
384
+ return undefined;
385
+ }
386
+ const result: string[] = [];
387
+ for (const entry of value) {
388
+ if (typeof entry !== "string" || entry.trim() === "") {
389
+ errors.push(`"${key}" entries must be non-empty strings.`);
390
+ continue;
391
+ }
392
+ result.push(entry);
393
+ }
394
+ return result;
395
+ }
396
+
397
+ function assignString(
398
+ target: Record<string, string | boolean>,
399
+ source: Record<string, unknown>,
400
+ key: string,
401
+ path: string,
402
+ errors: string[],
403
+ ): void {
404
+ const value = source[key];
405
+ if (value === undefined) return;
406
+ if (typeof value !== "string") {
407
+ errors.push(`"${path}.${key}" must be a string.`);
408
+ return;
409
+ }
410
+ target[key] = value;
411
+ }
412
+
413
+ function assignBoolean(
414
+ target: Record<string, string | boolean>,
415
+ source: Record<string, unknown>,
416
+ key: string,
417
+ path: string,
418
+ errors: string[],
419
+ ): void {
420
+ const value = source[key];
421
+ if (value === undefined) return;
422
+ if (typeof value !== "boolean") {
423
+ errors.push(`"${path}.${key}" must be a boolean.`);
424
+ return;
425
+ }
426
+ target[key] = value;
427
+ }
428
+
429
+ function isRecord(value: unknown): value is Record<string, unknown> {
430
+ return typeof value === "object" && value !== null && !Array.isArray(value);
431
+ }
432
+
433
+ async function isFile(path: string): Promise<boolean> {
434
+ try {
435
+ return (await stat(path)).isFile();
436
+ } catch {
437
+ return false;
438
+ }
439
+ }
440
+
441
+ function describeError(error: unknown): string {
442
+ return error instanceof Error ? error.message : String(error);
443
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Compiler formatter boundary for the CLI.
3
+ *
4
+ * `format` is a pure compiler module exported from the public barrel. The CLI
5
+ * reaches it through the shared {@link loadCompiler} boundary (bare package →
6
+ * built `dist` → TypeScript source under Bun), so there is no separate module
7
+ * resolution path. The export is validated before use so a stale compiler build
8
+ * fails with an actionable message instead of a call-site `TypeError`.
9
+ */
10
+
11
+ import type { Diagnostic } from "@xessa/compiler";
12
+ import { loadCompiler } from "./compiler.js";
13
+
14
+ /** Formatter module surface the CLI consumes. */
15
+ export interface FormatterApi {
16
+ /** Canonical formatting; errors return the original source unchanged. */
17
+ format(
18
+ source: string,
19
+ file: string,
20
+ ): { readonly formatted: string; readonly diagnostics: readonly Diagnostic[] };
21
+ }
22
+
23
+ let cached: Promise<FormatterApi> | undefined;
24
+
25
+ /** Load (and cache) the compiler formatter through the public API boundary. */
26
+ export function loadFormatter(): Promise<FormatterApi> {
27
+ cached ??= resolveFormatter();
28
+ return cached;
29
+ }
30
+
31
+ async function resolveFormatter(): Promise<FormatterApi> {
32
+ const compiler = await loadCompiler();
33
+ const format = (compiler as unknown as { format?: unknown }).format;
34
+ if (typeof format === "function") {
35
+ return { format: format as FormatterApi["format"] };
36
+ }
37
+
38
+ // Development fallback: a stale checked-out `dist` may predate the barrel
39
+ // export. Under Bun the TypeScript formatter source is always loadable.
40
+ if (typeof (globalThis as { Bun?: unknown }).Bun !== "undefined") {
41
+ try {
42
+ const loaded = (await import(
43
+ new URL("../../compiler/src/formatter/index.ts", import.meta.url).href
44
+ )) as { format?: unknown };
45
+ if (typeof loaded.format === "function") {
46
+ return { format: loaded.format as FormatterApi["format"] };
47
+ }
48
+ } catch {
49
+ // Fall through to the actionable error below.
50
+ }
51
+ }
52
+
53
+ throw new Error(
54
+ 'Unable to load the Xessa formatter: the compiler build does not export "format". Rebuild it with "bun run build" in packages/compiler.',
55
+ );
56
+ }