@storysnap/cli 1.0.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/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/api/client.js +156 -0
- package/dist/cli/commands/init.js +55 -0
- package/dist/cli/commands/login.js +114 -0
- package/dist/cli/commands/logout.js +36 -0
- package/dist/cli/commands/test.js +366 -0
- package/dist/cli/commands/update.js +229 -0
- package/dist/cli/index.js +93 -0
- package/dist/cli/reporter.js +140 -0
- package/dist/config/credentials.js +69 -0
- package/dist/config/loader.js +102 -0
- package/dist/config/schema.js +59 -0
- package/dist/core/compare.js +139 -0
- package/dist/core/screenshot.js +123 -0
- package/dist/core/storybook.js +124 -0
- package/dist/core/types.js +2 -0
- package/dist/storage/index.js +15 -0
- package/dist/storage/local.js +64 -0
- package/dist/storage/s3.js +109 -0
- package/package.json +79 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
export async function discoverStories(config) {
|
|
5
|
+
let indexData;
|
|
6
|
+
if (config.storybookStaticDir) {
|
|
7
|
+
indexData = await loadStaticIndex(config.storybookStaticDir);
|
|
8
|
+
}
|
|
9
|
+
else if (config.storybookUrl) {
|
|
10
|
+
indexData = await fetchRemoteIndex(config.storybookUrl);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
throw new Error('Either storybookStaticDir or storybookUrl must be provided');
|
|
14
|
+
}
|
|
15
|
+
const stories = parseIndex(indexData);
|
|
16
|
+
return filterStories(stories, config.include, config.exclude);
|
|
17
|
+
}
|
|
18
|
+
async function loadStaticIndex(staticDir) {
|
|
19
|
+
// Try index.json first (Storybook 7+)
|
|
20
|
+
const indexPath = join(staticDir, 'index.json');
|
|
21
|
+
if (existsSync(indexPath)) {
|
|
22
|
+
const content = await readFile(indexPath, 'utf-8');
|
|
23
|
+
return JSON.parse(content);
|
|
24
|
+
}
|
|
25
|
+
// Fall back to stories.json (Storybook 6)
|
|
26
|
+
const storiesPath = join(staticDir, 'stories.json');
|
|
27
|
+
if (existsSync(storiesPath)) {
|
|
28
|
+
const content = await readFile(storiesPath, 'utf-8');
|
|
29
|
+
return JSON.parse(content);
|
|
30
|
+
}
|
|
31
|
+
throw new Error(`No index.json or stories.json found in ${staticDir}. ` +
|
|
32
|
+
'Make sure you have built Storybook with "build-storybook".');
|
|
33
|
+
}
|
|
34
|
+
async function fetchRemoteIndex(url) {
|
|
35
|
+
const baseUrl = url.replace(/\/$/, '');
|
|
36
|
+
// Try index.json first (Storybook 7+)
|
|
37
|
+
try {
|
|
38
|
+
const response = await fetch(`${baseUrl}/index.json`);
|
|
39
|
+
if (response.ok) {
|
|
40
|
+
return (await response.json());
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// Continue to try stories.json
|
|
45
|
+
}
|
|
46
|
+
// Fall back to stories.json (Storybook 6)
|
|
47
|
+
try {
|
|
48
|
+
const response = await fetch(`${baseUrl}/stories.json`);
|
|
49
|
+
if (response.ok) {
|
|
50
|
+
return (await response.json());
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// Continue to error
|
|
55
|
+
}
|
|
56
|
+
throw new Error(`Could not fetch story index from ${url}. ` +
|
|
57
|
+
'Make sure Storybook is running and accessible.');
|
|
58
|
+
}
|
|
59
|
+
function parseIndex(index) {
|
|
60
|
+
const stories = [];
|
|
61
|
+
// Check if this is v7+ format (has entries) or v6 format (has stories)
|
|
62
|
+
if ('entries' in index && index.entries) {
|
|
63
|
+
for (const entry of Object.values(index.entries)) {
|
|
64
|
+
// Skip docs entries, only include stories
|
|
65
|
+
if (entry.type === 'docs') {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
stories.push({
|
|
69
|
+
id: entry.id,
|
|
70
|
+
title: entry.title,
|
|
71
|
+
name: entry.name,
|
|
72
|
+
componentPath: entry.importPath,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if ('stories' in index && index.stories) {
|
|
77
|
+
for (const story of Object.values(index.stories)) {
|
|
78
|
+
stories.push({
|
|
79
|
+
id: story.id,
|
|
80
|
+
title: story.title || story.kind || '',
|
|
81
|
+
name: story.name,
|
|
82
|
+
componentPath: story.importPath,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return stories;
|
|
87
|
+
}
|
|
88
|
+
function filterStories(stories, include, exclude) {
|
|
89
|
+
let filtered = stories;
|
|
90
|
+
if (include && include.length > 0) {
|
|
91
|
+
filtered = filtered.filter((story) => include.some((pattern) => matchPattern(story, pattern)));
|
|
92
|
+
}
|
|
93
|
+
if (exclude && exclude.length > 0) {
|
|
94
|
+
filtered = filtered.filter((story) => !exclude.some((pattern) => matchPattern(story, pattern)));
|
|
95
|
+
}
|
|
96
|
+
return filtered;
|
|
97
|
+
}
|
|
98
|
+
function matchPattern(story, pattern) {
|
|
99
|
+
// Simple glob-like matching: * matches any characters
|
|
100
|
+
const regex = new RegExp('^' +
|
|
101
|
+
pattern
|
|
102
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
103
|
+
.replace(/\*/g, '.*') +
|
|
104
|
+
'$', 'i');
|
|
105
|
+
// Match against story ID, title, or full title/name combination
|
|
106
|
+
const fullName = `${story.title}/${story.name}`;
|
|
107
|
+
return (regex.test(story.id) ||
|
|
108
|
+
regex.test(story.title) ||
|
|
109
|
+
regex.test(story.name) ||
|
|
110
|
+
regex.test(fullName));
|
|
111
|
+
}
|
|
112
|
+
export function getStoryUrl(baseUrl, storyId) {
|
|
113
|
+
const cleanBaseUrl = baseUrl.replace(/\/$/, '');
|
|
114
|
+
return `${cleanBaseUrl}/iframe.html?id=${encodeURIComponent(storyId)}&viewMode=story`;
|
|
115
|
+
}
|
|
116
|
+
export function getStoryKey(storyId, browser) {
|
|
117
|
+
// Convert story ID to a safe filename, optionally including browser
|
|
118
|
+
const safeId = storyId.replace(/[^a-zA-Z0-9-]/g, '-');
|
|
119
|
+
if (browser) {
|
|
120
|
+
return `${safeId}-${browser}.png`;
|
|
121
|
+
}
|
|
122
|
+
return `${safeId}.png`;
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=storybook.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { S3Storage } from './s3.js';
|
|
2
|
+
import { LocalStorage } from './local.js';
|
|
3
|
+
export function createStorage(config) {
|
|
4
|
+
switch (config.type) {
|
|
5
|
+
case 's3':
|
|
6
|
+
return new S3Storage(config);
|
|
7
|
+
case 'local':
|
|
8
|
+
return new LocalStorage(config);
|
|
9
|
+
default:
|
|
10
|
+
throw new Error(`Unknown storage type: ${config.type}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export { S3Storage } from './s3.js';
|
|
14
|
+
export { LocalStorage, ensureDir } from './local.js';
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readFile, writeFile, mkdir, readdir, unlink } from 'node:fs/promises';
|
|
3
|
+
import { join, dirname } from 'node:path';
|
|
4
|
+
export class LocalStorage {
|
|
5
|
+
directory;
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.directory = config.directory;
|
|
8
|
+
}
|
|
9
|
+
getPath(key) {
|
|
10
|
+
return join(this.directory, key);
|
|
11
|
+
}
|
|
12
|
+
async upload(key, data) {
|
|
13
|
+
const filePath = this.getPath(key);
|
|
14
|
+
const dir = dirname(filePath);
|
|
15
|
+
if (!existsSync(dir)) {
|
|
16
|
+
await mkdir(dir, { recursive: true });
|
|
17
|
+
}
|
|
18
|
+
await writeFile(filePath, data);
|
|
19
|
+
}
|
|
20
|
+
async download(key) {
|
|
21
|
+
const filePath = this.getPath(key);
|
|
22
|
+
if (!existsSync(filePath)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return readFile(filePath);
|
|
26
|
+
}
|
|
27
|
+
async exists(key) {
|
|
28
|
+
return existsSync(this.getPath(key));
|
|
29
|
+
}
|
|
30
|
+
async list(prefix) {
|
|
31
|
+
const searchDir = prefix ? join(this.directory, prefix) : this.directory;
|
|
32
|
+
if (!existsSync(searchDir)) {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
const keys = [];
|
|
36
|
+
async function walkDir(dir, basePath) {
|
|
37
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
38
|
+
for (const entry of entries) {
|
|
39
|
+
const fullPath = join(dir, entry.name);
|
|
40
|
+
const relativePath = basePath ? join(basePath, entry.name) : entry.name;
|
|
41
|
+
if (entry.isDirectory()) {
|
|
42
|
+
await walkDir(fullPath, relativePath);
|
|
43
|
+
}
|
|
44
|
+
else if (entry.isFile()) {
|
|
45
|
+
keys.push(relativePath);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
await walkDir(searchDir, prefix || '');
|
|
50
|
+
return keys;
|
|
51
|
+
}
|
|
52
|
+
async delete(key) {
|
|
53
|
+
const filePath = this.getPath(key);
|
|
54
|
+
if (existsSync(filePath)) {
|
|
55
|
+
await unlink(filePath);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export async function ensureDir(dir) {
|
|
60
|
+
if (!existsSync(dir)) {
|
|
61
|
+
await mkdir(dir, { recursive: true });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=local.js.map
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { S3Client, PutObjectCommand, GetObjectCommand, HeadObjectCommand, ListObjectsV2Command, DeleteObjectCommand, } from '@aws-sdk/client-s3';
|
|
2
|
+
export class S3Storage {
|
|
3
|
+
client;
|
|
4
|
+
bucket;
|
|
5
|
+
prefix;
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.bucket = config.bucket;
|
|
8
|
+
this.prefix = config.prefix || '';
|
|
9
|
+
const clientConfig = {
|
|
10
|
+
region: config.region,
|
|
11
|
+
};
|
|
12
|
+
if (config.endpoint) {
|
|
13
|
+
clientConfig.endpoint = config.endpoint;
|
|
14
|
+
clientConfig.forcePathStyle = true;
|
|
15
|
+
}
|
|
16
|
+
if (config.accessKeyId && config.secretAccessKey) {
|
|
17
|
+
clientConfig.credentials = {
|
|
18
|
+
accessKeyId: config.accessKeyId,
|
|
19
|
+
secretAccessKey: config.secretAccessKey,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
this.client = new S3Client(clientConfig);
|
|
23
|
+
}
|
|
24
|
+
getKey(key) {
|
|
25
|
+
return this.prefix ? `${this.prefix}${key}` : key;
|
|
26
|
+
}
|
|
27
|
+
async upload(key, data) {
|
|
28
|
+
const command = new PutObjectCommand({
|
|
29
|
+
Bucket: this.bucket,
|
|
30
|
+
Key: this.getKey(key),
|
|
31
|
+
Body: data,
|
|
32
|
+
ContentType: 'image/png',
|
|
33
|
+
});
|
|
34
|
+
await this.client.send(command);
|
|
35
|
+
}
|
|
36
|
+
async download(key) {
|
|
37
|
+
try {
|
|
38
|
+
const command = new GetObjectCommand({
|
|
39
|
+
Bucket: this.bucket,
|
|
40
|
+
Key: this.getKey(key),
|
|
41
|
+
});
|
|
42
|
+
const response = await this.client.send(command);
|
|
43
|
+
if (!response.Body) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const chunks = [];
|
|
47
|
+
for await (const chunk of response.Body) {
|
|
48
|
+
chunks.push(chunk);
|
|
49
|
+
}
|
|
50
|
+
return Buffer.concat(chunks);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error instanceof Error && error.name === 'NoSuchKey') {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async exists(key) {
|
|
60
|
+
try {
|
|
61
|
+
const command = new HeadObjectCommand({
|
|
62
|
+
Bucket: this.bucket,
|
|
63
|
+
Key: this.getKey(key),
|
|
64
|
+
});
|
|
65
|
+
await this.client.send(command);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error instanceof Error && error.name === 'NotFound') {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async list(prefix) {
|
|
76
|
+
const fullPrefix = this.prefix + (prefix || '');
|
|
77
|
+
const keys = [];
|
|
78
|
+
let continuationToken;
|
|
79
|
+
do {
|
|
80
|
+
const command = new ListObjectsV2Command({
|
|
81
|
+
Bucket: this.bucket,
|
|
82
|
+
Prefix: fullPrefix,
|
|
83
|
+
ContinuationToken: continuationToken,
|
|
84
|
+
});
|
|
85
|
+
const response = await this.client.send(command);
|
|
86
|
+
if (response.Contents) {
|
|
87
|
+
for (const object of response.Contents) {
|
|
88
|
+
if (object.Key) {
|
|
89
|
+
// Remove the prefix from the key
|
|
90
|
+
const key = this.prefix
|
|
91
|
+
? object.Key.slice(this.prefix.length)
|
|
92
|
+
: object.Key;
|
|
93
|
+
keys.push(key);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
continuationToken = response.NextContinuationToken;
|
|
98
|
+
} while (continuationToken);
|
|
99
|
+
return keys;
|
|
100
|
+
}
|
|
101
|
+
async delete(key) {
|
|
102
|
+
const command = new DeleteObjectCommand({
|
|
103
|
+
Bucket: this.bucket,
|
|
104
|
+
Key: this.getKey(key),
|
|
105
|
+
});
|
|
106
|
+
await this.client.send(command);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=s3.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@storysnap/cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Visual regression testing for Storybook - capture screenshots, detect changes, and maintain baselines",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/cli/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"storysnap": "./dist/cli/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"!dist/**/*.test.js",
|
|
13
|
+
"!dist/**/*.test.d.ts",
|
|
14
|
+
"!dist/**/*.map",
|
|
15
|
+
"!dist/**/*.d.ts",
|
|
16
|
+
"!dist/**/*.d.ts.map",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"storybook",
|
|
25
|
+
"visual-regression",
|
|
26
|
+
"screenshot",
|
|
27
|
+
"testing",
|
|
28
|
+
"playwright",
|
|
29
|
+
"visual-testing",
|
|
30
|
+
"snapshot",
|
|
31
|
+
"diff",
|
|
32
|
+
"ci",
|
|
33
|
+
"chromium",
|
|
34
|
+
"firefox",
|
|
35
|
+
"webkit"
|
|
36
|
+
],
|
|
37
|
+
"author": "",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/Kosai106/storysnap.git",
|
|
42
|
+
"directory": "packages/cli"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/Kosai106/storysnap/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/Kosai106/storysnap#readme",
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@aws-sdk/client-s3": "^3.400.0",
|
|
50
|
+
"chalk": "^5.3.0",
|
|
51
|
+
"commander": "^11.0.0",
|
|
52
|
+
"glob": "^10.3.0",
|
|
53
|
+
"ora": "^7.0.0",
|
|
54
|
+
"pixelmatch": "^5.3.0",
|
|
55
|
+
"playwright": "^1.40.0",
|
|
56
|
+
"pngjs": "^7.0.0",
|
|
57
|
+
"zod": "^3.22.0"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@types/node": "^20.10.0",
|
|
61
|
+
"@types/pixelmatch": "^5.2.6",
|
|
62
|
+
"@types/pngjs": "^6.0.4",
|
|
63
|
+
"@vitest/coverage-v8": "^2.0.0",
|
|
64
|
+
"typescript": "^5.3.0",
|
|
65
|
+
"vitest": "^2.0.0",
|
|
66
|
+
"@storysnap/shared": "1.0.0"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=18.0.0"
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsc",
|
|
73
|
+
"dev": "tsc --watch",
|
|
74
|
+
"typecheck": "tsc --noEmit",
|
|
75
|
+
"test": "vitest run",
|
|
76
|
+
"test:watch": "vitest",
|
|
77
|
+
"test:coverage": "vitest run --coverage"
|
|
78
|
+
}
|
|
79
|
+
}
|