-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
70 lines (65 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env node
const fs = require("fs");
const converter = require("./converter");
const Result = require("folktale/result");
function getArgs(arguments) {
let argObj = {
"-f": null, // String
"-o": null, // String
isValuesExsit() {
return this["-f"] && this["-o"];
},
};
arguments.forEach((item, index) => {
switch (item) {
case "-f":
argObj["-f"] = arguments[index + 1];
break;
case "-o":
argObj["-o"] = arguments[index + 1];
break;
}
});
return argObj;
}
try {
const argObj = getArgs(process.argv);
if (!argObj.isValuesExsit()) {
console.error(
`Arguments error. Please fill in the file name and format.\n\ne.g: 'mermaidConverter -f ./myMD.md -o md'\n\n - f: File name\n - o: Output format\n md: SVG to markdown\n SVG: Markdown to SVG`
);
return;
}
const mdFileString = fs.readFileSync(argObj["-f"], "utf8");
const convertedMdFileString = Result.try((_) =>
((output, mdString) => {
let convertedResult;
switch (output) {
case "md":
console.log("Convert to markdown");
convertedResult = converter.SVGToMd(mdString);
break;
case "SVG":
console.log("Convert to SVG");
convertedResult = converter.mdToSVG(mdString);
break;
default:
console.warn(
"No matching conversion output format found, so no conversion"
);
convertedResult = mdString;
}
return convertedResult;
})(argObj["-o"], mdFileString)
);
if (convertedMdFileString.getOrElse(null) === null) {
console.error(
"Conversion error. Could it be a format error:\n",
convertedMdFileString.value
);
return;
}
fs.writeFileSync(argObj["-f"], convertedMdFileString.getOrElse(null));
} catch (err) {
console.error("Unknown error:\n", err);
}