Skip to content

Commit

Permalink
feat(version): support --version flag (#138)
Browse files Browse the repository at this point in the history
This adds support for the `--version` CLI flag. This outputs the version
number for both shx and the bundled version of ShellJS. This adds a test
to strictly assert the output format, so that this can be reliably
parsed if necessary.

This also reorganizes the structure of some test cases (without changing
their logic).

Lastly, this changes the file modes for 2 files. These were previously
unnecessarily executable.

Fixes #111
Test: case: 'supports --version'
  • Loading branch information
nfischer authored Jun 20, 2018
1 parent fb8ad28 commit c0d7b8b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Empty file modified src/printCmdRet.js
100755 → 100644
Empty file.
6 changes: 6 additions & 0 deletions src/shx.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ shell.help = help;

export function shx(argv) {
const parsedArgs = minimist(argv.slice(2), { stopEarly: true, boolean: true });
if (parsedArgs.version) {
const shxVersion = require('../package.json').version;
const shelljsVersion = require('shelljs/package.json').version;
console.log(`shx v${shxVersion} (using ShellJS v${shelljsVersion})`);
return EXIT_CODES.SUCCESS;
}
const [fnName, ...args] = parsedArgs._;
if (!fnName) {
console.error('Error: Missing ShellJS command name');
Expand Down
33 changes: 21 additions & 12 deletions test/specs/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,20 @@ describe('cli', () => {
output.stderr.should.equal('');
});

it('allows --silent to change config.silent', () => {
const output = cli('--silent', 'ls', 'fakeFileName');
output.stdout.should.equal('');
output.stderr.should.equal('');
output.code.should.equal(2);
});
describe('global flags', () => {
it('supports --version', () => {
const output = cli('--version');
output.stdout.should.match(/shx v\S+ \(using ShellJS v\S+\)\n/);
output.stderr.should.equal('');
output.code.should.equal(0);
});

it('accepts stdin', () => {
mocks.stdin('foo\nbar\nfoobar');
const output = cli('grep', 'foo');
output.stdout.should.equal('foo\nfoobar\n');
output.stderr.should.equal('');
output.code.should.equal(0);
it('allows --silent to change config.silent', () => {
const output = cli('--silent', 'ls', 'fakeFileName');
output.stdout.should.equal('');
output.stderr.should.equal('');
output.code.should.equal(2);
});
});

describe('stdin', () => {
Expand All @@ -136,6 +137,14 @@ describe('cli', () => {
process.stdin.isTTY = oldTTY;
});

it('has basic support', () => {
mocks.stdin('foo\nbar\nfoobar');
const output = cli('grep', 'foo');
output.stdout.should.equal('foo\nfoobar\n');
output.stderr.should.equal('');
output.code.should.equal(0);
});

it('reads stdin for commands that accept stdin', () => {
process.stdin.isTTY = undefined;
shouldReadStdin(['cat']).should.equal(true);
Expand Down

0 comments on commit c0d7b8b

Please sign in to comment.