From b416bb6dc97efdaeefe46b6a5b19f9bccaf6439b Mon Sep 17 00:00:00 2001 From: Craigory V Coppola Date: Wed, 1 Sep 2021 19:37:40 -0400 Subject: [PATCH] fix(core): npmrc should be obeyed during nx migrate (#6781) --- packages/tao/src/commands/migrate.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/tao/src/commands/migrate.ts b/packages/tao/src/commands/migrate.ts index e817ee1b13b57..f6cc70cfc5876 100644 --- a/packages/tao/src/commands/migrate.ts +++ b/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'; @@ -13,6 +13,7 @@ import { readJsonFile, writeJsonFile, } from '../utils/fileutils'; +import { appRootPath } from '../utils/app-root'; type Dependencies = 'dependencies' | 'devDependencies'; @@ -442,6 +443,14 @@ function createFetcher() { ): Promise { 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}`, { @@ -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; +}