generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from storybookjs/feat/cli-wrapper
Add CLI wrapper
- Loading branch information
Showing
9 changed files
with
189 additions
and
15 deletions.
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