Skip to content

Commit

Permalink
fix(@schematics/update): update pacote to version 11
Browse files Browse the repository at this point in the history
With this change Pacote is updated to version 11.1.13.

This also requires normalization of options because Pacote now passes the options to `npm-registry-fetch` which requires some options to be camelCased.

Partially addresses #19624
  • Loading branch information
alan-agius4 authored and filipesilva committed Dec 16, 2020
1 parent 36889b6 commit 17cc701
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/schematics/update/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@yarnpkg/lockfile": "1.1.0",
"ini": "2.0.0",
"npm-package-arg": "^8.0.0",
"pacote": "9.5.12",
"pacote": "11.1.13",
"semver": "7.3.4",
"semver-intersect": "1.4.0"
}
Expand Down
59 changes: 42 additions & 17 deletions packages/schematics/update/update/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ const ini = require('ini');
const lockfile = require('@yarnpkg/lockfile');
const pacote = require('pacote');

const npmPackageJsonCache = new Map<string, Promise<Partial<NpmRepositoryPackageJson>>>();
let npmrc: { [key: string]: string };
type PackageManagerOptions = Record<string, unknown>;

const npmPackageJsonCache = new Map<string, Promise<Partial<NpmRepositoryPackageJson>>>();
let npmrc: PackageManagerOptions;

function readOptions(
logger: logging.LoggerApi,
yarn = false,
showPotentials = false,
): Record<string, string> {
): PackageManagerOptions {
const cwd = process.cwd();
const baseFilename = yarn ? 'yarnrc' : 'npmrc';
const dotFilename = '.' + baseFilename;
Expand Down Expand Up @@ -55,25 +56,48 @@ function readOptions(
logger.info(`Locating potential ${baseFilename} files:`);
}

let options: { [key: string]: string } = {};
const options: PackageManagerOptions = {};
for (const location of [...defaultConfigLocations, ...projectConfigLocations]) {
if (existsSync(location)) {
if (showPotentials) {
logger.info(`Trying '${location}'...found.`);
}

const data = readFileSync(location, 'utf8');
options = {
...options,
...(yarn ? lockfile.parse(data) : ini.parse(data)),
};

if (options.cafile) {
const cafile = path.resolve(path.dirname(location), options.cafile);
delete options.cafile;
try {
options.ca = readFileSync(cafile, 'utf8').replace(/\r?\n/, '\\n');
} catch { }
// Normalize RC options that are needed by 'npm-registry-fetch'.
// See: https://github.com/npm/npm-registry-fetch/blob/ebddbe78a5f67118c1f7af2e02c8a22bcaf9e850/index.js#L99-L126
const rcConfig: PackageManagerOptions = yarn ? lockfile.parse(data) : ini.parse(data);
for (const [key, value] of Object.entries(rcConfig)) {
switch (key) {
case 'noproxy':
case 'no-proxy':
options['noProxy'] = value;
break;
case 'maxsockets':
options['maxSockets'] = value;
break;
case 'https-proxy':
case 'proxy':
options['proxy'] = value;
break;
case 'strict-ssl':
options['strictSSL'] = value;
break;
case 'local-address':
options['localAddress'] = value;
break;
case 'cafile':
if (typeof value === 'string') {
const cafile = path.resolve(path.dirname(location), value);
try {
options['ca'] = readFileSync(cafile, 'utf8').replace(/\r?\n/, '\\n');
} catch { }
}
break;
default:
options[key] = value;
break;
}
}
} else if (showPotentials) {
logger.info(`Trying '${location}'...not found.`);
Expand All @@ -82,8 +106,9 @@ function readOptions(

// Substitute any environment variable references
for (const key in options) {
if (typeof options[key] === 'string') {
options[key] = options[key].replace(/\$\{([^\}]+)\}/, (_, name) => process.env[name] || '');
const value = options[key];
if (typeof value === 'string') {
options[key] = value.replace(/\$\{([^\}]+)\}/, (_, name) => process.env[name] || '');
}
}

Expand Down

0 comments on commit 17cc701

Please sign in to comment.