forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.ts
52 lines (45 loc) · 1.52 KB
/
input.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
import load from '@commitlint/load';
import type {DistinctQuestion, PromptModule} from 'inquirer';
import format from './library/format';
import getPrompt from './library/get-prompt';
import settings from './settings';
import type {InputSetting, Result} from './library/types';
import {getHasName, getMaxLength, getRules} from './library/utils';
import InputCustomPrompt from './inquirer/InputCustomPrompt';
/**
* Get user input by interactive prompt based on
* conventional-changelog-lint rules.
* @param prompter
* @return commit message
*/
export async function input(prompter: PromptModule): Promise<string> {
const {rules} = await load();
const parts = ['type', 'scope', 'subject', 'body', 'footer'] as const;
const headerParts = ['type', 'scope', 'subject'];
const headerLengthRule = getRules('header', rules).find(
getHasName('max-length')
);
const maxLength = getMaxLength(headerLengthRule);
try {
const questions: DistinctQuestion<Result>[] = [];
prompter.registerPrompt('input-custom', InputCustomPrompt);
for (const input of parts) {
const inputSetting: InputSetting = settings[input];
const inputRules = getRules(input, rules);
if (headerParts.includes(input) && maxLength < Infinity) {
inputSetting.header = {
length: maxLength,
};
}
const question = getPrompt(input, inputRules, inputSetting);
if (question) {
questions.push(question);
}
}
const results = await prompter<Result>(questions);
return format(results);
} catch (err) {
console.error(err);
return '';
}
}