@@ -5,15 +5,14 @@ import type { Server } from 'node:http';
55import { join , resolve as resolvePath } from 'node:path' ;
66
77import { program } from 'commander' ;
8- import detectFreePort from 'detect-port' ;
9- import killProcessOnPort from 'kill-port' ;
108import pLimit from 'p-limit' ;
119import picocolors from 'picocolors' ;
1210import { parseConfigFile , runServer } from 'verdaccio' ;
1311
1412import { npmAuth } from './npm-auth' ;
1513import { maxConcurrentTasks } from './utils/concurrency' ;
1614import { PACKS_DIRECTORY , ROOT_DIRECTORY } from './utils/constants' ;
15+ import { killPort } from './utils/port' ;
1716import { getCodeWorkspaces } from './utils/workspace' ;
1817
1918program
@@ -37,29 +36,11 @@ const pathExists = async (p: string) => {
3736 }
3837} ;
3938
40- const isPortUsed = async ( port : number ) => ( await detectFreePort ( port ) ) !== port ;
41- const sleep = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
42-
43- const kill = async ( port : number ) => {
44- if ( await isPortUsed ( port ) ) {
45- await killProcessOnPort ( port ) ;
46-
47- let attempts = 0 ;
48- while ( ( await isPortUsed ( port ) ) && attempts < 10 ) {
49- await sleep ( 1000 ) ;
50- attempts ++ ;
51- }
52- if ( await isPortUsed ( port ) ) {
53- throw new Error ( `Failed to free port ${ port } after ${ attempts } attempts` ) ;
54- }
55- }
56- } ;
57-
5839type Servers = { close : ( ) => Promise < void > } ;
5940const startVerdaccio = async ( ) => {
6041 // Kill Verdaccio related processes if they are already running
61- await kill ( 6001 ) ;
62- await kill ( 6002 ) ;
42+ await killPort ( 6001 ) ;
43+ await killPort ( 6002 ) ;
6344
6445 const ready = {
6546 proxy : false ,
@@ -256,7 +237,10 @@ const run = async () => {
256237} ;
257238
258239run ( ) . catch ( async ( e ) => {
259- await servers ?. close ( ) ;
260- await rm ( join ( root , '.npmrc' ) , { force : true } ) ;
261- throw e ;
240+ try {
241+ await servers ?. close ( ) ;
242+ } finally {
243+ await rm ( join ( root , '.npmrc' ) , { force : true } ) ;
244+ throw e ;
245+ }
262246} ) ;
0 commit comments