@digital-commons-official/codeceptjs-storyboard 0.0.0-stage → 0.3.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/src/theme.js ADDED
@@ -0,0 +1,67 @@
1
+ const fs = require('fs/promises')
2
+ const path = require('path')
3
+
4
+ const PROPERTIES = {
5
+ bg: '--sb-bg',
6
+ card: '--sb-card',
7
+ slot: '--sb-slot',
8
+ text: '--sb-text',
9
+ dim: '--sb-dim',
10
+ accent: '--sb-accent',
11
+ badge: '--sb-badge',
12
+ codeBg: '--sb-code-bg',
13
+ given: '--sb-given',
14
+ when: '--sb-when',
15
+ then: '--sb-then',
16
+ fail: '--sb-fail',
17
+ sans: '--sb-font-sans',
18
+ mono: '--sb-font-mono'
19
+ }
20
+
21
+ function normalizeTheme (input) {
22
+ if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('theme must be an object')
23
+ const theme = {}
24
+ for (const key of Object.keys(PROPERTIES)) {
25
+ if (!Object.hasOwn(input, key)) continue
26
+ const value = String(input[key]).trim()
27
+ const font = key === 'sans' || key === 'mono'
28
+ const valid = font
29
+ ? /^[a-zA-Z0-9 ,"'_-]+$/.test(value)
30
+ : /^(?:#[\da-f]{3}|#[\da-f]{4}|#[\da-f]{6}|#[\da-f]{8}|[a-z]+|(?:rgb|hsl)a?\([\d.,% /+-]+\))$/i.test(value)
31
+ if (!valid) throw new Error(`invalid theme value for ${key}`)
32
+ theme[key] = font ? value.replace(/"/g, "'") : value
33
+ }
34
+ return theme
35
+ }
36
+
37
+ function parseCss (css) {
38
+ const declarations = new Map()
39
+ const source = css.replace(/\/\*[\s\S]*?\*\//g, '')
40
+ for (const match of source.matchAll(/(--sb-[\w-]+)\s*:\s*([^;}]+)/g)) declarations.set(match[1], match[2].trim())
41
+ const input = {}
42
+ for (const [key, property] of Object.entries(PROPERTIES)) {
43
+ if (declarations.has(property)) input[key] = declarations.get(property)
44
+ }
45
+ if (!Object.keys(input).length) throw new Error('no supported theme properties')
46
+ return normalizeTheme(input)
47
+ }
48
+
49
+ async function loadTheme (input, baseDir, warn = console.warn) {
50
+ try {
51
+ if (typeof input !== 'string') return normalizeTheme(input)
52
+ let css
53
+ if (/^https?:\/\//i.test(input)) {
54
+ const response = await fetch(input, { signal: AbortSignal.timeout(5000) })
55
+ if (!response.ok) throw new Error(`HTTP ${response.status}`)
56
+ css = await response.text()
57
+ } else {
58
+ css = await fs.readFile(path.resolve(baseDir, input), 'utf8')
59
+ }
60
+ return parseCss(css)
61
+ } catch (error) {
62
+ warn(`storyboard: could not load theme (${error.message}); using the default theme`)
63
+ return {}
64
+ }
65
+ }
66
+
67
+ module.exports = { PROPERTIES, normalizeTheme, loadTheme }