Skip to content

Commit

Permalink
fix(js): fix yarn setup to work with yarn3 (nrwl#17716)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and meeroslav committed Jun 23, 2023
1 parent dbbb4af commit b2e60e6
Showing 1 changed file with 117 additions and 26 deletions.
143 changes: 117 additions & 26 deletions packages/js/src/executors/verdaccio/verdaccio.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as detectPort from 'detect-port';
import { join, resolve } from 'path';

import { VerdaccioExecutorSchema } from './schema';
import { major } from 'semver';

let childProcess: ChildProcess;

Expand Down Expand Up @@ -170,39 +171,129 @@ function setupNpm(options: VerdaccioExecutorSchema) {
};
}

function setupYarn(options: VerdaccioExecutorSchema) {
try {
execSync('yarn --version');
} catch (e) {
return () => {};
}
function getYarnUnsafeHttpWhitelist(
isYarnV1: boolean,
options: VerdaccioExecutorSchema
) {
return isYarnV1
? new Set<string>(
JSON.parse(
execSync(
`yarn config get unsafeHttpWhitelist --json` + options.location ===
'home'
? ' --home'
: ''
).toString()
)
)
: null;
}

function setYarnUnsafeHttpWhitelist(
currentWhitelist: Set<string>,
options: VerdaccioExecutorSchema
) {
execSync(
`yarn config set unsafeHttpWhitelist --json '${JSON.stringify(
Array.from(currentWhitelist)
)}'` +
options.location ===
'home'
? ' --home'
: ''
);
}

let yarnRegistryPath;
function setupYarn(options: VerdaccioExecutorSchema) {
try {
yarnRegistryPath = execSync(`yarn config get registry`)
?.toString()
?.trim()
?.replace('\u001b[2K\u001b[1G', ''); // strip out ansi codes
execSync(`yarn config set registry http://localhost:${options.port}/`);
logger.info(`Set yarn registry to http://localhost:${options.port}/`);
} catch (e) {
throw new Error(
`Failed to set yarn registry to http://localhost:${options.port}/: ${e.message}`
);
}
const isYarnV1 = major(execSync('yarn --version').toString().trim()) === 1;

return () => {
try {
if (yarnRegistryPath) {
execSync(`yarn config set registry ${yarnRegistryPath}`);
logger.info(`Reset yarn registry to ${yarnRegistryPath}`);
} else {
execSync(`yarn config delete registry`);
const registryConfigName = isYarnV1 ? 'registry' : 'npmRegistryServer';

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 ===
'home'
? ' --home'
: ''
);

logger.info(`Set yarn registry to http://localhost:${options.port}/`);

const currentWhitelist: Set<string> | null = getYarnUnsafeHttpWhitelist(
isYarnV1,
options
);

let whitelistedLocalhost = false;

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`
);
}
}

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 (currentWhitelist.has('localhost')) {
currentWhitelist.delete('localhost');

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 reset yarn registry: ${e.message}`);
throw new Error(
`Failed to set yarn registry to http://localhost:${options.port}/: ${e.message}`
);
}
};
} catch (e) {
return () => {};
}
}

export default verdaccioExecutor;

0 comments on commit b2e60e6

Please sign in to comment.