-
Notifications
You must be signed in to change notification settings - Fork 24
/
bash2json.js
38 lines (35 loc) · 969 Bytes
/
bash2json.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
// Convert Shellscript function into JSON array
const fs = require("fs");
// Constants
inputFile = "./test.sh";
outputFile = "./test.json";
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
pipe(
// Read input file
() => {
try {
return fs.readFileSync(inputFile, "utf8");
} catch (err) {
`Cannot read input file: ${inputFile}. Error: ${err}`;
}
},
// lines
string => string.split("\n"),
lines =>
// replacing leading spaces (tab size = 2: "\s\s") with "\t"
lines.map(line => line.replace(/^\s+/, m => m.replace(/\s\s/g, "\t"))),
// escaping \
lines => lines.map(line => line.replace("\\", "\\")),
// escaping $
lines => lines.map(line => line.replace(/\$/g, "\\$")),
// to JSON
o => JSON.stringify(o, null, 2),
// write output file
data => {
try {
fs.writeFileSync(outputFile, data);
} catch (error) {
`Cannot write output file: ${outputFile}. Error: ${err}`;
}
}
)();