Skip to content

Commit

Permalink
cli tweak to verbose (#25)
Browse files Browse the repository at this point in the history
* cli tweak to verbose

* tweak testcase
  • Loading branch information
xudafeng committed Jun 6, 2017
1 parent dee99af commit 4a9e127
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -94,6 +94,9 @@ $ detect
# detect pointed port
$ detect 80

# output verbose log
$ detect --verbose

# more help
$ detect --help
```
Expand Down
37 changes: 26 additions & 11 deletions bin/detect-port
Expand Up @@ -5,25 +5,42 @@
const pkg = require('../package');

const args = process.argv.slice(2);
const arg_0 = args[0];
let arg_0 = args[0];

if (arg_0 && !!~['-v', '--version'].indexOf(arg_0.toLowerCase())) {
console.log(pkg.version);
process.exit(0);
}

const port = parseInt(arg_0, 10);
const removeByValue = (arr, val) => {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === val) {
arr.splice(i, 1);
break;
}
}
};

const main = require('..');

const port = parseInt(arg_0, 10);
const isVerbose = !!~args.indexOf('--verbose');

removeByValue(args, '--verbose');
arg_0 = args[0];

if (!arg_0) {
const random = Math.floor(9000 + Math.random() * (65535 - 9000));

main(random, (err, port) => {
if (err) {
console.log(`get available port failed with ${err}`);
if (isVerbose) {
if (err) {
console.log(`get available port failed with ${err}`);
}
console.log(`get available port ${port} randomly`);
} else {
console.log(port || random);
}
console.log(`get available port ${port} randomly`);
});
} else if (isNaN(port)) {
console.log();
Expand All @@ -36,20 +53,16 @@ if (!arg_0) {
console.log(' Options:');
console.log();
console.log(' -v, --version output version and exit');
console.log(' -s, --silent output port without verbose log');
console.log(' -h, --help output usage information');
console.log(' --verbose output verbose log');
console.log();
console.log(' Further help:');
console.log();
console.log(` ${pkg.homepage}`);
console.log();
} else {
const isSilent = !!~process.argv.indexOf('-s') || !!~process.argv.indexOf('--silent');

main(port, (err, _port) => {
if (isSilent) {
console.log(_port || port);
} else {
if (isVerbose) {
if (err) {
console.log(`get available port failed with ${err}`);
}
Expand All @@ -59,6 +72,8 @@ if (!arg_0) {
}

console.log(`get available port ${_port}`);
} else {
console.log(_port || port);
}
});
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "detect-port",
"version": "1.1.4",
"version": "1.2.0",
"description": "detect available port",
"keywords": [
"detect",
Expand Down
11 changes: 8 additions & 3 deletions test/cli.test.js
@@ -1,9 +1,9 @@
'use strict';

const path = require('path');
const CliTest = require('command-line-test');
const assert = require('assert');
const pkg = require('../package');
const CliTest = require('command-line-test');

const cliTest = new CliTest();
const binFile = path.resolve(pkg.bin[pkg.name]);
Expand All @@ -30,15 +30,20 @@ describe('command-line tool test', () => {

it('should output available port randomly', function* () {
const res = yield cliTest.execFile(binFile, [], {});
const port = parseInt(res.stdout.split(' ')[3], 10);
const port = parseInt(res.stdout.trim(), 10);
assert(port >= 9000 && port < 65535);
});

it('should output available port from the given port', function* () {
const givenPort = 9000;
const res = yield cliTest.execFile(binFile, [ givenPort ], {});
const port = parseInt(res.stdout.split(' ')[3], 10);
const port = parseInt(res.stdout.trim(), 10);
assert(port >= givenPort && port < 65535);
});

it('should output verbose logs', function* () {
const res = yield cliTest.execFile(binFile, [ '--verbose' ], {});
assert(res.stdout.includes('random'));
});

});
8 changes: 4 additions & 4 deletions test/detect-port.test.js
@@ -1,12 +1,12 @@
'use strict';

const assert = require('assert');
const net = require('net');
const pedding = require('pedding');
const address = require('address');
const mm = require('mm');
const dns = require('dns');
const net = require('net');
const detectPort = require('..');
const assert = require('assert');
const pedding = require('pedding');
const address = require('address');

describe('detect port test', () => {
const servers = [];
Expand Down

0 comments on commit 4a9e127

Please sign in to comment.