-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat: Enhance cli command line #107
Changes from 2 commits
9022da9
726a228
f00a08e
cb171c7
7d62465
998ca5e
f290505
aa288b9
3e5a7ea
9d6d134
aecc925
cadabd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,65 @@ | ||
const { inspect } = require('util') | ||
const commander = require('commander') | ||
const fs = require('fs') | ||
const fsPath = require('path') | ||
const GBFS = require('./') | ||
const pjson = require('./package.json'); | ||
|
||
if (!process.argv[2]) { | ||
console.error('Usage: gbfs-validator GBFS_URL') | ||
process.exit(1) | ||
getFeedValidationReport = async (url) => { | ||
const gbfs = new GBFS(url) | ||
return gbfs.validation() | ||
} | ||
|
||
const gbfs = new GBFS(process.argv[2]) | ||
parseOptions = () => { | ||
commander | ||
.version(pjson.version, '-v, --version') | ||
.usage('[OPTIONS]...') | ||
.option('-u, --url <dataset_folder>', 'URL of the GBFS feed') | ||
davidgamez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.option('-p, --print', 'Print report to console') | ||
.option('-vb, --verbose', 'Verbose mode prints debugging console logs') | ||
.option('-s, --save-report <report_path>', 'Local path to output report file') | ||
.parse(process.argv) | ||
|
||
if (require.main === module) { | ||
gbfs | ||
.validation() | ||
.then(result => console.log(inspect(result, { depth: null, colors: true }))) | ||
} else { | ||
module.exports = gbfs.validation() | ||
return commander.opts() | ||
} | ||
|
||
const options = parseOptions(); | ||
|
||
const saveReport = (report, filePath) => { | ||
const dirname = fsPath.dirname(filePath); | ||
if (!fs.existsSync(dirname)) { | ||
fs.mkdirSync(dirname, { recursive: true }); | ||
} | ||
fs.writeFileSync(filePath, JSON.stringify(report)) | ||
} | ||
|
||
const processFeedValidation = async () => { | ||
if (options.verbose) { | ||
console.log("Started GBFS validation with options: \n " + inspect(options, { depth: null, colors: true })) | ||
} | ||
try { | ||
const report = await getFeedValidationReport(options.url) | ||
if (options.print) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe make it default ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think changing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, what do you think about removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good solution too. My only concern is to drive the use of the command line too much. Having separate options give more control over what goes to a file and stdout. |
||
console.log(inspect(report, { depth: null, colors: true })) | ||
} | ||
if (options.saveReport) { | ||
saveReport(report, options.saveReport) | ||
} | ||
} catch (error) { | ||
console.error(`Critical error while validating GBFS feed => ${error}`) | ||
process.exit(1); | ||
} | ||
} | ||
|
||
if (options.url) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this parameter is mandatory, it may be preferable to pass the URL such as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I'll add the option as mandatory. I considered adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting for the local files :) |
||
processFeedValidation().then( | ||
() => { | ||
if (options.verbose) { | ||
console.log("Validation completed") | ||
} | ||
} | ||
) | ||
} else { | ||
commander.help() | ||
process.exit(1) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always
1.0.0
because we don't use it right now.We return the git hash instead https://github.com/MobilityData/gbfs-validator/blob/master/gbfs-validator/gbfs.js#L3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in preparation for publishing an npm package. In the case of the command line, if we would like to include the commit hash, more work is needed to make available the hash information on local computers.