From 4da739a970c1e27b05ddab115eafde6e83d598b2 Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Wed, 5 Dec 2018 15:16:34 -0800 Subject: [PATCH] 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 --- .../src/schematics/bazel-workspace/index.ts | 30 ++++++++++++++++++- .../schematics/bazel-workspace/index_spec.ts | 16 +++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/bazel/src/schematics/bazel-workspace/index.ts b/packages/bazel/src/schematics/bazel-workspace/index.ts index 5c0ca019d6..acdb3b7d9d 100644 --- a/packages/bazel/src/schematics/bazel-workspace/index.ts +++ b/packages/bazel/src/schematics/bazel-workspace/index.ts @@ -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', }; diff --git a/packages/bazel/src/schematics/bazel-workspace/index_spec.ts b/packages/bazel/src/schematics/bazel-workspace/index_spec.ts index 9c4eca8b5a..885e9a71d7 100644 --- a/packages/bazel/src/schematics/bazel-workspace/index_spec.ts +++ b/packages/bazel/src/schematics/bazel-workspace/index_spec.ts @@ -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};