-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
executable file
·134 lines (116 loc) · 3.87 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
/* eslint-disable no-console */
/* eslint-disable global-require */
const fs = require('fs');
const debug = require('debug')('osm-search-data-export');
const commander = require('commander');
const pkg = require('./package.json');
const exportSearchData = require('./index');
const program = new commander.Command();
program
.name(pkg.name)
.version(pkg.version)
.option('--input <type>', 'input source')
.option('--output <type>', 'output target')
.option('--config <filename>', 'path to custom config')
.option('--inpath <filename>', 'path to input file if applicable')
.option('--outpath <filename>', 'path to output file if applicable')
.option('--cachepath <filename>', 'path to cache file used for overpass input')
.option('--bbox <value>', 'bounding box, comma separated floating values in order left, bottom, right top')
.option('--timeout <seconds>', 'timeout for overpass query')
.on('--help', () => {
console.log('');
console.log('Input types:');
console.log(' json - JSON file that holds an array of OSM objects');
console.log(' overpass - Fetch OSM data from Overpass using a bounding box');
console.log(' pbf - Fetch OSM data from a PBF export file');
console.log('');
console.log('Output types:');
console.log(' json - Write search data to a JSON file');
console.log(' json-compact - Write search data to a JSON file in a more compact style');
console.log(' sqlite - Write search data into a SQLite db file');
console.log('');
console.log('Examples:');
console.log(` $ ${pkg.name} --input pbf --inpath data.pbf --output json --outpath search.json`);
console.log(` $ ${pkg.name} --input overpass --bbox -21.604769,-64.819679,-21.477032,-64.631195 --output sqlite --outpath search.db`);
})
.parse(process.argv);
// No params
if (process.argv.length <= 2) {
program.help();
process.exit(1);
}
try {
const options = program.opts();
let input;
let output;
let userConfig;
switch (options.input) {
case 'json':
debug('Using JSON file input');
input = require('./src/input/json')({
inPath: options.inpath,
});
break;
case 'memory':
throw new Error('Memory input is not supported in CLI');
case 'overpass':
debug('Using Overpass input');
input = require('./src/input/overpass')({
bbox: options.bbox,
cachePath: options.cachepath,
timeout: options.timeout,
});
break;
case 'pbf':
debug('Using PBF file input');
input = require('./src/input/pbf')({
inPath: options.inpath,
});
break;
default:
throw new Error('Unknown input');
}
switch (options.output) {
case 'json':
debug('Using JSON file output');
output = require('./src/output/json')({
outPath: options.outpath,
});
break;
case 'json-compact':
debug('Using JSON compact file output');
output = require('./src/output/json-compact')({
outPath: options.outpath,
});
break;
case 'memory':
throw new Error('Memory output is not supported in CLI');
case 'multi':
throw new Error('Multi output is not supported in CLI');
case 'sqlite':
debug('Using SQLite file output');
output = require('./src/output/sqlite')({
outPath: options.outpath,
});
break;
default:
throw new Error('Unknown output');
}
if (options.config) {
if (!fs.existsSync(options.config)) {
throw new Error('Unknown config path');
}
try {
userConfig = JSON.parse(fs.readFileSync(options.config));
} catch (error) {
throw new Error('Could not parse config');
}
}
// Run!
exportSearchData(input, output, userConfig);
} catch (error) {
console.log(`${pkg.name}: ${error.message}`);
console.log(`Try '${pkg.name} --help' for more information.`);
process.exit(1);
}