Skip to content

Commit f04dd28

Browse files
committed
Cleanup
1 parent ae1ebf8 commit f04dd28

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

scripts/run-registry.ts

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import type { Server } from 'node:http';
55
import { join, resolve as resolvePath } from 'node:path';
66

77
import { program } from 'commander';
8-
import detectFreePort from 'detect-port';
9-
import killProcessOnPort from 'kill-port';
108
import pLimit from 'p-limit';
119
import picocolors from 'picocolors';
1210
import { parseConfigFile, runServer } from 'verdaccio';
1311

1412
import { npmAuth } from './npm-auth';
1513
import { maxConcurrentTasks } from './utils/concurrency';
1614
import { PACKS_DIRECTORY, ROOT_DIRECTORY } from './utils/constants';
15+
import { killPort } from './utils/port';
1716
import { getCodeWorkspaces } from './utils/workspace';
1817

1918
program
@@ -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-
5839
type Servers = { close: () => Promise<void> };
5940
const 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

258239
run().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
});

scripts/tasks/run-registry.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import detectFreePort from 'detect-port';
21
import waitOn from 'wait-on';
32

43
import type { Task } from '../task';
54
import { CODE_DIRECTORY } from '../utils/constants';
65
import { exec } from '../utils/exec';
6+
import { isPortUsed } from '../utils/port';
7+
8+
const REGISTRY_PORT = 6001;
9+
const VERDACCIO_PORT = 6002;
710

811
export async function runRegistry({ dryRun, debug }: { dryRun?: boolean; debug?: boolean }) {
912
const controller = new AbortController();
@@ -20,20 +23,19 @@ export async function runRegistry({ dryRun, debug }: { dryRun?: boolean; debug?:
2023
});
2124
await waitOn({
2225
log: true,
23-
resources: ['http://localhost:6001', 'http://localhost:6002'],
26+
resources: [`http://localhost:${REGISTRY_PORT}`, `http://localhost:${VERDACCIO_PORT}`],
2427
interval: 16,
2528
timeout: 20000,
2629
});
2730
return controller;
2831
}
2932

30-
const REGISTRY_PORT = 6001;
3133
export const runRegistryTask: Task = {
3234
description: 'Run the internal npm server',
3335
service: true,
3436
dependsOn: ['publish'],
3537
async ready() {
36-
return (await detectFreePort(REGISTRY_PORT)) !== REGISTRY_PORT;
38+
return (await isPortUsed(REGISTRY_PORT)) && (await isPortUsed(VERDACCIO_PORT));
3739
},
3840
async run(_, options) {
3941
return runRegistry(options);

scripts/utils/port.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import detectFreePort from 'detect-port';
2+
import killProcessOnPort from 'kill-port';
3+
4+
export const isPortUsed = async (port: number) => (await detectFreePort(port)) !== port;
5+
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
6+
7+
export const killPort = async (port: number) => {
8+
if (await isPortUsed(port)) {
9+
await killProcessOnPort(port);
10+
11+
let attempts = 0;
12+
while ((await isPortUsed(port)) && attempts < 20) {
13+
await sleep(1000);
14+
attempts++;
15+
}
16+
if (await isPortUsed(port)) {
17+
throw new Error(`Failed to free port ${port} after ${attempts} attempts`);
18+
}
19+
}
20+
};

0 commit comments

Comments
 (0)