Skip to content

Commit

Permalink
use hafas-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 31, 2018
1 parent 70c5ded commit f1d0f9e
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 506 deletions.
106 changes: 25 additions & 81 deletions dep.js
Expand Up @@ -2,31 +2,20 @@
'use strict'

const mri = require('mri')
const so = require('so')
const chalk = require('chalk')
const queryLocation = require('@derhuerst/location')
const chalk = require('chalk')
const hafas = require('vbb-hafas')
const createDeparturesCli = require('hafas-cli/departures')

const pkg = require('./package.json')
const lib = require('./index')
const render = require('./render')


const productColor = require('./lib/product-color')
const productSymbol = require('./lib/product-symbol')
const lineColor = require('./lib/line-color')

const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h', 'version', 'v', 'location', 'l']
})
const opt = {
station: argv._[0]
, help: argv.help || argv.h
, version: argv.version || argv.v
, location: argv.location || argv.l
, duration: argv.duration || argv.d || 15
, when: argv.when || argv.w || null
}



if (opt.help === true) {
if (argv.help || argv.h) {
process.stdout.write(`
vbb-dep [station] [options]
Expand All @@ -37,81 +26,36 @@ Options:
--location -l Use current location. OS X only.
--duration -d Show departures for the next n minutes. Default: 15
--when -w A date & time string like "tomorrow 2 pm". Default: now
--products -p Allowed transportation types.
Default: suburban,subway,tram,bus,ferry,express,regional
`)
process.exit(0)
}

if (opt.version === true) {
if (argv.version || argv.v) {
process.stdout.write(`vbb-dep v${pkg.version}\n`)
process.exit(0)
}



const locate = () => new Promise((yay, nay) => {
queryLocation((err, loc) => {
if (err) nay(err)
else yay(loc)
})
})

const showError = function (err) {
if (process.env.NODE_DEBUG === 'vbb-cli') console.error(err)
process.stderr.write(chalk.red(err.message) + '\n')
else process.stderr.write(chalk.red(err.message || (err + '')) + '\n')
process.exit(err.code || 1)
}

const main = so(function* (opt) {
let station, when, duration, products

if (opt.location) {
try {
station = yield lib.queryCloseStations('Where?', yield locate())
} catch (err) {
showError(err)
}
} else if (!opt.station || opt.station === true) {
station = yield lib.queryStation('Where?')
} else station = opt.station
try {
station = yield lib.parseStation(station)
} catch (err) {
showError(err)
}

// query date & time
if (opt.when === true) when = yield lib.queryWhen('When?')
else if ('string' === typeof opt.when) when = lib.parseWhen(opt.when)
else when = new Date()

// duration
if (opt.duration === true) {
duration = yield lib.queryDuration('Show departures for how many minutes?', 15)
} else duration = lib.parseDuration(opt.duration, 15)

// means of transport
if (opt.products === true) {
products = yield lib.queryProducts('Which means of transport?')
} else products = lib.parseProducts(opt.products)

const departures = yield lib.departures({station, when, duration, products})

// render departures
if (departures.length === 0) {
process.stdout.write(chalk.red('No departures.'))
}

const table = render.table()
for (let dep of departures) {
table.push([
render.product(dep.line.product)
, render.line(dep.line)
, render.when(dep.when)
, render.station(dep.direction)
])
}
process.stdout.write(table.toString() + '\n')
const departuresCli = createDeparturesCli(hafas, {
productColor, productSymbol,
lineColor
})

main(opt).catch(showError)
departuresCli({
station: argv._[0],
useCurrentLocation: argv.location || argv.l,
duration: argv.duration || argv.d,
queryDuration: (argv.duration || argv.d) === true,
when: argv.when || argv.w,
queryWhen: (argv.when || argv.w) === true,
products: argv.products || argv.p,
queryProducts: (argv.products || argv.p) === true
})
.catch(showError)
192 changes: 0 additions & 192 deletions index.js

This file was deleted.

61 changes: 61 additions & 0 deletions journey.js
@@ -0,0 +1,61 @@
#!/usr/bin/env node
'use strict'

const mri = require('mri')
const chalk = require('chalk')
const hafas = require('vbb-hafas')
const createJourneysCli = require('hafas-cli/journeys')

const pkg = require('./package.json')
const productColor = require('./lib/product-color')
const productSymbol = require('./lib/product-symbol')
const lineColor = require('./lib/line-color')

const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h', 'version', 'v', 'location', 'l']
})

if (argv.help || argv.h) {
process.stdout.write(`
vbb-journey [origin] [destination] [options]
Arguments:
origin Station number (e.g. 900000023201) or query (e.g. "Zoo").
destination Station number (e.g. 900000023201) or query (e.g. "Zoo").
Options:
--results -r The number of journeys to show. Default: 4
--products -p Allowed transportation types. Default: "all"
"all" = "suburban,subway,tram,bus,ferry,express,regional"
--when -w A date & time string like "tomorrow 2 pm". Default: now
`)
process.exit(0)
}

if (argv.version || argv.v) {
process.stdout.write(`vbb-journey v${pkg.version}\n`)
process.exit(0)
}

const showError = function (err) {
if (process.env.NODE_DEBUG === 'vbb-cli') console.error(err)
else process.stderr.write(chalk.red(err.message || (err + '')) + '\n')
process.exit(err.code || 1)
}

const departuresCli = createJourneysCli(hafas, {
productColor, productSymbol,
lineColor
})
departuresCli({
origin: argv._[0],
destination: argv._[1],
results: argv.results || argv.r || 4,
queryResults: (argv.results || argv.r) === true,
when: argv.when || argv.w,
queryWhen: (argv.when || argv.w) === true,
products: argv.products || argv.p,
queryProducts: (argv.products || argv.p) === true
})
.catch(showError)

0 comments on commit f1d0f9e

Please sign in to comment.