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

Allow piping into semver to read versions #161

Closed
wants to merge 5 commits into from
Closed
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
26 changes: 23 additions & 3 deletions bin/semver
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,29 @@ var argv = process.argv.slice(2)
, semver = require("../semver")
, reverse = false

main()
preMain()

function main () {
if (!argv.length) return help()
function preMain() {
if (!process.stdin.isTTY) {
process.stdin.setEncoding('utf8');

process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
versions = versions.concat(chunk.split(/\s/));
}
});

process.stdin.on('end', () => {
main(versions.length !== 0);
});
} else {
main(false);
}
}

function main (piped) {
if (!argv.length && !piped) return help()
while (argv.length) {
var a = argv.shift()
var i = a.indexOf('=')
Expand Down Expand Up @@ -56,6 +75,7 @@ function main () {
break
case "-h": case "--help": case "-?":
return help()
break
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the break here was missing. This is not the main purpose of the patch though.

Copy link
Contributor

@isaacs isaacs Jun 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A break isn't strictly necessary here, since the return is just as effective at ending the block. But if a linter or something is complaining about it, whatever.

default:
versions.push(a)
break
Expand Down
39 changes: 39 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var test = require('tap').test;
var execSync = require('child_process').execSync
var exec = require('child_process').exec;

test('the command line using pipes or arguments produces the same output', function (t) {
if (process.version.substring(0,5) === 'v0.10' || process.version.substring(0,5) === 'v0.12') {
t.plan(6);
var bin = 'node ./bin/semver ';
[[bin+'-i 1.0.0', 'echo 1.0.0 | '+bin+'-i'],
[bin+'-i major 1.0.0', 'echo 1.0.0 | '+bin+'-i major'],
[bin+'-i prerelease 1.0.0', 'echo 1.0.0 | '+bin+'-i prerelease'],
[bin+'-i preminor 1.0.0', 'echo 1.0.0 | '+bin+'-i preminor'],
[bin+'-i prepatch --preid alpha 1.0.0', 'echo 1.0.0 | '+bin+'-i prepatch --preid alpha'],
[bin+'-r \\>1.0.0 0.8.0 1.1.1', 'echo 0.8.0 1.1.1 | '+bin+'-r \\>1.0.0']
].forEach(function(tab) {
exec(tab[0], function(error, stdout, stderr) {
var resCli = stdout + stderr;
exec(tab[1], function(error, stdout, stderr) {
var resPipe = stdout + stderr;
t.assert(resCli === resPipe);
});
});
});
} else {
var bin = 'node ./bin/semver ';
[[bin+'-i 1.0.0', 'echo 1.0.0 | '+bin+'-i'],
[bin+'-i major 1.0.0', 'echo 1.0.0 | '+bin+'-i major'],
[bin+'-i prerelease 1.0.0', 'echo 1.0.0 | '+bin+'-i prerelease'],
[bin+'-i preminor 1.0.0', 'echo 1.0.0 | '+bin+'-i preminor'],
[bin+'-i prepatch --preid alpha 1.0.0', 'echo 1.0.0 | '+bin+'-i prepatch --preid alpha'],
[bin+'-r \\>1.0.0 0.8.0 1.1.1', 'echo 0.8.0 1.1.1 | '+bin+'-r \\>1.0.0']
].forEach(function(tab) {
var resCli = execSync(tab[0]);
var resPipe = execSync(tab[1]);
t.assert(resCli.equals(resPipe));
});
t.end();
}
})