From 19338bc06ffd96a403b0bf23d45f763d4ba2e574 Mon Sep 17 00:00:00 2001 From: Tine Kondo Date: Sat, 23 Nov 2024 16:40:10 +0000 Subject: [PATCH] feat(testing): allow customizing the default address used to start the local registry --- .../src/plugins/jest/start-local-registry.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/js/src/plugins/jest/start-local-registry.ts b/packages/js/src/plugins/jest/start-local-registry.ts index 3a87e00e92ba7..8df3a83819ae9 100644 --- a/packages/js/src/plugins/jest/start-local-registry.ts +++ b/packages/js/src/plugins/jest/start-local-registry.ts @@ -6,17 +6,20 @@ import { execSync, fork } from 'child_process'; * @param storage the storage location for the local registry * @param verbose whether to log verbose output * @param clearStorage whether to clear the verdaccio storage before running the registry + * @param listenAddress the address that verdaccio should listen to (default to `localhost`) */ export function startLocalRegistry({ localRegistryTarget, storage, verbose, clearStorage, + listenAddress = 'localhost', }: { localRegistryTarget: string; storage?: string; verbose?: boolean; clearStorage?: boolean; + listenAddress?: string; }) { if (!localRegistryTarget) { throw new Error(`localRegistryTarget is required`); @@ -36,17 +39,21 @@ export function startLocalRegistry({ const listener = (data) => { if (verbose) { process.stdout.write(data); + console.log('Waiting for local registry to start...'); } - if (data.toString().includes('http://localhost:')) { + if (data.toString().includes(`http://${listenAddress}:`)) { const port = parseInt( - data.toString().match(/localhost:(?\d+)/)?.groups?.port + data.toString().match(new RegExp(`${listenAddress}:(?\\d+)`)) + ?.groups?.port ); - console.log('Local registry started on port ' + port); - const registry = `http://localhost:${port}`; + const registry = `http://${listenAddress}:${port}`; + + console.log(`Local registry started on ${registry}`); + process.env.npm_config_registry = registry; execSync( - `npm config set //localhost:${port}/:_authToken "secretVerdaccioToken"`, + `npm config set //${listenAddress}:${port}/:_authToken "secretVerdaccioToken"`, { windowsHide: false, } @@ -56,13 +63,13 @@ export function startLocalRegistry({ process.env.YARN_REGISTRY = registry; // yarnv2 process.env.YARN_NPM_REGISTRY_SERVER = registry; - process.env.YARN_UNSAFE_HTTP_WHITELIST = 'localhost'; + process.env.YARN_UNSAFE_HTTP_WHITELIST = listenAddress; console.log('Set npm and yarn config registry to ' + registry); resolve(() => { childProcess.kill(); - execSync(`npm config delete //localhost:${port}/:_authToken`, { + execSync(`npm config delete //${listenAddress}:${port}/:_authToken`, { windowsHide: false, }); });