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

feat(core): improve migrations performance by using registry to fetch packages information #9592

Merged
merged 1 commit into from Mar 29, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -111,6 +111,7 @@
"@types/react-dom": "17.0.3",
"@types/react-router-dom": "5.1.7",
"@types/semver": "^7.3.8",
"@types/tar-stream": "^2.2.2",
"@types/tmp": "^0.2.0",
"@types/yargs": "^17.0.10",
"@typescript-eslint/eslint-plugin": "5.10.1",
Expand Down Expand Up @@ -230,6 +231,7 @@
"styled-components": "5.0.0",
"stylus": "^0.55.0",
"stylus-loader": "^6.2.0",
"tar-stream": "~2.2.0",
"tcp-port-used": "^1.0.2",
"terser-webpack-plugin": "^5.3.0",
"tmp": "~0.2.1",
Expand Down
10 changes: 7 additions & 3 deletions packages/make-angular-cli-faster/src/utilities/migration.ts
@@ -1,7 +1,11 @@
import { getPackageManagerCommand, output, readJsonFile } from '@nrwl/devkit';
import {
getPackageManagerCommand,
output,
readJsonFile,
workspaceRoot,
} from '@nrwl/devkit';
import { execSync } from 'child_process';
import { prompt } from 'enquirer';
import { appRootPath } from 'nx/src/utils/app-root';
import { lt, lte, major, satisfies } from 'semver';
import { resolvePackageVersion } from './package-manager';
import { MigrationDefinition } from './types';
Expand Down Expand Up @@ -168,7 +172,7 @@ async function promptForVersion(version: string): Promise<boolean> {

function getInstalledAngularVersion(): string {
const packageJsonPath = require.resolve('@angular/core/package.json', {
paths: [appRootPath],
paths: [workspaceRoot],
});
return readJsonFile(packageJsonPath).version;
}
Expand Down
46 changes: 10 additions & 36 deletions packages/make-angular-cli-faster/src/utilities/package-manager.ts
@@ -1,15 +1,17 @@
import {
getPackageManagerCommand,
readJsonFile,
writeJsonFile,
workspaceRoot,
} from '@nrwl/devkit';
import { execSync } from 'child_process';
import { copyFileSync, existsSync, unlinkSync, writeFileSync } from 'fs';
import { appRootPath } from 'nx/src/utils/app-root';
import { writeFileSync } from 'fs';
import { sortObjectByKeys } from 'nx/src/utils/object-sort';
import { dirname, join } from 'path';
import {
resolvePackageVersionUsingInstallation,
resolvePackageVersionUsingRegistry,
} from 'nx/src/utils/package-manager';
import { join } from 'path';
import { gte, major } from 'semver';
import { dirSync } from 'tmp';
import { MigrationDefinition } from './types';

// version when the Nx CLI changed from @nrwl/tao & @nrwl/cli to nx
Expand All @@ -19,7 +21,7 @@ export function installDependencies(
{ packageName, version }: MigrationDefinition,
useNxCloud: boolean
): void {
const json = readJsonFile(join(appRootPath, 'package.json'));
const json = readJsonFile(join(workspaceRoot, 'package.json'));

json.devDependencies ??= {};
json.devDependencies['@nrwl/workspace'] = version;
Expand Down Expand Up @@ -57,37 +59,9 @@ export function resolvePackageVersion(
packageName: string,
version: string
): string {
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`);
}

const pmc = getPackageManagerCommand();
execSync(`${pmc.add} ${packageName}@${version}`, { stdio: [], cwd: dir });

const packageJsonPath = require.resolve(`${packageName}/package.json`, {
paths: [dir],
});
const { version: resolvedVersion } = readJsonFile(packageJsonPath);

try {
unlinkSync(dir);
return resolvePackageVersionUsingRegistry(packageName, version);
} catch {
// It's okay if this fails, the OS will clean it up eventually
}

return resolvedVersion;
}

function checkForNPMRC(): string | null {
let directory = process.cwd();
while (!existsSync(join(directory, 'package.json'))) {
directory = dirname(directory);
return resolvePackageVersionUsingInstallation(packageName, version);
}
const path = join(directory, '.npmrc');
return existsSync(path) ? path : null;
}
1 change: 1 addition & 0 deletions packages/nx/package.json
Expand Up @@ -51,6 +51,7 @@
"open": "^8.4.0",
"rxjs": "^6.5.4",
"semver": "7.3.4",
"tar-stream": "~2.2.0",
"tmp": "~0.2.1",
"yargs": "^17.4.0",
"yargs-parser": "21.0.1",
Expand Down