storybook-onbook-plugin 0.2.1 → 0.2.3
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/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +395 -0
- package/dist/index.d.ts +11 -2
- package/dist/index.js +453 -1
- package/dist/screenshot-service/index.d.ts +46 -4
- package/dist/screenshot-service/index.js +213 -5
- package/dist/screenshot-service/utils/browser/index.d.ts +12 -2
- package/dist/screenshot-service/utils/browser/index.js +25 -1
- package/package.json +28 -9
- package/dist/cli/generate-screenshots.d.ts +0 -3
- package/dist/cli/generate-screenshots.d.ts.map +0 -1
- package/dist/cli/generate-screenshots.js +0 -169
- package/dist/component-loc/component-loc.d.ts +0 -7
- package/dist/component-loc/component-loc.d.ts.map +0 -1
- package/dist/component-loc/component-loc.js +0 -58
- package/dist/component-loc/index.d.ts +0 -2
- package/dist/component-loc/index.d.ts.map +0 -1
- package/dist/component-loc/index.js +0 -1
- package/dist/handlers/handleStoryFileChange/handleStoryFileChange.d.ts +0 -3
- package/dist/handlers/handleStoryFileChange/handleStoryFileChange.d.ts.map +0 -1
- package/dist/handlers/handleStoryFileChange/handleStoryFileChange.js +0 -100
- package/dist/handlers/handleStoryFileChange/index.d.ts +0 -2
- package/dist/handlers/handleStoryFileChange/index.d.ts.map +0 -1
- package/dist/handlers/handleStoryFileChange/index.js +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/screenshot-service/constants.d.ts +0 -8
- package/dist/screenshot-service/constants.d.ts.map +0 -1
- package/dist/screenshot-service/constants.js +0 -11
- package/dist/screenshot-service/index.d.ts.map +0 -1
- package/dist/screenshot-service/screenshot-service.d.ts +0 -8
- package/dist/screenshot-service/screenshot-service.d.ts.map +0 -1
- package/dist/screenshot-service/screenshot-service.js +0 -36
- package/dist/screenshot-service/types.d.ts +0 -18
- package/dist/screenshot-service/types.d.ts.map +0 -1
- package/dist/screenshot-service/types.js +0 -1
- package/dist/screenshot-service/utils/browser/browser.d.ts +0 -10
- package/dist/screenshot-service/utils/browser/browser.d.ts.map +0 -1
- package/dist/screenshot-service/utils/browser/browser.js +0 -29
- package/dist/screenshot-service/utils/browser/index.d.ts.map +0 -1
- package/dist/screenshot-service/utils/screenshot/index.d.ts +0 -2
- package/dist/screenshot-service/utils/screenshot/index.d.ts.map +0 -1
- package/dist/screenshot-service/utils/screenshot/index.js +0 -1
- package/dist/screenshot-service/utils/screenshot/screenshot.d.ts +0 -26
- package/dist/screenshot-service/utils/screenshot/screenshot.d.ts.map +0 -1
- package/dist/screenshot-service/utils/screenshot/screenshot.js +0 -128
- package/dist/storybook-onlook-plugin.d.ts +0 -9
- package/dist/storybook-onlook-plugin.d.ts.map +0 -1
- package/dist/storybook-onlook-plugin.js +0 -148
- package/dist/utils/fileSystem/fileSystem.d.ts +0 -9
- package/dist/utils/fileSystem/fileSystem.d.ts.map +0 -1
- package/dist/utils/fileSystem/fileSystem.js +0 -24
- package/dist/utils/fileSystem/index.d.ts +0 -2
- package/dist/utils/fileSystem/index.d.ts.map +0 -1
- package/dist/utils/fileSystem/index.js +0 -1
- package/dist/utils/findGitRoot/findGitRoot.d.ts +0 -5
- package/dist/utils/findGitRoot/findGitRoot.d.ts.map +0 -1
- package/dist/utils/findGitRoot/findGitRoot.js +0 -15
- package/dist/utils/findGitRoot/index.d.ts +0 -2
- package/dist/utils/findGitRoot/index.d.ts.map +0 -1
- package/dist/utils/findGitRoot/index.js +0 -1
- package/dist/utils/manifest/index.d.ts +0 -2
- package/dist/utils/manifest/index.d.ts.map +0 -1
- package/dist/utils/manifest/index.js +0 -1
- package/dist/utils/manifest/manifest.d.ts +0 -21
- package/dist/utils/manifest/manifest.d.ts.map +0 -1
- package/dist/utils/manifest/manifest.js +0 -44
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { command, string, run } from '@drizzle-team/brocli';
|
|
3
|
+
import { spawn } from 'child_process';
|
|
4
|
+
import path, { resolve, join, dirname } from 'path';
|
|
5
|
+
import crypto from 'crypto';
|
|
6
|
+
import fs, { existsSync, readFileSync } from 'fs';
|
|
7
|
+
import { chromium } from 'playwright';
|
|
8
|
+
|
|
9
|
+
var CACHE_DIR = path.join(process.cwd(), ".storybook-cache");
|
|
10
|
+
var SCREENSHOTS_DIR = path.join(CACHE_DIR, "screenshots");
|
|
11
|
+
var MANIFEST_PATH = path.join(CACHE_DIR, "manifest.json");
|
|
12
|
+
var VIEWPORT_WIDTH = 1920;
|
|
13
|
+
var VIEWPORT_HEIGHT = 1080;
|
|
14
|
+
var MIN_COMPONENT_WIDTH = 420;
|
|
15
|
+
var MIN_COMPONENT_HEIGHT = 280;
|
|
16
|
+
|
|
17
|
+
// src/utils/fileSystem/fileSystem.ts
|
|
18
|
+
function ensureCacheDirectories() {
|
|
19
|
+
if (!fs.existsSync(CACHE_DIR)) {
|
|
20
|
+
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
|
21
|
+
}
|
|
22
|
+
if (!fs.existsSync(SCREENSHOTS_DIR)) {
|
|
23
|
+
fs.mkdirSync(SCREENSHOTS_DIR, { recursive: true });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function computeFileHash(filePath) {
|
|
27
|
+
if (!fs.existsSync(filePath)) {
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
31
|
+
return crypto.createHash("sha256").update(content).digest("hex");
|
|
32
|
+
}
|
|
33
|
+
function loadManifest() {
|
|
34
|
+
if (fs.existsSync(MANIFEST_PATH)) {
|
|
35
|
+
const content = fs.readFileSync(MANIFEST_PATH, "utf-8");
|
|
36
|
+
return JSON.parse(content);
|
|
37
|
+
}
|
|
38
|
+
return { stories: {} };
|
|
39
|
+
}
|
|
40
|
+
function saveManifest(manifest) {
|
|
41
|
+
ensureCacheDirectories();
|
|
42
|
+
fs.writeFileSync(MANIFEST_PATH, JSON.stringify(manifest, null, 2));
|
|
43
|
+
}
|
|
44
|
+
function updateManifest(storyId, sourcePath, fileHash, boundingBox) {
|
|
45
|
+
const manifest = loadManifest();
|
|
46
|
+
manifest.stories[storyId] = {
|
|
47
|
+
fileHash,
|
|
48
|
+
lastGenerated: (/* @__PURE__ */ new Date()).toISOString(),
|
|
49
|
+
sourcePath,
|
|
50
|
+
screenshots: {
|
|
51
|
+
light: `screenshots/${storyId}/light.png`,
|
|
52
|
+
dark: `screenshots/${storyId}/dark.png`
|
|
53
|
+
},
|
|
54
|
+
boundingBox: boundingBox ?? void 0
|
|
55
|
+
};
|
|
56
|
+
saveManifest(manifest);
|
|
57
|
+
}
|
|
58
|
+
var browser = null;
|
|
59
|
+
async function getBrowser() {
|
|
60
|
+
if (!browser) {
|
|
61
|
+
browser = await chromium.launch({
|
|
62
|
+
headless: true
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return browser;
|
|
66
|
+
}
|
|
67
|
+
async function closeBrowser() {
|
|
68
|
+
if (browser) {
|
|
69
|
+
try {
|
|
70
|
+
await browser.close();
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error("Error closing browser:", error);
|
|
73
|
+
} finally {
|
|
74
|
+
browser = null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function getScreenshotPath(storyId, theme) {
|
|
79
|
+
const storyDir = path.join(SCREENSHOTS_DIR, storyId);
|
|
80
|
+
return path.join(storyDir, `${theme}.png`);
|
|
81
|
+
}
|
|
82
|
+
async function captureScreenshotBuffer(storyId, theme, width = VIEWPORT_WIDTH, height = VIEWPORT_HEIGHT, storybookUrl = "http://localhost:6006") {
|
|
83
|
+
const browser2 = await getBrowser();
|
|
84
|
+
const context = await browser2.newContext({
|
|
85
|
+
viewport: { width, height },
|
|
86
|
+
deviceScaleFactor: 2
|
|
87
|
+
});
|
|
88
|
+
const page = await context.newPage();
|
|
89
|
+
try {
|
|
90
|
+
const url = `${storybookUrl}/iframe.html?id=${storyId}&viewMode=story&globals=theme:${theme}`;
|
|
91
|
+
await page.goto(url, { timeout: 15e3 });
|
|
92
|
+
await page.waitForLoadState("domcontentloaded");
|
|
93
|
+
await page.waitForLoadState("load");
|
|
94
|
+
await page.waitForLoadState("networkidle");
|
|
95
|
+
await page.evaluate(() => document.fonts.ready);
|
|
96
|
+
await page.evaluate(async () => {
|
|
97
|
+
const images = document.querySelectorAll("img");
|
|
98
|
+
await Promise.all(
|
|
99
|
+
Array.from(images).map((img) => {
|
|
100
|
+
if (img.complete) return Promise.resolve();
|
|
101
|
+
return new Promise((resolve3) => {
|
|
102
|
+
img.addEventListener("load", resolve3);
|
|
103
|
+
img.addEventListener("error", resolve3);
|
|
104
|
+
});
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
const contentBounds = await page.evaluate(() => {
|
|
109
|
+
const root = document.querySelector("#storybook-root");
|
|
110
|
+
if (!root) return null;
|
|
111
|
+
const children = root.querySelectorAll("*");
|
|
112
|
+
if (children.length === 0) return null;
|
|
113
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
114
|
+
children.forEach((child) => {
|
|
115
|
+
const rect = child.getBoundingClientRect();
|
|
116
|
+
if (rect.width === 0 || rect.height === 0) return;
|
|
117
|
+
minX = Math.min(minX, rect.left);
|
|
118
|
+
minY = Math.min(minY, rect.top);
|
|
119
|
+
maxX = Math.max(maxX, rect.right);
|
|
120
|
+
maxY = Math.max(maxY, rect.bottom);
|
|
121
|
+
});
|
|
122
|
+
if (minX === Infinity) return null;
|
|
123
|
+
return {
|
|
124
|
+
x: minX,
|
|
125
|
+
y: minY,
|
|
126
|
+
width: maxX - minX,
|
|
127
|
+
height: maxY - minY
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
let screenshotBuffer;
|
|
131
|
+
let resultBoundingBox = null;
|
|
132
|
+
if (contentBounds && contentBounds.width > 0 && contentBounds.height > 0) {
|
|
133
|
+
const PADDING = 20;
|
|
134
|
+
const clippedWidth = Math.min(width, contentBounds.width + PADDING);
|
|
135
|
+
const clippedHeight = Math.min(height, contentBounds.height + PADDING);
|
|
136
|
+
resultBoundingBox = {
|
|
137
|
+
width: Math.max(MIN_COMPONENT_WIDTH, Math.round(clippedWidth)),
|
|
138
|
+
height: Math.max(MIN_COMPONENT_HEIGHT, Math.round(clippedHeight))
|
|
139
|
+
};
|
|
140
|
+
screenshotBuffer = await page.screenshot({
|
|
141
|
+
type: "png",
|
|
142
|
+
clip: {
|
|
143
|
+
x: Math.max(0, contentBounds.x - 10),
|
|
144
|
+
y: Math.max(0, contentBounds.y - 10),
|
|
145
|
+
width: clippedWidth,
|
|
146
|
+
height: clippedHeight
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
screenshotBuffer = await page.screenshot({ type: "png" });
|
|
151
|
+
}
|
|
152
|
+
return { buffer: screenshotBuffer, boundingBox: resultBoundingBox };
|
|
153
|
+
} finally {
|
|
154
|
+
await context.close();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async function generateScreenshot(storyId, theme, storybookUrl = "http://localhost:6006") {
|
|
158
|
+
try {
|
|
159
|
+
ensureCacheDirectories();
|
|
160
|
+
const storyDir = path.join(SCREENSHOTS_DIR, storyId);
|
|
161
|
+
if (!fs.existsSync(storyDir)) {
|
|
162
|
+
fs.mkdirSync(storyDir, { recursive: true });
|
|
163
|
+
}
|
|
164
|
+
const screenshotPath = getScreenshotPath(storyId, theme);
|
|
165
|
+
const { buffer, boundingBox } = await captureScreenshotBuffer(
|
|
166
|
+
storyId,
|
|
167
|
+
theme,
|
|
168
|
+
VIEWPORT_WIDTH,
|
|
169
|
+
VIEWPORT_HEIGHT,
|
|
170
|
+
storybookUrl
|
|
171
|
+
);
|
|
172
|
+
fs.writeFileSync(screenshotPath, buffer);
|
|
173
|
+
return { path: screenshotPath, boundingBox };
|
|
174
|
+
} catch (error) {
|
|
175
|
+
console.error(`Error generating screenshot for ${storyId} (${theme}):`, error);
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/screenshot-service/screenshot-service.ts
|
|
181
|
+
async function generateAllScreenshots(stories, storybookUrl = "http://localhost:6006") {
|
|
182
|
+
console.log(`Generating screenshots for ${stories.length} stories...`);
|
|
183
|
+
const BATCH_SIZE = 10;
|
|
184
|
+
const batches = [];
|
|
185
|
+
for (let i = 0; i < stories.length; i += BATCH_SIZE) {
|
|
186
|
+
batches.push(stories.slice(i, i + BATCH_SIZE));
|
|
187
|
+
}
|
|
188
|
+
let completed = 0;
|
|
189
|
+
for (const batch of batches) {
|
|
190
|
+
await Promise.all(
|
|
191
|
+
batch.map(async (story) => {
|
|
192
|
+
const [lightResult, darkResult] = await Promise.all([
|
|
193
|
+
generateScreenshot(story.id, "light", storybookUrl),
|
|
194
|
+
generateScreenshot(story.id, "dark", storybookUrl)
|
|
195
|
+
]);
|
|
196
|
+
if (lightResult && darkResult) {
|
|
197
|
+
const fileHash = computeFileHash(story.importPath);
|
|
198
|
+
updateManifest(story.id, story.importPath, fileHash, lightResult.boundingBox);
|
|
199
|
+
}
|
|
200
|
+
completed++;
|
|
201
|
+
console.log(
|
|
202
|
+
`[${completed}/${stories.length}] Generated screenshots for ${story.id}`
|
|
203
|
+
);
|
|
204
|
+
})
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
await closeBrowser();
|
|
208
|
+
console.log("Screenshot generation complete!");
|
|
209
|
+
}
|
|
210
|
+
var LOCKFILES = {
|
|
211
|
+
"bun.lockb": "bun",
|
|
212
|
+
"bun.lock": "bun",
|
|
213
|
+
"pnpm-lock.yaml": "pnpm",
|
|
214
|
+
"yarn.lock": "yarn",
|
|
215
|
+
"package-lock.json": "npm"
|
|
216
|
+
};
|
|
217
|
+
function findRepoRoot(startDir) {
|
|
218
|
+
let currentDir = resolve(startDir);
|
|
219
|
+
while (true) {
|
|
220
|
+
for (const lockfile of Object.keys(LOCKFILES)) {
|
|
221
|
+
if (existsSync(join(currentDir, lockfile))) {
|
|
222
|
+
return currentDir;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const parentDir = dirname(currentDir);
|
|
226
|
+
if (parentDir === currentDir) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
currentDir = parentDir;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function detectPackageManager(repoRoot) {
|
|
233
|
+
for (const [lockfile, pm] of Object.entries(LOCKFILES)) {
|
|
234
|
+
if (existsSync(join(repoRoot, lockfile))) {
|
|
235
|
+
return pm;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return "npm";
|
|
239
|
+
}
|
|
240
|
+
function getStorybookScript(storybookDir) {
|
|
241
|
+
const packageJsonPath = join(storybookDir, "package.json");
|
|
242
|
+
if (!existsSync(packageJsonPath)) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
246
|
+
return packageJson.scripts?.storybook ?? null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/cli/generate-screenshots/generate-screenshots.ts
|
|
250
|
+
function getStorybookCommand(storybookDir) {
|
|
251
|
+
const script = getStorybookScript(storybookDir);
|
|
252
|
+
if (script === null) {
|
|
253
|
+
const packageJsonPath = join(storybookDir, "package.json");
|
|
254
|
+
throw new Error(
|
|
255
|
+
`No "storybook" script found in ${packageJsonPath}. Use --storybook-dir to specify the correct directory or add a storybook script.`
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const repoRoot = findRepoRoot(storybookDir);
|
|
259
|
+
const pm = repoRoot ? detectPackageManager(repoRoot) : "npm";
|
|
260
|
+
console.log(
|
|
261
|
+
`\u{1F4E6} Detected package manager: ${pm}${repoRoot ? ` (from ${repoRoot})` : ""}`
|
|
262
|
+
);
|
|
263
|
+
return `${pm} run storybook`;
|
|
264
|
+
}
|
|
265
|
+
async function isStorybookRunning(url) {
|
|
266
|
+
try {
|
|
267
|
+
const response = await fetch(`${url}/index.json`, {
|
|
268
|
+
signal: AbortSignal.timeout(2e3)
|
|
269
|
+
});
|
|
270
|
+
return response.ok;
|
|
271
|
+
} catch {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async function fetchStoryIndex(url) {
|
|
276
|
+
const indexUrl = `${url}/index.json`;
|
|
277
|
+
const response = await fetch(indexUrl);
|
|
278
|
+
if (!response.ok) {
|
|
279
|
+
throw new Error(`Failed to fetch story index: ${response.statusText}`);
|
|
280
|
+
}
|
|
281
|
+
const data = await response.json();
|
|
282
|
+
return Object.values(data.entries);
|
|
283
|
+
}
|
|
284
|
+
async function startStorybook(cmd, storybookDir) {
|
|
285
|
+
console.log(`\u{1F680} Starting Storybook with: ${cmd} (in ${storybookDir})`);
|
|
286
|
+
const [command2, ...args] = cmd.split(" ");
|
|
287
|
+
if (!command2) {
|
|
288
|
+
throw new Error("Invalid storybook command");
|
|
289
|
+
}
|
|
290
|
+
const storybookProcess = spawn(command2, [...args, "--", "--no-open"], {
|
|
291
|
+
cwd: storybookDir,
|
|
292
|
+
stdio: "pipe",
|
|
293
|
+
shell: true
|
|
294
|
+
});
|
|
295
|
+
return new Promise((resolve3, reject) => {
|
|
296
|
+
let started = false;
|
|
297
|
+
const timeout = setTimeout(() => {
|
|
298
|
+
if (!started) {
|
|
299
|
+
storybookProcess.kill();
|
|
300
|
+
reject(new Error("Storybook failed to start within 60 seconds"));
|
|
301
|
+
}
|
|
302
|
+
}, 6e4);
|
|
303
|
+
storybookProcess.stdout?.on("data", (data) => {
|
|
304
|
+
const output = data.toString();
|
|
305
|
+
if (output.includes("Local:") || output.includes("localhost:")) {
|
|
306
|
+
if (!started) {
|
|
307
|
+
started = true;
|
|
308
|
+
clearTimeout(timeout);
|
|
309
|
+
console.log("\u2705 Storybook is ready!");
|
|
310
|
+
resolve3(storybookProcess);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
storybookProcess.stderr?.on("data", (data) => {
|
|
315
|
+
const output = data.toString();
|
|
316
|
+
if (output.toLowerCase().includes("error")) {
|
|
317
|
+
console.error(output);
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
storybookProcess.on("error", (error) => {
|
|
321
|
+
clearTimeout(timeout);
|
|
322
|
+
reject(error);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
async function warmupStorybook(url, firstStoryId) {
|
|
327
|
+
console.log("\u{1F525} Warming up Storybook...");
|
|
328
|
+
const browser2 = await getBrowser();
|
|
329
|
+
const context = await browser2.newContext();
|
|
330
|
+
const page = await context.newPage();
|
|
331
|
+
try {
|
|
332
|
+
const warmupUrl = `${url}/iframe.html?id=${firstStoryId}&viewMode=story`;
|
|
333
|
+
await page.goto(warmupUrl, { timeout: 15e3 });
|
|
334
|
+
await page.waitForLoadState("networkidle");
|
|
335
|
+
console.log("\u2705 Storybook warmed up");
|
|
336
|
+
} catch {
|
|
337
|
+
console.log("\u26A0\uFE0F Warmup had issues, proceeding anyway");
|
|
338
|
+
} finally {
|
|
339
|
+
await context.close();
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async function generateScreenshots(options = {}) {
|
|
343
|
+
const url = "http://localhost:6006";
|
|
344
|
+
const storybookDir = resolve(options.storybookDir ?? process.cwd());
|
|
345
|
+
let storybookProcess = null;
|
|
346
|
+
let weStartedStorybook = false;
|
|
347
|
+
console.log("\u{1F4F8} Generating Storybook screenshots...");
|
|
348
|
+
console.log(`\u{1F4C2} Storybook directory: ${storybookDir}`);
|
|
349
|
+
try {
|
|
350
|
+
const alreadyRunning = await isStorybookRunning(url);
|
|
351
|
+
if (alreadyRunning) {
|
|
352
|
+
console.log("\u2705 Storybook is already running");
|
|
353
|
+
} else {
|
|
354
|
+
const cmd = getStorybookCommand(storybookDir);
|
|
355
|
+
storybookProcess = await startStorybook(cmd, storybookDir);
|
|
356
|
+
weStartedStorybook = true;
|
|
357
|
+
}
|
|
358
|
+
const stories = await fetchStoryIndex(url);
|
|
359
|
+
console.log(`Found ${stories.length} stories`);
|
|
360
|
+
const firstStory = stories[0];
|
|
361
|
+
if (!firstStory) {
|
|
362
|
+
throw new Error("No stories found");
|
|
363
|
+
}
|
|
364
|
+
await warmupStorybook(url, firstStory.id);
|
|
365
|
+
await generateAllScreenshots(
|
|
366
|
+
stories.map((story) => ({
|
|
367
|
+
id: story.id,
|
|
368
|
+
importPath: story.importPath
|
|
369
|
+
})),
|
|
370
|
+
url
|
|
371
|
+
);
|
|
372
|
+
console.log("\u2705 Screenshot generation complete!");
|
|
373
|
+
} finally {
|
|
374
|
+
if (storybookProcess && weStartedStorybook) {
|
|
375
|
+
console.log("\u{1F6D1} Stopping Storybook...");
|
|
376
|
+
storybookProcess.kill();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/cli/index.ts
|
|
382
|
+
var generateScreenshotsCommand = command({
|
|
383
|
+
name: "generate-screenshots",
|
|
384
|
+
desc: "Generate screenshots for all Storybook stories",
|
|
385
|
+
options: {
|
|
386
|
+
storybookDir: string().alias("d").desc("Directory containing Storybook (defaults to current directory)")
|
|
387
|
+
},
|
|
388
|
+
handler: async (opts) => {
|
|
389
|
+
await generateScreenshots({ storybookDir: opts.storybookDir });
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
run([generateScreenshotsCommand], {
|
|
393
|
+
name: "storybook-onbook-plugin",
|
|
394
|
+
description: "Storybook plugin for Onbook - generate screenshots and more"
|
|
395
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { PluginOption } from 'vite';
|
|
2
|
+
|
|
3
|
+
type OnlookPluginOptions = {
|
|
4
|
+
/** Storybook port (default: 6006) */
|
|
5
|
+
port?: number;
|
|
6
|
+
/** Additional allowed origins for CORS (merged with defaults) */
|
|
7
|
+
allowedOrigins?: string[];
|
|
8
|
+
};
|
|
9
|
+
declare function storybookOnlookPlugin(options?: OnlookPluginOptions): PluginOption[];
|
|
10
|
+
|
|
11
|
+
export { type OnlookPluginOptions, storybookOnlookPlugin };
|