Skip to content

Commit

Permalink
fix(schematics): add a new line when updating json file
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Oct 31, 2017
1 parent d0ddb62 commit c09ba79
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/schematics/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as path from 'path';
import * as ts from 'typescript';
import { addBootstrapToModule } from '@schematics/angular/utility/ast-utils';
import { insertImport } from '@schematics/angular/utility/route-utils';
import { addApp } from '../utility/config-file-utils';
import { serializeJson, addApp } from '../utility/fileutils';

function addBootstrap(path: string): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -77,7 +77,7 @@ function addAppToAngularCliJson(options: Schema): Rule {
}
});

host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2));
host.overwrite('.angular-cli.json', serializeJson(json));
return host;
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/schematics/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { Schema } from './schema';
import { names, toFileName } from '@nrwl/schematics';
import * as path from 'path';
import { addApp } from '../utility/config-file-utils';
import { serializeJson, addApp } from '../utility/fileutils';

function addLibToAngularCliJson(options: Schema): Rule {
return (host: Tree) => {
Expand All @@ -30,7 +30,7 @@ function addLibToAngularCliJson(options: Schema): Rule {
appRoot: ''
});

host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2));
host.overwrite('.angular-cli.json', serializeJson(json));
return host;
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/schematics/src/ngrx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { insertImport } from '@schematics/angular/utility/route-utils';
import { Schema } from './schema';
import { InsertChange } from '@schematics/angular/utility/change';
import { ngrxVersion } from '../utility/lib-versions';
import { serializeJson } from '../utility/fileutils';

function addImportsToModule(name: string, options: Schema): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -118,7 +119,7 @@ function addNgRxToPackageJson() {
if (!json['dependencies']['@ngrx/store-devtools']) {
json['dependencies']['@ngrx/store-devtools'] = ngrxVersion;
}
host.overwrite('package.json', JSON.stringify(json, null, 2));
host.overwrite('package.json', serializeJson(json));
return host;
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/schematics/src/utility/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Tree, Rule } from '@angular-devkit/schematics';
import { angularJsVersion } from './lib-versions';
import { serializeJson } from './fileutils';

export function addUpgradeToPackageJson(): Rule {
return (host: Tree) => {
Expand All @@ -18,7 +19,7 @@ export function addUpgradeToPackageJson(): Rule {
json['dependencies']['angular'] = angularJsVersion;
}

host.overwrite('package.json', JSON.stringify(json, null, 2));
host.overwrite('package.json', serializeJson(json));
return host;
};
}
15 changes: 0 additions & 15 deletions packages/schematics/src/utility/config-file-utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addApp } from './config-file-utils';
import { addApp } from './fileutils';

describe('configFileUtils', () => {
describe('fileutils', () => {
describe('sortApps', () => {
it('should handle undefined', () => {
expect(addApp(undefined, { name: 'a' })).toEqual([{ name: 'a' }]);
Expand Down
20 changes: 20 additions & 0 deletions packages/schematics/src/utility/fileutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ export function updateJsonFile(path: string, callback: (a: any) => any) {
callback(json);
fs.writeFileSync(path, JSON.stringify(json, null, 2));
}

export function addApp(apps: any[] | undefined, newApp: any): any[] {
if (!apps) {
apps = [];
}
apps.push(newApp);

apps.sort((a: any, b: any) => {
if (a.main && !b.main) return -1;
if (!a.main && b.main) return 1;
if (a.name > b.name) return 1;
return -1;
});

return apps;
}

export function serializeJson(json: any): string {
return `${JSON.stringify(json, null, 2)}\n`;
}
6 changes: 3 additions & 3 deletions packages/schematics/src/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as path from 'path';
import { angularCliVersion, ngrxVersion, nxVersion, prettierVersion, schematicsVersion } from '../utility/lib-versions';
import * as fs from 'fs';
import { join } from 'path';
import { updateJsonFile } from '../utility/fileutils';
import { serializeJson, updateJsonFile } from '../utility/fileutils';
import { toFileName } from '@nrwl/schematics';

function updatePackageJson() {
Expand Down Expand Up @@ -59,7 +59,7 @@ function updatePackageJson() {
packageJson.devDependencies['prettier'] = prettierVersion;
}
packageJson.scripts['format'] = `prettier --single-quote --print-width 120 --write '{apps,libs}/**/*.ts'`;
host.overwrite('package.json', JSON.stringify(packageJson, null, 2));
host.overwrite('package.json', serializeJson(packageJson));
return host;
};
}
Expand Down Expand Up @@ -97,7 +97,7 @@ function updateAngularCLIJson(options: Schema) {
angularCliJson.defaults.schematics['postGenerate'] = 'npm run format';
angularCliJson.defaults.schematics['newProject'] = ['app', 'lib'];

host.overwrite('.angular-cli.json', JSON.stringify(angularCliJson, null, 2));
host.overwrite('.angular-cli.json', serializeJson(angularCliJson));

return host;
};
Expand Down

0 comments on commit c09ba79

Please sign in to comment.