@storysnap/cli 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/dist/cli/commands/init.js +3 -2
- package/dist/cli/commands/test.d.ts +2 -2
- package/dist/cli/commands/test.js +7 -23
- package/dist/cli/commands/update.d.ts +2 -2
- package/dist/cli/commands/update.js +8 -23
- package/dist/cli/index.js +11 -2
- package/dist/cli/select-stories.d.ts +25 -0
- package/dist/cli/select-stories.js +57 -0
- package/dist/config/loader.js +19 -13
- package/dist/core/changed.d.ts +26 -0
- package/dist/core/changed.js +78 -0
- package/dist/core/storybook.d.ts +1 -0
- package/dist/core/storybook.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,7 +112,9 @@ npx @storysnap/cli test [options]
|
|
|
112
112
|
|
|
113
113
|
Options:
|
|
114
114
|
-c, --config <path> Path to config file
|
|
115
|
-
-s, --story <pattern>
|
|
115
|
+
-s, --story <pattern> Only stories matching a pattern (repeatable)
|
|
116
|
+
-x, --exclude <pattern> Skip stories matching a pattern (repeatable)
|
|
117
|
+
--changed [ref] Only stories changed since branching off ref (default: main)
|
|
116
118
|
-b, --browser <browsers> Browsers to test (chromium,firefox,webkit)
|
|
117
119
|
--ci CI mode - fail on any visual diff or new story
|
|
118
120
|
--local Force local-only mode without using the API
|
|
@@ -128,7 +130,9 @@ npx @storysnap/cli update [options]
|
|
|
128
130
|
|
|
129
131
|
Options:
|
|
130
132
|
-c, --config <path> Path to config file
|
|
131
|
-
-s, --story <pattern>
|
|
133
|
+
-s, --story <pattern> Only stories matching a pattern (repeatable)
|
|
134
|
+
-x, --exclude <pattern> Skip stories matching a pattern (repeatable)
|
|
135
|
+
--changed [ref] Only stories changed since branching off ref (default: main)
|
|
132
136
|
-b, --browser <browsers> Browsers to test (chromium,firefox,webkit)
|
|
133
137
|
--dry-run Show what would be updated without making changes
|
|
134
138
|
--local Force local-only mode without using the API
|
|
@@ -40,9 +40,10 @@ export async function initCommand(options = {}) {
|
|
|
40
40
|
reporter.success(`Created ${fileName}`);
|
|
41
41
|
reporter.newLine();
|
|
42
42
|
reporter.info('Next steps:');
|
|
43
|
-
reporter.info(' 1.
|
|
44
|
-
reporter.info(' 2. Run "storysnap
|
|
43
|
+
reporter.info(' 1. Check the Storybook settings in the config');
|
|
44
|
+
reporter.info(' 2. Run "storysnap login" to use a StorySnap server, or skip it to keep baselines local');
|
|
45
45
|
reporter.info(' 3. Run "storysnap update" to save baselines');
|
|
46
|
+
reporter.info(' 4. Run "storysnap test" to compare against them');
|
|
46
47
|
}
|
|
47
48
|
catch (error) {
|
|
48
49
|
reporter.error(`Failed to create config: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { loadConfig } from '../../config/loader.js';
|
|
2
2
|
import { loadCredentials } from '../../config/credentials.js';
|
|
3
|
-
import { discoverStories } from '../../core/storybook.js';
|
|
4
3
|
import { captureScreenshots, getScreenshotKey, parseScreenshotKey } from '../../core/screenshot.js';
|
|
5
4
|
import { compareAllScreenshots, DEFAULT_MAX_DIFF_PERCENT } from '../../core/compare.js';
|
|
6
5
|
import { createStorage } from '../../storage/index.js';
|
|
7
6
|
import { createReporter } from '../reporter.js';
|
|
7
|
+
import { selectStories } from '../select-stories.js';
|
|
8
8
|
import { ApiClient } from '../../api/client.js';
|
|
9
9
|
function parseBrowsers(browserArg) {
|
|
10
10
|
if (!browserArg) {
|
|
@@ -63,21 +63,13 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
63
63
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
64
64
|
reporter.newLine();
|
|
65
65
|
const client = new ApiClient({ apiUrl, apiToken });
|
|
66
|
-
// Apply story filter if provided
|
|
67
|
-
if (options.story) {
|
|
68
|
-
config.include = [options.story];
|
|
69
|
-
}
|
|
70
66
|
// Screenshots are captured here, not by the server: the worker has no route
|
|
71
67
|
// to a Storybook running on this machine or on a CI runner.
|
|
72
|
-
reporter
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
reporter.failSpinner('No stories found');
|
|
76
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
77
|
-
process.exit(1);
|
|
68
|
+
const stories = await selectStories(reporter, config, options, 'tests');
|
|
69
|
+
if (!stories) {
|
|
70
|
+
return;
|
|
78
71
|
}
|
|
79
72
|
const totalTests = stories.length * config.browsers.length;
|
|
80
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} tests across ${config.browsers.length} browser(s))`);
|
|
81
73
|
reporter.startSpinner('Capturing screenshots...');
|
|
82
74
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
83
75
|
reporter.progress(completed, total, `${story.title}/${story.name} (${browser})`);
|
|
@@ -163,22 +155,14 @@ async function runLocalMode(reporter, config, options) {
|
|
|
163
155
|
}
|
|
164
156
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
165
157
|
reporter.newLine();
|
|
166
|
-
// Apply story filter if provided
|
|
167
|
-
if (options.story) {
|
|
168
|
-
config.include = [options.story];
|
|
169
|
-
}
|
|
170
158
|
// Create storage
|
|
171
159
|
const storage = createStorage(config.storage);
|
|
172
160
|
// Discover stories
|
|
173
|
-
reporter
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
reporter.failSpinner('No stories found');
|
|
177
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
178
|
-
process.exit(1);
|
|
161
|
+
const stories = await selectStories(reporter, config, options, 'tests');
|
|
162
|
+
if (!stories) {
|
|
163
|
+
return;
|
|
179
164
|
}
|
|
180
165
|
const totalTests = stories.length * config.browsers.length;
|
|
181
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} tests across ${config.browsers.length} browser(s))`);
|
|
182
166
|
// Capture screenshots
|
|
183
167
|
reporter.startSpinner('Capturing screenshots...');
|
|
184
168
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { type StorySelectionOptions } from '../select-stories.js';
|
|
2
|
+
export interface UpdateOptions extends StorySelectionOptions {
|
|
2
3
|
config?: string;
|
|
3
|
-
story?: string;
|
|
4
4
|
browser?: string;
|
|
5
5
|
storybookUrl?: string;
|
|
6
6
|
verbose?: boolean;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { loadConfig } from '../../config/loader.js';
|
|
2
2
|
import { loadCredentials } from '../../config/credentials.js';
|
|
3
|
-
import {
|
|
3
|
+
import { getStoryKey } from '../../core/storybook.js';
|
|
4
4
|
import { captureScreenshots, getScreenshotKey } from '../../core/screenshot.js';
|
|
5
5
|
import { createStorage } from '../../storage/index.js';
|
|
6
6
|
import { createReporter } from '../reporter.js';
|
|
7
|
+
import { selectStories } from '../select-stories.js';
|
|
7
8
|
import { ApiClient } from '../../api/client.js';
|
|
8
9
|
function parseBrowsers(browserArg) {
|
|
9
10
|
if (!browserArg) {
|
|
@@ -66,20 +67,12 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
66
67
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
67
68
|
reporter.newLine();
|
|
68
69
|
const client = new ApiClient({ apiUrl, apiToken });
|
|
69
|
-
// Apply story filter if provided
|
|
70
|
-
if (options.story) {
|
|
71
|
-
config.include = [options.story];
|
|
72
|
-
}
|
|
73
70
|
// Discover stories
|
|
74
|
-
reporter
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
reporter.failSpinner('No stories found');
|
|
78
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
79
|
-
process.exit(1);
|
|
71
|
+
const stories = await selectStories(reporter, config, options, 'baselines');
|
|
72
|
+
if (!stories) {
|
|
73
|
+
return;
|
|
80
74
|
}
|
|
81
75
|
const totalTests = stories.length * config.browsers.length;
|
|
82
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} baselines across ${config.browsers.length} browser(s))`);
|
|
83
76
|
// Capture screenshots
|
|
84
77
|
reporter.startSpinner('Capturing screenshots...');
|
|
85
78
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
@@ -153,22 +146,14 @@ async function runLocalMode(reporter, config, options) {
|
|
|
153
146
|
}
|
|
154
147
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
155
148
|
reporter.newLine();
|
|
156
|
-
// Apply story filter if provided
|
|
157
|
-
if (options.story) {
|
|
158
|
-
config.include = [options.story];
|
|
159
|
-
}
|
|
160
149
|
// Create storage
|
|
161
150
|
const storage = createStorage(config.storage);
|
|
162
151
|
// Discover stories
|
|
163
|
-
reporter
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
reporter.failSpinner('No stories found');
|
|
167
|
-
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
168
|
-
process.exit(1);
|
|
152
|
+
const stories = await selectStories(reporter, config, options, 'baselines');
|
|
153
|
+
if (!stories) {
|
|
154
|
+
return;
|
|
169
155
|
}
|
|
170
156
|
const totalTests = stories.length * config.browsers.length;
|
|
171
|
-
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} baselines across ${config.browsers.length} browser(s))`);
|
|
172
157
|
// Capture screenshots
|
|
173
158
|
reporter.startSpinner('Capturing screenshots...');
|
|
174
159
|
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
package/dist/cli/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { updateCommand } from './commands/update.js';
|
|
|
5
5
|
import { initCommand } from './commands/init.js';
|
|
6
6
|
import { loginCommand } from './commands/login.js';
|
|
7
7
|
import { logoutCommand } from './commands/logout.js';
|
|
8
|
+
import { collectPatterns } from './select-stories.js';
|
|
8
9
|
const program = new Command();
|
|
9
10
|
program
|
|
10
11
|
.name('storysnap')
|
|
@@ -47,7 +48,9 @@ program
|
|
|
47
48
|
.command('test')
|
|
48
49
|
.description('Run visual regression tests')
|
|
49
50
|
.option('-c, --config <path>', 'Path to config file')
|
|
50
|
-
.option('-s, --story <pattern>', '
|
|
51
|
+
.option('-s, --story <pattern>', 'Only stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
52
|
+
.option('-x, --exclude <pattern>', 'Skip stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
53
|
+
.option('--changed [ref]', 'Only stories whose files changed since branching off <ref> (default: main, then origin/main)')
|
|
51
54
|
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
52
55
|
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
53
56
|
.option('--branch <branch>', 'Branch name to record on the test run (API mode)')
|
|
@@ -59,6 +62,8 @@ program
|
|
|
59
62
|
await testCommand({
|
|
60
63
|
config: options.config,
|
|
61
64
|
story: options.story,
|
|
65
|
+
exclude: options.exclude,
|
|
66
|
+
changed: options.changed,
|
|
62
67
|
browser: options.browser,
|
|
63
68
|
storybookUrl: options.storybookUrl,
|
|
64
69
|
branch: options.branch,
|
|
@@ -72,7 +77,9 @@ program
|
|
|
72
77
|
.command('update')
|
|
73
78
|
.description('Update baseline screenshots')
|
|
74
79
|
.option('-c, --config <path>', 'Path to config file')
|
|
75
|
-
.option('-s, --story <pattern>', '
|
|
80
|
+
.option('-s, --story <pattern>', 'Only stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
81
|
+
.option('-x, --exclude <pattern>', 'Skip stories matching a pattern (* wildcards; repeat or comma-separate for several)', collectPatterns)
|
|
82
|
+
.option('--changed [ref]', 'Only stories whose files changed since branching off <ref> (default: main, then origin/main)')
|
|
76
83
|
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
77
84
|
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
78
85
|
.option('-v, --verbose', 'Verbose output')
|
|
@@ -82,6 +89,8 @@ program
|
|
|
82
89
|
await updateCommand({
|
|
83
90
|
config: options.config,
|
|
84
91
|
story: options.story,
|
|
92
|
+
exclude: options.exclude,
|
|
93
|
+
changed: options.changed,
|
|
85
94
|
browser: options.browser,
|
|
86
95
|
storybookUrl: options.storybookUrl,
|
|
87
96
|
verbose: options.verbose,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Story, StorySnapConfig } from '@storysnap/shared';
|
|
2
|
+
import type { Reporter } from './reporter.js';
|
|
3
|
+
export interface StorySelectionOptions {
|
|
4
|
+
/** --story patterns. They narrow the config's include list rather than replacing it. */
|
|
5
|
+
story?: string[];
|
|
6
|
+
/** --exclude patterns, applied on top of the config's exclude list. */
|
|
7
|
+
exclude?: string[];
|
|
8
|
+
/** --changed: true for the default ref, or the ref that was named. */
|
|
9
|
+
changed?: string | boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Commander collector for options that may be repeated or comma-separated,
|
|
13
|
+
* e.g. `--story "Button*" --story "Form/*"` or `--story "Button*,Form/*"`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function collectPatterns(value: string, previous?: string[]): string[];
|
|
16
|
+
/**
|
|
17
|
+
* Discovers stories and narrows them by the command-line filters, reporting the
|
|
18
|
+
* result on the spinner. `unit` names what each story-browser pair produces.
|
|
19
|
+
*
|
|
20
|
+
* Returns null when --changed leaves nothing to test, which is a normal outcome
|
|
21
|
+
* for a change that touches no stories. An empty result from the patterns is
|
|
22
|
+
* a mistake instead, so that exits like an empty Storybook does.
|
|
23
|
+
*/
|
|
24
|
+
export declare function selectStories(reporter: Reporter, config: StorySnapConfig, options: StorySelectionOptions, unit: 'tests' | 'baselines'): Promise<Story[] | null>;
|
|
25
|
+
//# sourceMappingURL=select-stories.d.ts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { discoverStories, filterStories } from '../core/storybook.js';
|
|
2
|
+
import { getChangedFiles, filterChangedStories } from '../core/changed.js';
|
|
3
|
+
/**
|
|
4
|
+
* Commander collector for options that may be repeated or comma-separated,
|
|
5
|
+
* e.g. `--story "Button*" --story "Form/*"` or `--story "Button*,Form/*"`.
|
|
6
|
+
*/
|
|
7
|
+
export function collectPatterns(value, previous = []) {
|
|
8
|
+
return previous.concat(value
|
|
9
|
+
.split(',')
|
|
10
|
+
.map((pattern) => pattern.trim())
|
|
11
|
+
.filter(Boolean));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Discovers stories and narrows them by the command-line filters, reporting the
|
|
15
|
+
* result on the spinner. `unit` names what each story-browser pair produces.
|
|
16
|
+
*
|
|
17
|
+
* Returns null when --changed leaves nothing to test, which is a normal outcome
|
|
18
|
+
* for a change that touches no stories. An empty result from the patterns is
|
|
19
|
+
* a mistake instead, so that exits like an empty Storybook does.
|
|
20
|
+
*/
|
|
21
|
+
export async function selectStories(reporter, config, options, unit) {
|
|
22
|
+
reporter.startSpinner('Discovering stories...');
|
|
23
|
+
const discovered = await discoverStories(config);
|
|
24
|
+
if (discovered.length === 0) {
|
|
25
|
+
reporter.failSpinner('No stories found');
|
|
26
|
+
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
let stories = filterStories(discovered, options.story, options.exclude);
|
|
30
|
+
if (stories.length === 0) {
|
|
31
|
+
reporter.failSpinner('No stories match the --story/--exclude filters');
|
|
32
|
+
reporter.info(`Patterns are matched against story IDs, titles and names, e.g. "button--primary" or "Components/Button/*".`);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
let changedSuffix = '';
|
|
36
|
+
if (options.changed) {
|
|
37
|
+
reporter.updateSpinner('Finding changed files...');
|
|
38
|
+
const { ref, files } = await getChangedFiles(typeof options.changed === 'string' ? options.changed : undefined);
|
|
39
|
+
const result = filterChangedStories(stories, files);
|
|
40
|
+
if (result.stories.length === 0) {
|
|
41
|
+
reporter.succeedSpinner(`No stories affected by changes since ${ref} - nothing to do`);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
if (result.unknown > 0) {
|
|
45
|
+
reporter.warning(`${reporter.formatStoryCount(result.unknown)} have no file path in the Storybook index, so --changed included them`);
|
|
46
|
+
}
|
|
47
|
+
stories = result.stories;
|
|
48
|
+
changedSuffix = ` changed since ${ref}`;
|
|
49
|
+
}
|
|
50
|
+
const total = stories.length * config.browsers.length;
|
|
51
|
+
const counted = stories.length === discovered.length
|
|
52
|
+
? `Found ${reporter.formatStoryCount(stories.length)}`
|
|
53
|
+
: `Selected ${stories.length} of ${reporter.formatStoryCount(discovered.length)}${changedSuffix}`;
|
|
54
|
+
reporter.succeedSpinner(`${counted} (${total} ${unit} across ${config.browsers.length} browser(s))`);
|
|
55
|
+
return stories;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=select-stories.js.map
|
package/dist/config/loader.js
CHANGED
|
@@ -58,13 +58,25 @@ const config: StorySnapConfig = {
|
|
|
58
58
|
storybookUrl: 'http://localhost:6006',
|
|
59
59
|
// storybookStaticDir: './storybook-static',
|
|
60
60
|
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
// Where baselines live depends on the mode the CLI runs in:
|
|
62
|
+
//
|
|
63
|
+
// - API mode: used when an API URL and token are available, from
|
|
64
|
+
// "storysnap login", STORYSNAP_API_URL/STORYSNAP_API_TOKEN, or apiUrl and
|
|
65
|
+
// apiToken here. The server stores everything and the storage setting
|
|
66
|
+
// below is ignored. Runs show up in the dashboard.
|
|
67
|
+
// - Local mode: used otherwise, or with --local. Baselines are written to
|
|
68
|
+
// .storysnap/baselines unless you set storage yourself, for example:
|
|
69
|
+
//
|
|
70
|
+
// storage: {
|
|
71
|
+
// type: 's3',
|
|
72
|
+
// bucket: 'my-snapshots',
|
|
73
|
+
// region: 'us-east-1',
|
|
74
|
+
// prefix: 'storysnap/',
|
|
75
|
+
// // For S3-compatible storage such as MinIO:
|
|
76
|
+
// // endpoint: 'http://localhost:9000',
|
|
77
|
+
// // accessKeyId: '...',
|
|
78
|
+
// // secretAccessKey: '...',
|
|
79
|
+
// },
|
|
68
80
|
|
|
69
81
|
// Screenshot settings
|
|
70
82
|
viewport: { width: 1280, height: 720 },
|
|
@@ -97,12 +109,6 @@ export function getDefaultConfigJs() {
|
|
|
97
109
|
export function getDefaultConfigJson() {
|
|
98
110
|
return JSON.stringify({
|
|
99
111
|
storybookUrl: 'http://localhost:6006',
|
|
100
|
-
storage: {
|
|
101
|
-
type: 's3',
|
|
102
|
-
bucket: 'my-snapshots',
|
|
103
|
-
region: 'us-east-1',
|
|
104
|
-
prefix: 'storysnap/',
|
|
105
|
-
},
|
|
106
112
|
viewport: { width: 1280, height: 720 },
|
|
107
113
|
threshold: 0.1,
|
|
108
114
|
maxDiffPercent: 0.1,
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Story } from '@storysnap/shared';
|
|
2
|
+
/**
|
|
3
|
+
* Files that differ from the point where HEAD branched off `ref`: committed,
|
|
4
|
+
* staged, unstaged and untracked. Comparing against the merge base rather than
|
|
5
|
+
* `ref` itself keeps commits that landed on `ref` since then out of the list.
|
|
6
|
+
*
|
|
7
|
+
* Paths are relative to `cwd`, which matches Storybook's importPath as long as
|
|
8
|
+
* the CLI runs from the directory Storybook runs from.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getChangedFiles(ref?: string, cwd?: string): Promise<{
|
|
11
|
+
ref: string;
|
|
12
|
+
files: string[];
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* Keeps the stories whose story file, or a file next to it sharing its name,
|
|
16
|
+
* is among `changedFiles`. That catches edits to the component a story file
|
|
17
|
+
* covers, but not to anything it imports from further away.
|
|
18
|
+
*
|
|
19
|
+
* Stories the index gives no file path for are kept, since there is no way to
|
|
20
|
+
* tell whether they changed; `unknown` counts them.
|
|
21
|
+
*/
|
|
22
|
+
export declare function filterChangedStories(stories: Story[], changedFiles: string[]): {
|
|
23
|
+
stories: Story[];
|
|
24
|
+
unknown: number;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=changed.d.ts.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { posix } from 'node:path';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
const execFileAsync = promisify(execFile);
|
|
5
|
+
/** Used by `--changed` with no ref. CI checkouts often only have the remote one. */
|
|
6
|
+
const DEFAULT_REFS = ['main', 'origin/main'];
|
|
7
|
+
async function git(args, cwd) {
|
|
8
|
+
const { stdout } = await execFileAsync('git', args, { cwd, maxBuffer: 64 * 1024 * 1024 });
|
|
9
|
+
return stdout;
|
|
10
|
+
}
|
|
11
|
+
/** Parses `-z` output, which leaves unusual file names unquoted. */
|
|
12
|
+
function splitNul(output) {
|
|
13
|
+
return output.split('\0').filter(Boolean);
|
|
14
|
+
}
|
|
15
|
+
async function findMergeBase(refs, cwd) {
|
|
16
|
+
for (const ref of refs) {
|
|
17
|
+
try {
|
|
18
|
+
const base = (await git(['merge-base', ref, 'HEAD'], cwd)).trim();
|
|
19
|
+
return { ref, base };
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// Try the next candidate
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const tried = refs.map((ref) => `"${ref}"`).join(' or ');
|
|
26
|
+
throw new Error(`Could not compare against ${tried}. Make sure this is a git repository and the ref exists - ` +
|
|
27
|
+
'CI checkouts are often shallow, so fetch it first (e.g. "git fetch origin main") ' +
|
|
28
|
+
'or name one explicitly with --changed <ref>.');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Files that differ from the point where HEAD branched off `ref`: committed,
|
|
32
|
+
* staged, unstaged and untracked. Comparing against the merge base rather than
|
|
33
|
+
* `ref` itself keeps commits that landed on `ref` since then out of the list.
|
|
34
|
+
*
|
|
35
|
+
* Paths are relative to `cwd`, which matches Storybook's importPath as long as
|
|
36
|
+
* the CLI runs from the directory Storybook runs from.
|
|
37
|
+
*/
|
|
38
|
+
export async function getChangedFiles(ref, cwd = process.cwd()) {
|
|
39
|
+
const { ref: resolvedRef, base } = await findMergeBase(ref ? [ref] : DEFAULT_REFS, cwd);
|
|
40
|
+
const [tracked, untracked] = await Promise.all([
|
|
41
|
+
git(['diff', '--name-only', '--relative', '-z', base], cwd),
|
|
42
|
+
git(['ls-files', '--others', '--exclude-standard', '-z'], cwd),
|
|
43
|
+
]);
|
|
44
|
+
return { ref: resolvedRef, files: [...splitNul(tracked), ...splitNul(untracked)] };
|
|
45
|
+
}
|
|
46
|
+
function normalizePath(path) {
|
|
47
|
+
return path.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Groups a file with its siblings by directory and the name before the first
|
|
51
|
+
* dot, so Button.stories.tsx, Button.tsx and Button.module.css share a key.
|
|
52
|
+
*/
|
|
53
|
+
function siblingKey(path) {
|
|
54
|
+
const normalized = normalizePath(path);
|
|
55
|
+
const name = posix.basename(normalized).split('.')[0];
|
|
56
|
+
return `${posix.dirname(normalized)}/${name}`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Keeps the stories whose story file, or a file next to it sharing its name,
|
|
60
|
+
* is among `changedFiles`. That catches edits to the component a story file
|
|
61
|
+
* covers, but not to anything it imports from further away.
|
|
62
|
+
*
|
|
63
|
+
* Stories the index gives no file path for are kept, since there is no way to
|
|
64
|
+
* tell whether they changed; `unknown` counts them.
|
|
65
|
+
*/
|
|
66
|
+
export function filterChangedStories(stories, changedFiles) {
|
|
67
|
+
const changed = new Set(changedFiles.map(siblingKey));
|
|
68
|
+
let unknown = 0;
|
|
69
|
+
const selected = stories.filter((story) => {
|
|
70
|
+
if (!story.componentPath) {
|
|
71
|
+
unknown++;
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return changed.has(siblingKey(story.componentPath));
|
|
75
|
+
});
|
|
76
|
+
return { stories: selected, unknown };
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=changed.js.map
|
package/dist/core/storybook.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Story, StorySnapConfig, Browser } from '@storysnap/shared';
|
|
2
2
|
export declare function discoverStories(config: StorySnapConfig): Promise<Story[]>;
|
|
3
|
+
export declare function filterStories(stories: Story[], include?: string[], exclude?: string[]): Story[];
|
|
3
4
|
export declare function getStoryUrl(baseUrl: string, storyId: string): string;
|
|
4
5
|
export declare function getStoryKey(storyId: string, browser?: Browser): string;
|
|
5
6
|
//# sourceMappingURL=storybook.d.ts.map
|
package/dist/core/storybook.js
CHANGED
|
@@ -85,7 +85,7 @@ function parseIndex(index) {
|
|
|
85
85
|
}
|
|
86
86
|
return stories;
|
|
87
87
|
}
|
|
88
|
-
function filterStories(stories, include, exclude) {
|
|
88
|
+
export function filterStories(stories, include, exclude) {
|
|
89
89
|
let filtered = stories;
|
|
90
90
|
if (include && include.length > 0) {
|
|
91
91
|
filtered = filtered.filter((story) => include.some((pattern) => matchPattern(story, pattern)));
|
package/package.json
CHANGED