-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
51 lines (42 loc) · 1.45 KB
/
config.ts
File metadata and controls
51 lines (42 loc) · 1.45 KB
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
import fs from 'fs'
import path from 'path'
import { AttributifyConfig, Pattern } from './types/types'
const defaultConfig: Required<AttributifyConfig> = {
prefix: 'attr_',
presets: [] as (Pattern[] | (() => Pattern[]))[],
themes: {},
defaultTheme: 'default'
}
let cachedConfig: Required<AttributifyConfig> | null = null
const processPresets = (presets: (Pattern[] | (() => Pattern[]))[]): (Pattern[] | (() => Pattern[]))[] => {
return presets.map(preset => {
if (typeof preset === 'function') {
return preset()
}
return preset
}) as (Pattern[] | (() => Pattern[]))[]
}
export function loadConfig(cwd: string): Required<AttributifyConfig> {
if (cachedConfig) {
return cachedConfig
}
const configPath = path.resolve(cwd, 'attributify.config.js')
if (fs.existsSync(configPath)) {
try {
delete require.cache[configPath]
// eslint-disable-next-line @typescript-eslint/no-require-imports
const userConfig = require(configPath) as AttributifyConfig
cachedConfig = {
prefix: userConfig.prefix || defaultConfig.prefix,
presets: processPresets(userConfig.presets),
themes: userConfig.themes || defaultConfig.themes,
defaultTheme: userConfig.defaultTheme || defaultConfig.defaultTheme
}
return cachedConfig
} catch (error) {
console.warn(`Error while reading config file: ${configPath}`, error)
}
}
cachedConfig = defaultConfig
return cachedConfig
}