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 0
/
Copy pathbin.js
executable file
·88 lines (72 loc) · 2.56 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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const fs = require('fs')
const { resolve } = require('path')
const isEmpty = require('is-empty-file')
const { DateTime } = require('luxon')
const stringify = require('csv-stringify')
const isString = require('lodash/isString')
const pump = require('pump')
const pify = require('pify')
const pMkdir = pify(fs.mkdir)
const pUnlink = pify(fs.unlink)
const pPump = pify(pump)
const generateGTFS = require('.')
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-cp-gtfs [options] start-date end-date gtfs-directory
Arguments:
start-date Feed start date: YYYY-MM-DD (in Europe/Lisbon timezone)
end-date Feed end date: YYYY-MM-DD (included, in Europe/Lisbon timezone)
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)
}
const main = async (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: `YYYY-MM-DD`')
}
const start = DateTime.fromFormat(opt.start, 'yyyy-MM-dd', { zone: 'Europe/Lisbon' }).toJSDate()
const end = DateTime.fromFormat(opt.end, 'yyyy-MM-dd', { zone: 'Europe/Lisbon' }).toJSDate()
if (+start > +end) throw new Error('`end` cannot be before `start`')
const gtfs = await generateGTFS(start, end)
// create directory if necessary
const directory = resolve(opt.directory)
await (pMkdir(directory, { recursive: true }).catch(error => {
if (error.code !== 'EEXIST') throw error
}))
const jobs = Object.keys(gtfs).map(file => {
const writeStream = fs.createWriteStream(resolve(directory, `${file}.txt`))
return pPump(gtfs[file], stringify({ delimiter: ',' }), writeStream)
})
await Promise.all(jobs)
const actions = await Promise.all(Object.keys(gtfs).map(file => {
const filePath = resolve(directory, `${file}.txt`)
if (isEmpty(filePath)) { // @todo
return pUnlink(filePath).then(() => 'deleted')
}
return Promise.resolve('written')
}))
console.log(`${actions.filter(a => a === 'written').length} files written`)
}
main(opt)
.catch(console.error)