@dreamtree-org/graphify 1.2.0 → 1.4.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.
@@ -246,22 +246,73 @@ var init_security = __esm({
246
246
  }
247
247
  });
248
248
 
249
+ // src/store/serialize.ts
250
+ function serializeGraph(graph) {
251
+ return graph.export();
252
+ }
253
+ function deserializeGraph(data) {
254
+ return import_graphology2.default.from(data);
255
+ }
256
+ var import_graphology2;
257
+ var init_serialize = __esm({
258
+ "src/store/serialize.ts"() {
259
+ "use strict";
260
+ init_cjs_shims();
261
+ import_graphology2 = __toESM(require("graphology"), 1);
262
+ }
263
+ });
264
+
265
+ // src/store/localFile.ts
266
+ var fs2, LocalFileGraphStore;
267
+ var init_localFile = __esm({
268
+ "src/store/localFile.ts"() {
269
+ "use strict";
270
+ init_cjs_shims();
271
+ fs2 = __toESM(require("fs/promises"), 1);
272
+ init_security();
273
+ init_serialize();
274
+ LocalFileGraphStore = class {
275
+ async load(ref) {
276
+ let jsonPath;
277
+ try {
278
+ jsonPath = validateGraphPath("graph.json", ref);
279
+ } catch (error) {
280
+ if (error.message.startsWith("Base directory does not exist")) return null;
281
+ throw error;
282
+ }
283
+ let raw;
284
+ try {
285
+ raw = await fs2.readFile(jsonPath, "utf-8");
286
+ } catch (error) {
287
+ if (error.code === "ENOENT") return null;
288
+ throw error;
289
+ }
290
+ return deserializeGraph(JSON.parse(raw));
291
+ }
292
+ async save(ref, graph) {
293
+ await fs2.mkdir(ref, { recursive: true });
294
+ const jsonPath = validateGraphPath("graph.json", ref);
295
+ await fs2.writeFile(jsonPath, JSON.stringify(serializeGraph(graph), null, 2), "utf-8");
296
+ }
297
+ };
298
+ }
299
+ });
300
+
249
301
  // src/graphStore.ts
250
302
  async function loadGraph(outDir = path2.join(process.cwd(), "graphify-out")) {
251
- const jsonPath = validateGraphPath("graph.json", outDir);
252
- const raw = await fs3.readFile(jsonPath, "utf-8");
253
- const data = JSON.parse(raw);
254
- return import_graphology2.default.from(data);
303
+ const graph = await new LocalFileGraphStore().load(outDir);
304
+ if (graph === null) {
305
+ throw new Error(`No graph found in ${outDir} \u2014 run \`graphify <path>\` to build one first.`);
306
+ }
307
+ return graph;
255
308
  }
256
- var fs3, path2, import_graphology2;
309
+ var path2;
257
310
  var init_graphStore = __esm({
258
311
  "src/graphStore.ts"() {
259
312
  "use strict";
260
313
  init_cjs_shims();
261
- fs3 = __toESM(require("fs/promises"), 1);
262
314
  path2 = __toESM(require("path"), 1);
263
- import_graphology2 = __toESM(require("graphology"), 1);
264
- init_security();
315
+ init_localFile();
265
316
  }
266
317
  });
267
318
 
@@ -1355,9 +1406,10 @@ function cluster(graph, options = {}) {
1355
1406
  // src/export.ts
1356
1407
  init_cjs_shims();
1357
1408
  var import_node_module = require("module");
1358
- var fs2 = __toESM(require("fs/promises"), 1);
1409
+ var fs3 = __toESM(require("fs/promises"), 1);
1359
1410
  var path = __toESM(require("path"), 1);
1360
1411
  init_security();
1412
+ init_localFile();
1361
1413
  var require2 = (0, import_node_module.createRequire)(importMetaUrl);
1362
1414
  function resolveVisNetworkAssets() {
1363
1415
  const packageJsonPath = require2.resolve("vis-network/package.json");
@@ -1423,8 +1475,8 @@ community: ${communityLabel}` : ""}`,
1423
1475
  async function renderHtml(graph) {
1424
1476
  const { jsPath, cssPath } = resolveVisNetworkAssets();
1425
1477
  const [visJs, visCss] = await Promise.all([
1426
- fs2.readFile(jsPath, "utf-8"),
1427
- fs2.readFile(cssPath, "utf-8")
1478
+ fs3.readFile(jsPath, "utf-8"),
1479
+ fs3.readFile(cssPath, "utf-8")
1428
1480
  ]);
1429
1481
  const { nodes, edges } = buildVisNodesAndEdges(graph);
1430
1482
  const dataJson = safeInlineJson({ nodes, edges });
@@ -1465,16 +1517,15 @@ ${visJs}
1465
1517
  }
1466
1518
  async function exportGraph(graph, options, report) {
1467
1519
  const outDir = path.resolve(options.outDir);
1468
- await fs2.mkdir(outDir, { recursive: true });
1469
- const jsonPath = validateGraphPath("graph.json", outDir);
1470
- await fs2.writeFile(jsonPath, JSON.stringify(graph.toJSON(), null, 2), "utf-8");
1520
+ await fs3.mkdir(outDir, { recursive: true });
1521
+ await new LocalFileGraphStore().save(outDir, graph);
1471
1522
  if (options.html !== false) {
1472
1523
  const htmlPath = validateGraphPath("graph.html", outDir);
1473
- await fs2.writeFile(htmlPath, await renderHtml(graph), "utf-8");
1524
+ await fs3.writeFile(htmlPath, await renderHtml(graph), "utf-8");
1474
1525
  }
1475
1526
  if (report !== void 0) {
1476
1527
  const reportPath = validateGraphPath("GRAPH_REPORT.md", outDir);
1477
- await fs2.writeFile(reportPath, report, "utf-8");
1528
+ await fs3.writeFile(reportPath, report, "utf-8");
1478
1529
  }
1479
1530
  const notImplemented = [];
1480
1531
  if (options.svg) notImplemented.push("--svg");
@@ -3912,6 +3963,11 @@ async function runPipeline(root, options = {}) {
3912
3963
  },
3913
3964
  report
3914
3965
  );
3966
+ if (options.store) {
3967
+ const storeRef = options.storeRef ?? outDir;
3968
+ progress(`Saving graph to store (${storeRef}) ...`);
3969
+ await options.store.save(storeRef, graph);
3970
+ }
3915
3971
  progress("Done.");
3916
3972
  return { manifest, graph, analysis, report };
3917
3973
  }