source: trip-planner-front/node_modules/webpack-dev-server/lib/utils/findPort.js

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
RevLine 
[6a3a178]1'use strict';
2
3const pRetry = require('p-retry');
4const portfinder = require('portfinder');
5const defaultPort = require('./defaultPort');
6const defaultTo = require('./defaultTo');
7const tryParseInt = require('./tryParseInt');
8
9function 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
22function 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
39module.exports = findPort;
Note: See TracBrowser for help on using the repository browser.