Skip to content

Commit

Permalink
feat(testing): allow customizing the default address used to start th…
Browse files Browse the repository at this point in the history
…e local registry
  • Loading branch information
tinesoft authored and xiongemi committed Dec 2, 2024
1 parent 6b87005 commit 547dc7f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/js/src/plugins/jest/start-local-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ 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,
}: {
localRegistryTarget: string;
storage?: string;
verbose?: boolean;
clearStorage?: boolean;
listenAddress?: string;
}) {
listenAddress ??= 'localhost';
if (!localRegistryTarget) {
throw new Error(`localRegistryTarget is required`);
}
Expand All @@ -36,17 +40,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:(?<port>\d+)/)?.groups?.port
data.toString().match(new RegExp(`${listenAddress}:(?<port>\\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,
}
Expand All @@ -56,13 +64,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,
});
});
Expand Down

0 comments on commit 547dc7f

Please sign in to comment.