Skip to content

Commit

Permalink
add roughjsconfig option
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahsonder committed Jun 27, 2024
1 parent 53769eb commit 6267cc0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Created a CLI for MemoryViz.
- Added `--height` and `--width` options to MemoryViz CLI.
- Added `--roughjs_config` option to MemoryViz CLI.

### 🐛 Bug fixes

Expand Down
30 changes: 22 additions & 8 deletions memory-viz/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ const path = require("path");
const { draw } = require("memory-viz");
const { program } = require("commander");

function parseRoughjsConfig(input) {
let config = {};
const pairs = input.split(",");

pairs.forEach((pair) => {
const [key, val] = pair.split("=");
config[key] = val;
});

return config;
}

program
.description(
"Command line interface for generating memory model diagrams with MemoryViz"
Expand All @@ -14,14 +26,20 @@ program
"path to a file containing MemoryViz-compatible JSON"
)
.option("--width <value>", "width of generated SVG", "1300")
.option("--height <value>", "height of generated SVG");
.option("--height <value>", "height of generated SVG")
.option(
"--roughjs-config <key1=value1,key2=value2,...>",
"various options to configure how the SVG is drawn" +
" - refer to rough.js documentation for available options",
parseRoughjsConfig
);

program.parse();

const filePath = program.args[0];
const absolutePath = path.resolve(process.cwd(), filePath);

// Checks if absolutePath exists and that it is a JSON file
// Checks if absolutePath exists
let fileContent;
if (!fs.existsSync(absolutePath)) {
console.error(`Error: File ${absolutePath} does not exist.`);
Expand All @@ -40,17 +58,13 @@ try {

let m;
try {
// TODO: Replace seed with command-line arguments
m = draw(data, true, {
width: program.opts().width,
height: program.opts().height,
roughjs_config: { options: { seed: 12345 } },
roughjs_config: { options: program.opts().roughjsConfig },
});
} catch (err) {
console.error(
`This is valid JSON but not valid Memory Models JSON.` +
`Please refer to the repo for more details.`
);
console.error(`Error: ${err.message}`);
process.exit(1);
}

Expand Down
Loading

0 comments on commit 6267cc0

Please sign in to comment.