-
Notifications
You must be signed in to change notification settings - Fork 1
/
storybook-camera.js
56 lines (51 loc) · 1.99 KB
/
storybook-camera.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const puppeteer = require("puppeteer")
const connect = require("connect")
const serveStatic = require("serve-static")
const fs = require("fs")
const del = require("del")
const config = JSON.parse(fs.readFileSync("./storybook-camera.json", "utf8"))
const HOST = `http://localhost:${config.port}`
console.log(`Remove ${config.outdir} if exists`)
del.sync([config.outdir])
console.log(`Create ${config.outdir} directory`)
fs.mkdirSync(config.outdir)
console.log("Start storybook server")
const app = connect()
app.use(serveStatic(config.storydir))
server = app.listen(config.port)
;(async () => {
console.log("Open browser")
const browser = await puppeteer.launch({ args: ["--no-sandbox", "--disable-setuid-sandbox"] })
const page = await browser.newPage()
try {
for (const kind of Object.keys(config.storiesByKind)) {
console.log(`--------- ${kind} ----------`)
for (const story of config.storiesByKind[kind]) {
page.setViewport({
width: story.width || config.viewport.width || 1,
height: story.height || config.viewport.height || 1,
})
const res = await page.goto(`${HOST}/iframe.html?selectedKind=${kind}&selectedStory=${story.story}`, {
waitUntil: "networkidle0",
timeout: story.timeout || config.timeout,
})
const status = res.status()
if (status < 400) {
await page.screenshot({
path: `${config.outdir}/${kind}-${story.title || story.story}.png`,
fullPage: true,
})
}
console.log(` >>> ${status}: ${story.title || story.story}`)
}
}
} catch (e) {
console.log(e)
process.exit(1)
} finally {
console.log("Close browser")
await browser.close()
console.log("Stop storybook server")
server.close()
}
})()