@storysnap/cli 1.3.0 → 2.1.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 +3 -3
- package/dist/api/client.d.ts +19 -3
- package/dist/api/client.js +2 -1
- package/dist/cli/commands/test.d.ts +1 -0
- package/dist/cli/commands/test.js +18 -5
- package/dist/cli/commands/update.d.ts +1 -0
- package/dist/cli/commands/update.js +8 -1
- package/dist/cli/index.js +6 -2
- package/dist/cli/review.d.ts +9 -0
- package/dist/cli/review.js +10 -0
- package/dist/config/schema.d.ts +19 -167
- package/dist/core/changed.js +10 -23
- package/dist/core/git-context.d.ts +25 -0
- package/dist/core/git-context.js +75 -0
- package/dist/core/git.d.ts +11 -0
- package/dist/core/git.js +25 -0
- package/dist/core/hash.d.ts +3 -0
- package/dist/core/hash.js +6 -0
- package/package.json +16 -17
package/README.md
CHANGED
|
@@ -183,7 +183,7 @@ jobs:
|
|
|
183
183
|
- uses: actions/checkout@v4
|
|
184
184
|
- uses: actions/setup-node@v4
|
|
185
185
|
with:
|
|
186
|
-
node-version: '
|
|
186
|
+
node-version: '22'
|
|
187
187
|
|
|
188
188
|
- name: Install dependencies
|
|
189
189
|
run: npm ci
|
|
@@ -204,7 +204,7 @@ jobs:
|
|
|
204
204
|
|
|
205
205
|
```yaml
|
|
206
206
|
visual-tests:
|
|
207
|
-
image: mcr.microsoft.com/playwright:v1.
|
|
207
|
+
image: mcr.microsoft.com/playwright:v1.63.0-noble
|
|
208
208
|
script:
|
|
209
209
|
- npm ci
|
|
210
210
|
- npm run build-storybook
|
|
@@ -285,7 +285,7 @@ npx playwright install-deps
|
|
|
285
285
|
|
|
286
286
|
## Requirements
|
|
287
287
|
|
|
288
|
-
- Node.js
|
|
288
|
+
- Node.js 22.12 or higher
|
|
289
289
|
- A running Storybook instance (v6.0+)
|
|
290
290
|
|
|
291
291
|
## License
|
package/dist/api/client.d.ts
CHANGED
|
@@ -6,6 +6,10 @@ export interface TestRunInput {
|
|
|
6
6
|
storybookUrl?: string;
|
|
7
7
|
branch?: string;
|
|
8
8
|
commit?: string;
|
|
9
|
+
/** The branch `branch` will merge into; its baselines fill in for missing ones. */
|
|
10
|
+
baseBranch?: string;
|
|
11
|
+
/** The commit `branch` forked from `baseBranch` at. */
|
|
12
|
+
mergeBase?: string;
|
|
9
13
|
/** pixelmatch per-pixel colour sensitivity (0-1). */
|
|
10
14
|
threshold?: number;
|
|
11
15
|
/** Percentage of the image allowed to differ before a story fails (0-100). */
|
|
@@ -18,6 +22,8 @@ export interface CreateResultInput {
|
|
|
18
22
|
browser: string;
|
|
19
23
|
viewportWidth: number;
|
|
20
24
|
viewportHeight: number;
|
|
25
|
+
/** SHA-256 of the PNG, so the server can recognise a screenshot approved on another branch. */
|
|
26
|
+
contentHash?: string;
|
|
21
27
|
}
|
|
22
28
|
export interface ResultUploadTarget {
|
|
23
29
|
resultId: string;
|
|
@@ -31,6 +37,12 @@ export interface TestRun {
|
|
|
31
37
|
status: 'pending' | 'running' | 'passed' | 'failed' | 'error';
|
|
32
38
|
branch?: string;
|
|
33
39
|
commit?: string;
|
|
40
|
+
baseBranch?: string;
|
|
41
|
+
mergeBase?: string;
|
|
42
|
+
/** The branch review this run is a push of; absent on the default branch. */
|
|
43
|
+
reviewId?: string | null;
|
|
44
|
+
/** Its page in the dashboard, when the server knows where that is. Only on creation. */
|
|
45
|
+
reviewUrl?: string | null;
|
|
34
46
|
totalStories: number;
|
|
35
47
|
passedStories: number;
|
|
36
48
|
failedStories: number;
|
|
@@ -70,6 +82,7 @@ export interface BaselineUploadUrl {
|
|
|
70
82
|
export interface Baseline {
|
|
71
83
|
id: string;
|
|
72
84
|
projectId: string;
|
|
85
|
+
branch?: string;
|
|
73
86
|
storyId: string;
|
|
74
87
|
browser: string;
|
|
75
88
|
viewportWidth: number;
|
|
@@ -82,8 +95,8 @@ export interface Baseline {
|
|
|
82
95
|
}
|
|
83
96
|
export declare class ApiError extends Error {
|
|
84
97
|
statusCode: number;
|
|
85
|
-
response?: unknown
|
|
86
|
-
constructor(message: string, statusCode: number, response?: unknown
|
|
98
|
+
response?: unknown;
|
|
99
|
+
constructor(message: string, statusCode: number, response?: unknown);
|
|
87
100
|
}
|
|
88
101
|
export declare class ApiClient {
|
|
89
102
|
private apiUrl;
|
|
@@ -109,7 +122,7 @@ export declare class ApiClient {
|
|
|
109
122
|
uploadImage(uploadUrl: string, imageBuffer: Buffer): Promise<void>;
|
|
110
123
|
getTestRun(id: string): Promise<TestRunWithResults>;
|
|
111
124
|
listTestRuns(limit?: number, offset?: number): Promise<TestRun[]>;
|
|
112
|
-
getBaselineUploadUrl(storyId: string, browser: string, viewportWidth: number, viewportHeight: number): Promise<BaselineUploadUrl>;
|
|
125
|
+
getBaselineUploadUrl(storyId: string, browser: string, viewportWidth: number, viewportHeight: number, branch?: string): Promise<BaselineUploadUrl>;
|
|
113
126
|
uploadBaseline(uploadUrl: string, imageBuffer: Buffer): Promise<void>;
|
|
114
127
|
/**
|
|
115
128
|
* Points the baseline record at an already-uploaded image. Without this the
|
|
@@ -121,6 +134,9 @@ export declare class ApiClient {
|
|
|
121
134
|
viewportWidth: number;
|
|
122
135
|
viewportHeight: number;
|
|
123
136
|
storageKey: string;
|
|
137
|
+
/** Omitted, the server uses the project's default branch. */
|
|
138
|
+
branch?: string;
|
|
139
|
+
contentHash?: string;
|
|
124
140
|
}): Promise<Baseline>;
|
|
125
141
|
listBaselines(limit?: number, offset?: number, storyId?: string, browser?: string): Promise<Baseline[]>;
|
|
126
142
|
approveBaseline(baselineId: string, testResultId?: string): Promise<Baseline>;
|
package/dist/api/client.js
CHANGED
|
@@ -99,12 +99,13 @@ export class ApiClient {
|
|
|
99
99
|
const response = await this.request('GET', `/api/v1/test-runs?limit=${limit}&offset=${offset}`);
|
|
100
100
|
return response.data;
|
|
101
101
|
}
|
|
102
|
-
async getBaselineUploadUrl(storyId, browser, viewportWidth, viewportHeight) {
|
|
102
|
+
async getBaselineUploadUrl(storyId, browser, viewportWidth, viewportHeight, branch) {
|
|
103
103
|
const response = await this.request('POST', '/api/v1/baselines/upload-url', {
|
|
104
104
|
storyId,
|
|
105
105
|
browser,
|
|
106
106
|
viewportWidth,
|
|
107
107
|
viewportHeight,
|
|
108
|
+
branch,
|
|
108
109
|
contentType: 'image/png',
|
|
109
110
|
});
|
|
110
111
|
return response.data;
|
|
@@ -2,10 +2,13 @@ import { loadConfig } from '../../config/loader.js';
|
|
|
2
2
|
import { loadCredentials } from '../../config/credentials.js';
|
|
3
3
|
import { captureScreenshots, getScreenshotKey, parseScreenshotKey } from '../../core/screenshot.js';
|
|
4
4
|
import { mapWithConcurrency } from '../../core/concurrency.js';
|
|
5
|
+
import { detectGitContext, describeGitContext } from '../../core/git-context.js';
|
|
6
|
+
import { hashScreenshot } from '../../core/hash.js';
|
|
5
7
|
import { compareAllScreenshots, DEFAULT_MAX_DIFF_PERCENT } from '../../core/compare.js';
|
|
6
8
|
import { createStorage } from '../../storage/index.js';
|
|
7
9
|
import { createReporter } from '../reporter.js';
|
|
8
10
|
import { selectStories } from '../select-stories.js';
|
|
11
|
+
import { describeReview } from '../review.js';
|
|
9
12
|
import { ApiClient } from '../../api/client.js';
|
|
10
13
|
function parseBrowsers(browserArg) {
|
|
11
14
|
if (!browserArg) {
|
|
@@ -79,14 +82,17 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
79
82
|
reporter.progress(completed, total, `${story.title}/${story.name} (${browser})`);
|
|
80
83
|
});
|
|
81
84
|
reporter.succeedSpinner('Screenshots captured');
|
|
85
|
+
const gitContext = await detectGitContext({
|
|
86
|
+
overrides: { branch: options.branch, commit: options.commit, baseBranch: options.baseBranch },
|
|
87
|
+
});
|
|
88
|
+
reporter.info(describeGitContext(gitContext));
|
|
82
89
|
reporter.startSpinner('Creating test run...');
|
|
83
90
|
// Only sent when the config actually declares them. Left undefined the server
|
|
84
91
|
// applies the project's own defaults, which is what makes `PATCH /projects/me`
|
|
85
92
|
// worth having; a config that does declare them overrides for this run.
|
|
86
93
|
const testRun = await client.createTestRun({
|
|
87
94
|
storybookUrl: config.storybookUrl,
|
|
88
|
-
|
|
89
|
-
commit: options.commit,
|
|
95
|
+
...gitContext,
|
|
90
96
|
threshold: config.threshold,
|
|
91
97
|
maxDiffPercent: config.maxDiffPercent,
|
|
92
98
|
});
|
|
@@ -115,7 +121,7 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
115
121
|
});
|
|
116
122
|
reporter.succeedSpinner('Tests complete');
|
|
117
123
|
// Report results
|
|
118
|
-
reportApiResults(reporter, completedTestRun, options, totalTests - uploaded);
|
|
124
|
+
reportApiResults(reporter, completedTestRun, options, totalTests - uploaded, describeReview(testRun));
|
|
119
125
|
}
|
|
120
126
|
/**
|
|
121
127
|
* Registers each captured screenshot with the API and PUTs the PNG to the
|
|
@@ -142,6 +148,7 @@ async function uploadScreenshots(client, testRunId, config, stories, screenshots
|
|
|
142
148
|
browser,
|
|
143
149
|
viewportWidth: config.viewport.width,
|
|
144
150
|
viewportHeight: config.viewport.height,
|
|
151
|
+
contentHash: hashScreenshot(screenshot.buffer),
|
|
145
152
|
});
|
|
146
153
|
await client.uploadImage(target.uploadUrl, screenshot.buffer);
|
|
147
154
|
uploaded++;
|
|
@@ -233,7 +240,7 @@ function reportLocalResult(story, comparison) {
|
|
|
233
240
|
break;
|
|
234
241
|
}
|
|
235
242
|
}
|
|
236
|
-
function reportApiResults(reporter, testRun, options, missing) {
|
|
243
|
+
function reportApiResults(reporter, testRun, options, missing, review) {
|
|
237
244
|
reporter.header('Results');
|
|
238
245
|
for (const result of testRun.results) {
|
|
239
246
|
const storyName = result.storyName || result.storyId;
|
|
@@ -289,8 +296,14 @@ function reportApiResults(reporter, testRun, options, missing) {
|
|
|
289
296
|
}
|
|
290
297
|
console.log(` ${parts.join(', ')} of ${testRun.totalStories} stories`);
|
|
291
298
|
reporter.newLine();
|
|
299
|
+
if (review) {
|
|
300
|
+
console.log(`\x1b[1mReview:\x1b[0m ${review}`);
|
|
301
|
+
reporter.newLine();
|
|
302
|
+
}
|
|
292
303
|
if (testRun.status === 'failed') {
|
|
293
|
-
console.log('\x1b[31mVisual regression tests failed.\x1b[0m',
|
|
304
|
+
console.log('\x1b[31mVisual regression tests failed.\x1b[0m', review
|
|
305
|
+
? '\x1b[2mApprove the changes in the review above, or fix them and push again.\x1b[0m'
|
|
306
|
+
: '\x1b[2mReview changes in the dashboard or run "storysnap update" to update baselines.\x1b[0m');
|
|
294
307
|
}
|
|
295
308
|
else if (missing > 0) {
|
|
296
309
|
console.log(`\x1b[31m${missing} screenshot(s) were not captured or uploaded.\x1b[0m`, '\x1b[2mSee the warnings above.\x1b[0m');
|
|
@@ -3,6 +3,8 @@ import { loadCredentials } from '../../config/credentials.js';
|
|
|
3
3
|
import { getStoryKey } from '../../core/storybook.js';
|
|
4
4
|
import { captureScreenshots, getScreenshotKey } from '../../core/screenshot.js';
|
|
5
5
|
import { mapWithConcurrency } from '../../core/concurrency.js';
|
|
6
|
+
import { detectGitContext } from '../../core/git-context.js';
|
|
7
|
+
import { hashScreenshot } from '../../core/hash.js';
|
|
6
8
|
import { createStorage } from '../../storage/index.js';
|
|
7
9
|
import { createReporter } from '../reporter.js';
|
|
8
10
|
import { selectStories } from '../select-stories.js';
|
|
@@ -71,6 +73,9 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
71
73
|
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
72
74
|
reporter.newLine();
|
|
73
75
|
const client = new ApiClient({ apiUrl, apiToken });
|
|
76
|
+
// Baselines land on this branch only; other branches keep theirs.
|
|
77
|
+
const { branch } = await detectGitContext({ overrides: { branch: options.branch } });
|
|
78
|
+
reporter.info(branch ? `Branch: ${branch}` : "Branch: not detected - updating the project's default branch (pass --branch to name one)");
|
|
74
79
|
// Discover stories
|
|
75
80
|
const stories = await selectStories(reporter, config, options, 'baselines');
|
|
76
81
|
if (!stories) {
|
|
@@ -99,7 +104,7 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
99
104
|
if (!options.dryRun) {
|
|
100
105
|
try {
|
|
101
106
|
// Get presigned upload URL from API
|
|
102
|
-
const uploadInfo = await client.getBaselineUploadUrl(story.id, browser, config.viewport.width, config.viewport.height);
|
|
107
|
+
const uploadInfo = await client.getBaselineUploadUrl(story.id, browser, config.viewport.width, config.viewport.height, branch);
|
|
103
108
|
// Upload directly to S3 using presigned URL
|
|
104
109
|
await client.uploadBaseline(uploadInfo.url, screenshot.buffer);
|
|
105
110
|
// Record the baseline. The upload alone leaves an orphaned object -
|
|
@@ -110,6 +115,8 @@ async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
|
110
115
|
viewportWidth: config.viewport.width,
|
|
111
116
|
viewportHeight: config.viewport.height,
|
|
112
117
|
storageKey: uploadInfo.storageKey,
|
|
118
|
+
branch,
|
|
119
|
+
contentHash: hashScreenshot(screenshot.buffer),
|
|
113
120
|
});
|
|
114
121
|
uploaded++;
|
|
115
122
|
}
|
package/dist/cli/index.js
CHANGED
|
@@ -61,8 +61,9 @@ program
|
|
|
61
61
|
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
62
62
|
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
63
63
|
.option('--concurrency <n>', 'Stories to capture at once (default: 4)', parseConcurrency)
|
|
64
|
-
.option('--branch <branch>', 'Branch
|
|
65
|
-
.option('--commit <sha>', 'Commit SHA
|
|
64
|
+
.option('--branch <branch>', 'Branch of the test run (API mode; default: detected from CI or git)')
|
|
65
|
+
.option('--commit <sha>', 'Commit SHA of the test run (API mode; default: detected from CI or git)')
|
|
66
|
+
.option('--base-branch <branch>', 'Branch to compare against where this one has no baseline (API mode; default: the PR/MR target, else the project default branch)')
|
|
66
67
|
.option('--ci', 'CI mode - fail on any visual diff or new story')
|
|
67
68
|
.option('-v, --verbose', 'Verbose output')
|
|
68
69
|
.option('--local', 'Force local-only mode without using the API')
|
|
@@ -77,6 +78,7 @@ program
|
|
|
77
78
|
concurrency: options.concurrency,
|
|
78
79
|
branch: options.branch,
|
|
79
80
|
commit: options.commit,
|
|
81
|
+
baseBranch: options.baseBranch,
|
|
80
82
|
ci: options.ci,
|
|
81
83
|
verbose: options.verbose,
|
|
82
84
|
local: options.local,
|
|
@@ -92,6 +94,7 @@ program
|
|
|
92
94
|
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
93
95
|
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
94
96
|
.option('--concurrency <n>', 'Stories to capture at once (default: 4)', parseConcurrency)
|
|
97
|
+
.option('--branch <branch>', 'Branch whose baselines to update (API mode; default: detected from CI or git)')
|
|
95
98
|
.option('-v, --verbose', 'Verbose output')
|
|
96
99
|
.option('--dry-run', 'Show what would be updated without making changes')
|
|
97
100
|
.option('--local', 'Force local-only mode without using the API')
|
|
@@ -104,6 +107,7 @@ program
|
|
|
104
107
|
browser: options.browser,
|
|
105
108
|
storybookUrl: options.storybookUrl,
|
|
106
109
|
concurrency: options.concurrency,
|
|
110
|
+
branch: options.branch,
|
|
107
111
|
verbose: options.verbose,
|
|
108
112
|
dryRun: options.dryRun,
|
|
109
113
|
local: options.local,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where to review this push, for a run on a branch with a review: its
|
|
3
|
+
* dashboard link, or its id when the server has no DASHBOARD_URL to build one.
|
|
4
|
+
*/
|
|
5
|
+
export declare function describeReview(run: {
|
|
6
|
+
reviewId?: string | null;
|
|
7
|
+
reviewUrl?: string | null;
|
|
8
|
+
}): string | undefined;
|
|
9
|
+
//# sourceMappingURL=review.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where to review this push, for a run on a branch with a review: its
|
|
3
|
+
* dashboard link, or its id when the server has no DASHBOARD_URL to build one.
|
|
4
|
+
*/
|
|
5
|
+
export function describeReview(run) {
|
|
6
|
+
if (!run.reviewId)
|
|
7
|
+
return undefined;
|
|
8
|
+
return run.reviewUrl ?? `review ${run.reviewId} (set DASHBOARD_URL on the server for a link)`;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=review.js.map
|
package/dist/config/schema.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
declare const browserSchema: z.ZodEnum<
|
|
2
|
+
declare const browserSchema: z.ZodEnum<{
|
|
3
|
+
chromium: "chromium";
|
|
4
|
+
firefox: "firefox";
|
|
5
|
+
webkit: "webkit";
|
|
6
|
+
}>;
|
|
3
7
|
export type Browser = z.infer<typeof browserSchema>;
|
|
4
|
-
export declare const configSchema: z.
|
|
8
|
+
export declare const configSchema: z.ZodObject<{
|
|
5
9
|
storybookUrl: z.ZodOptional<z.ZodString>;
|
|
6
10
|
storybookStaticDir: z.ZodOptional<z.ZodString>;
|
|
7
|
-
storage: z.ZodDefault<z.ZodDiscriminatedUnion<
|
|
11
|
+
storage: z.ZodDefault<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
8
12
|
type: z.ZodLiteral<"s3">;
|
|
9
13
|
bucket: z.ZodString;
|
|
10
14
|
region: z.ZodString;
|
|
@@ -12,184 +16,32 @@ export declare const configSchema: z.ZodEffects<z.ZodObject<{
|
|
|
12
16
|
endpoint: z.ZodOptional<z.ZodString>;
|
|
13
17
|
accessKeyId: z.ZodOptional<z.ZodString>;
|
|
14
18
|
secretAccessKey: z.ZodOptional<z.ZodString>;
|
|
15
|
-
},
|
|
16
|
-
type: "s3";
|
|
17
|
-
bucket: string;
|
|
18
|
-
region: string;
|
|
19
|
-
prefix?: string | undefined;
|
|
20
|
-
endpoint?: string | undefined;
|
|
21
|
-
accessKeyId?: string | undefined;
|
|
22
|
-
secretAccessKey?: string | undefined;
|
|
23
|
-
}, {
|
|
24
|
-
type: "s3";
|
|
25
|
-
bucket: string;
|
|
26
|
-
region: string;
|
|
27
|
-
prefix?: string | undefined;
|
|
28
|
-
endpoint?: string | undefined;
|
|
29
|
-
accessKeyId?: string | undefined;
|
|
30
|
-
secretAccessKey?: string | undefined;
|
|
31
|
-
}>, z.ZodObject<{
|
|
19
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
32
20
|
type: z.ZodLiteral<"local">;
|
|
33
21
|
directory: z.ZodString;
|
|
34
|
-
},
|
|
35
|
-
type: "local";
|
|
36
|
-
directory: string;
|
|
37
|
-
}, {
|
|
38
|
-
type: "local";
|
|
39
|
-
directory: string;
|
|
40
|
-
}>]>>;
|
|
22
|
+
}, z.core.$strip>], "type">>;
|
|
41
23
|
viewport: z.ZodDefault<z.ZodObject<{
|
|
42
24
|
width: z.ZodDefault<z.ZodNumber>;
|
|
43
25
|
height: z.ZodDefault<z.ZodNumber>;
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}>>;
|
|
51
|
-
browsers: z.ZodDefault<z.ZodArray<z.ZodEnum<["chromium", "firefox", "webkit"]>, "many">>;
|
|
26
|
+
}, z.core.$strip>>;
|
|
27
|
+
browsers: z.ZodDefault<z.ZodArray<z.ZodEnum<{
|
|
28
|
+
chromium: "chromium";
|
|
29
|
+
firefox: "firefox";
|
|
30
|
+
webkit: "webkit";
|
|
31
|
+
}>>>;
|
|
52
32
|
threshold: z.ZodOptional<z.ZodNumber>;
|
|
53
33
|
maxDiffPercent: z.ZodOptional<z.ZodNumber>;
|
|
54
34
|
localSnapshotsDir: z.ZodOptional<z.ZodString>;
|
|
55
|
-
include: z.ZodOptional<z.ZodArray<z.ZodString
|
|
56
|
-
exclude: z.ZodOptional<z.ZodArray<z.ZodString
|
|
35
|
+
include: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
36
|
+
exclude: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
57
37
|
waitForSelector: z.ZodDefault<z.ZodString>;
|
|
58
38
|
waitTimeout: z.ZodDefault<z.ZodNumber>;
|
|
59
39
|
concurrency: z.ZodDefault<z.ZodNumber>;
|
|
60
|
-
ignoreSelectors: z.ZodOptional<z.ZodArray<z.ZodString
|
|
40
|
+
ignoreSelectors: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
61
41
|
ignoreColor: z.ZodOptional<z.ZodString>;
|
|
62
42
|
apiUrl: z.ZodOptional<z.ZodString>;
|
|
63
43
|
apiToken: z.ZodOptional<z.ZodString>;
|
|
64
|
-
},
|
|
65
|
-
storage: {
|
|
66
|
-
type: "s3";
|
|
67
|
-
bucket: string;
|
|
68
|
-
region: string;
|
|
69
|
-
prefix?: string | undefined;
|
|
70
|
-
endpoint?: string | undefined;
|
|
71
|
-
accessKeyId?: string | undefined;
|
|
72
|
-
secretAccessKey?: string | undefined;
|
|
73
|
-
} | {
|
|
74
|
-
type: "local";
|
|
75
|
-
directory: string;
|
|
76
|
-
};
|
|
77
|
-
viewport: {
|
|
78
|
-
width: number;
|
|
79
|
-
height: number;
|
|
80
|
-
};
|
|
81
|
-
browsers: ("chromium" | "firefox" | "webkit")[];
|
|
82
|
-
waitForSelector: string;
|
|
83
|
-
waitTimeout: number;
|
|
84
|
-
concurrency: number;
|
|
85
|
-
storybookUrl?: string | undefined;
|
|
86
|
-
storybookStaticDir?: string | undefined;
|
|
87
|
-
threshold?: number | undefined;
|
|
88
|
-
maxDiffPercent?: number | undefined;
|
|
89
|
-
localSnapshotsDir?: string | undefined;
|
|
90
|
-
include?: string[] | undefined;
|
|
91
|
-
exclude?: string[] | undefined;
|
|
92
|
-
ignoreSelectors?: string[] | undefined;
|
|
93
|
-
ignoreColor?: string | undefined;
|
|
94
|
-
apiUrl?: string | undefined;
|
|
95
|
-
apiToken?: string | undefined;
|
|
96
|
-
}, {
|
|
97
|
-
storybookUrl?: string | undefined;
|
|
98
|
-
storybookStaticDir?: string | undefined;
|
|
99
|
-
storage?: {
|
|
100
|
-
type: "s3";
|
|
101
|
-
bucket: string;
|
|
102
|
-
region: string;
|
|
103
|
-
prefix?: string | undefined;
|
|
104
|
-
endpoint?: string | undefined;
|
|
105
|
-
accessKeyId?: string | undefined;
|
|
106
|
-
secretAccessKey?: string | undefined;
|
|
107
|
-
} | {
|
|
108
|
-
type: "local";
|
|
109
|
-
directory: string;
|
|
110
|
-
} | undefined;
|
|
111
|
-
viewport?: {
|
|
112
|
-
width?: number | undefined;
|
|
113
|
-
height?: number | undefined;
|
|
114
|
-
} | undefined;
|
|
115
|
-
browsers?: ("chromium" | "firefox" | "webkit")[] | undefined;
|
|
116
|
-
threshold?: number | undefined;
|
|
117
|
-
maxDiffPercent?: number | undefined;
|
|
118
|
-
localSnapshotsDir?: string | undefined;
|
|
119
|
-
include?: string[] | undefined;
|
|
120
|
-
exclude?: string[] | undefined;
|
|
121
|
-
waitForSelector?: string | undefined;
|
|
122
|
-
waitTimeout?: number | undefined;
|
|
123
|
-
concurrency?: number | undefined;
|
|
124
|
-
ignoreSelectors?: string[] | undefined;
|
|
125
|
-
ignoreColor?: string | undefined;
|
|
126
|
-
apiUrl?: string | undefined;
|
|
127
|
-
apiToken?: string | undefined;
|
|
128
|
-
}>, {
|
|
129
|
-
storage: {
|
|
130
|
-
type: "s3";
|
|
131
|
-
bucket: string;
|
|
132
|
-
region: string;
|
|
133
|
-
prefix?: string | undefined;
|
|
134
|
-
endpoint?: string | undefined;
|
|
135
|
-
accessKeyId?: string | undefined;
|
|
136
|
-
secretAccessKey?: string | undefined;
|
|
137
|
-
} | {
|
|
138
|
-
type: "local";
|
|
139
|
-
directory: string;
|
|
140
|
-
};
|
|
141
|
-
viewport: {
|
|
142
|
-
width: number;
|
|
143
|
-
height: number;
|
|
144
|
-
};
|
|
145
|
-
browsers: ("chromium" | "firefox" | "webkit")[];
|
|
146
|
-
waitForSelector: string;
|
|
147
|
-
waitTimeout: number;
|
|
148
|
-
concurrency: number;
|
|
149
|
-
storybookUrl?: string | undefined;
|
|
150
|
-
storybookStaticDir?: string | undefined;
|
|
151
|
-
threshold?: number | undefined;
|
|
152
|
-
maxDiffPercent?: number | undefined;
|
|
153
|
-
localSnapshotsDir?: string | undefined;
|
|
154
|
-
include?: string[] | undefined;
|
|
155
|
-
exclude?: string[] | undefined;
|
|
156
|
-
ignoreSelectors?: string[] | undefined;
|
|
157
|
-
ignoreColor?: string | undefined;
|
|
158
|
-
apiUrl?: string | undefined;
|
|
159
|
-
apiToken?: string | undefined;
|
|
160
|
-
}, {
|
|
161
|
-
storybookUrl?: string | undefined;
|
|
162
|
-
storybookStaticDir?: string | undefined;
|
|
163
|
-
storage?: {
|
|
164
|
-
type: "s3";
|
|
165
|
-
bucket: string;
|
|
166
|
-
region: string;
|
|
167
|
-
prefix?: string | undefined;
|
|
168
|
-
endpoint?: string | undefined;
|
|
169
|
-
accessKeyId?: string | undefined;
|
|
170
|
-
secretAccessKey?: string | undefined;
|
|
171
|
-
} | {
|
|
172
|
-
type: "local";
|
|
173
|
-
directory: string;
|
|
174
|
-
} | undefined;
|
|
175
|
-
viewport?: {
|
|
176
|
-
width?: number | undefined;
|
|
177
|
-
height?: number | undefined;
|
|
178
|
-
} | undefined;
|
|
179
|
-
browsers?: ("chromium" | "firefox" | "webkit")[] | undefined;
|
|
180
|
-
threshold?: number | undefined;
|
|
181
|
-
maxDiffPercent?: number | undefined;
|
|
182
|
-
localSnapshotsDir?: string | undefined;
|
|
183
|
-
include?: string[] | undefined;
|
|
184
|
-
exclude?: string[] | undefined;
|
|
185
|
-
waitForSelector?: string | undefined;
|
|
186
|
-
waitTimeout?: number | undefined;
|
|
187
|
-
concurrency?: number | undefined;
|
|
188
|
-
ignoreSelectors?: string[] | undefined;
|
|
189
|
-
ignoreColor?: string | undefined;
|
|
190
|
-
apiUrl?: string | undefined;
|
|
191
|
-
apiToken?: string | undefined;
|
|
192
|
-
}>;
|
|
44
|
+
}, z.core.$strip>;
|
|
193
45
|
export type ConfigInput = z.input<typeof configSchema>;
|
|
194
46
|
export type Config = z.output<typeof configSchema>;
|
|
195
47
|
export declare function validateConfig(input: unknown): Config;
|
package/dist/core/changed.js
CHANGED
|
@@ -1,32 +1,11 @@
|
|
|
1
|
-
import { execFile } from 'node:child_process';
|
|
2
1
|
import { posix } from 'node:path';
|
|
3
|
-
import {
|
|
4
|
-
const execFileAsync = promisify(execFile);
|
|
2
|
+
import { git, findMergeBase } from './git.js';
|
|
5
3
|
/** Used by `--changed` with no ref. CI checkouts often only have the remote one. */
|
|
6
4
|
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
5
|
/** Parses `-z` output, which leaves unusual file names unquoted. */
|
|
12
6
|
function splitNul(output) {
|
|
13
7
|
return output.split('\0').filter(Boolean);
|
|
14
8
|
}
|
|
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
9
|
/**
|
|
31
10
|
* Files that differ from the point where HEAD branched off `ref`: committed,
|
|
32
11
|
* staged, unstaged and untracked. Comparing against the merge base rather than
|
|
@@ -36,7 +15,15 @@ async function findMergeBase(refs, cwd) {
|
|
|
36
15
|
* the CLI runs from the directory Storybook runs from.
|
|
37
16
|
*/
|
|
38
17
|
export async function getChangedFiles(ref, cwd = process.cwd()) {
|
|
39
|
-
const
|
|
18
|
+
const refs = ref ? [ref] : DEFAULT_REFS;
|
|
19
|
+
const found = await findMergeBase(refs, cwd);
|
|
20
|
+
if (!found) {
|
|
21
|
+
const tried = refs.map((r) => `"${r}"`).join(' or ');
|
|
22
|
+
throw new Error(`Could not compare against ${tried}. Make sure this is a git repository and the ref exists - ` +
|
|
23
|
+
'CI checkouts are often shallow, so fetch it first (e.g. "git fetch origin main") ' +
|
|
24
|
+
'or name one explicitly with --changed <ref>.');
|
|
25
|
+
}
|
|
26
|
+
const { ref: resolvedRef, base } = found;
|
|
40
27
|
const [tracked, untracked] = await Promise.all([
|
|
41
28
|
git(['diff', '--name-only', '--relative', '-z', base], cwd),
|
|
42
29
|
git(['ls-files', '--others', '--exclude-standard', '-z'], cwd),
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Where a run comes from. Anything left undefined the server fills in. */
|
|
2
|
+
export interface GitContext {
|
|
3
|
+
branch?: string;
|
|
4
|
+
commit?: string;
|
|
5
|
+
/** The branch this one will merge into; the server falls back to its baselines. */
|
|
6
|
+
baseBranch?: string;
|
|
7
|
+
/** The commit `branch` forked from `baseBranch` at. */
|
|
8
|
+
mergeBase?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface GitContextOptions {
|
|
11
|
+
/** Explicit flags, which win over anything detected. */
|
|
12
|
+
overrides?: Pick<GitContext, 'branch' | 'commit' | 'baseBranch'>;
|
|
13
|
+
env?: NodeJS.ProcessEnv;
|
|
14
|
+
cwd?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Works out the branch, commit, base branch and merge base of this run, best
|
|
18
|
+
* effort: flags, then CI variables, then git. Nothing here fails a run - with
|
|
19
|
+
* no git at all every field may come back undefined, and the server treats
|
|
20
|
+
* the run as being on the project's default branch.
|
|
21
|
+
*/
|
|
22
|
+
export declare function detectGitContext(options?: GitContextOptions): Promise<GitContext>;
|
|
23
|
+
/** "Branch feature/x (compared against main @ abc1234)", or what was not detected. */
|
|
24
|
+
export declare function describeGitContext(context: GitContext): string;
|
|
25
|
+
//# sourceMappingURL=git-context.d.ts.map
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { git, findMergeBase } from './git.js';
|
|
3
|
+
/** CI providers set some of these to an empty string rather than leaving them out. */
|
|
4
|
+
function first(...values) {
|
|
5
|
+
return values.find((value) => value !== undefined && value.trim() !== '')?.trim();
|
|
6
|
+
}
|
|
7
|
+
async function tryGit(args, cwd) {
|
|
8
|
+
try {
|
|
9
|
+
return first(await git(args, cwd));
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The head commit of the pull request that triggered a GitHub Actions run.
|
|
17
|
+
* GITHUB_SHA is a merge commit GitHub made for the run, which nothing outside
|
|
18
|
+
* the run knows about - a status posted on it never shows up on the PR.
|
|
19
|
+
*/
|
|
20
|
+
async function githubPullRequestHead(env) {
|
|
21
|
+
if (!env.GITHUB_EVENT_PATH) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const event = JSON.parse(await readFile(env.GITHUB_EVENT_PATH, 'utf8'));
|
|
26
|
+
return first(event.pull_request?.head?.sha);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Works out the branch, commit, base branch and merge base of this run, best
|
|
34
|
+
* effort: flags, then CI variables, then git. Nothing here fails a run - with
|
|
35
|
+
* no git at all every field may come back undefined, and the server treats
|
|
36
|
+
* the run as being on the project's default branch.
|
|
37
|
+
*/
|
|
38
|
+
export async function detectGitContext(options = {}) {
|
|
39
|
+
const { overrides = {}, env = process.env, cwd = process.cwd() } = options;
|
|
40
|
+
const branch = first(overrides.branch,
|
|
41
|
+
// Pull request events check out a detached merge commit; this is the PR's branch.
|
|
42
|
+
env.GITHUB_HEAD_REF, env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME, env.GITHUB_REF_TYPE === 'branch' ? env.GITHUB_REF_NAME : undefined,
|
|
43
|
+
// Unlike CI_COMMIT_REF_NAME, never a tag.
|
|
44
|
+
env.CI_COMMIT_BRANCH) ?? (await tryGit(['symbolic-ref', '--short', '-q', 'HEAD'], cwd));
|
|
45
|
+
const commit = first(overrides.commit, await githubPullRequestHead(env), env.GITHUB_SHA,
|
|
46
|
+
// Set in merged-results pipelines, where CI_COMMIT_SHA is the merge commit.
|
|
47
|
+
env.CI_MERGE_REQUEST_SOURCE_BRANCH_SHA, env.CI_COMMIT_SHA) ?? (await tryGit(['rev-parse', 'HEAD'], cwd));
|
|
48
|
+
const baseBranch = first(overrides.baseBranch, env.GITHUB_BASE_REF, env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME);
|
|
49
|
+
let mergeBase;
|
|
50
|
+
if (baseBranch && baseBranch !== branch) {
|
|
51
|
+
// The remote first: a local copy of the base is often behind it.
|
|
52
|
+
const refs = [`origin/${baseBranch}`, baseBranch];
|
|
53
|
+
// On a GitHub PR, HEAD is the merge commit, whose merge base with the
|
|
54
|
+
// target is the target itself; the PR's own head gives the fork point.
|
|
55
|
+
const heads = [...new Set([commit, 'HEAD'].filter((h) => Boolean(h)))];
|
|
56
|
+
for (const head of heads) {
|
|
57
|
+
mergeBase = (await findMergeBase(refs, cwd, head))?.base;
|
|
58
|
+
if (mergeBase)
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return { branch, commit, baseBranch, mergeBase };
|
|
63
|
+
}
|
|
64
|
+
/** "Branch feature/x (compared against main @ abc1234)", or what was not detected. */
|
|
65
|
+
export function describeGitContext(context) {
|
|
66
|
+
if (!context.branch) {
|
|
67
|
+
return "Branch: not detected - using the project's default branch (pass --branch to name one)";
|
|
68
|
+
}
|
|
69
|
+
if (!context.baseBranch || context.baseBranch === context.branch) {
|
|
70
|
+
return `Branch: ${context.branch}`;
|
|
71
|
+
}
|
|
72
|
+
const at = context.mergeBase ? ` @ ${context.mergeBase.slice(0, 7)}` : '';
|
|
73
|
+
return `Branch: ${context.branch} (compared against ${context.baseBranch}${at})`;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=git-context.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function git(args: string[], cwd: string): Promise<string>;
|
|
2
|
+
/**
|
|
3
|
+
* The commit `head` branched off the first of `refs` that resolves, or
|
|
4
|
+
* undefined when none does (no git, not a repository, or a shallow clone
|
|
5
|
+
* that never fetched the ref).
|
|
6
|
+
*/
|
|
7
|
+
export declare function findMergeBase(refs: string[], cwd: string, head?: string): Promise<{
|
|
8
|
+
ref: string;
|
|
9
|
+
base: string;
|
|
10
|
+
} | undefined>;
|
|
11
|
+
//# sourceMappingURL=git.d.ts.map
|
package/dist/core/git.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
const execFileAsync = promisify(execFile);
|
|
4
|
+
export async function git(args, cwd) {
|
|
5
|
+
const { stdout } = await execFileAsync('git', args, { cwd, maxBuffer: 64 * 1024 * 1024 });
|
|
6
|
+
return stdout;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The commit `head` branched off the first of `refs` that resolves, or
|
|
10
|
+
* undefined when none does (no git, not a repository, or a shallow clone
|
|
11
|
+
* that never fetched the ref).
|
|
12
|
+
*/
|
|
13
|
+
export async function findMergeBase(refs, cwd, head = 'HEAD') {
|
|
14
|
+
for (const ref of refs) {
|
|
15
|
+
try {
|
|
16
|
+
const base = (await git(['merge-base', ref, head], cwd)).trim();
|
|
17
|
+
return { ref, base };
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// Try the next candidate
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=git.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storysnap/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Visual regression testing for Storybook - capture screenshots, detect changes, and maintain baselines",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -53,27 +53,26 @@
|
|
|
53
53
|
},
|
|
54
54
|
"homepage": "https://github.com/Kosai106/storysnap#readme",
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@aws-sdk/client-s3": "^3.
|
|
57
|
-
"chalk": "^
|
|
58
|
-
"commander": "^
|
|
59
|
-
"glob": "^
|
|
60
|
-
"ora": "^
|
|
61
|
-
"pixelmatch": "^
|
|
62
|
-
"playwright": "^1.
|
|
56
|
+
"@aws-sdk/client-s3": "^3.1147.0",
|
|
57
|
+
"chalk": "^6.0.1",
|
|
58
|
+
"commander": "^15.0.0",
|
|
59
|
+
"glob": "^13.0.6",
|
|
60
|
+
"ora": "^9.4.1",
|
|
61
|
+
"pixelmatch": "^8.0.0",
|
|
62
|
+
"playwright": "^1.63.0",
|
|
63
63
|
"pngjs": "^7.0.0",
|
|
64
|
-
"zod": "^
|
|
64
|
+
"zod": "^4.6.5"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@
|
|
68
|
-
"@types/
|
|
69
|
-
"@types/pngjs": "^6.0.
|
|
70
|
-
"@vitest/coverage-v8": "^
|
|
71
|
-
"typescript": "^
|
|
72
|
-
"vitest": "^
|
|
73
|
-
"@storysnap/shared": "1.0.0"
|
|
67
|
+
"@storysnap/shared": "1.0.0",
|
|
68
|
+
"@types/node": "^22.12.0",
|
|
69
|
+
"@types/pngjs": "^6.0.5",
|
|
70
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
71
|
+
"typescript": "^7.0.2",
|
|
72
|
+
"vitest": "^5.0.3"
|
|
74
73
|
},
|
|
75
74
|
"engines": {
|
|
76
|
-
"node": ">=
|
|
75
|
+
"node": ">=22.12.0"
|
|
77
76
|
},
|
|
78
77
|
"scripts": {
|
|
79
78
|
"build": "tsc",
|