fix(js): fix yarn3 verdaccio setup (#17742)

This commit is contained in:
Jason Jean 2023-06-22 23:27:59 -04:00 committed by GitHub
parent f176f213fb
commit 512b8ba911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 98 deletions

View File

@ -171,19 +171,11 @@ function setupNpm(options: VerdaccioExecutorSchema) {
}; };
} }
function getYarnUnsafeHttpWhitelist( function getYarnUnsafeHttpWhitelist(isYarnV1: boolean) {
isYarnV1: boolean, return !isYarnV1
options: VerdaccioExecutorSchema
) {
return isYarnV1
? new Set<string>( ? new Set<string>(
JSON.parse( JSON.parse(
execSync( execSync(`yarn config get unsafeHttpWhitelist --json`).toString()
`yarn config get unsafeHttpWhitelist --json` + options.location ===
'home'
? ' --home'
: ''
).toString()
) )
) )
: null; : null;
@ -193,21 +185,29 @@ function setYarnUnsafeHttpWhitelist(
currentWhitelist: Set<string>, currentWhitelist: Set<string>,
options: VerdaccioExecutorSchema options: VerdaccioExecutorSchema
) { ) {
if (currentWhitelist.size > 1) {
execSync( execSync(
`yarn config set unsafeHttpWhitelist --json '${JSON.stringify( `yarn config set unsafeHttpWhitelist --json '${JSON.stringify(
Array.from(currentWhitelist) Array.from(currentWhitelist)
)}'` + )}'` + (options.location === 'user' ? ' --home' : '')
options.location ===
'home'
? ' --home'
: ''
); );
} else {
execSync(
`yarn config unset unsafeHttpWhitelist` +
(options.location === 'user' ? ' --home' : '')
);
}
} }
function setupYarn(options: VerdaccioExecutorSchema) { function setupYarn(options: VerdaccioExecutorSchema) {
try { let isYarnV1;
const isYarnV1 = major(execSync('yarn --version').toString().trim()) === 1;
try {
isYarnV1 = major(execSync('yarn --version').toString().trim()) === 1;
} catch {
// This would fail if yarn is not installed which is okay
return () => {};
}
try { try {
const registryConfigName = isYarnV1 ? 'registry' : 'npmRegistryServer'; const registryConfigName = isYarnV1 ? 'registry' : 'npmRegistryServer';
@ -218,23 +218,17 @@ function setupYarn(options: VerdaccioExecutorSchema) {
execSync( execSync(
`yarn config set ${registryConfigName} http://localhost:${options.port}/` + `yarn config set ${registryConfigName} http://localhost:${options.port}/` +
options.location === (options.location === 'user' ? ' --home' : '')
'home'
? ' --home'
: ''
); );
logger.info(`Set yarn registry to http://localhost:${options.port}/`); logger.info(`Set yarn registry to http://localhost:${options.port}/`);
const currentWhitelist: Set<string> | null = getYarnUnsafeHttpWhitelist( const currentWhitelist: Set<string> | null =
isYarnV1, getYarnUnsafeHttpWhitelist(isYarnV1);
options
);
let whitelistedLocalhost = false; let whitelistedLocalhost = false;
if (!isYarnV1) { if (!isYarnV1 && !currentWhitelist.has('localhost')) {
if (!currentWhitelist.has('localhost')) {
whitelistedLocalhost = true; whitelistedLocalhost = true;
currentWhitelist.add('localhost'); currentWhitelist.add('localhost');
@ -243,35 +237,29 @@ function setupYarn(options: VerdaccioExecutorSchema) {
`Whitelisted http://localhost:${options.port}/ as an unsafe http server` `Whitelisted http://localhost:${options.port}/ as an unsafe http server`
); );
} }
}
return () => { return () => {
try { try {
if (yarnRegistryPath) { if (yarnRegistryPath) {
execSync( execSync(
`yarn config set ${registryConfigName} ${yarnRegistryPath}` + `yarn config set ${registryConfigName} ${yarnRegistryPath}` +
options.location === (options.location === 'user' ? ' --home' : '')
'home'
? ' --home'
: ''
); );
logger.info( logger.info(
`Reset yarn ${registryConfigName} to ${yarnRegistryPath}` `Reset yarn ${registryConfigName} to ${yarnRegistryPath}`
); );
} else { } else {
execSync( execSync(
`yarn config delete ${registryConfigName}` + options.location === `yarn config ${
'home' isYarnV1 ? 'delete' : 'unset'
? ' --home' } ${registryConfigName}` +
: '' (options.location === 'user' ? ' --home' : '')
); );
} }
if (whitelistedLocalhost) { if (whitelistedLocalhost) {
const currentWhitelist: Set<string> = getYarnUnsafeHttpWhitelist( const currentWhitelist: Set<string> =
isYarnV1, getYarnUnsafeHttpWhitelist(isYarnV1);
options
);
if (currentWhitelist.has('localhost')) { if (currentWhitelist.has('localhost')) {
currentWhitelist.delete('localhost'); currentWhitelist.delete('localhost');
@ -291,9 +279,6 @@ function setupYarn(options: VerdaccioExecutorSchema) {
`Failed to set yarn registry to http://localhost:${options.port}/: ${e.message}` `Failed to set yarn registry to http://localhost:${options.port}/: ${e.message}`
); );
} }
} catch (e) {
return () => {};
}
} }
export default verdaccioExecutor; export default verdaccioExecutor;

View File

@ -40,10 +40,16 @@ export function startLocalRegistry({
const registry = `http://localhost:${port}`; const registry = `http://localhost:${port}`;
process.env.npm_config_registry = registry; process.env.npm_config_registry = registry;
process.env.YARN_REGISTRY = registry;
execSync( execSync(
`npm config set //localhost:${port}/:_authToken "secretVerdaccioToken"` `npm config set //localhost:${port}/:_authToken "secretVerdaccioToken"`
); );
// yarnv1
process.env.YARN_REGISTRY = registry;
// yarnv2
process.env.YARN_NPM_REGISTRY_SERVER = registry;
process.env.YARN_UNSAFE_HTTP_WHITELIST = 'localhost';
console.log('Set npm and yarn config registry to ' + registry); console.log('Set npm and yarn config registry to ' + registry);
resolve(() => { resolve(() => {