refactor(@angular/cli): remove resolve dependency

This commit is contained in:
Charles Lyding 2018-05-09 09:47:32 -04:00 committed by Mike Brocchi
parent 8b3cada6ea
commit e54213e488
7 changed files with 61 additions and 103 deletions

View File

@ -46,8 +46,6 @@ require.extensions['.ts'] = function (m, filename) {
// lazy: true
// });
const resolve = require('resolve');
const builtinModules = Object.keys(process.binding('natives'));
// 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}`))) {
if (!builtinModules.includes(request)) {
try {
if (isAngularProject) {
return oldLoad.call(this, resolve.sync(request, { basedir: process.cwd() }), parent);
return oldLoad.call(this, require.resolve(request, { paths: [process.cwd()] }), parent);
}
} catch (e) {
// Do nothing. Fallback to the old method.

4
package-lock.json generated
View File

@ -7167,7 +7167,8 @@
"path-parse": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz",
"integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME="
"integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=",
"dev": true
},
"path-to-regexp": {
"version": "0.1.7",
@ -7857,6 +7858,7 @@
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.6.0.tgz",
"integrity": "sha512-mw7JQNu5ExIkcw4LPih0owX/TZXjD/ZUF/ZQ/pDnkw3ZKhDcZZw5klmBlj6gVMwjQ3Pz5Jgu7F3d0jcDVuEWdw==",
"dev": true,
"requires": {
"path-parse": "^1.0.5"
}

View File

@ -48,7 +48,6 @@
"@schematics/angular": "github:angular/schematics-angular-builds",
"@schematics/update": "github:angular/schematics-update-builds",
"opn": "~5.3.0",
"resolve": "^1.1.7",
"rxjs": "^6.0.0",
"semver": "^5.3.0",
"symbol-observable": "^1.2.0",

View File

@ -12,11 +12,9 @@ const isWarningEnabled = require('../utilities/config').isWarningEnabled;
const fs = require('fs');
const packageJson = require('../package.json');
const path = require('path');
const resolve = require('resolve');
const stripIndents = require('@angular-devkit/core').tags.stripIndents;
const yellow = require('@angular-devkit/core').terminal.yellow;
const SemVer = require('semver').SemVer;
const events = require('events');
function _fromPackageJson(cwd?: string) {
cwd = cwd || process.cwd();
@ -62,75 +60,62 @@ if (process.env['NG_CLI_PROFILING']) {
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
}
resolve('@angular/cli', { basedir: process.cwd() },
function (error: Error, projectLocalCli: string) {
let cli;
if (error) {
// If there is an error, resolve could not find the ng-cli
// library from a package.json. Instead, include it from a relative
// path to this script file (which is likely a globally installed
// npm package). Most common cause for hitting this is `ng new`
cli = require('./cli');
} else {
// This was run from a global, check local version.
const globalVersion = new SemVer(packageJson['version']);
let localVersion;
let shouldWarn = false;
let cli;
try {
const projectLocalCli = require.resolve('@angular/cli', { paths: [ process.cwd() ]});
try {
localVersion = _fromPackageJson();
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
shouldWarn = true;
}
// This was run from a global, check local version.
const globalVersion = new SemVer(packageJson['version']);
let localVersion;
let shouldWarn = false;
if (shouldWarn && isWarningEnabled('versionMismatch')) {
let warning = yellow(stripIndents`
Your global Angular CLI version (${globalVersion}) is greater than your local
version (${localVersion}). The local Angular CLI version is used.
To disable this warning use "ng config -g cli.warnings.versionMismatch false".
`);
// Don't show warning colorised on `ng completion`
if (process.argv[2] !== 'completion') {
// eslint-disable-next-line no-console
console.log(warning);
} else {
// eslint-disable-next-line no-console
console.error(warning);
process.exit(1);
}
}
// No error implies a projectLocalCli, which will load whatever
// version of ng-cli you have installed in a local package.json
cli = require(projectLocalCli);
}
if ('default' in cli) {
cli = cli['default'];
}
let standardInput;
try {
standardInput = process.stdin;
} catch (e) {
delete process.stdin;
process.stdin = new events.EventEmitter();
standardInput = process.stdin;
}
cli({
cliArgs: process.argv.slice(2),
inputStream: standardInput,
outputStream: process.stdout
}).then(function (exitCode: number) {
process.exit(exitCode);
}).catch(function(err: Error) {
console.log('Unknown error: ' + err.toString());
process.exit(127);
});
try {
localVersion = _fromPackageJson();
shouldWarn = localVersion && globalVersion.compare(localVersion) > 0;
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
shouldWarn = true;
}
);
if (shouldWarn && isWarningEnabled('versionMismatch')) {
let warning = yellow(stripIndents`
Your global Angular CLI version (${globalVersion}) is greater than your local
version (${localVersion}). The local Angular CLI version is used.
To disable this warning use "ng config -g cli.warnings.versionMismatch false".
`);
// Don't show warning colorised on `ng completion`
if (process.argv[2] !== 'completion') {
// eslint-disable-next-line no-console
console.log(warning);
} else {
// eslint-disable-next-line no-console
console.error(warning);
process.exit(1);
}
}
// No error implies a projectLocalCli, which will load whatever
// version of ng-cli you have installed in a local package.json
cli = require(projectLocalCli);
} catch {
// If there is an error, resolve could not find the ng-cli
// library from a package.json. Instead, include it from a relative
// path to this script file (which is likely a globally installed
// npm package). Most common cause for hitting this is `ng new`
cli = require('./cli');
}
if ('default' in cli) {
cli = cli['default'];
}
cli({ cliArgs: process.argv.slice(2) })
.then((exitCode: number) => {
process.exit(exitCode);
})
.catch((err: Error) => {
console.log('Unknown error: ' + err.toString());
process.exit(127);
});

View File

@ -36,7 +36,6 @@
"@schematics/angular": "0.6.1",
"@schematics/update": "0.6.1",
"opn": "~5.3.0",
"resolve": "^1.1.7",
"rxjs": "^6.0.0",
"semver": "^5.1.0",
"symbol-observable": "^1.2.0",

View File

@ -4,8 +4,6 @@ import * as path from 'path';
import { isWarningEnabled } from '../utilities/config';
import { requireProjectModule } from '../utilities/require-project-module';
const resolve = require('resolve');
export class Version {
private _semver: SemVer = null;
@ -31,28 +29,6 @@ export class Version {
toString() { return this._version; }
static fromProject(): Version {
let packageJson: any = null;
try {
const angularCliPath = resolve.sync('@angular/cli', {
basedir: process.cwd(),
packageFilter: (pkg: any, _pkgFile: string) => {
return packageJson = pkg;
}
});
if (angularCliPath && packageJson) {
try {
return new Version(packageJson.version);
} catch {
return new Version(null);
}
}
} catch {
return new Version(null);
}
}
static assertCompatibleAngularVersion(projectRoot: string) {
let angularPkgJson;
let rxjsPkgJson;

View File

@ -1,8 +1,7 @@
const resolve = require('resolve');
// resolve dependencies within the target project
export function resolveProjectModule(root: string, moduleName: string) {
return resolve.sync(moduleName, { basedir: root });
return require.resolve(moduleName, { paths: [root] });
}
// require dependencies within the target project