Skip to content

Commit 8c41651

Browse files
authored
refactor(prompt): migrate prompt to typescript (#2371)
* refactor(prompt): port prompt to typescript * chore(prompt): correct docs * fix(prompt): correct typeguard of ruleIsActive * chore(prompt): fix spacing * fix: cleanup package.json from no longer used lerna calls * chore: fix watch command * refactor(prompt): refactor prompt cli to use utils
1 parent 6ea5a9a commit 8c41651

28 files changed

+695
-481
lines changed

@commitlint/prompt/package.json

+2-19
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,8 @@
77
"lib/"
88
],
99
"scripts": {
10-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
11-
"commit": "git-cz",
1210
"deps": "dep-check",
13-
"pkg": "pkg-check --skip-import",
14-
"start": "yarn run watch",
15-
"watch": "babel src --out-dir lib --watch --source-maps"
16-
},
17-
"babel": {
18-
"presets": [
19-
"commitlint"
20-
],
21-
"ignore": [
22-
"**/*.test.js"
23-
]
11+
"pkg": "pkg-check --skip-import"
2412
},
2513
"config": {
2614
"commitizen": {
@@ -48,15 +36,10 @@
4836
"node": ">=v10.22.1"
4937
},
5038
"devDependencies": {
51-
"@babel/cli": "^7.11.6",
52-
"@babel/core": "^7.11.6",
5339
"@commitlint/utils": "^11.0.0",
54-
"babel-preset-commitlint": "^11.0.0",
55-
"commitizen": "4.2.2",
56-
"cross-env": "7.0.3"
40+
"commitizen": "4.2.2"
5741
},
5842
"dependencies": {
59-
"@babel/runtime": "^7.11.2",
6043
"@commitlint/load": "^11.0.0",
6144
"chalk": "^4.0.0",
6245
"lodash": "^4.17.19",

@commitlint/prompt/src/index.js

-13
This file was deleted.

@commitlint/prompt/src/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2+
// @ts-ignore
3+
import vorpal from 'vorpal';
4+
import input from './input';
5+
6+
type Commit = (input: string) => void;
7+
8+
/**
9+
* Entry point for commitizen
10+
* @param _ inquirer instance passed by commitizen, unused
11+
* @param commit callback to execute with complete commit message
12+
* @return generated commit message
13+
*/
14+
export async function prompter(_: unknown, commit: Commit): Promise<void> {
15+
const message = await input(vorpal);
16+
commit(message);
17+
}

@commitlint/prompt/src/input.js

-94
This file was deleted.

@commitlint/prompt/src/input.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import load from '@commitlint/load';
2+
import throat from 'throat';
3+
4+
import format from './library/format';
5+
import getPrompt from './library/get-prompt';
6+
import settings from './settings';
7+
import {InputSetting, Prompter, Result} from './library/types';
8+
import {getHasName, getMaxLength, getRules} from './library/utils';
9+
10+
export default input;
11+
12+
/**
13+
* Get user input by interactive prompt based on
14+
* conventional-changelog-lint rules.
15+
* @param prompter
16+
* @return commit message
17+
*/
18+
async function input(prompter: () => Prompter): Promise<string> {
19+
const results: Result = {
20+
type: null,
21+
scope: null,
22+
subject: null,
23+
body: null,
24+
footer: null,
25+
};
26+
27+
const {rules} = await load();
28+
const parts = ['type', 'scope', 'subject', 'body', 'footer'] as const;
29+
const headerParts = ['type', 'scope', 'subject'];
30+
31+
const headerLengthRule = getRules('header', rules).find(
32+
getHasName('max-length')
33+
);
34+
const maxLength = getMaxLength(headerLengthRule);
35+
36+
await Promise.all(
37+
parts.map(
38+
throat(1, async (input) => {
39+
const inputRules = getRules(input, rules);
40+
const inputSettings: InputSetting = settings[input];
41+
42+
if (headerParts.includes(input) && maxLength < Infinity) {
43+
inputSettings.header = {
44+
length: maxLength,
45+
};
46+
}
47+
48+
results[input] = await getPrompt(input, {
49+
rules: inputRules,
50+
settings: inputSettings,
51+
results,
52+
prompter,
53+
});
54+
})
55+
)
56+
).catch((err) => {
57+
console.error(err);
58+
return '';
59+
});
60+
61+
// Return the results
62+
return format(results);
63+
}

@commitlint/prompt/src/library/enum-rule-is-active.js

-12
This file was deleted.

@commitlint/prompt/src/library/format.js renamed to @commitlint/prompt/src/library/format.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import chalk from 'chalk';
2+
import {Result} from './types';
23

34
export default format;
45

56
/**
67
* Get formatted commit message
7-
* @param {object} input object containing structured results
8-
* @param {boolean} debug show debug information in commit message
9-
* @return {string} formatted debug message
8+
* @param input object containing structured results
9+
* @param debug show debug information in commit message
10+
* @return formatted debug message
1011
*/
11-
function format(input, debug = false) {
12+
function format(input: Result, debug = false): string {
1213
const results = debug
13-
? Object.entries(input || {}).reduce((registry, item) => {
14-
const [name, value] = item;
15-
registry[name] =
14+
? Object.entries(input).reduce<Result>((registry, [name, value]) => {
15+
registry[name as 'type' | 'scope' | 'subject' | 'body' | 'footer'] =
1616
value === null ? chalk.grey(`<${name}>`) : chalk.bold(value);
1717
return registry;
1818
}, {})

@commitlint/prompt/src/library/get-forced-case-fn.js

-68
This file was deleted.

0 commit comments

Comments
 (0)