-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathcheck-port.ts
30 lines (27 loc) · 966 Bytes
/
check-port.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Observable } from 'rxjs';
const portfinder = require('portfinder');
export function checkPort(port: number, host: string, basePort = 49152): Observable<number> {
return new Observable(obs => {
portfinder.basePort = basePort;
// tslint:disable:no-any
portfinder.getPort({ port, host }, (err: any, foundPort: number) => {
if (err) {
obs.error(err);
} else if (port !== foundPort && port !== 0) {
// If the port isn't available and we weren't looking for any port, throw error.
obs.error(`Port ${port} is already in use. Use '--port' to specify a different port.`);
} else {
// Otherwise, our found port is good.
obs.next(foundPort);
obs.complete();
}
});
});
}