Skip to content

Commit

Permalink
feat(testing): allow custom address for local registry (#29050)
Browse files Browse the repository at this point in the history
… output<!-- Please make sure you have read the submission guidelines
before posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
The `startLocalRegistry()` always assumed that the local registry was
started on `localhost` , which is not necessary the case, if for example
user has set the `listenAddress` option in the underlying `verdaccio
executor` or via the `verdaccio config
file`https://github.com/nrwl/nx/blob/128778e7d1a8d7f2b078b2401d47270c0c15a2ce/packages/js/src/executors/verdaccio/verdaccio.impl.ts#L130

As a result the `startLocalregistry()`, will never return as, it is
waiting forever to detect the local registry URL in the process output:
https://github.com/nrwl/nx/blob/128778e7d1a8d7f2b078b2401d47270c0c15a2ce/packages/js/src/plugins/jest/start-local-registry.ts#L40

This customization of `verdaccio` listen address is something required,
namely when running within a **devcontainer**. In that case, the address
must be set to `0.0.0.0`, so that the registry can be accessed from
`http://localhost:4873` from host machine.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Allow customizing the `listenAddress` when starting the local registry,
via an additional option that can be provided when calling the method.
It default value will be `localhost`.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #28238
  • Loading branch information
tinesoft authored Dec 10, 2024
1 parent ee91b63 commit cfb67cf
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 cfb67cf

Please sign in to comment.