From 69d9c98d3be0c845c0b8bab96a099c4392364a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20R=C3=B6der?= Date: Sun, 2 Jun 2019 14:51:05 +0200 Subject: [PATCH 1/3] Improve angular tsconfig.app.json detection --- lib/cli/generators/ANGULAR/angular-helpers.js | 31 +++++++++++++++++++ lib/cli/generators/ANGULAR/index.js | 2 ++ .../ANGULAR/template/.storybook/tsconfig.json | 21 +++---------- 3 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 lib/cli/generators/ANGULAR/angular-helpers.js diff --git a/lib/cli/generators/ANGULAR/angular-helpers.js b/lib/cli/generators/ANGULAR/angular-helpers.js new file mode 100644 index 00000000000..0c48ed9a721 --- /dev/null +++ b/lib/cli/generators/ANGULAR/angular-helpers.js @@ -0,0 +1,31 @@ +import * as path from 'path'; +import * as fs from 'fs'; +import { getAngularAppTsConfigPath } from '../../lib/helpers'; + +export function readFileAsJson(jsonPath) { + const filePath = path.resolve(jsonPath); + if (!fs.existsSync(filePath)) { + return false; + } + + const jsonContent = fs.readFileSync(filePath, 'utf8'); + return JSON.parse(jsonContent); +} + +export function writeFileAsJson(jsonPath, content) { + const filePath = path.resolve(jsonPath); + if (!fs.existsSync(filePath)) { + return false; + } + + const jsonContent = fs.readFileSync(filePath, 'utf8'); + fs.writeFileSync(filePath, JSON.stringify(content, null, 2)); + return true; +} + +export function editStorybookTsConfig(tsconfigPath) { + const tsConfigJson = readFileAsJson(tsconfigPath); + const angularProjectTsConfigPath = getAngularAppTsConfigPath(); + tsConfigJson.extends = `../${angularProjectTsConfigPath}`; + writeFileAsJson(tsconfigPath, tsConfigJson); +} diff --git a/lib/cli/generators/ANGULAR/index.js b/lib/cli/generators/ANGULAR/index.js index 8e660baaff7..9017aee2277 100644 --- a/lib/cli/generators/ANGULAR/index.js +++ b/lib/cli/generators/ANGULAR/index.js @@ -1,5 +1,6 @@ import mergeDirs from 'merge-dirs'; import path from 'path'; +import { editStorybookTsConfig } from './angular-helpers'; import { getVersions, getPackageJson, @@ -70,4 +71,5 @@ export default async npmOptions => { await addDependencies(npmOptions); editAngularAppTsConfig(); + editStorybookTsConfig(path.resolve('./.storybook/tsconfig.json')); }; diff --git a/lib/cli/generators/ANGULAR/template/.storybook/tsconfig.json b/lib/cli/generators/ANGULAR/template/.storybook/tsconfig.json index f01af58ecde..28aadc68fd9 100644 --- a/lib/cli/generators/ANGULAR/template/.storybook/tsconfig.json +++ b/lib/cli/generators/ANGULAR/template/.storybook/tsconfig.json @@ -1,20 +1,9 @@ { - "extends": "../src/tsconfig.app.json", + "extends": "%SET_DURING_SB_INIT%", "compilerOptions": { - "types": [ - "node" - ] + "types": ["node"] }, - "exclude": [ - "../src/test.ts", - "../src/**/*.spec.ts", - "../projects/**/*.spec.ts" - ], - "include": [ - "../src/**/*", - "../projects/**/*" - ], - "files": [ - "./typings.d.ts" - ] + "exclude": ["../src/test.ts", "../src/**/*.spec.ts", "../projects/**/*.spec.ts"], + "include": ["../src/**/*", "../projects/**/*"], + "files": ["./typings.d.ts"] } From 3a92a9f79dbfea27a3978a75a4864a69f5a2725e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20R=C3=B6der?= Date: Sun, 2 Jun 2019 14:59:16 +0200 Subject: [PATCH 2/3] Boyscouting and refactoring --- lib/cli/generators/ANGULAR/angular-helpers.js | 53 ++++++++++++++----- lib/cli/generators/ANGULAR/index.js | 8 +-- lib/cli/lib/helpers.js | 39 ++++---------- 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/lib/cli/generators/ANGULAR/angular-helpers.js b/lib/cli/generators/ANGULAR/angular-helpers.js index 0c48ed9a721..8aab288b628 100644 --- a/lib/cli/generators/ANGULAR/angular-helpers.js +++ b/lib/cli/generators/ANGULAR/angular-helpers.js @@ -1,31 +1,56 @@ import * as path from 'path'; import * as fs from 'fs'; -import { getAngularAppTsConfigPath } from '../../lib/helpers'; +import { readFileAsJson, writeFileAsJson } from '../../lib/helpers'; -export function readFileAsJson(jsonPath) { - const filePath = path.resolve(jsonPath); - if (!fs.existsSync(filePath)) { +export function getAngularJson() { + const angularJsonPath = path.resolve('angular.json'); + if (!fs.existsSync(angularJsonPath)) { return false; } - const jsonContent = fs.readFileSync(filePath, 'utf8'); + const jsonContent = fs.readFileSync(angularJsonPath, 'utf8'); return JSON.parse(jsonContent); } -export function writeFileAsJson(jsonPath, content) { - const filePath = path.resolve(jsonPath); - if (!fs.existsSync(filePath)) { +export function getAngularAppTsConfigPath() { + const angularJson = getAngularJson(); + const { defaultProject } = angularJson; + const tsConfigPath = angularJson.projects[defaultProject].architect.build.options.tsConfig; + + if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) { + return false; + } + return tsConfigPath; +} + +export function getAngularAppTsConfigJson() { + const tsConfigPath = getAngularAppTsConfigPath(); + + if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) { return false; } - const jsonContent = fs.readFileSync(filePath, 'utf8'); - fs.writeFileSync(filePath, JSON.stringify(content, null, 2)); - return true; + 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'); + } +} + +function setStorybookTsconfigExtendsPath(tsconfigJson) { + const angularProjectTsConfigPath = getAngularAppTsConfigPath(); + const newTsconfigJson = { ...tsconfigJson }; + newTsconfigJson.extends = `../${angularProjectTsConfigPath}`; + return newTsconfigJson; } export function editStorybookTsConfig(tsconfigPath) { - const tsConfigJson = readFileAsJson(tsconfigPath); - const angularProjectTsConfigPath = getAngularAppTsConfigPath(); - tsConfigJson.extends = `../${angularProjectTsConfigPath}`; + let tsConfigJson = readFileAsJson(tsconfigPath); + tsConfigJson = setStorybookTsconfigExtendsPath(tsConfigJson); writeFileAsJson(tsconfigPath, tsConfigJson); } diff --git a/lib/cli/generators/ANGULAR/index.js b/lib/cli/generators/ANGULAR/index.js index 9017aee2277..d01191b8446 100644 --- a/lib/cli/generators/ANGULAR/index.js +++ b/lib/cli/generators/ANGULAR/index.js @@ -1,14 +1,16 @@ import mergeDirs from 'merge-dirs'; import path from 'path'; -import { editStorybookTsConfig } from './angular-helpers'; +import { + editStorybookTsConfig, + getAngularAppTsConfigJson, + writeAngularAppTsConfig, +} from './angular-helpers'; import { getVersions, getPackageJson, writePackageJson, getBabelDependencies, installDependencies, - getAngularAppTsConfigJson, - writeAngularAppTsConfig, } from '../../lib/helpers'; async function addDependencies(npmOptions) { diff --git a/lib/cli/lib/helpers.js b/lib/cli/lib/helpers.js index 2cffc0c502c..e710bf4cabc 100644 --- a/lib/cli/lib/helpers.js +++ b/lib/cli/lib/helpers.js @@ -62,44 +62,25 @@ export function getBowerJson() { return JSON.parse(jsonContent); } -export function getAngularJson() { - const angularJsonPath = path.resolve('angular.json'); - if (!fs.existsSync(angularJsonPath)) { +export function readFileAsJson(jsonPath) { + const filePath = path.resolve(jsonPath); + if (!fs.existsSync(filePath)) { return false; } - const jsonContent = fs.readFileSync(angularJsonPath, 'utf8'); + const jsonContent = fs.readFileSync(filePath, 'utf8'); return JSON.parse(jsonContent); } -export function getAngularAppTsConfigPath() { - const angularJson = getAngularJson(); - const { defaultProject } = angularJson; - const tsConfigPath = angularJson.projects[defaultProject].architect.build.options.tsConfig; - - if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) { - return false; - } - return tsConfigPath; -} - -export function getAngularAppTsConfigJson() { - const tsConfigPath = getAngularAppTsConfigPath(); - - if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) { +export function writeFileAsJson(jsonPath, content) { + const filePath = path.resolve(jsonPath); + if (!fs.existsSync(filePath)) { 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'); - } + const jsonContent = fs.readFileSync(filePath, 'utf8'); + fs.writeFileSync(filePath, JSON.stringify(content, null, 2)); + return true; } export function writePackageJson(packageJson) { 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 3/3] 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; }