This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: CLI parsing of --silent arg (#1955)
* fix: create helper function to parse global options * fix: use only one yargs instance * fix: export yargs parser and add some tests * fix: separate yargs parser for ease of testing * fix: remove unneeded dependency fixes #1947
- Loading branch information
Showing
3 changed files
with
119 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict' | ||
|
||
const yargs = require('yargs') | ||
const utils = require('./utils') | ||
const print = utils.print | ||
|
||
const parser = yargs | ||
.option('silent', { | ||
desc: 'Write no output', | ||
type: 'boolean', | ||
default: false, | ||
coerce: silent => { | ||
if (silent) utils.disablePrinting() | ||
return silent | ||
} | ||
}) | ||
.option('pass', { | ||
desc: 'Pass phrase for the keys', | ||
type: 'string', | ||
default: '' | ||
}) | ||
.epilog(utils.ipfsPathHelp) | ||
.demandCommand(1) | ||
.fail((msg, err, yargs) => { | ||
if (err) { | ||
throw err // preserve stack | ||
} | ||
print(msg) | ||
yargs.showHelp() | ||
}) | ||
.commandDir('commands') | ||
.middleware(argv => { | ||
// Function to get hold of a singleton ipfs instance | ||
argv.getIpfs = utils.singleton(cb => utils.getIPFS(argv, cb)) | ||
return argv | ||
}) | ||
.help() | ||
.strict() | ||
.completion() | ||
|
||
module.exports = parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
chai.use(dirtyChai) | ||
const parser = require('../../src/cli/parser') | ||
const YargsPromise = require('yargs-promise') | ||
|
||
describe('yargs cli parser', () => { | ||
let cli | ||
|
||
before(() => { | ||
cli = new YargsPromise(parser) | ||
}) | ||
|
||
it('should handle --silent flag correctly', (done) => { | ||
cli | ||
.parse('serve --silent src/init-files/init-docs/readme') | ||
.then(({ error, argv }) => { | ||
expect(error).to.not.exist() | ||
expect(argv).to.include({ silent: true, pass: '' }) | ||
expect(argv.getIpfs.instance).to.exist() | ||
done() | ||
}) | ||
.catch(({ error }) => { | ||
done(error) | ||
}) | ||
}) | ||
|
||
it('should handle --pass flag correctly', (done) => { | ||
cli | ||
.parse('serve --pass password') | ||
.then(({ error, argv }) => { | ||
expect(error).to.not.exist() | ||
expect(argv).to.include({ silent: true, pass: '' }) | ||
expect(argv.getIpfs.instance).to.exist() | ||
done() | ||
}) | ||
.catch(({ error }) => { | ||
done(error) | ||
}) | ||
}) | ||
}) |