/
capture.js
176 lines (142 loc) · 5.07 KB
/
capture.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const Path = require('path')
const fs = require('fs/promises')
const puppeteer = require('puppeteer')
const chalk = require('chalk')
const _ = require('lodash')
const config = require('@ekino/config')
const DEFAULT_FLAVOR = 'svg'
const CHART_SELECTOR = '#chart'
const VIEWPORT = {
chart: { width: 1400, height: 900 },
icons: { width: 1400, height: 4000 },
}
const ICON_VARIANTS = [
'light-neutral',
'light-colored',
'dark-neutral',
'dark-colored',
]
const projectDir = process.cwd()
const websiteDir = Path.join(projectDir, 'website')
const websiteCapturesDir = Path.join(websiteDir, 'src', 'assets', 'captures')
const websiteIconsDir = Path.join(websiteDir, 'src', 'assets', 'icons')
const getChartFileName = (chart, flavor) => `${chart}${flavor !== DEFAULT_FLAVOR ? `-${flavor}` : ''}.png`
const getChartWebsiteFilePath = (chart, flavor) => Path.join(
websiteCapturesDir,
getChartFileName(chart, flavor)
)
const getChartUrl = (chart, flavor) => {
const baseUrl = config.get('baseUrl')
const chunks = [baseUrl, chart]
if (flavor !== DEFAULT_FLAVOR) {
chunks.push(flavor)
}
return `${Path.join(...chunks)}/?capture=1`
}
const getChartIconFilePath = (chart, variant) => Path.join(
websiteIconsDir,
`${chart}-${variant}.png`
)
const captureChart = async (page, { pkg, chart, flavor, theme }) => {
const url = getChartUrl(chart, flavor)
console.log(chalk`{yellow Capturing chart {white ${chart}}} {dim (package: @nivo/${pkg}, flavor: ${flavor}, url: ${url})}`)
await page.setViewport(VIEWPORT.chart)
await page.goto(url)
if (theme !== undefined) {
console.log(chalk`{dim changing theme to: ${theme}}`)
const themeSelector = `#${theme}Theme`
await page.waitFor(themeSelector)
await page.click(themeSelector)
}
await page.waitFor(CHART_SELECTOR)
const element = await page.$(CHART_SELECTOR)
if (element === null) {
throw new Error(`Unable to find element matching selector: ${CHART_SELECTOR} (url: ${url})`)
}
const clip = await element.boundingBox()
const websiteFilePath = getChartWebsiteFilePath(chart, flavor)
// initially saved to the package doc dir
await page.screenshot({
path: websiteFilePath,
clip,
omitBackground: true,
})
// also save a copy in the website, for social images
await fs.copyFile(docFilePath, websiteFilePath)
console.log(chalk`{green saved to {white ${websiteFilePath}}}`)
console.log('')
}
const captureCharts = async () => {
const charts = config.get('capture').charts
console.log(chalk`{yellow Starting capture for ${charts.length} chart(s)}`)
console.log('')
try {
const browser = await puppeteer.launch({
headless: true
})
const page = await browser.newPage()
for (let chart of charts) {
for (let flavor of chart.flavors) {
await captureChart(page, { ...chart, flavor })
}
}
await browser.close()
console.log(chalk`{green Done!}`)
} catch (error) {
console.log('')
console.error(chalk`{red oops, something went wrong :(}`)
console.error(error)
process.exit(1)
}
}
const captureIcons = async () => {
const charts = config.get('capture').charts
const icons = charts.map(chart => chart.chart)
console.log(chalk`{yellow Starting capture for ${icons.length} chart icon(s)}`)
console.log('')
try {
const browser = await puppeteer.launch({
headless: true
})
const page = await browser.newPage()
await page.setViewport(VIEWPORT.icons)
await page.goto(`${Path.join(
config.get('baseUrl'),
'internal',
'icons'
)}/`)
for (let icon of icons) {
console.log(chalk`{yellow Capturing {white ${icon}} chart icons}`)
for (let variant of ICON_VARIANTS) {
const selector = `#${icon}-${_.camelCase(variant)}`
console.log(chalk`{dim variant: ${variant}, selector: ${selector}}`)
await page.waitFor(selector)
const element = await page.$(selector)
if (element === null) {
throw new Error(`Unable to find element matching selector: ${selector}`)
}
const iconFilePath = getChartIconFilePath(icon, variant)
const clip = await element.boundingBox()
await page.screenshot({
path: iconFilePath,
clip,
omitBackground: true,
})
console.log(chalk`{green saved to {white ${iconFilePath}}}`)
}
console.log('')
}
await browser.close()
console.log(chalk`{green Done!}`)
} catch (error) {
console.log('')
console.error(chalk`{red oops, something went wrong :(}`)
console.error(error)
process.exit(1)
}
}
const captureAll = async () => {
await captureCharts()
await captureIcons()
}
captureAll()