-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_theme_keys.js
48 lines (41 loc) · 1.71 KB
/
create_theme_keys.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
import { writeFileSync } from 'node:fs'
import { getTheme } from './src/theme.js'
const themeData = getTheme()
const flatten = (object, prefix = '', context = {}) => {
return Object.entries(object).reduce((context, [key, value]) => {
const path = prefix ? `${prefix}.${key}` : key
// if value is an object then recursively flatten it
// expect if its an array or firestore field value
if (typeof value === 'object' && !Array.isArray(value)) {
flatten(value, path, context)
} else {
context[path] = value
}
return context
}, context)
}
const flat = flatten(themeData)
// create an array of objects with format { internalKey: 'text.size.small', externalKey: 'TEXT_SIZE_SMALL' }
const keys = Object.entries(flat) //
.filter(([key]) => key.endsWith('._value')) //
.map(([key]) => {
const externalKey = key.substring(0, key.indexOf('._value')).split('').reduce((context, next) => {
if (next === '.') {
context += '_'
return context
} else if (next.charCodeAt(0) < 97 && next.charCodeAt(0) >= 65) {
context += '_'
}
return context += next.toUpperCase()
}, '')
return {
internalKey: key.substring(0, key.indexOf('._value')),
externalKey
}
})
const fileContent = ['// This file is auto generated by create_theme_keys.js. Please run it manually after adding new theme properties.']
keys.reduce((context, next) => {
context.push(`export const ${next.externalKey} = '${next.internalKey}'`)
return context
}, fileContent)
writeFileSync('src/theme-keys.js', fileContent.join('\n'), 'utf-8')