diff --git a/lib/cli/src/generators/ANGULAR/angular-helpers.ts b/lib/cli/src/generators/ANGULAR/angular-helpers.ts index bd9eecfa2bab..a1470c42b994 100644 --- a/lib/cli/src/generators/ANGULAR/angular-helpers.ts +++ b/lib/cli/src/generators/ANGULAR/angular-helpers.ts @@ -10,7 +10,7 @@ type TsConfig = { export function getAngularAppTsConfigPath() { const angularJson = readFileAsJson('angular.json', true); - const { defaultProject } = angularJson; + const defaultProject = getDefaultProjectName(angularJson); const tsConfigPath = angularJson.projects[defaultProject].architect.build.options.tsConfig; if (!tsConfigPath || !fs.existsSync(path.resolve(tsConfigPath))) { @@ -50,9 +50,23 @@ export function editStorybookTsConfig(tsconfigPath: string) { writeFileAsJson(tsconfigPath, tsConfigJson); } -export function isDefaultProjectSet() { - const angularJson = readFileAsJson('angular.json', true); - return angularJson && !!angularJson.defaultProject; +export function getDefaultProjectName(angularJson: any): string { + const { defaultProject, projects } = angularJson; + + if (projects.storybook) { + return 'storybook'; + } + + if (defaultProject) { + return defaultProject; + } + + const firstProjectName = Object.keys(projects)[0]; + if (firstProjectName) { + return firstProjectName; + } + + throw new Error('No angular projects found'); } export async function getBaseTsConfigName() { diff --git a/lib/cli/src/generators/ANGULAR/index.ts b/lib/cli/src/generators/ANGULAR/index.ts index 934ceb6295d5..152e32fbb781 100644 --- a/lib/cli/src/generators/ANGULAR/index.ts +++ b/lib/cli/src/generators/ANGULAR/index.ts @@ -1,7 +1,6 @@ import path from 'path'; import semver from '@storybook/semver'; import { - isDefaultProjectSet, editStorybookTsConfig, getAngularAppTsConfigJson, getAngularAppTsConfigPath, @@ -28,11 +27,6 @@ function editAngularAppTsConfig() { } const generator: Generator = async (packageManager, npmOptions, options) => { - if (!isDefaultProjectSet()) { - throw new Error( - 'Could not find a default project in your Angular workspace.\nSet a defaultProject in your angular.json and re-run the installation.' - ); - } const angularVersion = semver.coerce( packageManager.retrievePackageJson().dependencies['@angular/core'] )?.version;