Skip to content

Commit

Permalink
fix(devkit): ensurePackage should obey npmrc and yarnrc.yml
Browse files Browse the repository at this point in the history
Fixes #16644
  • Loading branch information
AgentEnder committed Apr 28, 2023
1 parent abc5055 commit f6a5a5f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
17 changes: 15 additions & 2 deletions packages/devkit/nx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
export function requireNx(): typeof import('nx/src/devkit-exports') {
// After Nx v18, this can be removed and replaced with either:
// - import {} from 'nx/src/devkit-exports'
// - import {} from 'nx/src/devkit-internals'
export function requireNx(): typeof import('nx/src/devkit-exports') &
Partial<typeof import('nx/src/devkit-internals')> {
try {
return require('nx/src/devkit-exports');
let result = { ...require('nx/src/devkit-exports') };
try {
result = {
...result,
// Remove in Nx v18, devkit should not support Nx v16.0.2 at that point.
...require('nx/src/devkit-internals'),
};
} catch {}
return result;
} catch {
// Remove in Nx V17, devkit should not support Nx < 16 at that point.
return require('./nx-reexports-pre16');
}
}
5 changes: 4 additions & 1 deletion packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
getPackageManagerCommand,
workspaceRoot,
detectPackageManager,
createTempNpmDirectory,
} = requireNx();

const UNIDENTIFIED_VERSION = 'UNIDENTIFIED_VERSION';
Expand Down Expand Up @@ -450,7 +451,9 @@ export function ensurePackage<T extends any = any>(
);
}

const tempDir = dirSync().name;
const { dir: tempDir } = createTempNpmDirectory?.() ?? {
dir: dirSync().name,
};

console.log(`Fetching ${pkg}...`);
const packageManager = detectPackageManager();
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Note to developers: STOP! These exports are available via requireNx in @nx/devkit.
*
* These may not be available in certain version of Nx, so be sure to check them first.
*/
export { createTempNpmDirectory } from './utils/package-manager';
25 changes: 18 additions & 7 deletions packages/nx/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { promisify } from 'util';
import { writeJsonFile } from './fileutils';
import { readModulePackageJson } from './package-json';
import { gte, lt } from 'semver';
import { workspaceRoot } from './workspace-root';

const execAsync = promisify(exec);

Expand Down Expand Up @@ -118,16 +119,30 @@ export function getPackageManagerVersion(
* 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.
*/
export function checkForNPMRC(
export function findFileInPackageJsonDirectory(
file: string,
directory: string = process.cwd()
): string | null {
while (!existsSync(join(directory, 'package.json'))) {
directory = dirname(directory);
}
const path = join(directory, '.npmrc');
const path = join(directory, file);
return existsSync(path) ? path : null;
}

export function copyPackageManagerConfigurationFiles(
root: string,
destination: string
) {
for (const packageManagerConfigFile of ['.npmrc', '.yarnrc', '.yarnrc.yml']) {
const f = findFileInPackageJsonDirectory(packageManagerConfigFile, root);
if (f) {
// Copy config file if it exists, so that the package manager still follows it.
copyFileSync(f, `${destination}/.npmrc`);
}
}
}

/**
* Creates a temporary directory where you can run package manager commands safely.
*
Expand All @@ -140,11 +155,7 @@ export function createTempNpmDirectory() {

// A package.json is needed for pnpm pack and for .npmrc to resolve
writeJsonFile(`${dir}/package.json`, {});
const npmrc = checkForNPMRC();
if (npmrc) {
// Copy npmrc if it exists, so that npm still follows it.
copyFileSync(npmrc, `${dir}/.npmrc`);
}
copyPackageManagerConfigurationFiles(workspaceRoot, dir);

const cleanup = async () => {
try {
Expand Down

0 comments on commit f6a5a5f

Please sign in to comment.