forked from pfgithub/scpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplistconv.js
50 lines (43 loc) · 1.21 KB
/
plistconv.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
const bplistc = require("bplist-creator");
const fs = require("fs");
const bplistp = require("bplist-parser");
const args = process.argv.slice(2);
// let action = args.shift().toLowerCase();
const [input, output] = args;
let action;
if (input.endsWith(".json") && output.endsWith(".shortcut")) {
action = "jsontoshortcut";
}
if (input.endsWith(".shortcut") && output.endsWith(".json")) {
action = "shortcuttojson";
}
if (!action) {
throw new Error(
"Invalid. Usage: plistconv <input.shortcut> <output.json> OR <input.json> <output.shortcut>"
);
}
function cfbtr(json) {
// hack to workaround buffers not saving into json
// console.log("doing", json);
if (json.type === "Buffer") {
return Buffer.from(json.data);
}
Object.keys(json).forEach(key => {
if (typeof json[key] === "object") {
json[key] = cfbtr(json[key]);
}
});
return json;
}
if (action === "shortcuttojson") {
bplistp.parseFile(input, (err, obj) => {
if (err) {
throw err;
}
fs.writeFileSync(output, JSON.stringify(obj, null, "\t"), "utf8");
});
} else if (action === "jsontoshortcut") {
const buffer = bplistc(cfbtr(JSON.parse(fs.readFileSync(input, "utf8"))));
fs.writeFileSync(output, buffer);
}
// console.log(toShortcut(od));