Skip to content

Commit cfa1649

Browse files
clydinmgechev
authored andcommitted
refactor(@angular/cli): use standard node resolution methods where possible
1 parent a355e7d commit cfa1649

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

packages/angular/cli/commands/add-impl.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { analytics, tags, terminal } from '@angular-devkit/core';
9-
import { ModuleNotFoundException, resolve } from '@angular-devkit/core/node';
109
import { NodePackageDoesNotSupportSchematics } from '@angular-devkit/schematics/tools';
11-
import { dirname } from 'path';
10+
import { dirname, join } from 'path';
1211
import { intersects, prerelease, rcompare, satisfies, valid, validRange } from 'semver';
1312
import { isPackageNameSafeForAnalytics } from '../models/analytics';
1413
import { Arguments } from '../models/interface';
@@ -160,15 +159,11 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
160159

161160
private isPackageInstalled(name: string): boolean {
162161
try {
163-
resolve(name, {
164-
checkLocal: true,
165-
basedir: this.workspace.root,
166-
resolvePackageJson: true,
167-
});
162+
require.resolve(join(name, 'package.json'), { paths: [this.workspace.root] });
168163

169164
return true;
170165
} catch (e) {
171-
if (!(e instanceof ModuleNotFoundException)) {
166+
if (e.code !== 'MODULE_NOT_FOUND') {
172167
throw e;
173168
}
174169
}
@@ -209,9 +204,9 @@ export class AddCommand extends SchematicCommand<AddCommandSchema> {
209204
private async findProjectVersion(name: string): Promise<string | null> {
210205
let installedPackage;
211206
try {
212-
installedPackage = resolve(
213-
name,
214-
{ checkLocal: true, basedir: this.workspace.root, resolvePackageJson: true },
207+
installedPackage = require.resolve(
208+
join(name, 'package.json'),
209+
{ paths: [this.workspace.root] },
215210
);
216211
} catch { }
217212

packages/angular/cli/lib/init.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'symbol-observable';
99
// symbol polyfill must go first
1010
// tslint:disable-next-line:ordered-imports import-groups
1111
import { tags, terminal } from '@angular-devkit/core';
12-
import { resolve } from '@angular-devkit/core/node';
1312
import * as fs from 'fs';
1413
import * as path from 'path';
1514
import { SemVer } from 'semver';
@@ -77,14 +76,7 @@ if (process.env['NG_CLI_PROFILING']) {
7776

7877
let cli;
7978
try {
80-
const projectLocalCli = resolve(
81-
'@angular/cli',
82-
{
83-
checkGlobal: false,
84-
basedir: process.cwd(),
85-
preserveSymlinks: true,
86-
},
87-
);
79+
const projectLocalCli = require.resolve('@angular/cli', { paths: [process.cwd()] });
8880

8981
// This was run from a global, check local version.
9082
const globalVersion = new SemVer(packageJson['version']);

packages/angular/cli/upgrade/version.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import { tags, terminal } from '@angular-devkit/core';
10-
import { resolve } from '@angular-devkit/core/node';
1110
import * as path from 'path';
1211
import { SemVer } from 'semver';
1312

@@ -41,13 +40,9 @@ export class Version {
4140
let rxjsPkgJson;
4241

4342
try {
44-
const resolveOptions = {
45-
basedir: projectRoot,
46-
checkGlobal: false,
47-
checkLocal: true,
48-
};
49-
const angularPackagePath = resolve('@angular/core/package.json', resolveOptions);
50-
const rxjsPackagePath = resolve('rxjs/package.json', resolveOptions);
43+
const resolveOptions = { paths: [projectRoot] };
44+
const angularPackagePath = require.resolve('@angular/core/package.json', resolveOptions);
45+
const rxjsPackagePath = require.resolve('rxjs/package.json', resolveOptions);
5146

5247
angularPkgJson = require(angularPackagePath);
5348
rxjsPkgJson = require(rxjsPackagePath);

0 commit comments

Comments
 (0)