-
Notifications
You must be signed in to change notification settings - Fork 152
/
dbf2json
executable file
·68 lines (59 loc) · 2.04 KB
/
dbf2json
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
#!/usr/bin/env node
var fs = require("fs"),
commander = require("commander"),
shapefile = require("../");
commander
.version(require("../package.json").version)
.usage("[options] [file]")
.description("Convert a dBASE file to JSON.")
.option("-o, --out <file>", "output file name; defaults to “-” for stdout", "-")
.option("-n, --newline-delimited", "output newline-delimited JSON")
.option("--encoding <encoding>", "input character encoding")
.parse(process.argv);
if (commander.args.length === 0) commander.args[0] = "-";
else if (commander.args.length !== 1) {
console.error();
console.error(" error: multiple input files");
console.error();
process.exit(1);
}
var out = (commander.out === "-" ? process.stdout : fs.createWriteStream(commander.out)).on("error", handleEpipe);
shapefile.openDbf(commander.args[0] === "-" ? process.stdin : commander.args[0], {encoding: commander.encoding})
.then(commander.newlineDelimited ? writeNewlineDelimitedProperties : writeProperties)
.catch(handleError);
function writeNewlineDelimitedProperties(source) {
return source.read().then(function repeat(result) {
if (result.done) return;
out.write(JSON.stringify(result.value));
out.write("\n");
return source.read().then(repeat);
}).then(function() {
if (out !== process.stdout) out.end();
});
}
function writeProperties(source) {
out.write("[");
return source.read().then(function(result) {
if (result.done) return;
out.write(JSON.stringify(result.value));
return source.read().then(function repeat(result) {
if (result.done) return;
out.write(",");
out.write(JSON.stringify(result.value));
return source.read().then(repeat);
});
}).then(function() {
out[out === process.stdout ? "write" : "end"]("]\n");
});
}
function handleEpipe(error) {
if (error.code === "EPIPE" || error.errno === "EPIPE") {
process.exit(0);
}
}
function handleError(error) {
console.error();
console.error(" error: " + error.message);
console.error();
process.exit(1);
}