From 2b9811dad4646dd2850fe6a48774c6fd2925aef8 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Mon, 4 Feb 2019 14:31:43 +0200 Subject: [PATCH] refactor(docs-infra): format `package.json` for readability in `ng-packages-installer` (#28510) PR Close #28510 --- aio/tools/ng-packages-installer/index.js | 6 +++--- aio/tools/ng-packages-installer/index.spec.js | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/aio/tools/ng-packages-installer/index.js b/aio/tools/ng-packages-installer/index.js index a3aeb3a9cc..23af920e66 100644 --- a/aio/tools/ng-packages-installer/index.js +++ b/aio/tools/ng-packages-installer/index.js @@ -88,10 +88,10 @@ class NgPackagesInstaller { }); }); - fs.writeFileSync(pkg.packageJsonPath, JSON.stringify(tmpConfig)); + fs.writeFileSync(pkg.packageJsonPath, JSON.stringify(tmpConfig, null, 2)); }); - const packageConfigFile = fs.readFileSync(pathToPackageConfig); + const packageConfigFile = fs.readFileSync(pathToPackageConfig, 'utf8'); const packageConfig = JSON.parse(packageConfigFile); const [dependencies, peers] = this._collectDependencies(packageConfig.dependencies || {}, packages); @@ -118,7 +118,7 @@ class NgPackagesInstaller { this._log(`Restoring original ${PACKAGE_JSON} for local Angular packages.`); Object.keys(packages).forEach(key => { const pkg = packages[key]; - fs.writeFileSync(pkg.packageJsonPath, JSON.stringify(pkg.config)); + fs.writeFileSync(pkg.packageJsonPath, JSON.stringify(pkg.config, null, 2)); }); } } diff --git a/aio/tools/ng-packages-installer/index.spec.js b/aio/tools/ng-packages-installer/index.spec.js index f205947380..e8950045ab 100644 --- a/aio/tools/ng-packages-installer/index.spec.js +++ b/aio/tools/ng-packages-installer/index.spec.js @@ -150,31 +150,32 @@ describe('NgPackagesInstaller', () => { const pkgJsonFor = pkgName => dummyNgPackages[`@angular/${pkgName}`].packageJsonPath; const pkgConfigFor = pkgName => copyJsonObj(dummyNgPackages[`@angular/${pkgName}`].config); const overwriteConfigFor = (pkgName, newProps) => Object.assign(pkgConfigFor(pkgName), newProps); + const stringifyConfig = config => JSON.stringify(config, null, 2); const allArgs = fs.writeFileSync.calls.allArgs(); const firstFiveArgs = allArgs.slice(0, 5); const lastFiveArgs = allArgs.slice(-5); expect(firstFiveArgs).toEqual([ - [pkgJsonFor('core'), JSON.stringify(overwriteConfigFor('core', {private: true}))], - [pkgJsonFor('common'), JSON.stringify(overwriteConfigFor('common', {private: true}))], - [pkgJsonFor('compiler'), JSON.stringify(overwriteConfigFor('compiler', {private: true}))], - [pkgJsonFor('compiler-cli'), JSON.stringify(overwriteConfigFor('compiler-cli', { + [pkgJsonFor('core'), stringifyConfig(overwriteConfigFor('core', {private: true}))], + [pkgJsonFor('common'), stringifyConfig(overwriteConfigFor('common', {private: true}))], + [pkgJsonFor('compiler'), stringifyConfig(overwriteConfigFor('compiler', {private: true}))], + [pkgJsonFor('compiler-cli'), stringifyConfig(overwriteConfigFor('compiler-cli', { private: true, dependencies: { '@angular/tsc-wrapped': `file:${toolsDir}/tsc-wrapped` } }))], - [pkgJsonFor('tsc-wrapped'), JSON.stringify(overwriteConfigFor('tsc-wrapped', { + [pkgJsonFor('tsc-wrapped'), stringifyConfig(overwriteConfigFor('tsc-wrapped', { private: true, devDependencies: { '@angular/common': `file:${packagesDir}/common` } }))], ]); expect(lastFiveArgs).toEqual(['core', 'common', 'compiler', 'compiler-cli', 'tsc-wrapped'] - .map(pkgName => [pkgJsonFor(pkgName), JSON.stringify(pkgConfigFor(pkgName))])); + .map(pkgName => [pkgJsonFor(pkgName), stringifyConfig(pkgConfigFor(pkgName))])); }); it('should load the package.json', () => { - expect(fs.readFileSync).toHaveBeenCalledWith(packageJsonPath); + expect(fs.readFileSync).toHaveBeenCalledWith(packageJsonPath, 'utf8'); }); it('should overwrite package.json with modified config', () => {