1 | const fs = require('fs-extra');
|
2 | const isPortReachable = require('is-port-reachable');
|
3 | const inquirer = require('inquirer');
|
4 |
|
5 |
|
6 | const getPathnameDevServerStart = require('../utils/get-pathname-dev-server-start');
|
7 | const getFreePort = require('./get-free-port');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const doValidatePort = async () => {
|
14 |
|
15 | if (__DEV__) {
|
16 | let infos;
|
17 | try {
|
18 | infos = await fs.readJson(getPathnameDevServerStart());
|
19 | } catch (e) {}
|
20 | if (typeof infos === 'object') return infos.port;
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | if (
|
29 | typeof process.env.SERVER_PORT === 'undefined' &&
|
30 | typeof __SERVER_PORT__ !== 'undefined'
|
31 | )
|
32 | process.env.SERVER_PORT = __SERVER_PORT__;
|
33 |
|
34 |
|
35 | const isPortInEnvFree = await isPortFree(process.env.SERVER_PORT);
|
36 |
|
37 |
|
38 | if (isPortInEnvFree) return process.env.SERVER_PORT;
|
39 |
|
40 |
|
41 | logPortTaken(process.env.SERVER_PORT);
|
42 |
|
43 |
|
44 | if (__DEV__) {
|
45 |
|
46 | await fs.writeFile(
|
47 | getPathnameDevServerStart(),
|
48 | `port ${process.env.SERVER_PORT} has been taken.`,
|
49 | 'utf-8'
|
50 | );
|
51 |
|
52 | return false;
|
53 | }
|
54 |
|
55 |
|
56 | let isPortTaken = true;
|
57 |
|
58 | let port;
|
59 |
|
60 | while (isPortTaken) {
|
61 | const askForPort = await inquirer.prompt([
|
62 | {
|
63 | type: 'input',
|
64 | name: 'port',
|
65 | message:
|
66 | 'Please input a new port number (leave empty for cancel)',
|
67 | validate: input => {
|
68 | if (!input) return true;
|
69 | if (isNaN(input)) return 'Must be a number or null';
|
70 | return true;
|
71 | }
|
72 | }
|
73 | ]);
|
74 | if (!askForPort.port) {
|
75 | isPortTaken = false;
|
76 | port = undefined;
|
77 | } else {
|
78 | port = await isPortFree(askForPort.port);
|
79 | isPortTaken = !port ? true : false;
|
80 | if (isPortTaken) logPortTaken(askForPort.port);
|
81 | }
|
82 | }
|
83 |
|
84 | if (port) return port;
|
85 | return false;
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | const isPortFree = async port => {
|
95 | const isPortOpen = !(await isPortReachable(port));
|
96 | if (isPortOpen) return port;
|
97 | return false;
|
98 | };
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | const logPortTaken = port => {
|
105 | console.log(
|
106 | `\x1b[31m×\x1b[0m ` +
|
107 | `\x1b[93m[koot/server]\x1b[0m port \x1b[32m${port}\x1b[0m has been taken.`
|
108 | );
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | const validatePort = async () => {
|
136 | const port = await doValidatePort();
|
137 | if (!port) return false;
|
138 | if (__DEV__) {
|
139 |
|
140 | const portFree = await getFreePort(port);
|
141 | process.env.SERVER_PORT = portFree;
|
142 | process.env.SERVER_PORT_DEV_MAIN = port;
|
143 | }
|
144 | return port;
|
145 | };
|
146 |
|
147 |
|
148 | module.exports = validatePort;
|