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:
parent
21d13dd15a
commit
4da739a970
|
@ -15,6 +15,31 @@ import {validateProjectName} from '@schematics/angular/utility/validation';
|
||||||
|
|
||||||
import {Schema as BazelWorkspaceOptions} from './schema';
|
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 {
|
export default function(options: BazelWorkspaceOptions): Rule {
|
||||||
return (host: Tree, context: SchematicContext) => {
|
return (host: Tree, context: SchematicContext) => {
|
||||||
if (!options.name) {
|
if (!options.name) {
|
||||||
|
@ -29,8 +54,11 @@ export default function(options: BazelWorkspaceOptions): Rule {
|
||||||
}
|
}
|
||||||
const appDir = `${newProjectRoot}/${options.name}`;
|
const appDir = `${newProjectRoot}/${options.name}`;
|
||||||
|
|
||||||
|
// If user already has angular installed, Bazel should use that version
|
||||||
|
const existingAngularVersion = findAngularVersion(options, host);
|
||||||
|
|
||||||
const workspaceVersions = {
|
const workspaceVersions = {
|
||||||
'ANGULAR_VERSION': '7.1.0',
|
'ANGULAR_VERSION': existingAngularVersion || '7.1.1',
|
||||||
'RULES_SASS_VERSION': '1.14.1',
|
'RULES_SASS_VERSION': '1.14.1',
|
||||||
'RXJS_VERSION': '6.3.3',
|
'RXJS_VERSION': '6.3.3',
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,7 +6,8 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* 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', () => {
|
describe('Bazel-workspace Schematic', () => {
|
||||||
const schematicRunner =
|
const schematicRunner =
|
||||||
|
@ -27,6 +28,19 @@ describe('Bazel-workspace Schematic', () => {
|
||||||
expect(files).toContain('/demo/yarn.lock');
|
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', () => {
|
describe('WORKSPACE', () => {
|
||||||
it('should contain project name', () => {
|
it('should contain project name', () => {
|
||||||
const options = {...defaultOptions};
|
const options = {...defaultOptions};
|
||||||
|
|
Loading…
Reference in New Issue