File tree Expand file tree Collapse file tree 3 files changed +35
-7
lines changed Expand file tree Collapse file tree 3 files changed +35
-7
lines changed Original file line number Diff line number Diff line change 4242npm i detect-port
4343```
4444
45+ CommonJS
46+
4547``` javascript
46- const detect = require (' detect-port' );
47- /**
48- * use as a promise
49- */
48+ const { detect } = require (' detect-port' );
5049
5150detect (port)
52- .then (_port => {
53- if (port == _port ) {
51+ .then (realPort => {
52+ if (port == realPort ) {
5453 console .log (` port: ${ port} was not occupied` );
5554 } else {
56- console .log (` port: ${ port} was occupied, try port: ${ _port } ` );
55+ console .log (` port: ${ port} was occupied, try port: ${ realPort } ` );
5756 }
5857 })
5958 .catch (err => {
6059 console .log (err);
6160 });
61+ ```
62+
63+ ESM and TypeScript
6264
65+ ``` ts
66+ import { detect } from ' detect-port' ;
67+
68+ detect (port )
69+ .then (realPort => {
70+ if (port == realPort ) {
71+ console .log (` port: ${port } was not occupied ` );
72+ } else {
73+ console .log (` port: ${port } was occupied, try port: ${realPort } ` );
74+ }
75+ })
76+ .catch (err => {
77+ console .log (err );
78+ });
6379```
6480
6581## Command Line Tool
Original file line number Diff line number Diff line change @@ -3,4 +3,7 @@ import { detectPort } from './detect-port.js';
33export default detectPort ;
44
55export * from './detect-port.js' ;
6+ // keep alias detectPort to detect
7+ export const detect = detectPort ;
8+
69export * from './wait-port.js' ;
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import { strict as assert } from 'node:assert';
44import { ip } from 'address' ;
55import mm from 'mm' ;
66import { detectPort } from '../src/detect-port.js' ;
7+ import detect from '../src/index.js' ;
8+ import { detect as detect2 } from '../src/index.js' ;
79
810describe ( 'test/detect-port.test.ts' , ( ) => {
911 afterEach ( mm . restore ) ;
@@ -74,6 +76,13 @@ describe('test/detect-port.test.ts', () => {
7476 assert ( port >= 1024 && port < 65535 ) ;
7577 } ) ;
7678
79+ it ( 'should detect work' , async ( ) => {
80+ let port = await detect ( ) ;
81+ assert ( port >= 1024 && port < 65535 ) ;
82+ port = await detect2 ( ) ;
83+ assert ( port >= 1024 && port < 65535 ) ;
84+ } ) ;
85+
7786 it ( 'with occupied port, like "listen EACCES: permission denied"' , async ( ) => {
7887 const port = 80 ;
7988 const realPort = await detectPort ( port ) ;
You can’t perform that action at this time.
0 commit comments