-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·63 lines (53 loc) · 1.55 KB
/
cli.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
#! /usr/bin/env node
var argv = require('minimist')(process.argv.slice(2));
var marco = require('./lib/marco.js');
var through = require('through2');
var split = require('split');
var usage = function() {
var text = [];
text.push('Usage: node cli.js [-s] [-m] [-c] [-h] [-i]');
text.push('');
text.push(' --state matches query to states');
text.push(' --municipality matches query against municipalities');
text.push(' --collection wraps results in a FeatureCollection');
text.push(' --input source should be a line-delimited JSON');
text.push(' --help prints this help message.');
text.push('');
return text.join('\n');
}
var state = argv.state || argv.s;
var municipality = argv.municipality || argv.m;
var source = argv.input || argv.i;
// STREAMING
function setPipe() {
var ts = through(() => {});
if (state) {
ts = marco.toStatePolygon({ source });
} else if (municipality) {
ts = marco.toMunicipalityPolygon({ source });
}
var transformed = process.stdin
.pipe(split())
.pipe(ts);
if (argv.collection || argv.c) {
transformed.pipe(marco.concatFeatureCollection());
} else {
transformed.pipe(process.stdout);
}
}
setPipe();
// SIMPLE QUERIES
if (state) {
marco.findState({ query: state, source }, (err, data) => {
if (!err && data)
console.log(JSON.stringify(data));
});
} else if (municipality) {
marco.findMunicipality({ query: municipality, source }, (err, data) => {
if (!err && data)
console.log(JSON.stringify(data));
});
} else {
console.log(usage());
process.exit(1);
}