This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plister.js
63 lines (53 loc) · 1.69 KB
/
plister.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
var fs = require("fs");
var plist = require("plist");
var JSONPath = require("jsonpath-plus");
var extend = require("extend");
var argv = require('minimist')(process.argv.slice(2), {
"boolean": true
});
var filename = argv._[0];
var path = argv.path;
var data = plist.parse(fs.readFileSync(filename, 'utf8'));
var options = {
depth: argv.depth ? argv.depth : 0
};
if (argv.exclude) options.exclude = JSON.parse(fs.readFileSync(argv.exclude, 'utf8'));
if (argv.include) options.include = JSON.parse(fs.readFileSync(argv.include, 'utf8'));
if (argv.verbose) {
console.log("Options: " + JSON.stringify(options, null, 2));
console.log("");
}
var obj = data;
if (path) {
// console.log("Searching path of '" + path + "'");
obj = JSONPath({json: obj, path: path});
}
if (options.depth) {
obj = lookup(obj, data.$objects, options.depth, options);
}
console.log(JSON.stringify(obj, null, 2));
function lookup(value, objects, depth, options) {
if (Array.isArray(value)) {
var result = [];
for (var i = 0; i < value.length; i++) {
result[i] = lookup(value[i], objects, depth, options);
}
return result;
}
if (value && typeof value == 'object') {
if (value.CF$UID != undefined && depth > 0) {
var result = lookup(objects[value.CF$UID], objects, depth-1, options);
result.$UID = value.CF$UID;
return result;
}
var result = {};
for (var key in value) {
//console.log('Looking up key "' + key + '"');
if ((!options.include || options.include.indexOf(key) >= 0)
&& (!options.exclude || options.exclude.indexOf(key) == -1))
result[key] = lookup(value[key], objects, depth, options);
}
return result;
}
return value;
}