fix(bazel): Respect existing angular installation (#27495)

If user has already installed Angular, Bazel should fetch the same
version. Otherwise, user will see an error in the post-install step
that performs version check.

PR Close #27495
This commit is contained in:
Keen Yee Liau 2018-12-05 15:16:34 -08:00 committed by Igor Minar
parent 21d13dd15a
commit 4da739a970
2 changed files with 44 additions and 2 deletions

View File

@ -15,6 +15,31 @@ import {validateProjectName} from '@schematics/angular/utility/validation';
import {Schema as BazelWorkspaceOptions} from './schema';
/**
* Look for package.json file for @angular/core in node_modules and extract its
* version.
*/
function findAngularVersion(options: BazelWorkspaceOptions, host: Tree): string|null {
// Need to look in multiple locations because we could be working in a subtree.
const candidates = [
'node_modules/@angular/core/package.json',
`${options.name}/node_modules/@angular/core/package.json`,
];
for (const candidate of candidates) {
if (host.exists(candidate)) {
try {
const packageJson = JSON.parse(host.read(candidate).toString());
if (packageJson.name === '@angular/core' && packageJson.version) {
return packageJson.version;
}
} catch {
}
}
}
return null;
}
export default function(options: BazelWorkspaceOptions): Rule {
return (host: Tree, context: SchematicContext) => {
if (!options.name) {
@ -29,8 +54,11 @@ export default function(options: BazelWorkspaceOptions): Rule {
}
const appDir = `${newProjectRoot}/${options.name}`;
// If user already has angular installed, Bazel should use that version
const existingAngularVersion = findAngularVersion(options, host);
const workspaceVersions = {
'ANGULAR_VERSION': '7.1.0',
'ANGULAR_VERSION': existingAngularVersion || '7.1.1',
'RULES_SASS_VERSION': '1.14.1',
'RXJS_VERSION': '6.3.3',
};

View File

@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
describe('Bazel-workspace Schematic', () => {
const schematicRunner =
@ -27,6 +28,19 @@ describe('Bazel-workspace Schematic', () => {
expect(files).toContain('/demo/yarn.lock');
});
it('should find existing Angular version', () => {
let host = new UnitTestTree(new HostTree);
host.create('/demo/node_modules/@angular/core/package.json', JSON.stringify({
name: '@angular/core',
version: '6.6.6',
}));
const options = {...defaultOptions};
host = schematicRunner.runSchematic('bazel-workspace', options, host);
expect(host.files).toContain('/demo/WORKSPACE');
const workspace = host.readContent('/demo/WORKSPACE');
expect(workspace).toMatch('ANGULAR_VERSION = "6.6.6"');
});
describe('WORKSPACE', () => {
it('should contain project name', () => {
const options = {...defaultOptions};