Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add folder mode #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ const {
},
'import-metadata': {
type: 'boolean',
},
'folder-mode': {
type: 'boolean',
}
},
allowPositionals: true,
Expand Down Expand Up @@ -148,6 +151,10 @@ Options:
- gtfs_data_imported_at (timestamp with time zone)
- gtfs_via_postgres_version (text)
- gtfs_via_postgres_options (jsonb)
--folder-mode Use folder name instead of file wildcard. Script automatically
parses file names in specified folder.
This mode works for Windows machines.
Example: gtfs-to-sql --folder-mode -- gtfs
Examples:
gtfs-to-sql some-gtfs/*.txt | sponge | psql -b # import into PostgreSQL
gtfs-to-sql -u -- some-gtfs/*.txt | gzip >gtfs.sql.gz # generate a gzipped SQL dump
Expand All @@ -167,11 +174,7 @@ const {basename, extname} = require('path')
const {pipeline} = require('stream')
const convertGtfsToSql = require('./index')
const DataError = require('./lib/data-error')

const files = args.map((file) => {
const name = basename(file, extname(file))
return {name, file}
})
const {readdirSync} = require('fs');

const opt = {
silent: !!flags.silent,
Expand All @@ -188,6 +191,7 @@ const opt = {
postgraphile: !!flags.postgraphile,
postgrest: !!flags.postgrest,
importMetadata: !!flags['import-metadata'],
folderMode: !!flags['folder-mode'],
}
if ('stops-without-level-id' in flags) {
opt.stopsWithoutLevelId = flags['stops-without-level-id']
Expand All @@ -210,6 +214,14 @@ if ('postgrest-query-cost-limit' in flags) {
opt.lowerCaseLanguageCodes = limit
}

const files = (opt.folderMode ? readdirSync(args[0]) : args).map((file) => {
const name = basename(file, extname(file))
return {
name,
file: opt.folderMode ? `${args[0]}/${file}` : file
}
});

pipeline(
convertGtfsToSql(files, opt),
process.stdout,
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ With the `--postgrest` flag, `gtfs-via-postgres` will augment the schema with a

[read more](docs/postgrest.md)

### Folder mode

With the `--folder-mode` flag, a folder name can be used instead of a file wildcard. All files in the given directory will be read and used in the script. Unsupported files can be ignored using the `-u` flag.

```
npm exec -- gtfs-to-sql --folder-mode -- gtfs | sponge | psql -b
```

### more guides

The [`docs` directory](docs) contains more instructions on how to use `gtfs-via-postgres`.
Expand Down