1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.checkPort = void 0;
|
---|
30 | const inquirer_1 = require("inquirer");
|
---|
31 | const net = __importStar(require("net"));
|
---|
32 | const tty_1 = require("./tty");
|
---|
33 | function createInUseError(port) {
|
---|
34 | return new Error(`Port ${port} is already in use. Use '--port' to specify a different port.`);
|
---|
35 | }
|
---|
36 | async function checkPort(port, host) {
|
---|
37 | if (port === 0) {
|
---|
38 | return 0;
|
---|
39 | }
|
---|
40 | return new Promise((resolve, reject) => {
|
---|
41 | const server = net.createServer();
|
---|
42 | server
|
---|
43 | .once('error', (err) => {
|
---|
44 | if (err.code !== 'EADDRINUSE') {
|
---|
45 | reject(err);
|
---|
46 | return;
|
---|
47 | }
|
---|
48 | if (!tty_1.isTTY) {
|
---|
49 | reject(createInUseError(port));
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | inquirer_1.prompt({
|
---|
53 | type: 'confirm',
|
---|
54 | name: 'useDifferent',
|
---|
55 | message: `Port ${port} is already in use.\nWould you like to use a different port?`,
|
---|
56 | default: true,
|
---|
57 | }).then((answers) => (answers.useDifferent ? resolve(0) : reject(createInUseError(port))), () => reject(createInUseError(port)));
|
---|
58 | })
|
---|
59 | .once('listening', () => {
|
---|
60 | server.close();
|
---|
61 | resolve(port);
|
---|
62 | })
|
---|
63 | .listen(port, host);
|
---|
64 | });
|
---|
65 | }
|
---|
66 | exports.checkPort = checkPort;
|
---|