@znight/cli 0.1.3 → 0.1.4

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.
package/README.md CHANGED
@@ -136,8 +136,9 @@ my-service/
136
136
 
137
137
  ## `znight generate`
138
138
 
139
- Runs the compiler and writes `src/.znight/container.generated.ts` when its content changed. Compiler
140
- diagnostics are printed with code frames; any error exits with code 1 (and nothing is written).
139
+ Runs the compiler and writes `src/.znight/container.generated.ts` when its content changed, with the
140
+ extra files compiler extensions emit next to it (an SDL schema...). Compiler diagnostics are printed
141
+ with code frames; any error exits with code 1 (and nothing is written).
141
142
 
142
143
  ```sh
143
144
  znight generate # ✓ Generated src/.znight/container.generated.ts (12 providers, 30 files, 48ms)
@@ -196,8 +197,9 @@ npx tsc --noEmit
196
197
  npx znight build
197
198
  ```
198
199
 
199
- `--check` names every generated file that is missing or out of date: the module and, in library
200
- mode (`library: true`), `dist/znight.providers.json`. A build that cleans `dist/` (tsdown, `tsc --build
200
+ `--check` names every generated file that is missing or out of date: the module, the extra files of
201
+ the compiler extensions (`extraFiles`) and, in library mode (`library: true`),
202
+ `dist/znight.providers.json`. A build that cleans `dist/` (tsdown, `tsc --build
201
203
  --clean`...) deletes that manifest, which `package.json` points to: run `znight generate` after such a
202
204
  build (the Vite plugin writes it itself at the end of `vite build`).
203
205
 
@@ -457,16 +459,16 @@ and the schematics are exported too.
457
459
  The CLI drives the project's `@znight/compiler` through its programmatic API (spec §6.2) and only
458
460
  relies on these shapes (each is optional; missing pieces are emulated):
459
461
 
460
- | Export | Used for |
461
- | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
462
- | `generate(options)` | `znight generate`; `written: string[]` tells whether the file changed |
463
- | `generate({ ...options, check })` | `znight generate --check`; `stale: boolean`, `staleFiles: string[]` (else compared with `diskCode` / `library`) |
464
- | `createCompiler(options)` | `znight generate --watch`: `generate(changes)` (`{ changed, added, removed }`), `isOutFile(file)`, `watchFiles` |
465
- | `includeRoots(compiler)` | `--watch`: the directories to watch recursively (else the whole project) |
466
- | `isRelevantChange(compiler, kind, file, watched, roots)` | `--watch`: the file events that trigger a compile (else every event but the generated module) |
467
- | `compile(options)` | fallback of the above (writes `diskCode`, else `code`) |
468
- | `formatDiagnostics(diagnostics, { color, cwd, summary: false })` | diagnostics output (a plain formatter otherwise) |
469
- | `loadZnightConfig(root)` | `znight generate`: `{ file, options }` of `znight.config.*` / `ZNIGHT_CONFIG` |
462
+ | Export | Used for |
463
+ | ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
464
+ | `generate(options)` | `znight generate`; `written: string[]` tells whether the file changed |
465
+ | `generate({ ...options, check })` | `znight generate --check`; `stale: boolean`, `staleFiles: string[]` (else compared with `diskCode` / `extraFiles` / `library`) |
466
+ | `createCompiler(options)` | `znight generate --watch`: `generate(changes)` (`{ changed, added, removed }`), `isOutFile(file)`, `watchFiles` |
467
+ | `includeRoots(compiler)` | `--watch`: the directories to watch recursively (else the whole project) |
468
+ | `isRelevantChange(compiler, kind, file, watched, roots)` | `--watch`: the file events that trigger a compile (else every event but the generated module) |
469
+ | `compile(options)` | fallback of the above (writes `diskCode`, else `code`, and `extraFiles`) |
470
+ | `formatDiagnostics(diagnostics, { color, cwd, summary: false })` | diagnostics output (a plain formatter otherwise) |
471
+ | `loadZnightConfig(root)` | `znight generate`: `{ file, options }` of `znight.config.*` / `ZNIGHT_CONFIG` |
470
472
 
471
473
  `znight build` and `znight dev` use the `znight()` Vite plugin of the project's Vite config, found by
472
474
  its name (`znight`) among the plugins of the config:
package/dist/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { n as main } from "./main-BWsiUsxN.mjs";
2
+ import { n as main } from "./main-DUyElIVZ.mjs";
3
3
  // src/cli.ts
4
4
  /**
5
5
  * The `znight` binary. Everything lives in `main()`; this file only wires the process: arguments,
package/dist/index.d.mts CHANGED
@@ -36,6 +36,12 @@ interface ZnightOptions {
36
36
  readonly aliases?: readonly StringAliasLike[] | undefined;
37
37
  readonly [option: string]: unknown;
38
38
  }
39
+ /** A file emitted by a compiler extension next to the generated module (`CompileResult.extraFiles`, §6.2). */
40
+ interface ExtraFileLike {
41
+ /** Absolute path. */
42
+ readonly file: string;
43
+ readonly content: string;
44
+ }
39
45
  /** What the CLI reads from a compile result (`CompileResult`, §6.2). */
40
46
  interface CompileResultLike {
41
47
  /** Content of the generated module (with errors: a module throwing them, or empty). */
@@ -50,6 +56,8 @@ interface CompileResultLike {
50
56
  } | undefined;
51
57
  /** Files the result depends on (`createCompiler()` results). */
52
58
  readonly watchFiles?: readonly string[] | undefined;
59
+ /** Files emitted by compiler extensions (an SDL schema...), absent from compilers without them. */
60
+ readonly extraFiles?: readonly ExtraFileLike[] | undefined;
53
61
  }
54
62
  /** Result of {@link CompilerApi.generate}. */
55
63
  interface GenerateResultLike extends CompileResultLike {
@@ -61,9 +69,9 @@ interface CheckResultLike extends CompileResultLike {
61
69
  /** The generated files on disk are missing or differ from what would be generated. */
62
70
  readonly stale: boolean;
63
71
  /**
64
- * The generated files that are missing or out of date (the module, and `znight.providers.json` in
65
- * library mode). Empty when `stale` is `false`; empty or absent with `stale` when the compiler
66
- * does not say which file is stale.
72
+ * The generated files that are missing or out of date (the module, the extra files of the
73
+ * extensions, and `znight.providers.json` in library mode). Empty when `stale` is `false`; empty or
74
+ * absent with `stale` when the compiler does not say which file is stale.
67
75
  */
68
76
  readonly staleFiles?: readonly string[] | undefined;
69
77
  }
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export { d as AppSupervisor, t as COMMANDS, _ as CancelledError, v as CliError, s as DEFAULT_FEATURES, y as ExitCode, c as FEATURES, l as HTTP_ADAPTERS, u as PACKAGE_MANAGERS, r as SCHEMATICS, b as UsageError, m as adaptCompilerModule, p as cliVersion, f as createContext, g as defineCommand, i as findSchematic, h as loadCompiler, n as main, a as planSchematic, o as renderProject } from "./main-BWsiUsxN.mjs";
1
+ export { d as AppSupervisor, t as COMMANDS, _ as CancelledError, v as CliError, s as DEFAULT_FEATURES, y as ExitCode, c as FEATURES, l as HTTP_ADAPTERS, u as PACKAGE_MANAGERS, r as SCHEMATICS, b as UsageError, m as adaptCompilerModule, p as cliVersion, f as createContext, g as defineCommand, i as findSchematic, h as loadCompiler, n as main, a as planSchematic, o as renderProject } from "./main-DUyElIVZ.mjs";
@@ -408,7 +408,8 @@ function adaptCompilerModule(module) {
408
408
  return written(await generate(options), before);
409
409
  }
410
410
  const result = await compileOnly(options);
411
- const changed = !hasErrors(result.diagnostics) && writeIfChanged(result.outFile, result.diskCode ?? result.code);
411
+ let changed = false;
412
+ if (!hasErrors(result.diagnostics)) for (const output of outputsOf(result, false)) changed = writeIfChanged(output.file, output.content) || changed;
412
413
  return {
413
414
  ...result,
414
415
  written: changed
@@ -451,21 +452,34 @@ function adaptCompilerModule(module) {
451
452
  };
452
453
  }
453
454
  /**
454
- * The generated files of a compile result that differ from the disk: the module, and
455
- * `znight.providers.json` in library mode. None with errors (nothing would be written).
455
+ * The generated files of a compile result: the module, the extra files of the extensions and, with
456
+ * `library`, `znight.providers.json` in library mode (`generate()` of the compiler writes the module
457
+ * and the extra files; library mode is a compiler feature).
456
458
  */
457
- function staleOutputs(result) {
458
- if (hasErrors(result.diagnostics)) return [];
459
+ function outputsOf(result, library) {
459
460
  const outputs = [{
460
461
  file: result.outFile,
461
462
  content: result.diskCode ?? result.code
462
463
  }];
464
+ for (const extra of result.extraFiles ?? []) outputs.push({
465
+ file: extra.file,
466
+ content: extra.content
467
+ });
463
468
  const { file, content } = result.library ?? {};
464
- if (typeof file === "string" && typeof content === "string") outputs.push({
469
+ if (library && typeof file === "string" && typeof content === "string") outputs.push({
465
470
  file,
466
471
  content
467
472
  });
468
- return outputs.filter((output) => readText(output.file) !== output.content).map((output) => output.file);
473
+ return outputs;
474
+ }
475
+ /**
476
+ * The generated files of a compile result that differ from the disk: the module, the extra files of
477
+ * the extensions and `znight.providers.json` in library mode. None with errors (nothing would be
478
+ * written).
479
+ */
480
+ function staleOutputs(result) {
481
+ if (hasErrors(result.diagnostics)) return [];
482
+ return outputsOf(result, true).filter((output) => readText(output.file) !== output.content).map((output) => output.file);
469
483
  }
470
484
  /** Absolute path of the generated module for `options`. */
471
485
  function resolveOutFile(options) {
@@ -1979,6 +1993,15 @@ function staleFilesOf(result) {
1979
1993
  }
1980
1994
  return [...stale].sort((a, b) => Number(b === result.outFile) - Number(a === result.outFile));
1981
1995
  }
1996
+ /**
1997
+ * The hint of a missing generated file: the module, an extra file of the compiler extensions (an SDL
1998
+ * schema...) or the library manifest (`dist/znight.providers.json`).
1999
+ */
2000
+ function missingHint(path, outFile, extraFiles) {
2001
+ if (path === outFile) return "run \"znight generate\" (it is git-ignored: generate it before tsc and tests)";
2002
+ if (extraFiles.has(path)) return "run \"znight generate\" (a compiler extension generates it from the sources)";
2003
+ return "run \"znight generate\" after the build: a build cleaning dist/ (tsdown, tsc --build --clean...) deletes it";
2004
+ }
1982
2005
  async function check(cli, compiler, options) {
1983
2006
  const root = options.root ?? cli.cwd;
1984
2007
  const started = performance.now();
@@ -1990,11 +2013,11 @@ async function check(cli, compiler, options) {
1990
2013
  }
1991
2014
  const file = displayPath(result.outFile, cli.cwd);
1992
2015
  const stale = staleFilesOf(result);
2016
+ const extraFiles = new Set(result.extraFiles?.map((extra) => extra.file));
1993
2017
  for (const path of stale) {
1994
2018
  const shown = displayPath(path, cli.cwd);
1995
- const hint = path === result.outFile ? "run \"znight generate\" (it is git-ignored: generate it before tsc and tests)" : "run \"znight generate\" after the build: a build cleaning dist/ (tsdown, tsc --build --clean...) deletes it";
1996
2019
  if (existsSync(path)) cli.reporter.error(`${shown} is out of date`, ["run \"znight generate\""]);
1997
- else cli.reporter.error(`${shown} is missing`, [hint]);
2020
+ else cli.reporter.error(`${shown} is missing`, [missingHint(path, result.outFile, extraFiles)]);
1998
2021
  }
1999
2022
  if (stale.length > 0) return 1;
2000
2023
  cli.reporter.success(`${cli.colors.bold(file)} is up to date ${cli.colors.dim(`(${statsOf(result, performance.now() - started)})`)}`);
@@ -3862,4 +3885,4 @@ function stackOf(error) {
3862
3885
  }
3863
3886
  export { CancelledError as _, planSchematic as a, UsageError as b, FEATURES as c, AppSupervisor as d, createContext as f, defineCommand as g, loadCompiler as h, findSchematic as i, HTTP_ADAPTERS as l, adaptCompilerModule as m, main as n, renderProject as o, cliVersion as p, SCHEMATICS as r, DEFAULT_FEATURES as s, COMMANDS as t, PACKAGE_MANAGERS as u, CliError as v, ExitCode as y };
3864
3887
 
3865
- //# sourceMappingURL=main-BWsiUsxN.mjs.map
3888
+ //# sourceMappingURL=main-DUyElIVZ.mjs.map