Skip to content

Commit 4594f3e

Browse files
committed
Promisify and reorganize import function
version bump
1 parent e4bbf69 commit 4594f3e

25 files changed

+2306
-332
lines changed

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ Install node-gtfs directly from [npm](https://npmjs.org):
3131
const gtfs = require('gtfs');
3232
const config = require('./config.json');
3333

34-
gtfs.import(config, (err) => {
35-
if (err) return console.error(err);
36-
37-
console.log('Import Successful')
34+
gtfs.import(config)
35+
.then(() => {
36+
console.log('Import Successful');
37+
})
38+
.catch(err => {
39+
console.error(err);
3840
});
3941

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

225-
gtfs.import(config, (err) => {
226-
if (err) return console.error(err);
227-
228-
console.log('Import Successful')
227+
gtfs.import(config)
228+
.then(() => {
229+
console.log('Import Successful');
230+
})
231+
.catch(err => {
232+
console.error(err);
229233
});
230234

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

251-
gtfs.import(config, (err) => {
252-
if (err) return console.error(err);
253-
254-
console.log('Import Successful')
255+
gtfs.import(config)
256+
.then(() => {
257+
console.log('Import Successful');
258+
})
259+
.catch(err => {
260+
console.error(err);
255261
});
256262

257263
## Query Methods

bin/gtfs-import.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env node
2+
23
const path = require('path');
4+
5+
const fs = require('fs-extra');
36
const untildify = require('untildify');
47
const argv = require('yargs')
58
.usage('Usage: $0 --config ./config.json')
@@ -20,31 +23,25 @@ const argv = require('yargs')
2023

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

23-
const configPath = path.resolve(untildify(argv.configPath));
24-
2526
function handleError(err) {
2627
console.error(err || 'Unknown Error');
2728
process.exit(1);
2829
}
2930

30-
function getConfig() {
31-
try {
32-
const config = require(configPath);
33-
34-
if (argv.skipDelete) {
35-
config.skipDelete = argv.skipDelete;
36-
}
37-
38-
return config;
39-
} catch (err) {
40-
handleError(new Error(`Cannot find configuration file at \`${configPath}\`. Use config-sample.json as a starting point, pass --configPath option`));
31+
fs.readFile(path.resolve(untildify(argv.configPath)), 'utf8')
32+
.then(data => JSON.parse(data))
33+
.then(config => {
34+
if (argv.skipDelete) {
35+
config.skipDelete = argv.skipDelete;
4136
}
42-
}
43-
44-
gtfs.import(getConfig(), err => {
45-
if (err) {
46-
handleError(err);
47-
}
48-
37+
return config;
38+
})
39+
.catch(err => {
40+
console.error(new Error(`Cannot find configuration file at \`${argv.configPath}\`. Use config-sample.json as a starting point, pass --configPath option`));
41+
handleError(err);
42+
})
43+
.then(gtfs.import)
44+
.then(() => {
4945
process.exit();
50-
});
46+
})
47+
.catch(handleError);

0 commit comments

Comments
 (0)