From 9115a7374a30bfdbab001d56afd9006e3a5caadd Mon Sep 17 00:00:00 2001 From: leowrites Date: Sun, 8 Sep 2024 17:48:35 -0400 Subject: [PATCH] make --output flexible to different paths --- memory-viz/bin/cli.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) mode change 100644 => 100755 memory-viz/bin/cli.js diff --git a/memory-viz/bin/cli.js b/memory-viz/bin/cli.js old mode 100644 new mode 100755 index a75353cd..7997aa2b --- a/memory-viz/bin/cli.js +++ b/memory-viz/bin/cli.js @@ -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 @@ -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();