-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Angular: Add angular builder to start + build storybook #15061
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1ce3a74
feat(angular): add angular builder to start storybook
ThibaudAV 4b9596d
fixup! feat(angular): add angular builder to start storybook
ThibaudAV d14690c
feat(angular): add angular builder to build storybook
ThibaudAV ccc4ef1
Fix deepscan
shilman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Architect } from '@angular-devkit/architect'; | ||
import { TestingArchitectHost } from '@angular-devkit/architect/testing'; | ||
import { schema } from '@angular-devkit/core'; | ||
import * as path from 'path'; | ||
|
||
const buildStandaloneMock = jest.fn().mockImplementation((_options: unknown) => Promise.resolve()); | ||
|
||
jest.mock('@storybook/angular/standalone', () => buildStandaloneMock); | ||
|
||
describe('Build Storybook Builder', () => { | ||
let architect: Architect; | ||
let architectHost: TestingArchitectHost; | ||
|
||
beforeEach(async () => { | ||
const registry = new schema.CoreSchemaRegistry(); | ||
registry.addPostTransform(schema.transforms.addUndefinedDefaults); | ||
|
||
architectHost = new TestingArchitectHost(); | ||
architect = new Architect(architectHost, registry); | ||
|
||
// This will either take a Node package name, or a path to the directory | ||
// for the package.json file. | ||
await architectHost.addBuilderFromPackage(path.join(__dirname, '../../..')); | ||
}); | ||
|
||
it('should work', async () => { | ||
const run = await architect.scheduleBuilder('@storybook/angular:build-storybook', { | ||
browserTarget: 'angular-cli:build-2', | ||
}); | ||
|
||
const output = await run.result; | ||
|
||
await run.stop(); | ||
|
||
expect(output.success).toBeTruthy(); | ||
expect(buildStandaloneMock).toHaveBeenCalledWith({ | ||
angularBrowserTarget: 'angular-cli:build-2', | ||
browserTarget: 'angular-cli:build-2', | ||
configDir: '.storybook', | ||
docs: false, | ||
loglevel: undefined, | ||
quiet: false, | ||
outputDir: 'storybook-static', | ||
staticDir: [], | ||
mode: 'static', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect'; | ||
import { JsonObject } from '@angular-devkit/core'; | ||
import { from, Observable, of } from 'rxjs'; | ||
import { CLIOptions } from '@storybook/core-common'; | ||
import { map, switchMap } from 'rxjs/operators'; | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import buildStandalone, { StandaloneOptions } from '@storybook/angular/standalone'; | ||
|
||
export type StorybookBuilderOptions = JsonObject & { | ||
browserTarget: string; | ||
} & Pick< | ||
// makes sure the option exists | ||
CLIOptions, | ||
'staticDir' | 'outputDir' | 'configDir' | 'loglevel' | 'quiet' | 'docs' | ||
>; | ||
|
||
export type StorybookBuilderOutput = JsonObject & BuilderOutput & {}; | ||
|
||
export default createBuilder(commandBuilder); | ||
|
||
function commandBuilder( | ||
options: StorybookBuilderOptions, | ||
_context: BuilderContext | ||
): Observable<StorybookBuilderOutput> { | ||
return of({}).pipe( | ||
map(() => ({ | ||
...options, | ||
angularBrowserTarget: options.browserTarget, | ||
})), | ||
switchMap((standaloneOptions) => runInstance({ ...standaloneOptions, mode: 'static' })), | ||
map(() => { | ||
return { success: true }; | ||
}) | ||
); | ||
} | ||
|
||
function runInstance(options: StandaloneOptions) { | ||
return from(buildStandalone(options)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"title": "Build Storybook", | ||
"description": "Serve up storybook in development mode.", | ||
"type": "object", | ||
"properties": { | ||
"browserTarget": { | ||
"type": "string", | ||
"description": "Build target to be served in project-name:builder:config format. Should generally target on the builder: '@angular-devkit/build-angular:browser'. Useful for Storybook to use options (styles, assets, ...).", | ||
"pattern": "^[^:\\s]+:[^:\\s]+(:[^\\s]+)?$" | ||
}, | ||
"staticDir": { | ||
"type": "array", | ||
"description": "Directory where to load static files from, array of strings.", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"outputDir": { | ||
"type": "string", | ||
"description": "Directory where to store built files.", | ||
"default": "storybook-static" | ||
}, | ||
"configDir": { | ||
"type": "string", | ||
"description": "Directory where to load Storybook configurations from.", | ||
"default": ".storybook" | ||
}, | ||
"loglevel": { | ||
"type": "string", | ||
"description": "Controls level of logging during build. Can be one of: [silly, verbose, info (default), warn, error, silent].", | ||
"pattern": "(silly|verbose|info|warn|silent)" | ||
}, | ||
"quiet": { | ||
"type": "boolean", | ||
"description": "Suppress verbose build output.", | ||
"default": false | ||
}, | ||
"docs": { | ||
"type": "boolean", | ||
"description": "Starts Storybook in documentation mode. Learn more about it : https://storybook.js.org/docs/react/writing-docs/build-documentation#preview-storybooks-documentation.", | ||
"default": false | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": ["browserTarget"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"builders": { | ||
"build-storybook": { | ||
"implementation": "./build-storybook", | ||
"schema": "./build-storybook/schema.json", | ||
"description": "Build storybook" | ||
}, | ||
"start-storybook": { | ||
"implementation": "./start-storybook", | ||
"schema": "./start-storybook/schema.json", | ||
"description": "Start storybook" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Architect } from '@angular-devkit/architect'; | ||
import { TestingArchitectHost } from '@angular-devkit/architect/testing'; | ||
import { schema } from '@angular-devkit/core'; | ||
import * as path from 'path'; | ||
|
||
const buildStandaloneMock = jest.fn().mockImplementation((_options: unknown) => Promise.resolve()); | ||
|
||
jest.mock('@storybook/angular/standalone', () => buildStandaloneMock); | ||
|
||
describe('Start Storybook Builder', () => { | ||
let architect: Architect; | ||
let architectHost: TestingArchitectHost; | ||
|
||
beforeEach(async () => { | ||
const registry = new schema.CoreSchemaRegistry(); | ||
registry.addPostTransform(schema.transforms.addUndefinedDefaults); | ||
|
||
architectHost = new TestingArchitectHost(); | ||
architect = new Architect(architectHost, registry); | ||
|
||
// This will either take a Node package name, or a path to the directory | ||
// for the package.json file. | ||
await architectHost.addBuilderFromPackage(path.join(__dirname, '../../..')); | ||
}); | ||
|
||
it('should work', async () => { | ||
const run = await architect.scheduleBuilder('@storybook/angular:start-storybook', { | ||
browserTarget: 'angular-cli:build-2', | ||
port: 4400, | ||
}); | ||
|
||
const output = await run.result; | ||
|
||
await run.stop(); | ||
|
||
expect(output.success).toBeTruthy(); | ||
expect(buildStandaloneMock).toHaveBeenCalledWith({ | ||
angularBrowserTarget: 'angular-cli:build-2', | ||
browserTarget: 'angular-cli:build-2', | ||
ci: false, | ||
configDir: '.storybook', | ||
docs: false, | ||
host: 'localhost', | ||
https: false, | ||
port: 4400, | ||
quiet: false, | ||
smokeTest: false, | ||
sslCa: undefined, | ||
sslCert: undefined, | ||
sslKey: undefined, | ||
staticDir: [], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect'; | ||
import { JsonObject } from '@angular-devkit/core'; | ||
import { from, Observable, of } from 'rxjs'; | ||
import { CLIOptions } from '@storybook/core-common'; | ||
import { map, switchMap } from 'rxjs/operators'; | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import buildStandalone, { StandaloneOptions } from '@storybook/angular/standalone'; | ||
|
||
export type StorybookBuilderOptions = JsonObject & { | ||
browserTarget: string; | ||
} & Pick< | ||
// makes sure the option exists | ||
CLIOptions, | ||
| 'port' | ||
| 'host' | ||
| 'staticDir' | ||
| 'configDir' | ||
| 'https' | ||
| 'sslCa' | ||
| 'sslCert' | ||
| 'sslKey' | ||
| 'smokeTest' | ||
| 'ci' | ||
| 'quiet' | ||
| 'docs' | ||
>; | ||
|
||
export type StorybookBuilderOutput = JsonObject & BuilderOutput & {}; | ||
|
||
export default createBuilder(commandBuilder); | ||
|
||
function commandBuilder( | ||
options: StorybookBuilderOptions, | ||
_context: BuilderContext | ||
): Observable<StorybookBuilderOutput> { | ||
return of({}).pipe( | ||
map(() => ({ | ||
...options, | ||
angularBrowserTarget: options.browserTarget, | ||
})), | ||
switchMap((standaloneOptions) => runInstance(standaloneOptions)), | ||
map(() => { | ||
return { success: true }; | ||
}) | ||
); | ||
} | ||
|
||
function runInstance(options: StandaloneOptions) { | ||
return from(buildStandalone(options)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"title": "Start Storybook", | ||
"description": "Serve up storybook in development mode.", | ||
"type": "object", | ||
"properties": { | ||
"browserTarget": { | ||
"type": "string", | ||
"description": "Build target to be served in project-name:builder:config format. Should generally target on the builder: '@angular-devkit/build-angular:browser'. Useful for Storybook to use options (styles, assets, ...).", | ||
"pattern": "^[^:\\s]+:[^:\\s]+(:[^\\s]+)?$" | ||
}, | ||
"port": { | ||
"type": "number", | ||
"description": "Port to listen on.", | ||
"default": 9009 | ||
}, | ||
"host": { | ||
"type": "string", | ||
"description": "Host to listen on.", | ||
"default": "localhost" | ||
}, | ||
"staticDir": { | ||
"type": "array", | ||
"description": "Directory where to load static files from, array of strings.", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"configDir": { | ||
"type": "string", | ||
"description": "Directory where to load Storybook configurations from.", | ||
"default": ".storybook" | ||
}, | ||
"https": { | ||
"type": "boolean", | ||
"description": "Serve Storybook over HTTPS. Note: You must provide your own certificate information.", | ||
"default": false | ||
}, | ||
"sslCa": { | ||
"type": "string", | ||
"description": "Provide an SSL certificate authority. (Optional with --https, required if using a self-signed certificate)." | ||
}, | ||
"sslCert": { | ||
"type": "string", | ||
"description": "Provide an SSL certificate. (Required with --https)." | ||
}, | ||
"sslKey": { | ||
"type": "string", | ||
"description": "SSL key to use for serving HTTPS." | ||
}, | ||
"smokeTest": { | ||
"type": "boolean", | ||
"description": "Exit after successful start.", | ||
"default": false | ||
}, | ||
"ci": { | ||
"type": "boolean", | ||
"description": "CI mode (skip interactive prompts, don't open browser).", | ||
"default": false | ||
}, | ||
"quiet": { | ||
"type": "boolean", | ||
"description": "Suppress verbose build output.", | ||
"default": false | ||
}, | ||
"docs": { | ||
"type": "boolean", | ||
"description": "Starts Storybook in documentation mode. Learn more about it : https://storybook.js.org/docs/react/writing-docs/build-documentation#preview-storybooks-documentation.", | ||
"default": false | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": ["browserTarget"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { CLIOptions, LoadOptions, BuilderOptions } from '@storybook/core-common'; | ||
|
||
export type StandaloneOptions = Partial< | ||
CLIOptions & | ||
LoadOptions & | ||
BuilderOptions & { | ||
mode?: 'static' | 'dev'; | ||
angularBrowserTarget: string; | ||
} | ||
>; | ||
|
||
declare module '@storybook/angular/standalone' { | ||
export default function buildStandalone(options: StandaloneOptions): Promise<unknown>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for? (15 and 16) I do lots of stuff with schematics but never have I seen this in tests, I'm curious 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this add all default props set in schema.json to test it
I was inspired by https://angular.io/guide/cli-builder#testing-a-builder ^^