1- const net = require ( 'net' ) ;
1+ const net = require ( 'net' ) ;
2+ const { execFile } = require ( 'child_process' ) ;
3+ let nextUniappTestPort = 9520 ;
24
35/**
46 * @description 检查端口是否被使用
57 * @param {Number } port
6- */
7- function isPortInUse ( port ) {
8- return new Promise ( ( resolve ) => {
9- const server = net . createServer ( ) . once ( 'error' , ( ) => {
10- resolve ( true ) ;
11- } ) . once ( 'listening' , ( ) => {
12- server . close ( ( ) => {
13- resolve ( false ) ;
14- } ) ;
15- } ) . listen ( port , '127.0.0.1' ) ;
16- } ) ;
8+ */
9+ function isPortInUse ( port ) {
10+ return new Promise ( ( resolve ) => {
11+ if ( process . platform == 'win32' ) {
12+ isPortInUseWithNetstat ( port ) . then ( resolve ) ;
13+ return ;
14+ } ;
15+ execFile ( 'lsof' , [ '-nP' , '-iTCP:' + port , '-sTCP:LISTEN' ] , ( error , stdout ) => {
16+ if ( ! error ) {
17+ resolve ( stdout . trim ( ) != '' ) ;
18+ return ;
19+ } ;
20+ if ( error . code === 1 ) {
21+ resolve ( false ) ;
22+ return ;
23+ } ;
24+ isPortInUseWithServer ( port ) . then ( resolve ) ;
25+ } ) ;
26+ } ) ;
27+ } ;
28+
29+ function isPortInUseWithNetstat ( port ) {
30+ return new Promise ( ( resolve ) => {
31+ execFile ( 'netstat' , [ '-ano' , '-p' , 'tcp' ] , ( error , stdout ) => {
32+ if ( error ) {
33+ isPortInUseWithServer ( port ) . then ( resolve ) ;
34+ return ;
35+ } ;
36+ const lines = stdout . split ( / \r ? \n / ) ;
37+ const portRegExp = new RegExp ( '(^|[^0-9])' + port + '([^0-9]|$)' ) ;
38+ const portInUse = lines . some ( line => {
39+ const columns = line . trim ( ) . split ( / \s + / ) ;
40+ if ( columns . length < 4 || columns [ 0 ] != 'TCP' ) {
41+ return false ;
42+ } ;
43+ return columns [ 3 ] == 'LISTENING' && portRegExp . test ( columns [ 1 ] ) ;
44+ } ) ;
45+ resolve ( portInUse ) ;
46+ } ) ;
47+ } ) ;
48+ } ;
49+
50+ function isPortInUseWithServer ( port ) {
51+ return new Promise ( ( resolve ) => {
52+ const server = net . createServer ( ) . once ( 'error' , ( ) => {
53+ resolve ( true ) ;
54+ } ) . once ( 'listening' , ( ) => {
55+ server . close ( ( ) => {
56+ resolve ( false ) ;
57+ } ) ;
58+ } ) . listen ( port ) ;
59+ } ) ;
1760} ;
1861
1962/**
2063 * @description 获取一个uni-app自动化测试可用的端口
21- */
22- async function findAvailableUniappTestPort ( ) {
23- let port = 9520 ;
24- let portInUse = true ;
25- while ( portInUse ) {
26- portInUse = await isPortInUse ( port ) ;
27- if ( portInUse ) {
28- port ++ ;
29- }
64+ */
65+ async function findAvailableUniappTestPort ( ) {
66+ let port = nextUniappTestPort ;
67+ let portInUse = true ;
68+ while ( portInUse ) {
69+ portInUse = await isPortInUse ( port ) ;
70+ if ( portInUse ) {
71+ port ++ ;
72+ }
3073 } ;
31- console . log ( "[自动化测试端口] is: " , port ) ;
32- return port ;
74+ nextUniappTestPort = port + 1 ;
75+ console . log ( "[自动化测试端口] is: " , port ) ;
76+ return port ;
3377} ;
3478
35- module . exports = findAvailableUniappTestPort ;
79+ module . exports = findAvailableUniappTestPort ;
80+
81+ // if (require.main === module) {
82+ // findAvailableUniappTestPort().then(port => {
83+ // console.log("Available port:", port);
84+ // });
85+ // };
0 commit comments