Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): npmrc should be obeyed during nx migrate #6781

Merged
merged 1 commit into from Sep 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}