Skip to content

Commit

Permalink
Fix cli (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
fetimo authored Jan 24, 2024
1 parent c683259 commit 411dd3c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Fs from "fs";
import Path from "path";
import meow from "meow";
import getStdin from "get-stdin";
import type { Parent } from "unist";
import { toD3Force, parse, toDot } from "./index";

const cli = meow(
Expand Down Expand Up @@ -32,6 +33,21 @@ const EXTENSION_TO_TYPE = {
};
type ExtKey = keyof typeof EXTENSION_TO_TYPE;

function getOutputFromType(type: string, parsed: Parent) {
switch (type) {
case "json": {
return JSON.stringify(parsed, null, 2);
}
case "d3.json": {
return JSON.stringify(toD3Force(parsed), null, 2);
}
case "dot": {
return toDot(parsed);
}
}
return '';
}

(async () => {
const [infile, outfile] = cli.input;
const inputStr = infile ? Fs.readFileSync(infile, "utf8") : await getStdin();
Expand All @@ -48,22 +64,7 @@ type ExtKey = keyof typeof EXTENSION_TO_TYPE;
}
}

let output: string = "";

switch (type) {
case "json": {
output = JSON.stringify(parsed, null, 2);
break;
}
case "d3.json": {
output = JSON.stringify(toD3Force(parsed), null, 2);
break;
}
case "dot": {
output = toDot(parsed);
break;
}
}
const output = getOutputFromType(type, parsed);

if (outfile) {
Fs.writeFileSync(outfile, output, "utf8");
Expand Down

0 comments on commit 411dd3c

Please sign in to comment.