This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin.js
executable file
·103 lines (84 loc) · 2.55 KB
/
bin.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const fs = require('fs')
const path = require('path')
const toPromise = require('stream-to-promise')
const isEmpty = require('is-empty-file')
const moment = require('moment-timezone')
const csv = require('csv-string').stringify
const isString = require('lodash.isstring')
const map = require('through2-map').obj
const generateGTFS = require('./index')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h', 'version', 'v']
})
const opt = {
start: argv._[0],
end: argv._[1],
directory: argv._[2],
help: argv.help || argv.h,
version: argv.version || argv.v
}
if (opt.help === true) {
process.stdout.write(`
build-mav-gtfs [options] start-date end-date gtfs-directory
Arguments:
start-date Feed start date: DD.MM.YYYY
end-date Feed end date: DD.MM.YYYY (included)
gtfs-directory Directory where the generated GTFS will be placed
Options:
--help -h Show this help message.
--version -v Show the version number.
`)
process.exit(0)
}
if (opt.version === true) {
process.stdout.write(`${pkg.version}\n`)
process.exit(0)
}
// main program
const files = ['agency', 'stops', 'routes', 'trips', 'stop_times', 'calendar_dates', 'feed_info']
const main = (opt) => {
if(!isString(opt.start) || !isString(opt.end) || opt.start.length != 10 || opt.end.length != 10){
throw new Error('missing or invalid `start-date` or `end-date` parameter, must look like this: `DD.MM.YYYY`')
}
const start = moment.tz(opt.start, 'DD.MM.YYYY', 'Europe/Lisbon')
const end = moment.tz(opt.end, 'DD.MM.YYYY', 'Europe/Lisbon')
if(+start > +end){
throw new Error('`end` before `start`')
}
const directory = path.resolve(opt.directory)
generateGTFS(start.toDate(), end.toDate())
.then((gtfs) => {
if(!fs.existsSync(directory)) fs.mkdirSync(directory)
fs.accessSync(directory, fs.constants.W_OK)
const streams = []
for(let file in gtfs){
const filePath = path.join(directory, file + '.txt')
streams.push(gtfs[file].pipe(map((x) => csv(x, ','))).pipe(fs.createWriteStream(filePath)))
}
Promise.all(streams.map(toPromise))
.then((done) => {
let i = 0
for(let file in gtfs){
const filePath = path.join(directory, file + '.txt')
if(isEmpty(filePath)){
fs.unlinkSync(filePath)
i++
}
}
console.log(`${files.length - i} files written`)
})
.catch((error) => {
console.error(error)
throw new Error(error)
})
})
.catch((error) => {
console.error(error)
throw new Error(error)
})
}
main(opt)