Skip to content

Commit

Permalink
make --output flexible to different paths
Browse files Browse the repository at this point in the history
  • Loading branch information
leowrites committed Sep 8, 2024
1 parent 553f364 commit 9115a73
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions memory-viz/bin/cli.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ function parseFilePath(input) {
}

function parseOutputPath(input) {
return pathExists(input, `Output path`);
let outputDir, filename;
if (isPathDirectory(input)) {
outputDir = input;
filename = "memory-viz.svg";
} else {
const parsedPath = path.parse(input);
outputDir = parsedPath.dir;
filename = `${parsedPath.name}.svg`;
}
return { outputDir, filename };
}

function isPathDirectory(rawOutputPath) {
return rawOutputPath.slice(-1) == "/";
}

// helper function for parsing paths
Expand Down Expand Up @@ -100,8 +113,13 @@ function runMemoryViz(jsonContent) {

try {
if (options.output) {
const outputName = path.parse(options.output).name + ".svg";
const outputPath = path.join(options.output, outputName);
const { outputDir, filename } = options.output;

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

const outputPath = path.join(outputDir, filename);
m.save(outputPath);
} else {
m.save();
Expand Down

0 comments on commit 9115a73

Please sign in to comment.