generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Add CLI wrapper #41
Merged
Merged
Add CLI wrapper #41
Changes from all commits
Commits
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
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
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,44 @@ | ||
import * as coreCommon from '@storybook/core-common'; | ||
|
||
import * as cliHelper from './helpers'; | ||
import { getCliOptions, getStorybookMain, defaultRunnerOptions } from './cli'; | ||
|
||
jest.mock('@storybook/core-common'); | ||
|
||
describe('CLI', () => { | ||
describe('getCliOptions', () => { | ||
it('returns default options if no extra option is passed', () => { | ||
const opts = getCliOptions(); | ||
expect(opts.runnerOptions).toMatchObject(defaultRunnerOptions); | ||
}); | ||
|
||
it('returns custom options if passed', () => { | ||
const customConfig = { configDir: 'custom', storiesJson: true }; | ||
jest.spyOn(cliHelper, 'getParsedCliOptions').mockReturnValue(customConfig); | ||
const opts = getCliOptions(); | ||
expect(opts.runnerOptions).toMatchObject(customConfig); | ||
}); | ||
}); | ||
|
||
describe('getStorybookMain', () => { | ||
it('should throw an error if no configuration is found', () => { | ||
expect(() => getStorybookMain('.storybook')).toThrow(); | ||
}); | ||
|
||
it('should return mainjs', () => { | ||
const mockedMain = { | ||
stories: [ | ||
{ | ||
directory: '../stories/basic', | ||
titlePrefix: 'Example', | ||
}, | ||
], | ||
}; | ||
|
||
jest.spyOn(coreCommon, 'serverRequire').mockImplementation(() => mockedMain); | ||
|
||
const res = getStorybookMain('.storybook'); | ||
expect(res).toMatchObject(mockedMain); | ||
}); | ||
}); | ||
}); |
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,63 @@ | ||
import { join, resolve } from 'path'; | ||
import { serverRequire, StorybookConfig } from '@storybook/core-common'; | ||
import { getParsedCliOptions } from './helpers'; | ||
|
||
type CliOptions = { | ||
runnerOptions: { | ||
storiesJson: boolean; | ||
configDir: string; | ||
}; | ||
jestOptions: string[]; | ||
}; | ||
|
||
type StorybookRunnerCommand = keyof CliOptions['runnerOptions']; | ||
|
||
const STORYBOOK_RUNNER_COMMANDS: StorybookRunnerCommand[] = ['storiesJson', 'configDir']; | ||
|
||
export const defaultRunnerOptions: CliOptions['runnerOptions'] = { | ||
configDir: '.storybook', | ||
storiesJson: false, | ||
}; | ||
|
||
let storybookMainConfig: StorybookConfig; | ||
|
||
export const getCliOptions = () => { | ||
const allOptions = getParsedCliOptions(); | ||
|
||
const defaultOptions: CliOptions = { | ||
runnerOptions: { ...defaultRunnerOptions }, | ||
jestOptions: process.argv.splice(0, 2), | ||
}; | ||
|
||
return Object.keys(allOptions).reduce((acc, key: any) => { | ||
if (STORYBOOK_RUNNER_COMMANDS.includes(key)) { | ||
//@ts-ignore | ||
acc.runnerOptions[key] = allOptions[key]; | ||
} else { | ||
if (allOptions[key] === true) { | ||
acc.jestOptions.push(`--${key}`); | ||
} else if (allOptions[key] === false) { | ||
acc.jestOptions.push(`--no-${key}`); | ||
} else { | ||
acc.jestOptions.push(`--${key}`, allOptions[key]); | ||
} | ||
} | ||
|
||
return acc; | ||
}, defaultOptions); | ||
}; | ||
|
||
export const getStorybookMain = (configDir: string) => { | ||
if (storybookMainConfig) { | ||
return storybookMainConfig; | ||
} | ||
|
||
storybookMainConfig = serverRequire(join(resolve(configDir), 'main')); | ||
if (!storybookMainConfig) { | ||
throw new Error( | ||
`Could not load main.js in ${configDir}. Is the config directory correct? You can change it by using --config-dir <path-to-dir>` | ||
); | ||
} | ||
|
||
return storybookMainConfig; | ||
}; |
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,41 @@ | ||
export const getParsedCliOptions = () => { | ||
const { program } = require('commander'); | ||
|
||
program | ||
.option('-s, --stories-json', 'Run in stories json mode (requires a compatible Storybook)') | ||
.option('-c, --config-dir <directory>', 'Directory where to load Storybook configurations from') | ||
.option('--watch', 'Run in watch mode') | ||
.option( | ||
'--maxWorkers <amount>', | ||
'Specifies the maximum number of workers the worker-pool will spawn for running tests' | ||
) | ||
.option('--no-cache', 'Disable the cache') | ||
.option('--clearCache', 'Deletes the Jest cache directory and then exits without running tests') | ||
.option('--verbose', 'Display individual test results with the test suite hierarchy'); | ||
|
||
program.exitOverride(); | ||
|
||
try { | ||
program.parse(); | ||
} catch (err) { | ||
switch (err.code) { | ||
case 'commander.unknownOption': { | ||
program.outputHelp(); | ||
console.warn( | ||
`\nIf you'd like this option to be supported, please open an issue at https://github.com/storybookjs/test-runner/issues/new\n` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
case 'commander.helpDisplayed': { | ||
process.exit(0); | ||
} | ||
|
||
default: { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
return program.opts(); | ||
}; |
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.
Do we still need to mock this?
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.
Yes, because of the
jest.spyOn(coreCommon, 'normalizeStories')
code below