Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
990 bytes
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | const pRetry = require('p-retry');
|
---|
4 | const portfinder = require('portfinder');
|
---|
5 | const defaultPort = require('./defaultPort');
|
---|
6 | const defaultTo = require('./defaultTo');
|
---|
7 | const tryParseInt = require('./tryParseInt');
|
---|
8 |
|
---|
9 | function runPortFinder() {
|
---|
10 | return new Promise((resolve, reject) => {
|
---|
11 | portfinder.basePort = defaultPort;
|
---|
12 | portfinder.getPort((error, port) => {
|
---|
13 | if (error) {
|
---|
14 | return reject(error);
|
---|
15 | }
|
---|
16 |
|
---|
17 | return resolve(port);
|
---|
18 | });
|
---|
19 | });
|
---|
20 | }
|
---|
21 |
|
---|
22 | function findPort(port) {
|
---|
23 | if (port) {
|
---|
24 | return Promise.resolve(port);
|
---|
25 | }
|
---|
26 |
|
---|
27 | // Try to find unused port and listen on it for 3 times,
|
---|
28 | // if port is not specified in options.
|
---|
29 | // Because NaN == null is false, defaultTo fails if parseInt returns NaN
|
---|
30 | // so the tryParseInt function is introduced to handle NaN
|
---|
31 | const defaultPortRetry = defaultTo(
|
---|
32 | tryParseInt(process.env.DEFAULT_PORT_RETRY),
|
---|
33 | 3
|
---|
34 | );
|
---|
35 |
|
---|
36 | return pRetry(runPortFinder, { retries: defaultPortRetry });
|
---|
37 | }
|
---|
38 |
|
---|
39 | module.exports = findPort;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.