Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bazel): Schematics should upgrade rxjs to 6.4.0 #28841

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 43 additions & 1 deletion packages/bazel/src/schematics/ng-add/index.ts
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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);
Expand All @@ -270,6 +311,7 @@ export default function(options: Schema): Rule {
updateAngularJsonToUseBazelBuilder(options),
updateGitignore(),
updateTsconfigJson(),
upgradeRxjs(),
]);
};
}
41 changes: 41 additions & 0 deletions packages/bazel/src/schematics/ng-add/index_spec.ts
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
}
});
}
});
});