forked from derhuerst/generate-gtfs-flex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen-booking-rules-txt.js
executable file
·72 lines (61 loc) · 1.61 KB
/
gen-booking-rules-txt.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: [
'help', 'h',
'version', 'v',
]
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
generate-booking-rules-txt <path-to-flex-rules> <gtfs-routes>
Examples:
generate-booking-rules-txt flex-rules.js gtfs/routes.txt >gtfs/booking_rules.txt
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`${pkg.name} v${pkg.version}\n`)
process.exit(0)
}
const showError = (err) => {
console.error(err)
process.exit(1)
}
const {resolve} = require('path')
const {Stringifier} = require('csv-stringify')
const createReadGtfsFile = require('./lib/read-gtfs-files')
const computeAllBookingRules = require('./lib/booking-rules')
const pathToFlexRules = argv._[0]
if (!pathToFlexRules) showError('Missing path-to-flex-rules.')
const flexRules = require(resolve(process.cwd(), pathToFlexRules))
const requiredGtfsFiles = [
'routes'
]
const readGtfsFile = createReadGtfsFile(requiredGtfsFiles, argv._.slice(1))
;(async () => {
const bookingRules = await computeAllBookingRules(flexRules, readGtfsFile)
const fields = new Set()
for (const [_, br] of bookingRules.entries()) {
for (const k of Object.keys(br)) fields.add(k)
}
const csv = new Stringifier({
quoted: true,
columns: Array.from(fields.values()),
header: true,
})
csv.pipe(process.stdout)
for (const [_, br] of bookingRules.entries()) {
const row = []
let i = 0
for (const field of fields) {
row[i++] = br[field]
}
csv.write(row)
}
csv.end()
})()
.catch(showError)