@diagc/cli 0.8.0 → 0.9.0

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/dist/lint.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { type LoadOptions } from './compile.js';
2
+ /** One line of `diagc lint` output. An `error` is what fails a compile too; a
3
+ * `warning` is what compile only mentions — lint fails on both. */
4
+ export interface LintReport {
5
+ file: string;
6
+ severity: 'error' | 'warning';
7
+ code: string;
8
+ message: string;
9
+ ref?: string;
10
+ }
11
+ /** Every finding for one source: its validation errors when it does not load,
12
+ * otherwise its warnings (core's lint included). */
13
+ export declare function lintFile(file: string, opts?: LoadOptions): Promise<LintReport[]>;
14
+ export declare function formatLintReport(r: LintReport): string;
package/dist/lint.js ADDED
@@ -0,0 +1,27 @@
1
+ import { diagramWarnings, errMessage } from '@diagc/core';
2
+ import { loadModel } from './compile.js';
3
+ const report = (file, severity, i) => ({
4
+ file,
5
+ severity,
6
+ code: i.code,
7
+ message: i.message,
8
+ ...(i.ref !== undefined ? { ref: i.ref } : {}),
9
+ });
10
+ /** Every finding for one source: its validation errors when it does not load,
11
+ * otherwise its warnings (core's lint included). */
12
+ export async function lintFile(file, opts) {
13
+ try {
14
+ const model = await loadModel(file, opts);
15
+ return diagramWarnings(model).map((w) => report(file, 'warning', w));
16
+ }
17
+ catch (e) {
18
+ // By name, not instanceof: a builder diagram throws from jiti's own copy of core.
19
+ if (e instanceof Error && e.name === 'DiagramValidationError' && Array.isArray(e.issues)) {
20
+ return e.issues.map((i) => report(file, 'error', i));
21
+ }
22
+ return [report(file, 'error', { code: 'load', message: errMessage(e) })];
23
+ }
24
+ }
25
+ export function formatLintReport(r) {
26
+ return `${r.file}: ${r.severity} ${r.code}: ${r.message}`;
27
+ }
@@ -1,3 +1,4 @@
1
+ import type { DiagramModel } from '@diagc/core';
1
2
  export interface PublishOptions {
2
3
  srcDir: string;
3
4
  artifactsDir: string;
@@ -17,4 +18,14 @@ export interface PublishResult {
17
18
  images: string[];
18
19
  gallery: string;
19
20
  }
21
+ /**
22
+ * The model with every image/shape ref it can read swapped for inline data, so
23
+ * the page stands alone. `cache` is shared across the diagrams of one run
24
+ * (refs are content hashes, so one read serves them all); `label` names the
25
+ * diagram in the warnings for refs it has to leave as they are.
26
+ */
27
+ export declare function inlineAssets(model: DiagramModel, dirs: {
28
+ libraryDir: string;
29
+ assetsDir: string;
30
+ }, cache: Map<string, Buffer>, label: string): Promise<DiagramModel>;
20
31
  export declare function publishDiagrams(opts: PublishOptions): Promise<PublishResult>;
@@ -13,13 +13,45 @@ async function readMaybe(p) {
13
13
  return undefined;
14
14
  }
15
15
  }
16
+ /**
17
+ * The model with every image/shape ref it can read swapped for inline data, so
18
+ * the page stands alone. `cache` is shared across the diagrams of one run
19
+ * (refs are content hashes, so one read serves them all); `label` names the
20
+ * diagram in the warnings for refs it has to leave as they are.
21
+ */
22
+ export async function inlineAssets(model, dirs, cache, label) {
23
+ // Pre-read every inlinable ref so the rewrite can stay a pure sync function.
24
+ for (const n of model.nodes) {
25
+ for (const ref of [n.image, n.shape]) {
26
+ if (ref === undefined || cache.has(ref))
27
+ continue;
28
+ const kind = classifyAssetRef(ref);
29
+ if (kind === 'skip')
30
+ continue;
31
+ const base = kind === 'library' ? dirs.libraryDir : dirs.assetsDir;
32
+ const rel = kind === 'library' ? ref.slice('/library/'.length)
33
+ : kind === 'api-assets' ? ref.slice('/api/assets/'.length)
34
+ : ref;
35
+ const filePath = safeAssetPath(base, rel);
36
+ if (filePath === undefined) {
37
+ console.warn(`publish: refusing out-of-root asset "${ref}" for ${label}; leaving ref as-is.`);
38
+ continue;
39
+ }
40
+ const bytes = await readMaybe(filePath);
41
+ if (bytes !== undefined)
42
+ cache.set(ref, bytes);
43
+ else
44
+ console.warn(`publish: could not read asset "${ref}" for ${label} (looked in ${filePath}); leaving ref as-is.`);
45
+ }
46
+ }
47
+ return rewriteAssetRefs(model, (ref) => cache.get(ref));
48
+ }
16
49
  export async function publishDiagrams(opts) {
17
50
  const shell = await readFile(opts.shellPath, 'utf8');
18
51
  await mkdir(opts.htmlDir, { recursive: true });
19
52
  if (opts.images)
20
53
  await mkdir(opts.staticDir, { recursive: true });
21
54
  const cache = new Map();
22
- const resolveAsset = (ref) => cache.get(ref);
23
55
  let all = await discoverDiagrams(opts.artifactsDir, opts.srcDir);
24
56
  if (opts.names !== undefined && opts.names.length > 0) {
25
57
  const want = new Set(opts.names);
@@ -49,31 +81,7 @@ export async function publishDiagrams(opts) {
49
81
  else
50
82
  console.warn(`publish: ignoring malformed drawings sidecar for diagram "${d.name}" (${d.drawingsPath}); the page ships without ink.`);
51
83
  }
52
- // Pre-read every inlinable ref so the rewrite can stay a pure sync function.
53
- for (const n of modelRaw.nodes) {
54
- for (const ref of [n.image, n.shape]) {
55
- if (ref === undefined || cache.has(ref))
56
- continue;
57
- const kind = classifyAssetRef(ref);
58
- if (kind === 'skip')
59
- continue;
60
- const base = kind === 'library' ? opts.libraryDir : opts.assetsDir;
61
- const rel = kind === 'library' ? ref.slice('/library/'.length)
62
- : kind === 'api-assets' ? ref.slice('/api/assets/'.length)
63
- : ref;
64
- const filePath = safeAssetPath(base, rel);
65
- if (filePath === undefined) {
66
- console.warn(`publish: refusing out-of-root asset "${ref}" for diagram "${d.name}"; leaving ref as-is.`);
67
- continue;
68
- }
69
- const bytes = await readMaybe(filePath);
70
- if (bytes !== undefined)
71
- cache.set(ref, bytes);
72
- else
73
- console.warn(`publish: could not read asset "${ref}" for diagram "${d.name}" (looked in ${filePath}); leaving ref as-is.`);
74
- }
75
- }
76
- const inlined = rewriteAssetRefs(modelRaw, resolveAsset);
84
+ const inlined = await inlineAssets(modelRaw, opts, cache, `diagram "${d.name}"`);
77
85
  const pagePath = path.join(opts.htmlDir, `${d.name}.html`);
78
86
  await mkdir(path.dirname(pagePath), { recursive: true });
79
87
  await writeFile(pagePath, stampHtml(shell, { model: inlined, layout, drawings }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagc/cli",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Author software-architecture diagrams as code, compile them to a validated model, edit them in a local studio, and publish pages and images.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "author": "Bogdan Frankovskyi",
@@ -44,7 +44,7 @@
44
44
  "fast-glob": "^3.3.0",
45
45
  "jiti": "^2.0.0",
46
46
  "playwright-core": "^1.62.0",
47
- "@diagc/core": "0.8.0"
47
+ "@diagc/core": "0.9.0"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"