diff --git a/lib/cli/generators/ANGULAR/index.js b/lib/cli/generators/ANGULAR/index.js index 7721b03eb9c..bd33a360b3f 100644 --- a/lib/cli/generators/ANGULAR/index.js +++ b/lib/cli/generators/ANGULAR/index.js @@ -1,8 +1,15 @@ import mergeDirs from 'merge-dirs'; import path from 'path'; -import { getVersions, getPackageJson, writePackageJson, installBabel } from '../../lib/helpers'; +import { + getVersions, + getPackageJson, + writePackageJson, + installBabel, + getAngularAppTsConfigJson, + writeAngularAppTsConfig, +} from '../../lib/helpers'; -export default async npmOptions => { +async function addDependencies(npmOptions) { const [ storybookVersion, notesVersion, @@ -17,7 +24,6 @@ export default async npmOptions => { '@storybook/addon-links', '@storybook/addons' ); - mergeDirs(path.resolve(__dirname, 'template'), '.', 'overwrite'); const packageJson = getPackageJson(); @@ -35,4 +41,27 @@ export default async npmOptions => { packageJson.scripts['build-storybook'] = 'build-storybook'; writePackageJson(packageJson); +} + +function editAngularAppTsConfig() { + const tsConfigJson = getAngularAppTsConfigJson(); + const glob = '**/*.stories.ts'; + if (!tsConfigJson) { + return; + } + + const { exclude = [] } = tsConfigJson; + if (exclude.includes(glob)) { + return; + } + + tsConfigJson.exclude = [...exclude, glob]; + writeAngularAppTsConfig(tsConfigJson); +} + +export default async npmOptions => { + mergeDirs(path.resolve(__dirname, 'template'), '.', 'overwrite'); + + await addDependencies(npmOptions); + editAngularAppTsConfig(); }; diff --git a/lib/cli/lib/helpers.js b/lib/cli/lib/helpers.js index 11aa3765df5..6959cc3f38b 100644 --- a/lib/cli/lib/helpers.js +++ b/lib/cli/lib/helpers.js @@ -62,6 +62,46 @@ export function getBowerJson() { return JSON.parse(jsonContent); } +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 { 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(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'); + } +} + export function writePackageJson(packageJson) { const content = `${JSON.stringify(packageJson, null, 2)}\n`; const packageJsonPath = path.resolve('package.json');