/
getConfiguration.js
60 lines (50 loc) 路 1.7 KB
/
getConfiguration.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
import Conf from 'conf'
import { cwd } from 'process'
import { pathExistsSync } from 'path-exists'
import { CONFIG, EMOJI_COMMIT_FORMATS } from '@constants/configuration'
const DEFAULT_CONFIGURATION = {
[CONFIG.AUTO_ADD]: false,
[CONFIG.EMOJI_FORMAT]: EMOJI_COMMIT_FORMATS.CODE,
[CONFIG.SCOPE_PROMPT]: false,
[CONFIG.GITMOJIS_URL]: 'https://gitmoji.dev/api/gitmojis'
}
const LOCAL_CONFIGURATION: typeof Conf = new Conf({
projectName: 'gitmoji',
schema: {
[CONFIG.AUTO_ADD]: { type: 'boolean' },
[CONFIG.EMOJI_FORMAT]: {
enum: Object.values(EMOJI_COMMIT_FORMATS)
},
[CONFIG.SCOPE_PROMPT]: { type: 'boolean' },
[CONFIG.GITMOJIS_URL]: { type: 'string', format: 'url' }
}
})
const getConfiguration = (): { get: Function, set: Function } => {
const loadConfig = (): {
[$Values<typeof CONFIG>]: string | boolean
} => {
const packageJson = `${cwd()}/package.json`
const configurationFile = `${cwd()}/.gitmojirc.json`
if (pathExistsSync(packageJson) && require(packageJson)?.gitmoji) {
return require(packageJson).gitmoji
}
if (pathExistsSync(configurationFile) && require(configurationFile)) {
return require(configurationFile)
}
return LOCAL_CONFIGURATION.store
}
return {
get: (key: string): string | boolean => {
const resolvedConfiguration = loadConfig()
const configuration =
typeof resolvedConfiguration === 'object' &&
Object.keys(resolvedConfiguration).length
? resolvedConfiguration
: DEFAULT_CONFIGURATION
return configuration[key]
},
set: (key: string, value: string | boolean): void =>
LOCAL_CONFIGURATION.set(key, value)
}
}
export default getConfiguration