| 1 | #!/usr/bin/env node
|
|---|
| 2 |
|
|---|
| 3 | 'use strict';
|
|---|
| 4 |
|
|---|
| 5 | const pkg = require('../package');
|
|---|
| 6 |
|
|---|
| 7 | const args = process.argv.slice(2);
|
|---|
| 8 | const arg_0 = args[0];
|
|---|
| 9 |
|
|---|
| 10 | if (!!~['-v', '--version'].indexOf(arg_0)) {
|
|---|
| 11 | console.log(pkg.version);
|
|---|
| 12 | process.exit(0);
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | const port = parseInt(arg_0, 10);
|
|---|
| 16 |
|
|---|
| 17 | const main = require('..');
|
|---|
| 18 |
|
|---|
| 19 | if (!arg_0) {
|
|---|
| 20 | const random = Math.floor(9000 + Math.random() * (65535 - 9000));
|
|---|
| 21 |
|
|---|
| 22 | main(random, (err, port) => {
|
|---|
| 23 | if (err) {
|
|---|
| 24 | console.log(`get available port failed with ${err}`);
|
|---|
| 25 | }
|
|---|
| 26 | console.log(`get available port ${port} randomly`);
|
|---|
| 27 | });
|
|---|
| 28 | } else if (isNaN(port)) {
|
|---|
| 29 | console.log();
|
|---|
| 30 | console.log(` \u001b[37m${pkg.description}\u001b[0m`);
|
|---|
| 31 | console.log();
|
|---|
| 32 | console.log(' Usage:');
|
|---|
| 33 | console.log();
|
|---|
| 34 | console.log(` ${pkg.name} [port]`);
|
|---|
| 35 | console.log();
|
|---|
| 36 | console.log(' Options:');
|
|---|
| 37 | console.log();
|
|---|
| 38 | console.log(' -v, --version show version and exit');
|
|---|
| 39 | console.log(' -h, --help output usage information');
|
|---|
| 40 | console.log();
|
|---|
| 41 | console.log(' Further help:');
|
|---|
| 42 | console.log();
|
|---|
| 43 | console.log(` ${pkg.homepage}`);
|
|---|
| 44 | console.log();
|
|---|
| 45 | } else {
|
|---|
| 46 | main(port, (err, _port) => {
|
|---|
| 47 | if (err) {
|
|---|
| 48 | console.log(`get available port failed with ${err}`);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | if (port !== _port) {
|
|---|
| 52 | console.log(`port ${port} was occupied`);
|
|---|
| 53 | }
|
|---|
| 54 | console.log(`get available port ${_port}`);
|
|---|
| 55 | });
|
|---|
| 56 | }
|
|---|