Skip to content

fix(ng-add): inserted dependencies should be sorted #12847

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

Merged
merged 2 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/lib/schematics/install/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ describe('material-install-schematic', () => {
it('should update package.json', () => {
const tree = runner.runSchematic('ng-add', {}, appTree);
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
const angularCoreVersion = packageJson.dependencies['@angular/core'];
const dependencies = packageJson.dependencies;
const angularCoreVersion = dependencies['@angular/core'];

expect(packageJson.dependencies['@angular/material']).toBeDefined();
expect(packageJson.dependencies['@angular/cdk']).toBeDefined();
expect(packageJson.dependencies['hammerjs']).toBeDefined();
expect(packageJson.dependencies['@angular/animations']).toBe(angularCoreVersion,
expect(dependencies['@angular/material']).toBeDefined();
expect(dependencies['@angular/cdk']).toBeDefined();
expect(dependencies['hammerjs']).toBeDefined();
expect(dependencies['@angular/animations']).toBe(angularCoreVersion,
'Expected the @angular/animations package to have the same version as @angular/core.');

expect(Object.keys(dependencies)).toEqual(Object.keys(dependencies).sort(),
'Expected the modified "dependencies" to be sorted alphabetically.');
});

it('should add hammerjs import to project main file', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/schematics/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ function addMaterialToPackageJson(options: Schema) {
// have the same version tag if possible.
const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core');

addPackageToPackageJson(host, 'dependencies', '@angular/cdk', `^${materialVersion}`);
addPackageToPackageJson(host, 'dependencies', '@angular/material', `^${materialVersion}`);
addPackageToPackageJson(host, 'dependencies', '@angular/animations',
addPackageToPackageJson(host, '@angular/cdk', `^${materialVersion}`);
addPackageToPackageJson(host, '@angular/material', `^${materialVersion}`);
addPackageToPackageJson(host, '@angular/animations',
ngCoreVersionTag || requiredAngularVersionRange);

if (options.gestures) {
addPackageToPackageJson(host, 'dependencies', 'hammerjs', hammerjsVersion);
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
}

context.addTask(new NodePackageInstallTask());
Expand Down
21 changes: 15 additions & 6 deletions src/lib/schematics/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@

import {Tree} from '@angular-devkit/schematics';

/**
* Sorts the keys of the given object.
* @returns A new object instance with sorted keys
*/
function sortObjectByKeys(obj: object) {
return Object.keys(obj).sort().reduce((result, key) => (result[key] = obj[key]) && result, {});
}

/** Adds a package to the package.json in the given host tree. */
export function addPackageToPackageJson(host: Tree, type: string, pkg: string,
version: string): Tree {
export function addPackageToPackageJson(host: Tree, pkg: string, version: string): Tree {

if (host.exists('package.json')) {
const sourceText = host.read('package.json')!.toString('utf-8');
const json = JSON.parse(sourceText);
if (!json[type]) {
json[type] = {};

if (!json.dependencies) {
json.dependencies = {};
}

if (!json[type][pkg]) {
json[type][pkg] = version;
if (!json.dependencies[pkg]) {
json.dependencies[pkg] = version;
json.dependencies = sortObjectByKeys(json.dependencies);
}

host.overwrite('package.json', JSON.stringify(json, null, 2));
Expand Down