From 512b8ba911ea661a5a2b8f74d7dbcc1332c2824c Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Thu, 22 Jun 2023 23:27:59 -0400 Subject: [PATCH] fix(js): fix yarn3 verdaccio setup (#17742) --- .../src/executors/verdaccio/verdaccio.impl.ts | 165 ++++++++---------- .../src/plugins/jest/start-local-registry.ts | 8 +- 2 files changed, 82 insertions(+), 91 deletions(-) diff --git a/packages/js/src/executors/verdaccio/verdaccio.impl.ts b/packages/js/src/executors/verdaccio/verdaccio.impl.ts index 9973f77eac68a..e1badd1decc50 100644 --- a/packages/js/src/executors/verdaccio/verdaccio.impl.ts +++ b/packages/js/src/executors/verdaccio/verdaccio.impl.ts @@ -171,19 +171,11 @@ function setupNpm(options: VerdaccioExecutorSchema) { }; } -function getYarnUnsafeHttpWhitelist( - isYarnV1: boolean, - options: VerdaccioExecutorSchema -) { - return isYarnV1 +function getYarnUnsafeHttpWhitelist(isYarnV1: boolean) { + return !isYarnV1 ? new Set( JSON.parse( - execSync( - `yarn config get unsafeHttpWhitelist --json` + options.location === - 'home' - ? ' --home' - : '' - ).toString() + execSync(`yarn config get unsafeHttpWhitelist --json`).toString() ) ) : null; @@ -193,106 +185,99 @@ function setYarnUnsafeHttpWhitelist( currentWhitelist: Set, options: VerdaccioExecutorSchema ) { - execSync( - `yarn config set unsafeHttpWhitelist --json '${JSON.stringify( - Array.from(currentWhitelist) - )}'` + - options.location === - 'home' - ? ' --home' - : '' - ); + if (currentWhitelist.size > 1) { + execSync( + `yarn config set unsafeHttpWhitelist --json '${JSON.stringify( + Array.from(currentWhitelist) + )}'` + (options.location === 'user' ? ' --home' : '') + ); + } else { + execSync( + `yarn config unset unsafeHttpWhitelist` + + (options.location === 'user' ? ' --home' : '') + ); + } } function setupYarn(options: VerdaccioExecutorSchema) { + let isYarnV1; + + try { + isYarnV1 = major(execSync('yarn --version').toString().trim()) === 1; + } catch { + // This would fail if yarn is not installed which is okay + return () => {}; + } try { - const isYarnV1 = major(execSync('yarn --version').toString().trim()) === 1; + const registryConfigName = isYarnV1 ? 'registry' : 'npmRegistryServer'; - try { - const registryConfigName = isYarnV1 ? 'registry' : 'npmRegistryServer'; + const yarnRegistryPath = execSync(`yarn config get ${registryConfigName}`) + ?.toString() + ?.trim() + ?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes - const yarnRegistryPath = execSync(`yarn config get ${registryConfigName}`) - ?.toString() - ?.trim() - ?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes + execSync( + `yarn config set ${registryConfigName} http://localhost:${options.port}/` + + (options.location === 'user' ? ' --home' : '') + ); - execSync( - `yarn config set ${registryConfigName} http://localhost:${options.port}/` + - options.location === - '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 | null = + getYarnUnsafeHttpWhitelist(isYarnV1); - const currentWhitelist: Set | null = getYarnUnsafeHttpWhitelist( - isYarnV1, - options - ); + let whitelistedLocalhost = false; - let whitelistedLocalhost = false; + if (!isYarnV1 && !currentWhitelist.has('localhost')) { + whitelistedLocalhost = true; + currentWhitelist.add('localhost'); - if (!isYarnV1) { - if (!currentWhitelist.has('localhost')) { - whitelistedLocalhost = true; - currentWhitelist.add('localhost'); + setYarnUnsafeHttpWhitelist(currentWhitelist, options); + logger.info( + `Whitelisted http://localhost:${options.port}/ as an unsafe http server` + ); + } - setYarnUnsafeHttpWhitelist(currentWhitelist, options); + return () => { + try { + if (yarnRegistryPath) { + execSync( + `yarn config set ${registryConfigName} ${yarnRegistryPath}` + + (options.location === 'user' ? ' --home' : '') + ); logger.info( - `Whitelisted http://localhost:${options.port}/ as an unsafe http server` + `Reset yarn ${registryConfigName} to ${yarnRegistryPath}` + ); + } else { + execSync( + `yarn config ${ + isYarnV1 ? 'delete' : 'unset' + } ${registryConfigName}` + + (options.location === 'user' ? ' --home' : '') ); } - } - - return () => { - try { - if (yarnRegistryPath) { - execSync( - `yarn config set ${registryConfigName} ${yarnRegistryPath}` + - options.location === - 'home' - ? ' --home' - : '' - ); - logger.info( - `Reset yarn ${registryConfigName} to ${yarnRegistryPath}` - ); - } else { - execSync( - `yarn config delete ${registryConfigName}` + options.location === - 'home' - ? ' --home' - : '' - ); - } - if (whitelistedLocalhost) { - const currentWhitelist: Set = getYarnUnsafeHttpWhitelist( - isYarnV1, - options - ); + if (whitelistedLocalhost) { + const currentWhitelist: Set = + getYarnUnsafeHttpWhitelist(isYarnV1); - if (currentWhitelist.has('localhost')) { - currentWhitelist.delete('localhost'); + if (currentWhitelist.has('localhost')) { + currentWhitelist.delete('localhost'); - setYarnUnsafeHttpWhitelist(currentWhitelist, options); - logger.info( - `Removed http://localhost:${options.port}/ as an unsafe http server` - ); - } + setYarnUnsafeHttpWhitelist(currentWhitelist, options); + logger.info( + `Removed http://localhost:${options.port}/ as an unsafe http server` + ); } - } catch (e) { - throw new Error(`Failed to reset yarn registry: ${e.message}`); } - }; - } catch (e) { - throw new Error( - `Failed to set yarn registry to http://localhost:${options.port}/: ${e.message}` - ); - } + } catch (e) { + throw new Error(`Failed to reset yarn registry: ${e.message}`); + } + }; } catch (e) { - return () => {}; + throw new Error( + `Failed to set yarn registry to http://localhost:${options.port}/: ${e.message}` + ); } } diff --git a/packages/js/src/plugins/jest/start-local-registry.ts b/packages/js/src/plugins/jest/start-local-registry.ts index d11352950db1a..6c15a6953550f 100644 --- a/packages/js/src/plugins/jest/start-local-registry.ts +++ b/packages/js/src/plugins/jest/start-local-registry.ts @@ -40,10 +40,16 @@ export function startLocalRegistry({ const registry = `http://localhost:${port}`; process.env.npm_config_registry = registry; - process.env.YARN_REGISTRY = registry; execSync( `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); resolve(() => {