-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
index.ts
116 lines (105 loc) · 3.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
BuilderContext,
BuilderOutput,
createBuilder,
targetFromTargetString,
Target,
} from '@angular-devkit/architect';
import { JsonObject } from '@angular-devkit/core';
import { from, Observable, of, throwError } from 'rxjs';
import type { CLIOptions } from '@storybook/core-common';
import { catchError, map, mapTo, switchMap } from 'rxjs/operators';
import { sync as findUpSync } from 'find-up';
import {
BrowserBuilderOptions,
ExtraEntryPoint,
StylePreprocessorOptions,
} from '@angular-devkit/build-angular';
// eslint-disable-next-line import/no-extraneous-dependencies
import buildStandalone, { StandaloneOptions } from '@storybook/angular/standalone';
import { runCompodoc } from '../utils/run-compodoc';
import { buildStandaloneErrorHandler } from '../utils/build-standalone-errors-handler';
export type StorybookBuilderOptions = JsonObject & {
browserTarget?: string | null;
tsConfig?: string;
compodoc: boolean;
compodocArgs: string[];
styles?: ExtraEntryPoint[];
stylePreprocessorOptions?: StylePreprocessorOptions;
} & Pick<
// makes sure the option exists
CLIOptions,
'outputDir' | 'configDir' | 'loglevel' | 'quiet' | 'docs' | 'webpackStatsJson'
>;
export type StorybookBuilderOutput = JsonObject & BuilderOutput & {};
export default createBuilder(commandBuilder);
function commandBuilder(
options: StorybookBuilderOptions,
context: BuilderContext
): Observable<StorybookBuilderOutput> {
return from(setup(options, context)).pipe(
switchMap(({ tsConfig }) => {
const runCompodoc$ = options.compodoc
? runCompodoc({ compodocArgs: options.compodocArgs, tsconfig: tsConfig }, context).pipe(
mapTo({ tsConfig })
)
: of({});
return runCompodoc$.pipe(mapTo({ tsConfig }));
}),
map(({ tsConfig }) => {
const {
browserTarget,
stylePreprocessorOptions,
styles,
configDir,
docs,
loglevel,
outputDir,
quiet,
webpackStatsJson,
} = options;
const standaloneOptions: StandaloneOptions = {
configDir,
docs,
loglevel,
outputDir,
quiet,
angularBrowserTarget: browserTarget,
angularBuilderContext: context,
angularBuilderOptions: {
...(stylePreprocessorOptions ? { stylePreprocessorOptions } : {}),
...(styles ? { styles } : {}),
},
tsConfig,
webpackStatsJson,
};
return standaloneOptions;
}),
switchMap((standaloneOptions) => runInstance({ ...standaloneOptions, mode: 'static' })),
map(() => {
return { success: true };
})
);
}
async function setup(options: StorybookBuilderOptions, context: BuilderContext) {
let browserOptions: (JsonObject & BrowserBuilderOptions) | undefined;
let browserTarget: Target | undefined;
if (options.browserTarget) {
browserTarget = targetFromTargetString(options.browserTarget);
browserOptions = await context.validateOptions<JsonObject & BrowserBuilderOptions>(
await context.getTargetOptions(browserTarget),
await context.getBuilderNameForTarget(browserTarget)
);
}
return {
tsConfig:
options.tsConfig ??
findUpSync('tsconfig.json', { cwd: options.configDir }) ??
browserOptions.tsConfig,
};
}
function runInstance(options: StandaloneOptions) {
return from(buildStandalone(options)).pipe(
catchError((error: any) => throwError(buildStandaloneErrorHandler(error)))
);
}