Skip to content

Commit

Permalink
fix(core): npmrc should be obeyed during nx migrate (#6781)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Sep 1, 2021
1 parent 98d3b4a commit b416bb6
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/tao/src/commands/migrate.ts
@@ -1,5 +1,5 @@
import { execSync } from 'child_process';
import { removeSync } from 'fs-extra';
import { removeSync, copyFileSync, existsSync } from 'fs-extra';
import * as yargsParser from 'yargs-parser';
import { dirname, join } from 'path';
import { gt, lte } from 'semver';
Expand All @@ -13,6 +13,7 @@ import {
readJsonFile,
writeJsonFile,
} from '../utils/fileutils';
import { appRootPath } from '../utils/app-root';

type Dependencies = 'dependencies' | 'devDependencies';

Expand Down Expand Up @@ -442,6 +443,14 @@ function createFetcher() {
): Promise<MigrationsJson> {
if (!cache[`${packageName}-${packageVersion}`]) {
const dir = dirSync().name;
const npmrc = checkForNPMRC();
if (npmrc) {
// Creating a package.json is needed for .npmrc to resolve
writeJsonFile(`${dir}/package.json`, {});
// Copy npmrc if it exists, so that npm still follows it.
copyFileSync(npmrc, `${dir}/.npmrc`);
}

logger.info(`Fetching ${packageName}@${packageVersion}`);
const pmc = getPackageManagerCommand();
execSync(`${pmc.add} ${packageName}@${packageVersion}`, {
Expand Down Expand Up @@ -741,3 +750,16 @@ export async function migrate(root: string, args: string[], isVerbose = false) {
}
});
}

/**
* Checks for a project level npmrc file by crawling up the file tree until
* hitting a package.json file, as this is how npm finds them as well.
*/
function checkForNPMRC(): string | null {
let directory = process.cwd();
while (!existsSync(join(directory, 'package.json'))) {
directory = dirname(directory);
}
const path = join(directory, '.npmrc');
return existsSync(path) ? path : null;
}

0 comments on commit b416bb6

Please sign in to comment.