ts-graphviz 1.2.4-dev.ebf0c074c → 1.3.1-dev.e5c510529

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
@@ -6,6 +6,7 @@
6
6
  [![code style: prettier](https://img.shields.io/badge/code%20style-prettier-ff69b4?logo=prettier&style=flat)](https://github.com/facebook/jest)
7
7
  [![test: jest](https://img.shields.io/badge/tested%20with-jest-99424f?logo=jest&style=flat)](https://github.com/facebook/jest)
8
8
  ![node version](https://img.shields.io/node/v/ts-graphviz)
9
+ [![deno version](https://img.shields.io/badge/deno-^1.28.0-black?logo=deno)](https://github.com/denoland/deno)
9
10
  ![npm](https://img.shields.io/npm/dm/ts-graphviz)
10
11
  [![All Contributors](https://img.shields.io/github/all-contributors/ts-graphviz/ts-graphviz?color=orange)](#contributors)
11
12
 
@@ -19,7 +20,7 @@
19
20
  [![Refarence](https://img.shields.io/badge/-Refarence-3178C6?logo=TypeScript&style=flat&logoColor=fff)](https://ts-graphviz.github.io/ts-graphviz/)
20
21
  [![Suponser](https://img.shields.io/badge/-Suponser-fff?logo=GitHub%20Sponsors&style=flat)](https://github.com/sponsors/kamiazya)
21
22
 
22
- > [English](https://github.com/ts-graphviz/ts-graphviz/blob/main/README.md) | [日本語](https://github.com/ts-graphviz/ts-graphviz/blob/main/README.ja.md)
23
+ > [English](https://github.com/ts-graphviz/ts-graphviz/blob/main/README.md) | [日本語](https://github.com/ts-graphviz/ts-graphviz/blob/main/README_ja.md)
23
24
 
24
25
  ## Key Features ✨
25
26
 
@@ -34,6 +35,8 @@
34
35
 
35
36
  ## Installation 💽
36
37
 
38
+ ### Node.js
39
+
37
40
  This package can then be installed using a package manager.
38
41
 
39
42
  ```bash
@@ -45,6 +48,16 @@ $ yarn add ts-graphviz
45
48
  $ pnpm add ts-graphviz
46
49
  ```
47
50
 
51
+ ### Deno 🦕
52
+
53
+ [Deno v1.28 and above supports npm](https://deno.land/manual/node/npm_specifiers).
54
+
55
+ You can install and use the package by specifying the following:
56
+
57
+ ```ts
58
+ import { toDot } from 'npm:ts-graphviz';
59
+ ```
60
+
48
61
  ## Usage 📑
49
62
 
50
63
  This section provides an overview of the package.
@@ -322,6 +335,56 @@ const dot = toDot(g);
322
335
 
323
336
  </details>
324
337
 
338
+ ### `ts-graphviz/adapter` Module 🔌
339
+
340
+ > This module status is ![beta](https://img.shields.io/badge/-beta-orange).
341
+
342
+
343
+ Provides an interface to run Graphviz dot commands.
344
+
345
+ [Graphviz](https://graphviz.gitlab.io/) must be installed so that the dot command can be executed.
346
+
347
+ Execute the dot command to output a DOT language string to a stream or file.
348
+
349
+ ![Adapter State Machine](./media/adapter-state-machine.svg)
350
+
351
+ This module provides the following functions.
352
+
353
+ - The `toStream` function converts **DOT** to **Stream**.
354
+ ```ts
355
+ import { toStream } from 'ts-graphviz/adapter';
356
+
357
+ const dot = `
358
+ digraph example {
359
+ node1 [
360
+ label = "My Node",
361
+ ]
362
+ }
363
+ `;
364
+
365
+ const stream = await toStream(dot, { format: 'svg' });
366
+ // Node.js
367
+ stream.pipe(process.stdout);
368
+ // Deno
369
+ await stream.pipeTo(Deno.stdout.writable);
370
+ ```
371
+ - Writes **DOT** to a file at the specified path `toFile` function
372
+ ```ts
373
+ import { toFile } from 'ts-graphviz/adapter';
374
+
375
+ const dot = `
376
+ digraph example {
377
+ node1 [
378
+ label = "My Node",
379
+ ]
380
+ }
381
+ `;
382
+
383
+ await toFile(dot, './result.svg', { format: 'svg' });
384
+ ```
385
+
386
+ > **Note** Designed to work with Node.js and Deno, Stream is runtime native.
387
+
325
388
  ### `ts-graphviz/ast` Module 🔢
326
389
 
327
390
  > This module status is ![beta](https://img.shields.io/badge/-beta-orange).
@@ -438,6 +501,17 @@ const ast = parse(`
438
501
 
439
502
  </details>
440
503
 
504
+ ## Who's using `ts-graphviz` 📜
505
+
506
+ - [Apollo GraphQL](https://github.com/apollographql)
507
+ - [AWS](https://github.com/aws)
508
+ - [IBM (StrongLoop)](https://strongloop.com)
509
+ - [The University of Tokyo](https://github.com/csg-tokyo)
510
+ - [Transmute](https://github.com/transmute-industries)
511
+
512
+ > **Note** Let us know that you're using `ts-graphviz` on [GitHub Discussions](https://github.com/ts-graphviz/ts-graphviz/discussions/736) 🙏
513
+
514
+
441
515
  ## Related Projects 💫
442
516
 
443
517
  Related projects can be found at [**ts-graphviz** GitHub Organization](https://github.com/ts-graphviz).
@@ -454,12 +528,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
454
528
  <table>
455
529
  <tbody>
456
530
  <tr>
457
- <td align="center"><a href="http://blog.kamiazya.tech/"><img src="https://avatars0.githubusercontent.com/u/35218186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yuki Yamazaki</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Code">💻</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Tests">⚠️</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Documentation">📖</a> <a href="#ideas-kamiazya" title="Ideas, Planning, & Feedback">🤔</a></td>
458
- <td align="center"><a href="https://laysent.com"><img src="https://avatars2.githubusercontent.com/u/1191606?v=4?s=100" width="100px;" alt=""/><br /><sub><b>LaySent</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/issues?q=author%3Alaysent" title="Bug reports">🐛</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=laysent" title="Tests">⚠️</a></td>
459
- <td align="center"><a href="https://github.com/elasticdotventures"><img src="https://avatars0.githubusercontent.com/u/35611074?v=4?s=100" width="100px;" alt=""/><br /><sub><b>elasticdotventures</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=elasticdotventures" title="Documentation">📖</a></td>
460
- <td align="center"><a href="https://github.com/ChristianMurphy"><img src="https://avatars.githubusercontent.com/u/3107513?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Christian Murphy</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=ChristianMurphy" title="Code">💻</a> <a href="#ideas-ChristianMurphy" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=ChristianMurphy" title="Documentation">📖</a></td>
531
+ <td align="center" valign="top" width="14.28%"><a href="http://blog.kamiazya.tech/"><img src="https://avatars0.githubusercontent.com/u/35218186?v=4?s=100" width="100px;" alt="Yuki Yamazaki"/><br /><sub><b>Yuki Yamazaki</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Code">💻</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Tests">⚠️</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Documentation">📖</a> <a href="#ideas-kamiazya" title="Ideas, Planning, & Feedback">🤔</a></td>
532
+ <td align="center" valign="top" width="14.28%"><a href="https://laysent.com"><img src="https://avatars2.githubusercontent.com/u/1191606?v=4?s=100" width="100px;" alt="LaySent"/><br /><sub><b>LaySent</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/issues?q=author%3Alaysent" title="Bug reports">🐛</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=laysent" title="Tests">⚠️</a></td>
533
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/elasticdotventures"><img src="https://avatars0.githubusercontent.com/u/35611074?v=4?s=100" width="100px;" alt="elasticdotventures"/><br /><sub><b>elasticdotventures</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=elasticdotventures" title="Documentation">📖</a></td>
534
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/ChristianMurphy"><img src="https://avatars.githubusercontent.com/u/3107513?v=4?s=100" width="100px;" alt="Christian Murphy"/><br /><sub><b>Christian Murphy</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=ChristianMurphy" title="Code">💻</a> <a href="#ideas-ChristianMurphy" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=ChristianMurphy" title="Documentation">📖</a></td>
535
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/ArtemAdamenko"><img src="https://avatars.githubusercontent.com/u/2178516?v=4?s=100" width="100px;" alt="Artem"/><br /><sub><b>Artem</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/issues?q=author%3AArtemAdamenko" title="Bug reports">🐛</a></td>
461
536
  </tr>
462
- </tobdy>
537
+ </tbody>
463
538
  </table>
464
539
 
465
540
  <!-- markdownlint-restore -->
@@ -0,0 +1,17 @@
1
+ export type Format = 'png' | 'svg' | 'json' | 'jpg' | 'pdf' | 'xdot' | 'plain' | 'dot_json';
2
+
3
+ export interface Options {
4
+ format?: Format;
5
+ suppressWarnings?: boolean;
6
+ dotCommand?: string;
7
+ }
8
+
9
+ /**
10
+ * Execute the Graphviz dot command and make a Stream of the results.
11
+ */
12
+ export function toStream(dot: string, options?: Options): Promise<ReadableStream>;
13
+
14
+ /**
15
+ * Execute the Graphviz dot command and output the results to a file.
16
+ */
17
+ export function toFile(dot: string, path: string, options?: Options): Promise<void>;
@@ -0,0 +1,33 @@
1
+ function commandBuilder({ dotCommand = 'dot', suppressWarnings = true, format = 'svg' } = {}) {
2
+ const args = [
3
+ ...(function* () {
4
+ if (suppressWarnings) yield '-q';
5
+ yield `-T${format}`;
6
+ })(),
7
+ ];
8
+ return [dotCommand, args];
9
+ }
10
+
11
+ /**
12
+ * Execute the Graphviz dot command and make a Stream of the results.
13
+ */
14
+ export async function toStream(dot, options) {
15
+ const [command, args] = commandBuilder(options);
16
+ const cp = new Deno.Command(command, {
17
+ args: args,
18
+ stdin: 'piped',
19
+ }).spawn();
20
+ const stdin = cp.stdin.getWriter();
21
+ await stdin.write(new TextEncoder().encode(dot));
22
+ await stdin.close();
23
+ return cp.stdout;
24
+ }
25
+
26
+ /**
27
+ * Execute the Graphviz dot command and output the results to a file.
28
+ */
29
+ export async function toFile(dot, path, options) {
30
+ const output = await Deno.open(path, { write: true });
31
+ const stream = await toStream(dot, options);
32
+ await stream.pipeTo(output.writable);
33
+ }
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var node_fs = require('node:fs');
6
+ var node_stream = require('node:stream');
7
+ var node_util = require('node:util');
8
+ var node_child_process = require('node:child_process');
9
+
10
+ /**
11
+ * @module ts-graphviz/adapter
12
+ * @beta
13
+ */
14
+ function commandBuilder({ dotCommand = 'dot', suppressWarnings = true, format = 'svg' } = {}) {
15
+ const args = [
16
+ ...(function* () {
17
+ if (suppressWarnings) yield '-q';
18
+ yield `-T${format}`;
19
+ })(),
20
+ ];
21
+ return [dotCommand, args];
22
+ }
23
+ /**
24
+ * NOTE:
25
+ * The node:stream/promises standard module is not provided in Node 14.
26
+ * Fix Node 14 to use node:stream/promises after LTS ends.
27
+ */
28
+ const pipeline = node_util.promisify(node_stream.pipeline);
29
+ /**
30
+ * Execute the Graphviz dot command and make a Stream of the results.
31
+ */
32
+ async function toStream(dot, options) {
33
+ const [command, args] = commandBuilder(options);
34
+ const p = node_child_process.spawn(command, args, { stdio: 'pipe' });
35
+ await pipeline(node_stream.Readable.from([dot]), p.stdin);
36
+ return p.stdout;
37
+ }
38
+ /**
39
+ * Execute the Graphviz dot command and output the results to a file.
40
+ */
41
+ async function toFile(dot, path, options) {
42
+ const stream = await toStream(dot, options);
43
+ await pipeline(stream, node_fs.createWriteStream(path));
44
+ }
45
+
46
+ exports.toFile = toFile;
47
+ exports.toStream = toStream;
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ declare type Format = 'png' | 'svg' | 'json' | 'jpg' | 'pdf' | 'xdot' | 'plain' | 'dot_json';
3
+ interface Options {
4
+ format?: Format;
5
+ suppressWarnings?: boolean;
6
+ dotCommand?: string;
7
+ }
8
+ /**
9
+ * Execute the Graphviz dot command and make a Stream of the results.
10
+ */
11
+ declare function toStream(dot: string, options?: Options): Promise<NodeJS.ReadableStream>;
12
+ /**
13
+ * Execute the Graphviz dot command and output the results to a file.
14
+ */
15
+ declare function toFile(dot: string, path: string, options?: Options): Promise<void>;
16
+
17
+ export { Format, Options, toFile, toStream };
@@ -0,0 +1,42 @@
1
+ import { createWriteStream } from 'node:fs';
2
+ import { pipeline as pipeline$1, Readable } from 'node:stream';
3
+ import { promisify } from 'node:util';
4
+ import { spawn } from 'node:child_process';
5
+
6
+ /**
7
+ * @module ts-graphviz/adapter
8
+ * @beta
9
+ */
10
+ function commandBuilder({ dotCommand = 'dot', suppressWarnings = true, format = 'svg' } = {}) {
11
+ const args = [
12
+ ...(function* () {
13
+ if (suppressWarnings) yield '-q';
14
+ yield `-T${format}`;
15
+ })(),
16
+ ];
17
+ return [dotCommand, args];
18
+ }
19
+ /**
20
+ * NOTE:
21
+ * The node:stream/promises standard module is not provided in Node 14.
22
+ * Fix Node 14 to use node:stream/promises after LTS ends.
23
+ */
24
+ const pipeline = promisify(pipeline$1);
25
+ /**
26
+ * Execute the Graphviz dot command and make a Stream of the results.
27
+ */
28
+ async function toStream(dot, options) {
29
+ const [command, args] = commandBuilder(options);
30
+ const p = spawn(command, args, { stdio: 'pipe' });
31
+ await pipeline(Readable.from([dot]), p.stdin);
32
+ return p.stdout;
33
+ }
34
+ /**
35
+ * Execute the Graphviz dot command and output the results to a file.
36
+ */
37
+ async function toFile(dot, path, options) {
38
+ const stream = await toStream(dot, options);
39
+ await pipeline(stream, createWriteStream(path));
40
+ }
41
+
42
+ export { toFile, toStream };
@@ -0,0 +1 @@
1
+ <svg width="172pt" height="211pt" viewBox="0 0 172.44 211.09" xmlns="http://www.w3.org/2000/svg"><g class="graph" transform="translate(4 207.09)"><path fill="#fff" stroke="transparent" d="M-4 4v-211.09h172.44V4H-4z"/><g class="node"><circle fill="none" stroke="#000" cx="89.85" cy="-171.89" r="31.4"/><text text-anchor="middle" x="89.85" y="-168.19" font-family="Times,serif" font-size="14">DOT</text></g><g class="node"><circle fill="none" stroke="#000" cx="44.85" cy="-44.85" r="44.69"/><text text-anchor="middle" x="44.85" y="-41.15" font-family="Times,serif" font-size="14">Stream</text></g><g class="edge"><path fill="none" stroke="#000" d="M66.46-151.09c-7.72 7.95-15.4 17.76-19.61 28.4-2.87 7.24-4.54 15.17-5.4 23.11"/><path stroke="#000" d="m44.94-99.32-4.18 9.73-2.8-10.21 6.98.48z"/><text text-anchor="middle" x="80.35" y="-111.49" font-family="Times,serif" font-size="14">toStream</text></g><g class="node"><circle fill="none" stroke="#000" cx="135.85" cy="-44.85" r="28.7"/><text text-anchor="middle" x="135.85" y="-41.15" font-family="Times,serif" font-size="14">File</text></g><g class="edge"><path fill="none" stroke="#000" d="M104.32-144.01c3.38 6.84 6.79 14.26 9.53 21.32 4.95 12.76 9.35 27.09 12.89 39.89"/><path stroke="#000" d="m130.18-83.48-.79 10.56-5.97-8.75 6.76-1.81z"/><text text-anchor="middle" x="139.85" y="-111.49" font-family="Times,serif" font-size="14">toFile</text></g></g></svg>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-graphviz",
3
- "version": "1.2.4-dev.ebf0c074c",
3
+ "version": "1.3.1-dev.e5c510529",
4
4
  "author": "kamiazya <yuki@kamiazya.tech>",
5
5
  "description": "Graphviz library for TypeScript.",
6
6
  "homepage": "https://ts-graphviz.github.io/ts-graphviz/",
@@ -25,21 +25,46 @@
25
25
  "types": "lib/index.d.ts",
26
26
  "exports": {
27
27
  ".": {
28
- "import": "./lib/index.js",
29
- "require": "./lib/index.cjs",
30
- "types": "./lib/index.d.ts"
28
+ "require": {
29
+ "default": "./lib/index.cjs",
30
+ "types": "./lib/index.d.ts"
31
+ },
32
+ "import": {
33
+ "default": "./lib/index.js",
34
+ "types": "./lib/index.d.ts"
35
+ }
31
36
  },
32
37
  "./ast": {
33
- "import": "./lib/ast/index.js",
34
- "require": "./lib/ast/index.cjs",
35
- "types": "./lib/ast/index.d.ts"
36
- }
38
+ "require": {
39
+ "default": "./lib/ast/index.cjs",
40
+ "types": "./lib/ast/index.d.ts"
41
+ },
42
+ "import": {
43
+ "default": "./lib/ast/index.js",
44
+ "types": "./lib/ast/index.d.ts"
45
+ }
46
+ },
47
+ "./adapter": {
48
+ "deno": {
49
+ "default": "./lib/adapter/deno/mod.js",
50
+ "types": "./lib/adapter/deno/mod.d.ts"
51
+ },
52
+ "node": {
53
+ "require": "./lib/adapter/index.cjs",
54
+ "import": "./lib/adapter/index.js",
55
+ "types": "./lib/adapter/index.d.ts"
56
+ }
57
+ },
58
+ "./package.json": "./package.json"
37
59
  },
38
- "imports": {
39
- "#lib/*": {
40
- "import": "./lib/*/index.js",
41
- "require": "./lib/*/index.cjs",
42
- "types": "./lib/*/index.d.ts"
60
+ "typesVersions": {
61
+ "*": {
62
+ "ast": [
63
+ "lib/ast"
64
+ ],
65
+ "adapter": [
66
+ "lib/adapter"
67
+ ]
43
68
  }
44
69
  },
45
70
  "license": "MIT",
@@ -49,7 +74,9 @@
49
74
  "scripts": {
50
75
  "build:peggy": "peggy --plugin ts-pegjs --extra-options-file src/ast/dot-shim/parser/peggy.options.json -o src/ast/dot-shim/parser/_parse.ts src/ast/dot-shim/parser/dot.peggy",
51
76
  "prebuild": "yarn build:peggy",
52
- "build": "tsc -p tsconfig.build.json && rollup -c",
77
+ "build:deno": "cp -r src/adapter/deno lib/adapter/deno",
78
+ "build:node": "tsc -p tsconfig.build.json && rollup -c",
79
+ "build": "yarn build:node && yarn build:deno",
53
80
  "postbuild": "prettier --write ./lib/**/index.{js,cjs,d.ts}",
54
81
  "pretest": "yarn build:peggy",
55
82
  "test": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest",
package/README.ja.md DELETED
@@ -1,498 +0,0 @@
1
- [![GitHub Action](https://github.com/kamiazya/ts-graphviz/workflows/NodeCI/badge.svg)](https://github.com/kamiazya/ts-graphviz/actions?workflow=NodeCI)
2
- [![npm version](https://badge.fury.io/js/ts-graphviz.svg)](https://badge.fury.io/js/ts-graphviz)
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)
5
- [![code style: prettier](https://img.shields.io/badge/code%20style-prettier-ff69b4?logo=prettier&style=flat)](https://github.com/facebook/jest)
6
- [![test: jest](https://img.shields.io/badge/tested%20with-jest-99424f?logo=jest&style=flat)](https://github.com/facebook/jest)
7
- ![node version](https://img.shields.io/node/v/ts-graphviz)
8
- ![npm](https://img.shields.io/npm/dm/ts-graphviz)
9
- [![All Contributors](https://img.shields.io/github/all-contributors/ts-graphviz/ts-graphviz?color=orange)](#contributors)
10
-
11
- # ts-graphviz
12
-
13
- TypeScriptの ための [Graphviz](https://graphviz.gitlab.io/) ライブラリ
14
-
15
- [![GitHub](https://img.shields.io/badge/-GitHub-181717?logo=GitHub&style=flat)](https://github.com/ts-graphviz/ts-graphviz)
16
- [![npm](https://img.shields.io/badge/-npm-CB3837?logo=npm&style=flat)](https://www.npmjs.com/package/ts-graphviz)
17
- [![yarn](https://img.shields.io/badge/-yarn-ffffff?logo=Yarn&style=flat)](https://yarnpkg.com/package/ts-graphviz)
18
- [![Refarence](https://img.shields.io/badge/-Refarence-3178C6?logo=TypeScript&style=flat&logoColor=fff)](https://ts-graphviz.github.io/ts-graphviz/)
19
- [![Suponser](https://img.shields.io/badge/-Suponser-fff?logo=GitHub%20Sponsors&style=flat)](https://github.com/sponsors/kamiazya)
20
-
21
- > [English](https://github.com/ts-graphviz/ts-graphviz/blob/main/README.md) | [日本語](https://github.com/ts-graphviz/ts-graphviz/blob/main/README.ja.md)
22
-
23
- ## 主な機能 ✨
24
-
25
- TypeScript に完全に統合された Graphviz DOT言語のモデルとASTを提供します。
26
-
27
- - **TypeScript フレンドリーな API**
28
- - DOT言語のモデルを提供しています。また属性と属性の型まで TypeScript の型定義が用意されています。
29
- - **パラダイムからの開放**
30
- - オブジェクト指向で設計され、命令的 API と 宣言的 API のどちらにも適合できるAPI提供しています。 プロジェクトにあったパラダイムを選択できます。
31
- - **あらゆるユースケースに対応**
32
- - モデルを提供する高レイヤーの API と ASTを取り扱う低レイヤーの API をどちらも提供し、あらゆるユースケースに対応します。
33
-
34
- ## インストール方法 💽
35
-
36
- このパッケージは、パッケージマネージャを使用してインストールすることができます。
37
-
38
- ```bash
39
- # npm
40
- $ npm install -S ts-graphviz
41
- # or yarn
42
- $ yarn add ts-graphviz
43
- # or pnpm
44
- $ pnpm add ts-graphviz
45
- ```
46
-
47
- ## 使い方 📑
48
-
49
- この項では、パッケージの概要について説明します。
50
-
51
- より詳細なAPIの仕様は、 TypeScript の型定義のコメントやそれを元に自動生成された [ドキュメント](https://ts-graphviz.github.io/ts-graphviz/) を参照してください。
52
-
53
- ### `ts-graphviz` モジュール 🚩
54
-
55
- DOT 言語をJavaScript/TypeScriptで扱うためのインターフェースである Model を提供します。
56
-
57
- ![ts-graphviz](./media/ts-graphviz.svg)
58
-
59
- #### オブジェクト指向 ❤️
60
-
61
- **Model** はオブジェクト指向に設計されており、 `Digraph`, `Graph`, `Subgraph`, `Node`, `Edge` のクラスを提供しています。
62
-
63
- **Model** を **DOT** (DOT言語の文字列)に変換する`toDot` 関数を提供しています。
64
-
65
- ```typescript
66
- import { attribute as _, Digraph, Subgraph, Node, Edge, toDot } from 'ts-graphviz';
67
-
68
- const G = new Digraph();
69
- const A = new Subgraph('A');
70
- const node1 = new Node('node1', {
71
- [_.color]: 'red'
72
- });
73
- const node2 = new Node('node2', {
74
- [_.color]: 'blue'
75
- });
76
- const edge = new Edge([node1, node2], {
77
- [_.label]: 'Edge Label',
78
- [_.color]: 'pink'
79
- });
80
- G.addSubgraph(A);
81
- A.addNode(node1);
82
- A.addNode(node2);
83
- A.addEdge(edge);
84
- const dot = toDot(G);
85
- // digraph {
86
- // subgraph "A" {
87
- // "node1" [
88
- // color = "red",
89
- // ];
90
- // "node2" [
91
- // color = "blue",
92
- // ];
93
- // "node1" -> "node2" [
94
- // label = "Edge Label",
95
- // color = "pink",
96
- // ];
97
- // }
98
- // }
99
- ```
100
-
101
- <details>
102
- <summary>高度な使い方</summary>
103
-
104
- ##### カスタム・クラス 🤖
105
-
106
- クラスを継承することで独自の実装を加えることもできます。
107
-
108
- ```typescript
109
- import { Digraph, Node, Edge, EdgeTargetTuple, attribute as _, toDot } from 'ts-graphviz';
110
-
111
- class MyCustomDigraph extends Digraph {
112
- constructor() {
113
- super('G', {
114
- [_.label]: 'This is Custom Digraph',
115
- });
116
- }
117
- }
118
- class MyCustomNode extends Node {
119
- constructor(id: string) {
120
- super(`node_${id}`, {
121
- [_.label]: `This is Custom Node ${id}`,
122
- });
123
- }
124
- }
125
-
126
- class MyCustomEdge extends Edge {
127
- constructor(targets: EdgeTargetTuple) {
128
- super(targets, {
129
- [_.label]: 'This is Custom Edge',
130
- });
131
- }
132
- }
133
-
134
- const digraph = new MyCustomDigraph();
135
- const node1 = new MyCustomNode('A');
136
- const node2 = new MyCustomNode('B');
137
- const edge = new MyCustomEdge([node1, node2]);
138
- digraph.addNode(node1);
139
- digraph.addNode(node2);
140
- digraph.addEdge(edge);
141
- const dot = toDot(digraph);
142
- // digraph "G" {
143
- // label = "This is Custom Digraph";
144
- // "node_A" [
145
- // label = "This is Custom Node A";
146
- // ];
147
- // "node_B" [
148
- // label = "This is Custom Node B";
149
- // ];
150
- // "node_A" -> "node_B" [
151
- // label = "This is Custom Edge";
152
- // ];
153
- // }
154
- ```
155
-
156
- ##### Models Context API ( `with` メソッド) 🧅
157
-
158
- あなたは _Models Context API_ をつかうことで、Graphの内部で生成されるオブジェクトもカスタムクラスにすることができます。
159
-
160
-
161
- `GraphBaseModel` の実装である `Digraph`, `Graph`, `Subgraph` が持つ `with` メソッドは、 カスタムモデルを事前定義するために提供されています。
162
-
163
- ```typescript
164
- const g = new Digraph();
165
- g.with({
166
- Node: MyCustomNode,
167
- Edge: MyCustomEdge,
168
- });
169
- const a = g.createNode('A'); // MyCustomNode
170
- const b = g.createNode('B'); // MyCustomNode
171
- g.createEdge([a, b]); // MyCustomEdge
172
- const dot = toDot(g);
173
- // digraph {
174
- // "node_A" [
175
- // label = "This is Custom Node A";
176
- // ];
177
- // "node_B" [
178
- // label = "This is Custom Node B";
179
- // ];
180
- // "node_A" -> "node_B" [
181
- // label = "This is Custom Edge";
182
- // ];
183
- // }
184
- ```
185
-
186
- ##### `fromDot` 関数 ⏪
187
-
188
- > この関数のステータスは ![beta](https://img.shields.io/badge/-beta-orange) です。
189
-
190
- このライブラリを使用するメインシナリオは `toDot` 関数を使用することにありますが、逆方向の変換もサポートしています。
191
-
192
- **DOT** を **Model** に変換により、コードの一部をDOT言語で記述することができます。
193
-
194
-
195
- ```typescript
196
- const G = fromDot(
197
- `digraph {
198
- node_A [
199
- label = "This is a Label of Node A";
200
- ];
201
- }`,
202
- );
203
-
204
- G.edge(['node_A', 'node_B']);
205
-
206
- const dot = toDot(G)
207
- // digraph {
208
- // "node_A" [
209
- // label = "This is a Label of Node A";
210
- // ];
211
- // "node_A" -> "node_B";
212
- // }
213
- ```
214
-
215
- </details>
216
-
217
-
218
- #### 宣言的な API 😎
219
-
220
- `Graph` や `Digraph` を作成する際に、より **DOT** 言語に近い記法を提供するために _Model Factory_ を使うことができます。
221
-
222
- **Model** にも宣言的な API を用意しており、一貫して宣言的なパラダイムを選択することもできます。
223
-
224
- ```typescript
225
- import { attribute as _, digraph, toDot } from 'ts-graphviz';
226
-
227
- const G = digraph('G', (g) => {
228
- const a = g.node('aa');
229
- const b = g.node('bb');
230
- const c = g.node('cc');
231
- g.edge([a, b, c], {
232
- [_.color]: 'red'
233
- });
234
- g.subgraph('A', (A) => {
235
- const Aa = A.node('Aaa', {
236
- [_.color]: 'pink'
237
- });
238
-
239
- const Ab = A.node('Abb', {
240
- [_.color]: 'violet'
241
- });
242
- const Ac = A.node('Acc');
243
- A.edge([Aa.port('a'), Ab, Ac, 'E'], {
244
- [_.color]: 'red'
245
- });
246
- });
247
- });
248
-
249
- const dot = toDot(G);
250
- // digraph "G" {
251
- // "aa";
252
- // "bb";
253
- // "cc";
254
- // subgraph "A" {
255
- // "Aaa" [
256
- // color = "pink",
257
- // ];
258
- // "Abb" [
259
- // color = "violet",
260
- // ];
261
- // "Acc";
262
- // "Aaa":"a" -> "Abb" -> "Acc" -> "E" [
263
- // color = "red",
264
- // ];
265
- // }
266
- // "aa" -> "bb" -> "cc" [
267
- // color = "red",
268
- // ];
269
- // }
270
- ```
271
-
272
- > **Note** もちろん、strictモードのグラフを作るAPIも提供しています。
273
- >
274
- > ```typescript
275
- > import { strict, toDot } from 'ts-graphviz';
276
- >
277
- > const G = strict.graph(...);
278
- > const dot = toDot(G);
279
- > // strict graph {
280
- > // }
281
- > ```
282
-
283
-
284
- <details>
285
- <summary>高度な使い方</summary>
286
-
287
- ##### Models Context API ( `withContext` 関数 ) 💈
288
-
289
-
290
- `withContext` 関数は、 _Model Factory_ 関数を返します。
291
-
292
- この _Model Factory_ は、 `Digraph` や `Graph` など、 `RootGraphModel` をカスタムクラスに置き換える手段を提供します。
293
-
294
- これのAPIにより、宣言的APIとカスタムクラスを統合する手段を提供します。
295
-
296
- ```typescript
297
- const { digraph } = withContext({
298
- Digraph: MyCustomDigraph,
299
- Node: MyCustomNode,
300
- Edge: MyCustomEdge,
301
- });
302
-
303
- const G = digraph((g) => {
304
- const a = g.node('A'); // MyCustomNode
305
- const b = g.node('B'); // MyCustomNode
306
- g.edge([a, b]); // MyCustomEdge
307
- });
308
- const dot = toDot(g);
309
- // digraph "G" {
310
- // label = "This is Custom Digraph";
311
- // "node_A" [
312
- // label = "This is Custom Node A";
313
- // ];
314
- // "node_B" [
315
- // label = "This is Custom Node B";
316
- // ];
317
- // "node_A" -> "node_B" [
318
- // label = "This is Custom Edge";
319
- // ];
320
- // }
321
- ```
322
-
323
- </details>
324
-
325
- ### `ts-graphviz/ast` モジュール 🔢
326
-
327
- > このパッケージのステータスは ![beta](https://img.shields.io/badge/-beta-orange) です。
328
-
329
- 高度な利用のためにASTを扱うためのAPIを提供しています。
330
-
331
- ![State Machine](./media/state-machine.svg)
332
-
333
- 状態遷移図で記載している通り、下記の関数を提供しています。
334
-
335
- - **Model** から **AST** に変換する `fromModel` 関数
336
- - **AST** から **Model** に変換する `toModel` 関数
337
- - **AST** から **DOT** に変換する `stringify` 関数
338
- - **DOT** から **AST** に変換する `parse` 関数
339
-
340
- > **Note** 上記の図からわかるように、`ts-graphviz` パッケージで提供している `toDot` 関数は、 `fromModel` と `stringify` の合成関数です。また、`fromDot` 関数は、 `parse` と `toModel` の合成関数です。
341
-
342
- 詳しい利用方法は整備中です。
343
- TypeScriptの型定義を参考にしてください。
344
-
345
- <details>
346
- <summary>parse 関数とAST</summary>
347
-
348
-
349
- ```typescript
350
- import { parse } from 'ts-graphviz/ast';
351
-
352
- const ast = parse(`
353
- digraph example {
354
- node1 [
355
- label = "My Node",
356
- ]
357
- }
358
- `);
359
- // {
360
- // type: 'Dot',
361
- // location: {
362
- // start: { offset: 3, line: 2, column: 3 },
363
- // end: { offset: 68, line: 7, column: 1 }
364
- // },
365
- // children: [
366
- // {
367
- // id: {
368
- // value: 'example',
369
- // quoted: false,
370
- // type: 'Literal',
371
- // location: {
372
- // start: { offset: 11, line: 2, column: 11 },
373
- // end: { offset: 18, line: 2, column: 18 }
374
- // },
375
- // children: []
376
- // },
377
- // directed: true,
378
- // strict: false,
379
- // type: 'Graph',
380
- // location: {
381
- // start: { offset: 3, line: 2, column: 3 },
382
- // end: { offset: 67, line: 6, column: 4 }
383
- // },
384
- // children: [
385
- // {
386
- // id: {
387
- // value: 'node1',
388
- // quoted: false,
389
- // type: 'Literal',
390
- // location: {
391
- // start: { offset: 25, line: 3, column: 5 },
392
- // end: { offset: 30, line: 3, column: 10 }
393
- // },
394
- // children: []
395
- // },
396
- // type: 'Node',
397
- // location: {
398
- // start: { offset: 25, line: 3, column: 5 },
399
- // end: { offset: 63, line: 5, column: 6 }
400
- // },
401
- // children: [
402
- // {
403
- // key: {
404
- // value: 'label',
405
- // quoted: false,
406
- // type: 'Literal',
407
- // location: {
408
- // start: { offset: 39, line: 4, column: 7 },
409
- // end: { offset: 44, line: 4, column: 12 }
410
- // },
411
- // children: []
412
- // },
413
- // value: {
414
- // value: 'My Node',
415
- // quoted: true,
416
- // type: 'Literal',
417
- // location: {
418
- // start: { offset: 47, line: 4, column: 15 },
419
- // end: { offset: 56, line: 4, column: 24 }
420
- // },
421
- // children: []
422
- // },
423
- // location: {
424
- // start: { offset: 39, line: 4, column: 7 },
425
- // end: { offset: 57, line: 4, column: 25 }
426
- // },
427
- // type: 'Attribute',
428
- // children: []
429
- // }
430
- // ]
431
- // }
432
- // ]
433
- // }
434
- // ]
435
- // }
436
- ```
437
-
438
- </details>
439
-
440
- ## 関連プロジェクト 💫
441
-
442
- 関連するプロジェクトは [**ts-graphviz** GitHub Organization](https://github.com/ts-graphviz) で確認することができます。
443
-
444
- TypeScript/JavaScript エコシステムで Graphviz との結合度を高め、より使いやすくすることを目的に様々な OSS を提供しています。
445
-
446
- ## コントリビュータ 👥
447
-
448
- この素晴らしい人たち([emoji key](https://allcontributors.org/docs/en/emoji-key))に感謝します。
449
-
450
- <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
451
- <!-- prettier-ignore-start -->
452
- <!-- markdownlint-disable -->
453
- <table>
454
- <tbody>
455
- <tr>
456
- <td align="center"><a href="http://blog.kamiazya.tech/"><img src="https://avatars0.githubusercontent.com/u/35218186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yuki Yamazaki</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Code">💻</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Tests">⚠️</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=kamiazya" title="Documentation">📖</a> <a href="#ideas-kamiazya" title="Ideas, Planning, & Feedback">🤔</a></td>
457
- <td align="center"><a href="https://laysent.com"><img src="https://avatars2.githubusercontent.com/u/1191606?v=4?s=100" width="100px;" alt=""/><br /><sub><b>LaySent</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/issues?q=author%3Alaysent" title="Bug reports">🐛</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=laysent" title="Tests">⚠️</a></td>
458
- <td align="center"><a href="https://github.com/elasticdotventures"><img src="https://avatars0.githubusercontent.com/u/35611074?v=4?s=100" width="100px;" alt=""/><br /><sub><b>elasticdotventures</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=elasticdotventures" title="Documentation">📖</a></td>
459
- <td align="center"><a href="https://github.com/ChristianMurphy"><img src="https://avatars.githubusercontent.com/u/3107513?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Christian Murphy</b></sub></a><br /><a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=ChristianMurphy" title="Code">💻</a> <a href="#ideas-ChristianMurphy" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/ts-graphviz/ts-graphviz/commits?author=ChristianMurphy" title="Documentation">📖</a></td>
460
- </tr>
461
- </tobdy>
462
- </table>
463
-
464
- <!-- markdownlint-restore -->
465
- <!-- prettier-ignore-end -->
466
-
467
- <!-- ALL-CONTRIBUTORS-LIST:END -->
468
-
469
- このプロジェクトは、[all-contributors](https://github.com/all-contributors/all-contributors) の仕様に準拠しています。
470
-
471
- どのような種類の貢献でも歓迎します。
472
-
473
- ## 貢献の方法 💪
474
-
475
- 一番簡単な貢献の方法は、ライブラリを使っていただくことと、 [リポジトリ](https://github.com/ts-graphviz/ts-graphviz) にスターをつけることです。
476
-
477
- ### 質問 💭
478
-
479
- [GitHub Discussions](https://github.com/ts-graphviz/ts-graphviz/discussions) で気軽に質問してください。
480
-
481
- ### バグの報告/追加機能の要望 💡
482
-
483
- [GitHub Issues](https://github.com/ts-graphviz/ts-graphviz/issues/new/choose) から登録してください。
484
-
485
- ### 機能開発/バグ修正 🧑‍💻
486
-
487
- [CONTRIBUTING.md](https://github.com/ts-graphviz/ts-graphviz/blob/main/CONTRIBUTING.md) を参照してください。
488
-
489
- ### 金銭的支援 💸
490
-
491
- コアメンバーの [kamiazya](https://github.com/sponsors/kamiazya) を支援してください。
492
-
493
- > **Note** たった1ドルでも、私には十分な開発のモチベーションになります 😊
494
-
495
- ## ライセンス ⚖️
496
-
497
- 本ソフトウェアはMITライセンスのもとで公開されています。
498
- [LICENSE](https://github.com/ts-graphviz/ts-graphviz/blob/main/LICENSE)を参照してください。