Skip to content

Commit

Permalink
Refactor and throw errors when the path is a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
leowrites committed Sep 10, 2024
1 parent 58236d9 commit 84beaf8
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions memory-viz/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,33 @@ const { program } = require("commander");
const { json } = require("node:stream/consumers");

function parseFilePath(input) {
if (input) {
return pathExists(input, `File`);
} else {
return undefined;
const fullPath = path.resolve(process.cwd(), input);
if (fs.existsSync(fullPath, "File")) {
return fullPath;
}
console.error(`Error: File ${fullPath} does not exist.`);
process.exit(1);
}

function parseOutputPath(input) {
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`;
console.error(`Error: Output path must be a file`);
process.exit(1);
}
const parsedPath = path.parse(input);
const outputDir = parsedPath.dir;
const filename = `${parsedPath.name}.svg`;
if (!fs.existsSync(outputDir)) {
console.error(`Error: Output directory does not exist`);
process.exit(1);
}
return { outputDir, filename };
return path.join(outputDir, filename);
}

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

// helper function for parsing paths
function pathExists(inputPath, errMsg) {
const fullPath = path.resolve(process.cwd(), inputPath);
if (!fs.existsSync(fullPath)) {
console.error(`Error: ${errMsg} ${fullPath} does not exist.`);
process.exit(1);
} else {
return fullPath;
}
}

function parseRoughjsConfig(input) {
const pairs = input.split(",");
const keyValuePairs = pairs.map((pair) => pair.split("="));
Expand Down Expand Up @@ -113,14 +105,7 @@ function runMemoryViz(jsonContent) {

try {
if (options.output) {
const { outputDir, filename } = options.output;

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

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

0 comments on commit 84beaf8

Please sign in to comment.