-
Notifications
You must be signed in to change notification settings - Fork 28
/
detect-port
executable file
·64 lines (56 loc) · 1.65 KB
/
detect-port
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
/* ================================================================
* detect-port by xdf(xudafeng[at]126.com)
*
* first created at : Tue Mar 17 2015 00:16:10 GMT+0800 (CST)
*
* ================================================================
* Copyright 2013 xdf
*
* Licensed under the MIT License
* You may not use this file except in compliance with the License.
*
* ================================================================ */
'use strict';
const pkg = require('../package');
const args = process.argv.slice(2);
const arg_0 = args[0];
if (!!~['-v', '--version'].indexOf(arg_0)) {
console.log(pkg.version);
process.exit(0);
}
const port = parseInt(arg_0, 10);
const main = require('..');
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}`);
}
console.log(`get available port ${port} randomly`);
});
} else if (isNaN(port)) {
console.log();
console.log(` \u001b[37m${pkg.description}\u001b[0m`);
console.log();
console.log(' Usage:');
console.log();
console.log(` ${pkg.name} [port]`);
console.log();
console.log(' Options:');
console.log();
console.log(' -v, --version show version and exit');
console.log(' -h, --help output usage information');
console.log();
console.log(' Further help:');
console.log();
console.log(` ${pkg.homepage}`);
console.log();
} else {
main(port, (err, port) => {
if (err) {
console.log(`get available port failed with ${err}`);
}
console.log(`get available port ${port}`);
});
}