Skip to content

Commit

Permalink
fix(core): npmrc should be obeyed during nx migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Aug 19, 2021
1 parent bd2a635 commit f539a47
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions 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';
Expand All @@ -13,6 +20,7 @@ import {
readJsonFile,
writeJsonFile,
} from '../utils/fileutils';
import { appRootPath } from '../utils/app-root';

type Dependencies = 'dependencies' | 'devDependencies';

Expand Down Expand Up @@ -439,7 +447,23 @@ function createFetcher() {
packageVersion: string
): Promise<MigrationsJson> {
if (!cache[`${packageName}-${packageVersion}`]) {
const dir = dirSync().name;
// Setting up a tmpDir. In order to obey various .npmrc files
// this is a bit more complex than it should be. `.npmrc` can be present
// in ~/, workspace root, global, or in the npm installation directory.
const tmpdir = `${appRootPath}/node_modules/.tmp`;
mkdirSync(tmpdir, { recursive: true }); // dirSync fails if tmpdir does not exist yet.
const dir = dirSync({
tmpdir: tmpdir,
}).name;
// Creating a package.json in the new tmp directory prevents us from
// installing packages in the actual workspace instead of this subdirectory.
writeFileSync(`${dir}/package.json`, '{}');
// Copy npmrc from workspace root if it exists, so that npm still follows it.
const npmrcPath = `${appRootPath}/.npmrc`;
if (existsSync(npmrcPath)) {
copyFileSync(npmrcPath, `${dir}/.npmrc`);
}

logger.info(`Fetching ${packageName}@${packageVersion}`);
const pmc = getPackageManagerCommand();
execSync(`${pmc.add} ${packageName}@${packageVersion}`, {
Expand Down

0 comments on commit f539a47

Please sign in to comment.