-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
executable file
·86 lines (71 loc) · 2.34 KB
/
cmd.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
var fs = require('fs')
var { Transform, pipeline } = require('stream')
var minimist = require('minimist')
var argv = minimist(process.argv.slice(2), {
alias: {
i: 'infile', o: 'outdir',
h: 'help', v: 'version'
},
boolean: [ 'help', 'version' ],
})
if (argv.version) {
return console.log(require('./package.json').version)
}
if (argv.help || argv.outdir === undefined) return usage()
fs.mkdirSync(argv.outdir, { recursive: true })
if (argv._[0] === 'load') {
var ingest = require('./')({ outdir: argv.outdir })
load(ingest, onerror)
} else if (argv._[0] === 'sort') {
var ingest = require('./')({ outdir: argv.outdir })
ingest.sort(onerror)
} else if (argv._[0] === 'build') {
var ingest = require('./')({ outdir: argv.outdir })
build(ingest, onerror)
} else if (argv._[0] === 'ingest') { // everything
var ingest = require('./')({ outdir: argv.outdir })
load(ingest, (err) => {
if (err) return onerror(err)
ingest.sort(function (err) {
if (err) return onerror(err)
build(ingest, onerror)
})
})
} else usage()
function load(ingest, cb) {
var split = require('split2')
var instream = argv.infile === undefined || argv.infile === '-'
? process.stdin : fs.createReadStream(argv.infile)
pipeline(instream, split(), new Transform({
transform: (line, enc, next) => ingest.write(line, next),
flush: (next) => ingest.flush(next),
}), cb)
}
function build(ingest, cb) {
var opts = {}
if (argv.size) opts.size = argv.size
if (typeof opts.size === 'string') opts.size = Number(opts.size.replace(/_/g,''))
ingest.build(opts, cb)
}
function onerror(err) {
if (err) {
console.error(err)
process.exit(1)
}
}
function usage() {
console.log(`
usage: sparse-geonames-ingest COMMAND {OPTIONS}
sparse-geonames-ingest ingest {-i INFILE} -o OUTDIR
Load, sort, and build in one command.
sparse-geonames-ingest load {-i INFILE} -o OUTDIR
Read geonames newline-delimited text file from INFILE or "-" (stdin),
writing output to OUTDIR.
sparse-geonames-ingest sort -o OUTDIR
Sort loaded data from OUTDIR, writing results to OUTDIR.
sparse-geonames-ingest build -o OUTDIR (--size=SIZE)
Build sorted data from OUTDIR, writing results to OUTDIR.
Each file in the output is at most SIZE bytes.
`.trim().replace(/^ {4}/mg,'')+'\n')
}