From b65ce1158cf3c7dd394e9ec1ffb61753488cd62c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20R=C3=B6der?= Date: Sun, 2 Jun 2019 15:14:48 +0200 Subject: [PATCH] Replace redundant code --- lib/cli/generators/ANGULAR/angular-helpers.js | 25 +++---------------- lib/cli/generators/ANGULAR/index.js | 5 ++-- lib/cli/lib/helpers.js | 2 +- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/lib/cli/generators/ANGULAR/angular-helpers.js b/lib/cli/generators/ANGULAR/angular-helpers.js index 8aab288b628..f9013d39bc8 100644 --- a/lib/cli/generators/ANGULAR/angular-helpers.js +++ b/lib/cli/generators/ANGULAR/angular-helpers.js @@ -2,18 +2,8 @@ import * as path from 'path'; import * as fs from 'fs'; import { readFileAsJson, writeFileAsJson } from '../../lib/helpers'; -export function getAngularJson() { - const angularJsonPath = path.resolve('angular.json'); - if (!fs.existsSync(angularJsonPath)) { - return false; - } - - const jsonContent = fs.readFileSync(angularJsonPath, 'utf8'); - return JSON.parse(jsonContent); -} - export function getAngularAppTsConfigPath() { - const angularJson = getAngularJson(); + const angularJson = readFileAsJson('angular.json'); const { defaultProject } = angularJson; const tsConfigPath = angularJson.projects[defaultProject].architect.build.options.tsConfig; @@ -26,20 +16,11 @@ export function getAngularAppTsConfigPath() { export function getAngularAppTsConfigJson() { const tsConfigPath = getAngularAppTsConfigPath(); - if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) { + if (!tsConfigPath) { return false; } - const jsonContent = fs.readFileSync(tsConfigPath, 'utf8'); - return JSON.parse(jsonContent); -} - -export function writeAngularAppTsConfig(tsConfigJson) { - const content = `${JSON.stringify(tsConfigJson, null, 2)}\n`; - const tsConfigPath = getAngularAppTsConfigPath(); - if (tsConfigPath) { - fs.writeFileSync(path.resolve(tsConfigPath), content, 'utf8'); - } + return readFileAsJson(tsConfigPath); } function setStorybookTsconfigExtendsPath(tsconfigJson) { diff --git a/lib/cli/generators/ANGULAR/index.js b/lib/cli/generators/ANGULAR/index.js index d01191b8446..d437358cf96 100644 --- a/lib/cli/generators/ANGULAR/index.js +++ b/lib/cli/generators/ANGULAR/index.js @@ -3,7 +3,7 @@ import path from 'path'; import { editStorybookTsConfig, getAngularAppTsConfigJson, - writeAngularAppTsConfig, + getAngularAppTsConfigPath, } from './angular-helpers'; import { getVersions, @@ -11,6 +11,7 @@ import { writePackageJson, getBabelDependencies, installDependencies, + writeFileAsJson, } from '../../lib/helpers'; async function addDependencies(npmOptions) { @@ -65,7 +66,7 @@ function editAngularAppTsConfig() { } tsConfigJson.exclude = [...exclude, glob]; - writeAngularAppTsConfig(tsConfigJson); + writeFileAsJson(getAngularAppTsConfigPath(), tsConfigJson); } export default async npmOptions => { diff --git a/lib/cli/lib/helpers.js b/lib/cli/lib/helpers.js index e710bf4cabc..03290d91ae5 100644 --- a/lib/cli/lib/helpers.js +++ b/lib/cli/lib/helpers.js @@ -79,7 +79,7 @@ export function writeFileAsJson(jsonPath, content) { } const jsonContent = fs.readFileSync(filePath, 'utf8'); - fs.writeFileSync(filePath, JSON.stringify(content, null, 2)); + fs.writeFileSync(filePath, `${JSON.stringify(content, null, 2)}\n`); return true; }