From ea71032ff2576c7a0fcb6a85c5cd421f2a35ad81 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Fri, 17 May 2024 17:35:50 +0200 Subject: [PATCH] Angular: Cleanup types --- .../src/builders/build-storybook/index.ts | 16 ++----- .../src/builders/start-storybook/index.ts | 8 +--- .../src/builders/utils/run-compodoc.ts | 1 - .../angular/src/builders/utils/setup.ts | 43 ++++++++----------- .../src/builders/utils/standalone-options.ts | 2 +- .../src/server/angular-cli-webpack.d.ts | 4 +- .../angular/src/server/preset-options.ts | 4 +- 7 files changed, 27 insertions(+), 51 deletions(-) diff --git a/code/frameworks/angular/src/builders/build-storybook/index.ts b/code/frameworks/angular/src/builders/build-storybook/index.ts index 97cc2985488f..a41c1490e41a 100644 --- a/code/frameworks/angular/src/builders/build-storybook/index.ts +++ b/code/frameworks/angular/src/builders/build-storybook/index.ts @@ -2,27 +2,21 @@ import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/ar import { JsonObject } from '@angular-devkit/core'; import { sync as findUpSync } from 'find-up'; import { sync as readUpSync } from 'read-pkg-up'; -import { StylePreprocessorOptions } from '@angular-devkit/build-angular'; import { CLIOptions } from '@storybook/types'; import { getEnvConfig, versions } from '@storybook/core-common'; import { addToGlobalContext } from '@storybook/telemetry'; import { buildStaticStandalone, withTelemetry } from '@storybook/core-server'; -import { - AssetPattern, - SourceMapUnion, - StyleClass, - StyleElement, -} from '@angular-devkit/build-angular/src/builders/browser/schema'; +import { StyleClass } from '@angular-devkit/build-angular/src/builders/browser/schema'; import { StandaloneOptions } from '../utils/standalone-options'; import { runCompodoc } from '../utils/run-compodoc'; import { errorSummary, printErrorDetails } from '../utils/error-handler'; -import { setup } from '../utils/setup'; +import { AngularBuilderOptions, setup } from '../utils/setup'; addToGlobalContext('cliVersion', versions.storybook); -export type StorybookBuilderOptions = JsonObject & { +export type StorybookBuilderOptions = AngularBuilderOptions & { browserTarget?: string | null; tsConfig?: string; test: boolean; @@ -30,10 +24,6 @@ export type StorybookBuilderOptions = JsonObject & { compodoc: boolean; compodocArgs: string[]; enableProdMode?: boolean; - styles?: StyleElement[]; - stylePreprocessorOptions?: StylePreprocessorOptions; - assets?: AssetPattern[]; - sourceMap?: SourceMapUnion; } & Pick< // makes sure the option exists CLIOptions, diff --git a/code/frameworks/angular/src/builders/start-storybook/index.ts b/code/frameworks/angular/src/builders/start-storybook/index.ts index cf33cd4b4e1c..194674b0ffd5 100644 --- a/code/frameworks/angular/src/builders/start-storybook/index.ts +++ b/code/frameworks/angular/src/builders/start-storybook/index.ts @@ -17,20 +17,16 @@ import { import { StandaloneOptions } from '../utils/standalone-options'; import { runCompodoc } from '../utils/run-compodoc'; import { printErrorDetails, errorSummary } from '../utils/error-handler'; -import { setup } from '../utils/setup'; +import { AngularBuilderOptions, setup } from '../utils/setup'; addToGlobalContext('cliVersion', versions.storybook); -export type StorybookBuilderOptions = JsonObject & { +export type StorybookBuilderOptions = AngularBuilderOptions & { browserTarget?: string | null; tsConfig?: string; compodoc: boolean; compodocArgs: string[]; enableProdMode?: boolean; - styles?: StyleElement[]; - stylePreprocessorOptions?: StylePreprocessorOptions; - assets?: AssetPattern[]; - sourceMap?: SourceMapUnion; } & Pick< // makes sure the option exists CLIOptions, diff --git a/code/frameworks/angular/src/builders/utils/run-compodoc.ts b/code/frameworks/angular/src/builders/utils/run-compodoc.ts index f916840e4631..512f884b408d 100644 --- a/code/frameworks/angular/src/builders/utils/run-compodoc.ts +++ b/code/frameworks/angular/src/builders/utils/run-compodoc.ts @@ -1,5 +1,4 @@ import { BuilderContext } from '@angular-devkit/architect'; -import { Observable } from 'rxjs'; import * as path from 'path'; import { JsPackageManagerFactory } from '@storybook/core-common'; diff --git a/code/frameworks/angular/src/builders/utils/setup.ts b/code/frameworks/angular/src/builders/utils/setup.ts index b7187260e6c7..976f1115c061 100644 --- a/code/frameworks/angular/src/builders/utils/setup.ts +++ b/code/frameworks/angular/src/builders/utils/setup.ts @@ -2,34 +2,24 @@ import { Target, targetFromTargetString } from '@angular-devkit/architect'; import { BuilderContext } from '@angular-devkit/architect'; import { JsonObject, logging } from '@angular-devkit/core'; import { sync as findUpSync } from 'find-up'; -import { BrowserBuilderOptions, StylePreprocessorOptions } from '@angular-devkit/build-angular'; +import { BrowserBuilderOptions } from '@angular-devkit/build-angular'; import { logger } from '@storybook/node-logger'; -import { - AssetPattern, - SourceMapUnion, - StyleElement, -} from '@angular-devkit/build-angular/src/builders/browser/schema'; -type AngularBuilderOptions = { - stylePreprocessorOptions?: StylePreprocessorOptions; - styles?: StyleElement[]; - assets?: AssetPattern[]; - sourceMap?: SourceMapUnion; -}; - -type Options = AngularBuilderOptions & { +export type AngularBuilderOptions = BrowserBuilderOptions & { browserTarget?: string | null; - tsConfig?: string; configDir?: string; }; -export async function setup(options: Options, context: BuilderContext) { - let browserOptions: (JsonObject & BrowserBuilderOptions) | undefined; +export async function setup( + { stylePreprocessorOptions, styles, assets, sourceMap, ...options }: AngularBuilderOptions, + context: BuilderContext +) { + let browserOptions: BrowserBuilderOptions | undefined; let browserTarget: Target | undefined; if (options.browserTarget) { browserTarget = targetFromTargetString(options.browserTarget); - browserOptions = await context.validateOptions( + browserOptions = await context.validateOptions( await context.getTargetOptions(browserTarget), await context.getBuilderNameForTarget(browserTarget) ); @@ -45,12 +35,11 @@ export async function setup(options: Options, context: BuilderContext) { const angularBuilderOptions = await getBuilderOptions( options.browserTarget, { - ...(options.stylePreprocessorOptions - ? { stylePreprocessorOptions: options.stylePreprocessorOptions } - : {}), - ...(options.styles ? { styles: options.styles } : {}), - ...(options.assets ? { assets: options.assets } : {}), - sourceMap: options.sourceMap ?? false, + ...options, + ...(stylePreprocessorOptions ? { stylePreprocessorOptions } : {}), + ...(styles ? { styles } : {}), + ...(assets ? { assets } : {}), + sourceMap: sourceMap ?? false, }, tsConfig, options.configDir, @@ -91,7 +80,7 @@ async function getBuilderOptions( tsConfig: string, configDir: string, builderContext: BuilderContext -) { +): Promise { /** * Get Browser Target options */ @@ -110,7 +99,9 @@ async function getBuilderOptions( ...browserTargetOptions, ...angularBuilderOptions, tsConfig: - tsConfig ?? findUpSync('tsconfig.json', { cwd: configDir }) ?? browserTargetOptions.tsConfig, + tsConfig ?? + findUpSync('tsconfig.json', { cwd: configDir }) ?? + (browserTargetOptions.tsConfig as string), }; logger.info(`=> Using angular project with "tsConfig:${builderOptions.tsConfig}"`); diff --git a/code/frameworks/angular/src/builders/utils/standalone-options.ts b/code/frameworks/angular/src/builders/utils/standalone-options.ts index 42f8a417b0c8..9889797bcbe3 100644 --- a/code/frameworks/angular/src/builders/utils/standalone-options.ts +++ b/code/frameworks/angular/src/builders/utils/standalone-options.ts @@ -1,7 +1,7 @@ import { BuilderContext } from '@angular-devkit/architect'; import { LoadOptions, CLIOptions, BuilderOptions } from '@storybook/types'; -import { AngularBuilderOptions } from '../../server/framework-preset-angular-cli'; +import { AngularBuilderOptions } from './setup'; export type StandaloneOptions = CLIOptions & LoadOptions & diff --git a/code/frameworks/angular/src/server/angular-cli-webpack.d.ts b/code/frameworks/angular/src/server/angular-cli-webpack.d.ts index b2f79e4d963a..8450a946025a 100644 --- a/code/frameworks/angular/src/server/angular-cli-webpack.d.ts +++ b/code/frameworks/angular/src/server/angular-cli-webpack.d.ts @@ -1,7 +1,7 @@ -import { JsonObject } from '@angular-devkit/core'; import { BuilderContext } from '@angular-devkit/architect'; +import { AngularBuilderOptions } from '../builders/utils/setup'; export declare function getWebpackConfig( baseConfig: any, - options: { builderOptions: JsonObject; builderContext: BuilderContext } + options: { builderOptions: AngularBuilderOptions; builderContext: BuilderContext } ): any; diff --git a/code/frameworks/angular/src/server/preset-options.ts b/code/frameworks/angular/src/server/preset-options.ts index 6b6c19d5ad39..c854b2834e39 100644 --- a/code/frameworks/angular/src/server/preset-options.ts +++ b/code/frameworks/angular/src/server/preset-options.ts @@ -1,13 +1,13 @@ import { Options as CoreOptions } from '@storybook/types'; import { BuilderContext } from '@angular-devkit/architect'; -import { JsonObject } from '@angular-devkit/core'; +import { AngularBuilderOptions } from '../builders/utils/setup'; export type PresetOptions = CoreOptions & { /* Allow to get the options of a targeted "browser builder" */ angularBrowserTarget?: string | null; /* Defined set of options. These will take over priority from angularBrowserTarget options */ - angularBuilderOptions?: JsonObject; + angularBuilderOptions?: AngularBuilderOptions; /* Angular context from builder */ angularBuilderContext?: BuilderContext | null; tsConfig?: string;