nx/scripts/local-registry/populate-storage.js
Jason Jean cf4a1f35e9
chore(repo): make local-registry continuous (#30789)
<!-- 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 -->

No continuous tasks are used in CI.

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

The local-registry is a continuous task and it is used in CI.

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

Fixes #
2025-04-28 20:45:34 -04:00

80 lines
2.3 KiB
JavaScript

// @ts-check
const { exec, execSync } = require('node:child_process');
const {
LARGE_BUFFER,
} = require('nx/src/executors/run-commands/run-commands.impl');
async function populateLocalRegistryStorage() {
const listenAddress = 'localhost';
const port = process.env.NX_LOCAL_REGISTRY_PORT ?? '4873';
const registry = `http://${listenAddress}:${port}`;
const authToken = 'secretVerdaccioToken';
while (true) {
await new Promise((resolve) => setTimeout(resolve, 250));
try {
await assertLocalRegistryIsRunning(registry);
break;
} catch {
console.log(`Waiting for Local registry to start on ${registry}...`);
}
}
process.env.npm_config_registry = registry;
// bun
process.env.BUN_CONFIG_REGISTRY = registry;
process.env.BUN_CONFIG_TOKEN = authToken;
// yarnv1
process.env.YARN_REGISTRY = registry;
// yarnv2
process.env.YARN_NPM_REGISTRY_SERVER = registry;
process.env.YARN_UNSAFE_HTTP_WHITELIST = listenAddress;
try {
const publishVersion = process.env.PUBLISHED_VERSION ?? 'major';
const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
console.log('Publishing packages to local registry to populate storage');
await runLocalRelease(publishVersion, isVerbose);
} catch (err) {
console.error('Error:', err);
process.exit(1);
}
}
exports.populateLocalRegistryStorage = populateLocalRegistryStorage;
function runLocalRelease(publishVersion, isVerbose) {
return new Promise((res, rej) => {
const publishProcess = exec(`pnpm nx-release --local ${publishVersion}`, {
env: process.env,
maxBuffer: LARGE_BUFFER,
});
let logs = Buffer.from('');
if (isVerbose) {
publishProcess?.stdout?.pipe(process.stdout);
publishProcess?.stderr?.pipe(process.stderr);
} else {
publishProcess?.stdout?.on('data', (data) => (logs += data));
publishProcess?.stderr?.on('data', (data) => (logs += data));
}
publishProcess.on('exit', (code) => {
if (code && code > 0) {
if (!isVerbose) {
console.log(logs.toString());
}
rej(code);
}
res(undefined);
});
});
}
exports.runLocalRelease = runLocalRelease;
async function assertLocalRegistryIsRunning(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
}