diff --git a/lib/cli/src/generators/ANGULAR/angular-helpers.ts b/lib/cli/src/generators/ANGULAR/angular-helpers.ts index bd9eecfa2bab..a1ab6d8f9221 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,33 @@ 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 | undefined { + const { defaultProject, projects } = angularJson; + + if (projects?.storybook) { + return 'storybook'; + } + + if (defaultProject) { + return defaultProject; + } + + const firstProjectName = projects ? Object.keys(projects)[0] : undefined; + if (firstProjectName) { + return firstProjectName; + } + + return undefined; +} + +export function checkForProjects() { + const { projects } = readFileAsJson('angular.json', true); + + if (!projects || Object.keys(projects).length === 0) { + throw new Error( + 'Could not find a project in your Angular workspace. \nAdd a project and re-run the installation' + ); + } } export async function getBaseTsConfigName() { diff --git a/lib/cli/src/generators/ANGULAR/index.ts b/lib/cli/src/generators/ANGULAR/index.ts index 934ceb6295d5..3ff3b1b4d852 100644 --- a/lib/cli/src/generators/ANGULAR/index.ts +++ b/lib/cli/src/generators/ANGULAR/index.ts @@ -1,7 +1,7 @@ import path from 'path'; import semver from '@storybook/semver'; import { - isDefaultProjectSet, + checkForProjects, editStorybookTsConfig, getAngularAppTsConfigJson, getAngularAppTsConfigPath, @@ -28,11 +28,8 @@ 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.' - ); - } + checkForProjects(); + const angularVersion = semver.coerce( packageManager.retrievePackageJson().dependencies['@angular/core'] )?.version;