nerdgram1337 1.4.1 → 1.4.3

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 (2) hide show
  1. package/dist/cli.js +65 -5
  2. package/package.json +1 -1
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
@@ -240,7 +261,13 @@ async function renderCommand(parsed, io) {
240
261
  if (!target) throw new CliError("render needs a file or a workspace path.");
241
262
  if (!output) throw new CliError("render needs -o out.svg or -o out.png.");
242
263
  const format = formatOf(output);
243
- 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);
244
271
  let bytes;
245
272
  if (local) {
246
273
  const source = await fs.readFile(target, "utf8");
@@ -249,7 +276,18 @@ async function renderCommand(parsed, io) {
249
276
  bytes = rendered.body;
250
277
  } else {
251
278
  const { base, token } = configOf(parsed.flags, io.env);
252
- 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
+ }
253
291
  bytes = result.bytes;
254
292
  }
255
293
  await fs.mkdir(path.dirname(path.resolve(output)), { recursive: true });
@@ -556,6 +594,18 @@ async function baselineCommand(parsed, io) {
556
594
  `Saved baseline ${body.name ?? name}${body.id ? ` (${body.id})` : ""}.`
557
595
  );
558
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
+ }
559
609
  function skipLabel(reason) {
560
610
  switch (reason) {
561
611
  case "locked":
@@ -564,6 +614,8 @@ function skipLabel(reason) {
564
614
  return "changed since the preview";
565
615
  case "edited":
566
616
  return "edited since the replace";
617
+ case "undone":
618
+ return "already undone";
567
619
  case "hidden":
568
620
  case "meta":
569
621
  case "not-diagram":
@@ -644,6 +696,9 @@ async function replaceCommand(parsed, io) {
644
696
  writeOut(io, isJson(parsed.flags), applied.json ?? {}, lines.join("\n"));
645
697
  }
646
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
+ }
647
702
  const diagramPath = parsed.positionals[0] ?? flagString(parsed.flags, "path");
648
703
  if (!diagramPath) throw new CliError("refresh needs a diagram path.");
649
704
  const dataFile = flagString(parsed.flags, "data");
@@ -673,6 +728,11 @@ async function main(argv, io = {}) {
673
728
  `);
674
729
  return 1;
675
730
  }
731
+ if (parsed.flags.get("version") === true) {
732
+ streams.stdout(`nerdgram ${cliVersion()}
733
+ `);
734
+ return 0;
735
+ }
676
736
  if (parsed.flags.get("help") === true || parsed.command === "help" || parsed.command === "") {
677
737
  streams.stdout(HELP);
678
738
  return parsed.command === "" && parsed.flags.get("help") !== true && parsed.positionals.length === 0 ? 0 : 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nerdgram1337",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "Command-line client for a local NerdGram1337 server",
5
5
  "type": "module",
6
6
  "license": "MIT",