diff --git a/packages/tao/src/commands/migrate.ts b/packages/tao/src/commands/migrate.ts index c81dbf4d0e659d..4106d7be143c1d 100644 --- a/packages/tao/src/commands/migrate.ts +++ b/packages/tao/src/commands/migrate.ts @@ -1,5 +1,12 @@ import { execSync } from 'child_process'; -import { removeSync } from 'fs-extra'; +import { + removeSync, + writeFileSync, + copyFileSync, + existsSync, + createFileSync, + mkdirSync, +} from 'fs-extra'; import * as yargsParser from 'yargs-parser'; import { dirname, join } from 'path'; import { gt, lte } from 'semver'; @@ -13,6 +20,7 @@ import { readJsonFile, writeJsonFile, } from '../utils/fileutils'; +import { appRootPath } from '../utils/app-root'; type Dependencies = 'dependencies' | 'devDependencies'; @@ -440,6 +448,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 + writeFileSync(`${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}`, { @@ -739,3 +755,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 = join(directory, '..'); + } + const path = join(directory, '.npmrc'); + return existsSync(path) ? path : null; +}