Skip to content

Commit e54213e

Browse files
clydinBrocco
authored andcommitted
refactor(@angular/cli): remove resolve dependency
1 parent 8b3cada commit e54213e

File tree

7 files changed

+58
-100
lines changed

7 files changed

+58
-100
lines changed

lib/bootstrap-local.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ require.extensions['.ts'] = function (m, filename) {
4646
// lazy: true
4747
// });
4848

49-
const resolve = require('resolve');
50-
5149
const builtinModules = Object.keys(process.binding('natives'));
5250

5351
// Look if there's a .angular-cli.json file, and if so toggle process.cwd() resolution.
@@ -83,7 +81,7 @@ if (!__dirname.match(new RegExp(`\\${path.sep}node_modules\\${path.sep}`))) {
8381
if (!builtinModules.includes(request)) {
8482
try {
8583
if (isAngularProject) {
86-
return oldLoad.call(this, resolve.sync(request, { basedir: process.cwd() }), parent);
84+
return oldLoad.call(this, require.resolve(request, { paths: [process.cwd()] }), parent);
8785
}
8886
} catch (e) {
8987
// Do nothing. Fallback to the old method.

package-lock.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@schematics/angular": "github:angular/schematics-angular-builds",
4949
"@schematics/update": "github:angular/schematics-update-builds",
5050
"opn": "~5.3.0",
51-
"resolve": "^1.1.7",
5251
"rxjs": "^6.0.0",
5352
"semver": "^5.3.0",
5453
"symbol-observable": "^1.2.0",

packages/@angular/cli/lib/init.ts

Lines changed: 53 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ const isWarningEnabled = require('../utilities/config').isWarningEnabled;
1212
const fs = require('fs');
1313
const packageJson = require('../package.json');
1414
const path = require('path');
15-
const resolve = require('resolve');
1615
const stripIndents = require('@angular-devkit/core').tags.stripIndents;
1716
const yellow = require('@angular-devkit/core').terminal.yellow;
1817
const SemVer = require('semver').SemVer;
19-
const events = require('events');
2018

2119
function _fromPackageJson(cwd?: string) {
2220
cwd = cwd || process.cwd();
@@ -62,75 +60,62 @@ if (process.env['NG_CLI_PROFILING']) {
6260
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
6361
}
6462

65-
resolve('@angular/cli', { basedir: process.cwd() },
66-
function (error: Error, projectLocalCli: string) {
67-
let cli;
68-
if (error) {
69-
// If there is an error, resolve could not find the ng-cli
70-
// library from a package.json. Instead, include it from a relative
71-
// path to this script file (which is likely a globally installed
72-
// npm package). Most common cause for hitting this is `ng new`
73-
cli = require('./cli');
74-
} else {
75-
// This was run from a global, check local version.
76-
const globalVersion = new SemVer(packageJson['version']);
77-
let localVersion;
78-
let shouldWarn = false;
79-
80-
try {
81-
localVersion = _fromPackageJson();
82-
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
83-
} catch (e) {
84-
// eslint-disable-next-line no-console
85-
console.error(e);
86-
shouldWarn = true;
87-
}
63+
let cli;
64+
try {
65+
const projectLocalCli = require.resolve('@angular/cli', { paths: [ process.cwd() ]});
66+
67+
// This was run from a global, check local version.
68+
const globalVersion = new SemVer(packageJson['version']);
69+
let localVersion;
70+
let shouldWarn = false;
71+
72+
try {
73+
localVersion = _fromPackageJson();
74+
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
75+
} catch (e) {
76+
// eslint-disable-next-line no-console
77+
console.error(e);
78+
shouldWarn = true;
79+
}
8880

89-
if (shouldWarn && isWarningEnabled('versionMismatch')) {
90-
let warning = yellow(stripIndents`
91-
Your global Angular CLI version (${globalVersion}) is greater than your local
92-
version (${localVersion}). The local Angular CLI version is used.
93-
94-
To disable this warning use "ng config -g cli.warnings.versionMismatch false".
95-
`);
96-
// Don't show warning colorised on `ng completion`
97-
if (process.argv[2] !== 'completion') {
98-
// eslint-disable-next-line no-console
99-
console.log(warning);
100-
} else {
101-
// eslint-disable-next-line no-console
102-
console.error(warning);
103-
process.exit(1);
104-
}
105-
}
81+
if (shouldWarn && isWarningEnabled('versionMismatch')) {
82+
let warning = yellow(stripIndents`
83+
Your global Angular CLI version (${globalVersion}) is greater than your local
84+
version (${localVersion}). The local Angular CLI version is used.
10685
107-
// No error implies a projectLocalCli, which will load whatever
108-
// version of ng-cli you have installed in a local package.json
109-
cli = require(projectLocalCli);
86+
To disable this warning use "ng config -g cli.warnings.versionMismatch false".
87+
`);
88+
// Don't show warning colorised on `ng completion`
89+
if (process.argv[2] !== 'completion') {
90+
// eslint-disable-next-line no-console
91+
console.log(warning);
92+
} else {
93+
// eslint-disable-next-line no-console
94+
console.error(warning);
95+
process.exit(1);
11096
}
97+
}
11198

112-
if ('default' in cli) {
113-
cli = cli['default'];
114-
}
99+
// No error implies a projectLocalCli, which will load whatever
100+
// version of ng-cli you have installed in a local package.json
101+
cli = require(projectLocalCli);
102+
} catch {
103+
// If there is an error, resolve could not find the ng-cli
104+
// library from a package.json. Instead, include it from a relative
105+
// path to this script file (which is likely a globally installed
106+
// npm package). Most common cause for hitting this is `ng new`
107+
cli = require('./cli');
108+
}
115109

116-
let standardInput;
117-
try {
118-
standardInput = process.stdin;
119-
} catch (e) {
120-
delete process.stdin;
121-
process.stdin = new events.EventEmitter();
122-
standardInput = process.stdin;
123-
}
110+
if ('default' in cli) {
111+
cli = cli['default'];
112+
}
124113

125-
cli({
126-
cliArgs: process.argv.slice(2),
127-
inputStream: standardInput,
128-
outputStream: process.stdout
129-
}).then(function (exitCode: number) {
130-
process.exit(exitCode);
131-
}).catch(function(err: Error) {
132-
console.log('Unknown error: ' + err.toString());
133-
process.exit(127);
134-
});
135-
}
136-
);
114+
cli({ cliArgs: process.argv.slice(2) })
115+
.then((exitCode: number) => {
116+
process.exit(exitCode);
117+
})
118+
.catch((err: Error) => {
119+
console.log('Unknown error: ' + err.toString());
120+
process.exit(127);
121+
});

packages/@angular/cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@schematics/angular": "0.6.1",
3737
"@schematics/update": "0.6.1",
3838
"opn": "~5.3.0",
39-
"resolve": "^1.1.7",
4039
"rxjs": "^6.0.0",
4140
"semver": "^5.1.0",
4241
"symbol-observable": "^1.2.0",

packages/@angular/cli/upgrade/version.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import * as path from 'path';
44
import { isWarningEnabled } from '../utilities/config';
55
import { requireProjectModule } from '../utilities/require-project-module';
66

7-
const resolve = require('resolve');
8-
97

108
export class Version {
119
private _semver: SemVer = null;
@@ -31,28 +29,6 @@ export class Version {
3129

3230
toString() { return this._version; }
3331

34-
static fromProject(): Version {
35-
let packageJson: any = null;
36-
37-
try {
38-
const angularCliPath = resolve.sync('@angular/cli', {
39-
basedir: process.cwd(),
40-
packageFilter: (pkg: any, _pkgFile: string) => {
41-
return packageJson = pkg;
42-
}
43-
});
44-
if (angularCliPath && packageJson) {
45-
try {
46-
return new Version(packageJson.version);
47-
} catch {
48-
return new Version(null);
49-
}
50-
}
51-
} catch {
52-
return new Version(null);
53-
}
54-
}
55-
5632
static assertCompatibleAngularVersion(projectRoot: string) {
5733
let angularPkgJson;
5834
let rxjsPkgJson;

packages/@angular/cli/utilities/require-project-module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const resolve = require('resolve');
21

32
// resolve dependencies within the target project
43
export function resolveProjectModule(root: string, moduleName: string) {
5-
return resolve.sync(moduleName, { basedir: root });
4+
return require.resolve(moduleName, { paths: [root] });
65
}
76

87
// require dependencies within the target project

0 commit comments

Comments
 (0)