Skip to content

Commit

Permalink
Promisify and reorganize import function
Browse files Browse the repository at this point in the history
version bump
  • Loading branch information
brendannee committed Jun 19, 2017
1 parent e4bbf69 commit 4594f3e
Show file tree
Hide file tree
Showing 25 changed files with 2,306 additions and 332 deletions.
30 changes: 18 additions & 12 deletions README.md
Expand Up @@ -31,10 +31,12 @@ Install node-gtfs directly from [npm](https://npmjs.org):
const gtfs = require('gtfs');
const config = require('./config.json');

gtfs.import(config, (err) => {
if (err) return console.error(err);

console.log('Import Successful')
gtfs.import(config)
.then(() => {
console.log('Import Successful');
})
.catch(err => {
console.error(err);
});

## Configuration
Expand Down Expand Up @@ -222,10 +224,12 @@ Use `gtfs.import()` in your code to run an import of a GTFS file specified in a
mongoose.Promise = global.Promise;
mongoose.connect(config.mongoUrl);

gtfs.import(config, (err) => {
if (err) return console.error(err);

console.log('Import Successful')
gtfs.import(config)
.then(() => {
console.log('Import Successful');
})
.catch(err => {
console.error(err);
});

Configuration can be a JSON object in your code
Expand All @@ -248,10 +252,12 @@ Configuration can be a JSON object in your code
mongoose.Promise = global.Promise;
mongoose.connect(config.mongoUrl);

gtfs.import(config, (err) => {
if (err) return console.error(err);

console.log('Import Successful')
gtfs.import(config)
.then(() => {
console.log('Import Successful');
})
.catch(err => {
console.error(err);
});

## Query Methods
Expand Down
39 changes: 18 additions & 21 deletions bin/gtfs-import.js
@@ -1,5 +1,8 @@
#!/usr/bin/env node

const path = require('path');

const fs = require('fs-extra');
const untildify = require('untildify');
const argv = require('yargs')
.usage('Usage: $0 --config ./config.json')
Expand All @@ -20,31 +23,25 @@ const argv = require('yargs')

const gtfs = require('../');

const configPath = path.resolve(untildify(argv.configPath));

function handleError(err) {
console.error(err || 'Unknown Error');
process.exit(1);
}

function getConfig() {
try {
const config = require(configPath);

if (argv.skipDelete) {
config.skipDelete = argv.skipDelete;
}

return config;
} catch (err) {
handleError(new Error(`Cannot find configuration file at \`${configPath}\`. Use config-sample.json as a starting point, pass --configPath option`));
fs.readFile(path.resolve(untildify(argv.configPath)), 'utf8')
.then(data => JSON.parse(data))
.then(config => {
if (argv.skipDelete) {
config.skipDelete = argv.skipDelete;
}
}

gtfs.import(getConfig(), err => {
if (err) {
handleError(err);
}

return config;
})
.catch(err => {
console.error(new Error(`Cannot find configuration file at \`${argv.configPath}\`. Use config-sample.json as a starting point, pass --configPath option`));
handleError(err);
})
.then(gtfs.import)
.then(() => {
process.exit();
});
})
.catch(handleError);

0 comments on commit 4594f3e

Please sign in to comment.