Skip to content

Commit

Permalink
fix(js): fix yarn3 verdaccio setup (#17742)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Jun 23, 2023
1 parent f176f21 commit 512b8ba
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 91 deletions.
165 changes: 75 additions & 90 deletions packages/js/src/executors/verdaccio/verdaccio.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,11 @@ function setupNpm(options: VerdaccioExecutorSchema) {
};
}

function getYarnUnsafeHttpWhitelist(
isYarnV1: boolean,
options: VerdaccioExecutorSchema
) {
return isYarnV1
function getYarnUnsafeHttpWhitelist(isYarnV1: boolean) {
return !isYarnV1
? new Set<string>(
JSON.parse(
execSync(
`yarn config get unsafeHttpWhitelist --json` + options.location ===
'home'
? ' --home'
: ''
).toString()
execSync(`yarn config get unsafeHttpWhitelist --json`).toString()
)
)
: null;
Expand All @@ -193,106 +185,99 @@ function setYarnUnsafeHttpWhitelist(
currentWhitelist: Set<string>,
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<string> | null =
getYarnUnsafeHttpWhitelist(isYarnV1);

const currentWhitelist: Set<string> | 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<string> = getYarnUnsafeHttpWhitelist(
isYarnV1,
options
);
if (whitelistedLocalhost) {
const currentWhitelist: Set<string> =
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}`
);
}
}

Expand Down
8 changes: 7 additions & 1 deletion packages/js/src/plugins/jest/start-local-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

1 comment on commit 512b8ba

@vercel
Copy link

@vercel vercel bot commented on 512b8ba Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.