-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·77 lines (57 loc) · 1.64 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
// todo: use import assertions once they're supported by Node.js & ESLint
// https://github.com/tc39/proposal-import-assertions
import {createRequire} from 'module'
const require = createRequire(import.meta.url)
import mri from 'mri'
import {isatty} from 'tty'
import differ from 'ansi-diff-stream'
import {stringify as asNdjson} from 'ndjson'
import {flattenOsmRelation as flatten} from './index.js'
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h', 'version', 'v', 'silent', 's']
})
if (argv.h || argv.help) {
process.stdout.write(`\
${pkg.name} <id> >data.ndjson
${pkg.description}
Options:
-s, --silent Don't report stats on stderr.
`)
process.exit(0)
}
if (argv.v || argv.version) {
process.stdout.write(pkg.name + ' ' + pkg.version + '\n')
process.exit(0)
}
if (!argv._[0]) {
process.stdout.write('Missing OSM id.\n')
process.exit(1)
}
const showError = (err) => {
console.error(err)
process.exit(1)
}
const data = flatten(+argv._[0], 1, 1)
data
.on('error', showError)
.pipe(asNdjson())
.pipe(process.stdout)
if (!argv.s && !argv.silent) {
const clearReports = isatty(process.stderr.fd) && !isatty(process.stdout.fd)
let reporter = process.stderr
if (clearReports) {
reporter = differ()
reporter.pipe(process.stderr)
}
const report = (stats) => {
reporter.write([
stats.relation + (stats.relation === 1 ? ' relation' : ' relations'),
stats.way + (stats.way === 1 ? ' way' : ' ways'),
stats.node + (stats.node === 1 ? ' node' : ' nodes'),
stats.queued + ' queued'
].join(', ') + (clearReports ? '' : '\n'))
}
data.on('stats', report)
}