Skip to content
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 option to make nocase in include and exclude comparisons #814

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ Options:
-f, --flat Flatten output filename. [boolean] [default: false]
-i, --include Including stories name rule. [array] [default: []]
-e, --exclude Excluding stories name rule. [array] [default: []]
--matchNocase 'include' and 'exclude' comparisons to be non case sensitive.
[boolean] [default: false]
--delay Waiting time [msec] before screenshot for each story. [number] [default: 0]
-V, --viewport Viewport. [array] [default: ["800x600"]]
--disableCssAnimation Disable CSS animation and transition. [boolean] [default: true]
Expand Down
7 changes: 7 additions & 0 deletions packages/storycap/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function createOptions(): MainOptions {
.option('flat', { boolean: true, alias: 'f', default: false, description: 'Flatten output filename.' })
.option('include', { array: true, alias: 'i', default: [], description: 'Including stories name rule.' })
.option('exclude', { array: true, alias: 'e', default: [], description: 'Excluding stories name rule.' })
.option('matchNocase', {
boolean: true,
default: false,
description: "'include' and 'exclude' comparisons to be non case sensitive.",
})
.option('delay', { number: true, default: 0, description: 'Waiting time [msec] before screenshot for each story.' })
.option('viewport', { array: true, alias: 'V', default: ['800x600'], description: 'Viewport.' })
.option('disableCssAnimation', {
Expand Down Expand Up @@ -115,6 +120,7 @@ function createOptions(): MainOptions {
flat,
include,
exclude,
matchNocase,
delay,
viewport,
parallel,
Expand Down Expand Up @@ -175,6 +181,7 @@ function createOptions(): MainOptions {
flat,
include,
exclude,
matchNocase,
delay,
viewports: viewport,
parallel,
Expand Down
21 changes: 16 additions & 5 deletions packages/storycap/src/node/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isMatch } from 'nanomatch';
import { isMatch, MatchOptions } from 'nanomatch';
import { StorybookConnection, StoriesBrowser, Story, sleep, ChromiumNotFoundError } from 'storycrawler';
import { CapturingBrowser } from './capturing-browser';
import { MainOptions, RunMode } from './types';
Expand Down Expand Up @@ -28,10 +28,19 @@ async function bootCapturingBrowserAsWorkers(connection: StorybookConnection, op
return { workers: browsers, closeWorkers: () => Promise.all(browsers.map(b => b.close.bind(b))) };
}

function filterStories(flatStories: Story[], include: string[], exclude: string[]): Story[] {
function filterStories(
flatStories: Story[],
include: string[],
exclude: string[],
matchOptions: MatchOptions,
): Story[] {
const conbined = flatStories.map(s => ({ ...s, name: s.kind + '/' + s.story }));
const included = include.length ? conbined.filter(s => include.some(rule => isMatch(s.name, rule))) : conbined;
const excluded = exclude.length ? included.filter(s => !exclude.some(rule => isMatch(s.name, rule))) : included;
const included = include.length
? conbined.filter(s => include.some(rule => isMatch(s.name, rule, matchOptions)))
: conbined;
const excluded = exclude.length
? included.filter(s => !exclude.some(rule => isMatch(s.name, rule, matchOptions)))
: included;
return excluded;
}

Expand Down Expand Up @@ -60,7 +69,9 @@ export async function main(mainOptions: MainOptions) {
const mode = await detectRunMode(storiesBrowser, mainOptions);
storiesBrowser.close();

const stories = filterStories(allStories, mainOptions.include, mainOptions.exclude);
const stories = filterStories(allStories, mainOptions.include, mainOptions.exclude, {
nocase: mainOptions.matchNocase,
});

if (stories.length === 0) {
logger.warn('There is no matched story. Check your include/exclude options.');
Expand Down
1 change: 1 addition & 0 deletions packages/storycap/src/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface MainOptions extends BaseBrowserOptions {
flat: boolean;
include: string[];
exclude: string[];
matchNocase: boolean;
disableCssAnimation: boolean;
disableWaitAssets: boolean;
trace: boolean;
Expand Down