Skip to content

Commit 4a9e127

Browse files
authored
cli tweak to verbose (#25)
* cli tweak to verbose * tweak testcase
1 parent dee99af commit 4a9e127

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ $ detect
9494
# detect pointed port
9595
$ detect 80
9696

97+
# output verbose log
98+
$ detect --verbose
99+
97100
# more help
98101
$ detect --help
99102
```

bin/detect-port

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,42 @@
55
const pkg = require('../package');
66

77
const args = process.argv.slice(2);
8-
const arg_0 = args[0];
8+
let arg_0 = args[0];
99

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

15-
const port = parseInt(arg_0, 10);
15+
const removeByValue = (arr, val) => {
16+
for (var i = 0; i < arr.length; i++) {
17+
if (arr[i] === val) {
18+
arr.splice(i, 1);
19+
break;
20+
}
21+
}
22+
};
1623

1724
const main = require('..');
1825

26+
const port = parseInt(arg_0, 10);
27+
const isVerbose = !!~args.indexOf('--verbose');
28+
29+
removeByValue(args, '--verbose');
30+
arg_0 = args[0];
31+
1932
if (!arg_0) {
2033
const random = Math.floor(9000 + Math.random() * (65535 - 9000));
2134

2235
main(random, (err, port) => {
23-
if (err) {
24-
console.log(`get available port failed with ${err}`);
36+
if (isVerbose) {
37+
if (err) {
38+
console.log(`get available port failed with ${err}`);
39+
}
40+
console.log(`get available port ${port} randomly`);
41+
} else {
42+
console.log(port || random);
2543
}
26-
console.log(`get available port ${port} randomly`);
2744
});
2845
} else if (isNaN(port)) {
2946
console.log();
@@ -36,20 +53,16 @@ if (!arg_0) {
3653
console.log(' Options:');
3754
console.log();
3855
console.log(' -v, --version output version and exit');
39-
console.log(' -s, --silent output port without verbose log');
4056
console.log(' -h, --help output usage information');
57+
console.log(' --verbose output verbose log');
4158
console.log();
4259
console.log(' Further help:');
4360
console.log();
4461
console.log(` ${pkg.homepage}`);
4562
console.log();
4663
} else {
47-
const isSilent = !!~process.argv.indexOf('-s') || !!~process.argv.indexOf('--silent');
48-
4964
main(port, (err, _port) => {
50-
if (isSilent) {
51-
console.log(_port || port);
52-
} else {
65+
if (isVerbose) {
5366
if (err) {
5467
console.log(`get available port failed with ${err}`);
5568
}
@@ -59,6 +72,8 @@ if (!arg_0) {
5972
}
6073

6174
console.log(`get available port ${_port}`);
75+
} else {
76+
console.log(_port || port);
6277
}
6378
});
6479
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "detect-port",
3-
"version": "1.1.4",
3+
"version": "1.2.0",
44
"description": "detect available port",
55
"keywords": [
66
"detect",

test/cli.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

33
const path = require('path');
4-
const CliTest = require('command-line-test');
54
const assert = require('assert');
65
const pkg = require('../package');
6+
const CliTest = require('command-line-test');
77

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

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

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

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

test/detect-port.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
const assert = require('assert');
4-
const net = require('net');
5-
const pedding = require('pedding');
6-
const address = require('address');
73
const mm = require('mm');
84
const dns = require('dns');
5+
const net = require('net');
96
const detectPort = require('..');
7+
const assert = require('assert');
8+
const pedding = require('pedding');
9+
const address = require('address');
1010

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

0 commit comments

Comments
 (0)