Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
66 lines (57 loc) · 2.04 KB
/
Copy pathcli.js
File metadata and controls
66 lines (57 loc) · 2.04 KB
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
#!/usr/bin/env node
const fs = require("fs");
const won = require("./won.js");
if (process.argv.some(el => el=="--help")) {
console.log(`Won stands for \"Web Object Notation\" and this CLI is a fast tool to do convertion between HTML, CSS and JSON.\n
Usage:
\`-jh <filepath>\` to convert from JSON to HTML
\`-hj <filepath>\` to convert from HTML to JSON
\`-cj <filepath>\` to convert from CSS to JSON
\`-jc <filepath>\` to convert from JSON to CSS
\`-o <filepath>\` to specify an output name for the file\n
Support: https://github.com/GianlucaTarantino/won`);
process.exit(0);
}
// In case user uses only "won"
if (process.argv.length<=3) {
console.log("Error: Wrong usage of won. Type \"won --help\" for help.");
process.exit(0);
}
const acceptedArgs = ["-jh", "-hj", "-cj", "-jc"];
const mainArg = acceptedArgs.indexOf(process.argv[2]);
if (mainArg<0) {
console.log("Error: First argument is invalid. Type \"won --help\" for help.");
process.exit(0);
}
let fileToRead = process.argv[3], fileOutput, outputContent;
let fileContent = fs.readFileSync(fileToRead, "utf-8");
switch (acceptedArgs[mainArg]) {
case "-jh":
outputContent = won.JSONtoHTML(JSON.parse(fileContent));
fileOutput = "outJSON.html";
break;
case "-hj":
outputContent = won.HTMLtoJSON(fileContent);
fileOutput = "outHTML.json";
break;
case "-cj":
outputContent = won.CSStoJSON(fileContent);
fileOutput = "outCSS.json";
break;
case "-jc":
outputContent = won.JSONtoCSS(JSON.parse(fileContent));
fileOutput = "outJSON.css";
break;
default:
throw "Unknown Error.";
}
if (process.argv.some((el) => el=="-o")) {
if (process.argv.length-1 == process.argv.indexOf("-o")) {
console.log("Error: Wrong usage of -o. Type \"won --help\" for help.");
process.exit(0);
}
fileOutput = process.argv[process.argv.indexOf("-o")+1];
}
fs.writeFile(fileOutput, outputContent, err => {
if (err) throw "Error: " + err;
});