-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathprompts.ts
56 lines (48 loc) · 1.56 KB
/
prompts.ts
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
import {PromptConfig, UserPromptConfig} from '@commitlint/types';
import isPlainObject from 'lodash/isPlainObject';
import defaultPromptConfigs from './defaultPromptConfigs';
const storeKey = Symbol('promptConfig');
const store: {
[storeKey]: PromptConfig;
} = {
[storeKey]: defaultPromptConfigs,
};
export function setPromptConfig(newPromptConfig: UserPromptConfig): void {
const {settings, messages, questions} = newPromptConfig;
if (messages) {
const requiredMessageKeys = Object.keys(defaultPromptConfigs.messages);
requiredMessageKeys.forEach((key: string) => {
const message = messages[key];
if (typeof message === 'string') {
store[storeKey]['messages'][key] = message;
}
});
}
if (questions && isPlainObject(questions)) {
store[storeKey]['questions'] = questions;
}
if (settings && isPlainObject(settings)) {
if (
settings['scopeEnumSeparator'] &&
!/^\/|\\|,$/.test(settings['scopeEnumSeparator'])
) {
console.log(
`prompt.settings.scopeEnumSeparator must be one of ',', '\\', '/'.`
);
process.exit(1);
}
store[storeKey]['settings'] = {
...defaultPromptConfigs.settings,
...settings,
};
}
}
export function getPromptMessages(): Readonly<PromptConfig['messages']> {
return (store[storeKey] && store[storeKey]['messages']) ?? {};
}
export function getPromptQuestions(): Readonly<PromptConfig['questions']> {
return (store[storeKey] && store[storeKey]['questions']) ?? {};
}
export function getPromptSettings(): Readonly<PromptConfig['settings']> {
return (store[storeKey] && store[storeKey]['settings']) ?? {};
}