cozy-ui 133.0.0-beta.1 → 133.0.1

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.
@@ -0,0 +1,74 @@
1
+ const fs = require('fs')
2
+
3
+ const { parseViewportArgument } = require('./helpers')
4
+ const fetchAllComponents = require('./fetchAllComponents')
5
+ const screenshotComponent = require('./screenshotComponent')
6
+
7
+ const loadJSON = filename => {
8
+ try {
9
+ return JSON.parse(fs.readFileSync(filename).toString())
10
+ } catch {
11
+ return null
12
+ }
13
+ }
14
+
15
+ const cacheToDisk = (fnToCache, options) =>
16
+ async function() {
17
+ const { cacheFile } = options
18
+ let res
19
+ if (cacheFile) {
20
+ res = loadJSON(cacheFile)
21
+ }
22
+ if (res) {
23
+ options.onLoadCache && options.onLoadCache()
24
+ } else {
25
+ res = await fnToCache.apply(this, arguments)
26
+ if (cacheFile) {
27
+ fs.writeFileSync(cacheFile, JSON.stringify(res, null, 2))
28
+ options.onSaveCache && options.onSaveCache()
29
+ }
30
+ }
31
+ return res
32
+ }
33
+
34
+ const screenshotReactStyleguide = async (page, args, config, theme) => {
35
+ const cachedFetchAllComponents = cacheToDisk(fetchAllComponents, {
36
+ cacheFile: args.cacheFile,
37
+ onLoadCache: () =>
38
+ console.log(`Using cached component list from ${args.cacheFile}`),
39
+ onSaveCache: () => console.log(`Saved component list to ${args.cacheFile}`)
40
+ })
41
+
42
+ let components = await cachedFetchAllComponents(page, args, config)
43
+ if (args.component) {
44
+ components = components.filter(component =>
45
+ component.name.includes(args.component)
46
+ )
47
+ }
48
+
49
+ console.log('⌛ Screenshotting components...')
50
+
51
+ for (const component of components) {
52
+ // Skip components in Deprecated folder
53
+ if (component.link.includes('Deprecated')) continue
54
+
55
+ const componentConfig = config[component.name] || {}
56
+ const componentViewportSpec =
57
+ (componentConfig.viewports && componentConfig.viewports[args.viewport]) ||
58
+ null
59
+ const componentViewport = componentViewportSpec
60
+ ? parseViewportArgument(componentViewportSpec)
61
+ : parseViewportArgument(args.viewport)
62
+ await page.setViewport(componentViewport)
63
+ await screenshotComponent(page, {
64
+ component,
65
+ componentConfig,
66
+ screenshotDir: args.screenshotDir,
67
+ viewport: componentViewport,
68
+ type: theme.type,
69
+ variant: theme.variant
70
+ })
71
+ }
72
+ }
73
+
74
+ module.exports = screenshotReactStyleguide