nerdgram1337 1.4.0 → 1.4.2

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
@@ -45,7 +45,7 @@ nerdgram render Welcome.mmd -o Welcome.svg
45
45
 
46
46
  The app still has to be reachable for every other command. NerdGram is a single-user app. The API is a tool port for programs you trust, not an always-on multi-user server. The browser routes have no token, so anyone who can reach the port can use them. The default URL is loopback. Point `NERDGRAM_URL` at another machine only when that machine has opened its Host setting to you.
47
47
 
48
- The full command list and the release notes are in the [repository README](https://github.com/NetworkNerd1337/NerdGram1337#command-line-tool).
48
+ The full command list is in the [command-line tool](https://github.com/NetworkNerd1337/NerdGram1337/blob/main/docs/cli.md) page. Release notes are in the [changelog](https://github.com/NetworkNerd1337/NerdGram1337/blob/main/CHANGELOG.md).
49
49
 
50
50
  ## License
51
51
 
package/dist/cli.js CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // server/src/cli.ts
4
+ import { readFileSync } from "node:fs";
4
5
  import fs from "node:fs/promises";
5
6
  import path from "node:path";
6
7
  import { fileURLToPath } from "node:url";
@@ -23,7 +24,8 @@ var BOOL_FLAGS = /* @__PURE__ */ new Set([
23
24
  "share-data",
24
25
  "with-data",
25
26
  "keep-data",
26
- "include-down"
27
+ "include-down",
28
+ "version"
27
29
  ]);
28
30
  var HELP = `nerdgram \u2014 automation client for NerdGram1337
29
31
 
@@ -52,8 +54,27 @@ Usage
52
54
 
53
55
  A local .mmd file is rendered and linted in this process. Anything else uses
54
56
  NERDGRAM_URL (default http://127.0.0.1:8787) and NERDGRAM_TOKEN.
55
- --url and --token override those. --json prints one JSON object.
56
- --server forces render to use the API. Exit status is 1 on errors.
57
+ --url and --token override those. --server forces render to use a workspace
58
+ path on the API, not a file on this computer. Exit status is 1 on errors.
59
+
60
+ Flags
61
+ --help Print this help and exit.
62
+ --version Print the nerdgram version and exit.
63
+ --url <url> Server URL. Overrides NERDGRAM_URL.
64
+ --token <token> Automation token. Overrides NERDGRAM_TOKEN.
65
+ --json Print one JSON object and nothing else. Success is
66
+ {"ok":true, ...} with that command's fields (path, hits,
67
+ diagnostics, folders, files, and the rest of the payload).
68
+ Failure is {"ok":false,"error","status"} on stdout.
69
+ --include-down Network data. Draw shutdown and down interfaces. Off by default.
70
+ --vlan <1-4094> Layer 2 network view. Keep links that carry this VLAN id.
71
+ --group-by <value>
72
+ cdp and lldp: platform or capability.
73
+ network with --view l3: vrf.
74
+ network with --view l2: site or role.
75
+ --view <l3|l2> generate --format network only. l3 is subnets. l2 is VLANs,
76
+ trunks, and LAGs. refresh rejects --view. The view is stored
77
+ in the diagram.
57
78
  Rename and move also move a CSV, JSON, network JSON, CDP, or LLDP file that
58
79
  sits next to the diagram and update nerdgram.source. duplicate --copy-data
59
80
  gives the copy its own data file. duplicate --share-data keeps both diagrams
@@ -81,7 +102,7 @@ not changed. --undo restores that replace and skips a file edited since.
81
102
  `;
82
103
  var renderModule = null;
83
104
  function loadRender() {
84
- renderModule ??= import("./chunk-4EOKHSPN.js");
105
+ renderModule ??= import("./chunk-YNM3RGIQ.js");
85
106
  return renderModule;
86
107
  }
87
108
  async function loadLint() {
@@ -94,11 +115,35 @@ async function shutdownCli() {
94
115
  }
95
116
  var CliError = class extends Error {
96
117
  status;
97
- constructor(message, status) {
118
+ details;
119
+ constructor(message, status, details) {
98
120
  super(message);
99
121
  this.status = status;
122
+ this.details = details;
100
123
  }
101
124
  };
125
+ function formatRowIssues(errors) {
126
+ if (!Array.isArray(errors)) return [];
127
+ const lines = [];
128
+ for (const item of errors) {
129
+ if (!item || typeof item !== "object") continue;
130
+ const issue = item;
131
+ if (typeof issue.message !== "string" || !issue.message.trim()) continue;
132
+ const message = issue.message.trim();
133
+ const field = typeof issue.path === "string" ? issue.path.trim() : "";
134
+ if (field && !message.includes(field)) {
135
+ lines.push(`${field}: ${message}`);
136
+ continue;
137
+ }
138
+ const row = typeof issue.row === "number" ? issue.row : 0;
139
+ if (row > 0 && !message.toLowerCase().includes(`row ${row}`)) {
140
+ lines.push(`row ${row}: ${message}`);
141
+ continue;
142
+ }
143
+ lines.push(message);
144
+ }
145
+ return lines;
146
+ }
102
147
  function parseArgs(argv) {
103
148
  const flags = /* @__PURE__ */ new Map();
104
149
  const positionals = [];
@@ -188,8 +233,11 @@ async function callApi(base, token, method, apiPath, body) {
188
233
  }
189
234
  if (!response.ok) {
190
235
  const record = json && typeof json === "object" ? json : {};
191
- const message = typeof record.error === "string" ? record.error : `Request failed (${response.status})`;
192
- throw new CliError(message, response.status);
236
+ const summary = typeof record.error === "string" ? record.error : `Request failed (${response.status})`;
237
+ const rows = formatRowIssues(record.errors);
238
+ const message = rows.length > 0 ? `${summary}
239
+ ${rows.join("\n")}` : summary;
240
+ throw new CliError(message, response.status, record);
193
241
  }
194
242
  return { status: response.status, json, bytes, contentType };
195
243
  }
@@ -213,7 +261,13 @@ async function renderCommand(parsed, io) {
213
261
  if (!target) throw new CliError("render needs a file or a workspace path.");
214
262
  if (!output) throw new CliError("render needs -o out.svg or -o out.png.");
215
263
  const format = formatOf(output);
216
- const local = !parsed.flags.get("server") && await pathExists(target);
264
+ const server = parsed.flags.get("server") === true;
265
+ if (server && await pathExists(target)) {
266
+ throw new CliError(
267
+ `${target} is a file on this computer. Drop --server to render it here. --server renders a workspace path such as Sites/Campus.mmd on the NerdGram server.`
268
+ );
269
+ }
270
+ const local = !server && await pathExists(target);
217
271
  let bytes;
218
272
  if (local) {
219
273
  const source = await fs.readFile(target, "utf8");
@@ -222,7 +276,18 @@ async function renderCommand(parsed, io) {
222
276
  bytes = rendered.body;
223
277
  } else {
224
278
  const { base, token } = configOf(parsed.flags, io.env);
225
- const result = await callApi(base, requireToken(token), "POST", "/api/v1/render", { path: target, format });
279
+ let result;
280
+ try {
281
+ result = await callApi(base, requireToken(token), "POST", "/api/v1/render", { path: target, format });
282
+ } catch (err) {
283
+ if (err instanceof CliError && err.status === 404) {
284
+ throw new CliError(
285
+ `No diagram named ${target} is in the workspace. --server uses a workspace path such as Sites/Campus.mmd. To render a file on this computer, drop --server.`,
286
+ 404
287
+ );
288
+ }
289
+ throw err;
290
+ }
226
291
  bytes = result.bytes;
227
292
  }
228
293
  await fs.mkdir(path.dirname(path.resolve(output)), { recursive: true });
@@ -427,7 +492,8 @@ async function deleteCommand(parsed, io) {
427
492
  ...keepData ? { deleteData: false } : {}
428
493
  });
429
494
  const body = result.json;
430
- writeOut(io, isJson(parsed.flags), body, `Moved ${body.path ?? itemPath} to the trash.`);
495
+ const kept = body.message ? ` ${body.message}` : "";
496
+ writeOut(io, isJson(parsed.flags), body, `Moved ${body.path ?? itemPath} to the trash.${kept}`);
431
497
  }
432
498
  async function generateCommand(parsed, io) {
433
499
  const diagramPath = flagString(parsed.flags, "path") ?? parsed.positionals[0];
@@ -528,6 +594,18 @@ async function baselineCommand(parsed, io) {
528
594
  `Saved baseline ${body.name ?? name}${body.id ? ` (${body.id})` : ""}.`
529
595
  );
530
596
  }
597
+ function cliVersion() {
598
+ const here = path.dirname(fileURLToPath(import.meta.url));
599
+ const candidates = [path.resolve(here, "../package.json"), path.resolve(here, "../../package.json")];
600
+ for (const file of candidates) {
601
+ try {
602
+ const parsed = JSON.parse(readFileSync(file, "utf8"));
603
+ if (typeof parsed.version === "string" && /^\d+\.\d+\.\d+$/.test(parsed.version)) return parsed.version;
604
+ } catch {
605
+ }
606
+ }
607
+ return "0.0.0";
608
+ }
531
609
  function skipLabel(reason) {
532
610
  switch (reason) {
533
611
  case "locked":
@@ -536,6 +614,8 @@ function skipLabel(reason) {
536
614
  return "changed since the preview";
537
615
  case "edited":
538
616
  return "edited since the replace";
617
+ case "undone":
618
+ return "already undone";
539
619
  case "hidden":
540
620
  case "meta":
541
621
  case "not-diagram":
@@ -616,6 +696,9 @@ async function replaceCommand(parsed, io) {
616
696
  writeOut(io, isJson(parsed.flags), applied.json ?? {}, lines.join("\n"));
617
697
  }
618
698
  async function refreshCommand(parsed, io) {
699
+ if (parsed.flags.has("view")) {
700
+ throw new CliError("refresh does not take --view. The view comes from the diagram.");
701
+ }
619
702
  const diagramPath = parsed.positionals[0] ?? flagString(parsed.flags, "path");
620
703
  if (!diagramPath) throw new CliError("refresh needs a diagram path.");
621
704
  const dataFile = flagString(parsed.flags, "data");
@@ -645,6 +728,11 @@ async function main(argv, io = {}) {
645
728
  `);
646
729
  return 1;
647
730
  }
731
+ if (parsed.flags.get("version") === true) {
732
+ streams.stdout(`nerdgram ${cliVersion()}
733
+ `);
734
+ return 0;
735
+ }
648
736
  if (parsed.flags.get("help") === true || parsed.command === "help" || parsed.command === "") {
649
737
  streams.stdout(HELP);
650
738
  return parsed.command === "" && parsed.flags.get("help") !== true && parsed.positionals.length === 0 ? 0 : 0;
@@ -707,9 +795,13 @@ async function main(argv, io = {}) {
707
795
  } catch (err) {
708
796
  const message = err instanceof Error ? err.message : String(err);
709
797
  const status = err instanceof CliError ? err.status : void 0;
710
- if (isJson(parsed.flags)) streams.stdout(`${JSON.stringify({ ok: false, error: message, status: status ?? null })}
711
- `);
712
- else streams.stderr(`${message}
798
+ if (isJson(parsed.flags)) {
799
+ const errors = err instanceof CliError && Array.isArray(err.details?.errors) ? err.details.errors : void 0;
800
+ streams.stdout(
801
+ `${JSON.stringify({ ok: false, error: message, status: status ?? null, ...errors ? { errors } : {} })}
802
+ `
803
+ );
804
+ } else streams.stderr(`${message}
713
805
  `);
714
806
  return 1;
715
807
  }
@@ -732,6 +824,7 @@ if (isDirectRun()) {
732
824
  });
733
825
  }
734
826
  export {
827
+ formatRowIssues,
735
828
  main,
736
829
  shutdownCli
737
830
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nerdgram1337",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Command-line client for a local NerdGram1337 server",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,7 +23,7 @@
23
23
  "url": "git+https://github.com/NetworkNerd1337/NerdGram1337.git",
24
24
  "directory": "cli"
25
25
  },
26
- "homepage": "https://github.com/NetworkNerd1337/NerdGram1337#command-line-tool",
26
+ "homepage": "https://github.com/NetworkNerd1337/NerdGram1337/blob/main/docs/cli.md",
27
27
  "bugs": {
28
28
  "url": "https://github.com/NetworkNerd1337/NerdGram1337/issues"
29
29
  },
File without changes