diff --git a/packages/bazel/src/schematics/ng-add/index.ts b/packages/bazel/src/schematics/ng-add/index.ts index 1dfba68596..2df18f4f57 100755 --- a/packages/bazel/src/schematics/ng-add/index.ts +++ b/packages/bazel/src/schematics/ng-add/index.ts @@ -13,7 +13,7 @@ import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates import {getWorkspacePath} from '@schematics/angular/utility/config'; import {findPropertyInAstObject, insertPropertyInAstObjectInOrder} from '@schematics/angular/utility/json-utils'; import {validateProjectName} from '@schematics/angular/utility/validation'; -import {isJsonAstObject, removeKeyValueInAstObject as removeKeyValueInAstObject, replacePropertyInAstObject} from '../utility/json-utils'; +import {isJsonAstObject, removeKeyValueInAstObject, replacePropertyInAstObject} from '../utility/json-utils'; import {Schema} from './schema'; /** @@ -257,6 +257,47 @@ function updateTsconfigJson(): Rule { }; } +/** + * @angular/bazel requires minimum version of rxjs to be 6.4.0. This function + * upgrades the version of rxjs in package.json if necessary. + */ +function upgradeRxjs() { + return (host: Tree, context: SchematicContext) => { + const packageJson = 'package.json'; + if (!host.exists(packageJson)) { + throw new Error(`Could not find ${packageJson}`); + } + const content = host.read(packageJson).toString(); + const jsonAst = parseJsonAst(content); + if (!isJsonAstObject(jsonAst)) { + throw new Error(`Failed to parse JSON for ${packageJson}`); + } + const deps = findPropertyInAstObject(jsonAst, 'dependencies'); + if (!isJsonAstObject(deps)) { + throw new Error(`Failed to find dependencies in ${packageJson}`); + } + const rxjs = findPropertyInAstObject(deps, 'rxjs'); + if (!rxjs) { + throw new Error(`Failed to find rxjs in dependencies of ${packageJson}`); + } + const value = rxjs.value as string; // value can be version or range + const match = value.match(/(\d)+\.(\d)+.(\d)+$/); + if (match) { + const [_, major, minor] = match; + if (major < '6' || (major === '6' && minor < '4')) { + const recorder = host.beginUpdate(packageJson); + replacePropertyInAstObject(recorder, deps, 'rxjs', '~6.4.0'); + host.commitUpdate(recorder); + } + } else { + context.logger.info( + 'Could not determine version of rxjs. \n' + + 'Please make sure that version is at least 6.4.0.'); + } + return host; + }; +} + export default function(options: Schema): Rule { return (host: Tree) => { validateProjectName(options.name); @@ -270,6 +311,7 @@ export default function(options: Schema): Rule { updateAngularJsonToUseBazelBuilder(options), updateGitignore(), updateTsconfigJson(), + upgradeRxjs(), ]); }; } diff --git a/packages/bazel/src/schematics/ng-add/index_spec.ts b/packages/bazel/src/schematics/ng-add/index_spec.ts index 9f426bf5ed..7c6a2dd482 100644 --- a/packages/bazel/src/schematics/ng-add/index_spec.ts +++ b/packages/bazel/src/schematics/ng-add/index_spec.ts @@ -21,6 +21,7 @@ describe('ng-add schematic', () => { name: 'demo', dependencies: { '@angular/core': '1.2.3', + 'rxjs': '~6.3.3', }, devDependencies: { 'typescript': '3.2.2', @@ -198,4 +199,44 @@ describe('ng-add schematic', () => { } }); }); + + describe('rxjs', () => { + const cases = [ + // version|upgrade + ['6.3.3', true], + ['~6.3.3', true], + ['^6.3.3', true], + ['~6.3.11', true], + ['6.4.0', false], + ['~6.4.0', false], + ['~6.4.1', false], + ['6.5.0', false], + ['~6.5.0', false], + ['^6.5.0', false], + ['~7.0.1', false], + ]; + for (const [version, upgrade] of cases) { + it(`should ${upgrade ? '' : 'not '}upgrade v${version}')`, () => { + host.overwrite('package.json', JSON.stringify({ + name: 'demo', + dependencies: { + '@angular/core': '1.2.3', + 'rxjs': version, + }, + devDependencies: { + 'typescript': '3.2.2', + }, + })); + host = schematicRunner.runSchematic('ng-add', defaultOptions, host); + expect(host.files).toContain('/package.json'); + const content = host.readContent('/package.json'); + const json = JSON.parse(content); + if (upgrade) { + expect(json.dependencies.rxjs).toBe('~6.4.0'); + } else { + expect(json.dependencies.rxjs).toBe(version); + } + }); + } + }); });