Skip to content

Commit

Permalink
fix(package): main points to non existant package
Browse files Browse the repository at this point in the history
"bin" mode was failing for similar reason

Added simple test for CLI

Add -v option (GraphViz version) for CLI

Fixes #114

Signed-off-by: GordonSmith <[email protected]>
  • Loading branch information
GordonSmith committed Aug 28, 2022
1 parent 2973874 commit 0128401
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 27 deletions.
12 changes: 6 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
"configurations": [
{
"name": "index.html",
"type": "pwa-msedge",
"type": "msedge",
"request": "launch",
"url": "http://localhost:8000/index.html",
"runtimeArgs": [],
"webRoot": "${workspaceRoot}"
},
{
"name": "helloworld.html",
"type": "pwa-msedge",
"type": "msedge",
"request": "launch",
"url": "http://localhost:8000/helloworld.html",
"webRoot": "${workspaceRoot}"
},
{
"name": "test-browser",
"type": "pwa-msedge",
"type": "msedge",
"request": "launch",
"url": "http://localhost:8000/test.html",
"webRoot": "${workspaceRoot}",
Expand All @@ -41,7 +41,7 @@
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
"type": "node"
},
{
"name": "CLI",
Expand All @@ -50,12 +50,12 @@
"args": [
"-K neato",
"-n 2",
"./input.dot"
"./src/__tests__/simple.dot"
],
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
"type": "node"
}
]
}
50 changes: 32 additions & 18 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const fs = require("fs");
const gvMod = require("../dist/graphviz.node.js");
const { exit } = require("process");
const gvMod = require("../dist/graphviz.js");

const yargs = require("yargs/yargs")(process.argv.slice(2))
.usage("Usage: dot-wasm [options] fileOrDot")
.demandCommand(1, 1)
.demandCommand(0, 1)
.example("dot-wasm -K neato -T xdot ./input.dot", "Execute NEATO layout and outputs XDOT format.")
.alias("K", "layout")
.nargs("K", 1)
Expand All @@ -21,6 +22,8 @@ const yargs = require("yargs/yargs")(process.argv.slice(2))
.alias("y", "invert-y")
.nargs("y", 0)
.describe("y", "By default, the coordinate system used in generic output formats, such as attributed dot, extended dot, plain and plain-ext, is the standard cartesian system with the origin in the lower left corner, and with increasing y coordinates as points move from bottom to top. If the -y flag is used, the coordinate system is inverted, so that increasing values of y correspond to movement from top to bottom.")
.nargs("v", 0)
.describe("v", "Echo GraphViz library version")
.help("h")
.alias("h", "help")
.epilog("https://github.com/hpcc-systems/hpcc-js-wasm")
Expand All @@ -36,24 +39,35 @@ try {
dot = argv._[0];
}

if (argv.n && argv.layout.trim() !== "neato") {
throw new Error("-n option is only supported with -T neato");
}
if (argv.v) {
gvMod.graphvizVersion().then(version => {
console.log(`GraphViz version: ${version}`);
}).catch(e => {
console.error(e.message);
exit(1);
})
} else {

const ext = {
};
if (argv.n) {
ext.nop = parseInt(argv.n);
}
if (argv.y) {
ext.yInvert = true;
}
if (argv.n && argv.layout.trim() !== "neato") {
throw new Error("-n option is only supported with -T neato");
}

const ext = {
};
if (argv.n) {
ext.nop = parseInt(argv.n);
}
if (argv.y) {
ext.yInvert = true;
}

gvMod.graphviz.layout(dot, argv.format?.trim() ?? "svg", argv.layout?.trim() ?? "dot", ext).then(response => {
console.log(response);
}).catch(e => {
console.error(e.message);
});
gvMod.graphviz.layout(dot, argv.format?.trim() ?? "svg", argv.layout?.trim() ?? "dot", ext).then(response => {
console.log(response);
}).catch(e => {
console.error(e.message);
exit(1);
});
}
} catch (e) {
console.error(`Error: ${e.message}\n`);
yargs.showHelp();
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
"patchwork",
"xml"
],
"main": "dist/index.node.js",
"main": "dist/index.js",
"module": "dist/index.es6",
"module-node": "dist/index.node.es6",
"browser": "dist/index.js",
"unpkg": "dist/index.min.js",
"jsdelivr": "dist/index.min.js",
Expand Down Expand Up @@ -64,9 +63,10 @@
"dev-start": "ws",
"lint": "eslint src/**/*.ts",
"lint-fix": "npm run lint -- --fix",
"test-cli": "node ./bin/cli.js -v",
"test-chrome": "karma start --single-run --browsers ChromeHeadless karma.conf.js",
"test-firefox": "karma start --single-run --browsers Firefox karma.conf.js",
"test-node": "mocha ./dist/test.js --reporter spec",
"test-node": "node ./bin/cli.js -v && mocha ./dist/test.js --reporter spec",
"test": "run-p test-chrome test-node",
"tag": "run-s standard-version git-push",
"purge-jsdelivr": "node ./utils/purge-jsdelivr.js",
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/simple.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
digraph G {
node [shape=rect];

subgraph cluster_0 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
a0 -> a1 -> a2 -> a3;
label = "process #1";
}

subgraph cluster_1 {
node [style=filled];
b0 -> b1 -> b2 -> b3;
label = "process #2";
color=blue
}

start -> a0;
start -> b0;
a1 -> b3;
b2 -> a3;
a3 -> a0;
a3 -> end;
b3 -> end;

start [shape=Mdiamond];
end [shape=Msquare];
}
`;

export const badDot = `
digraph G {
node [shape=rect];

subgraph cluster_0 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
a0 -> a1 -> a2 -> a3;
label = "process #1";
]

subgraph cluster_1 {
node [style=filled];
b0 -> b1 -> b2 -> b3;
label = "process #2";
color=blue
}

start -> a0;
start -> b0;
a1 -> b3;
b2 -> a3;
a3 -> a0;
a3 -> end;
b3 -> end;

start [shape=Mdiamond];
end [shape=Msquare];
}

0 comments on commit 0128401

Please sign in to comment.