Skip to content

Commit

Permalink
refactor(prompt): migrate prompt to typescript (#2371)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
armano2 committed Jan 4, 2021
1 parent 6ea5a9a commit 8c41651
Show file tree
Hide file tree
Showing 28 changed files with 695 additions and 481 deletions.
21 changes: 2 additions & 19 deletions @commitlint/prompt/package.json
Expand Up @@ -7,20 +7,8 @@
"lib/"
],
"scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"commit": "git-cz",
"deps": "dep-check",
"pkg": "pkg-check --skip-import",
"start": "yarn run watch",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"babel": {
"presets": [
"commitlint"
],
"ignore": [
"**/*.test.js"
]
"pkg": "pkg-check --skip-import"
},
"config": {
"commitizen": {
Expand Down Expand Up @@ -48,15 +36,10 @@
"node": ">=v10.22.1"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@commitlint/utils": "^11.0.0",
"babel-preset-commitlint": "^11.0.0",
"commitizen": "4.2.2",
"cross-env": "7.0.3"
"commitizen": "4.2.2"
},
"dependencies": {
"@babel/runtime": "^7.11.2",
"@commitlint/load": "^11.0.0",
"chalk": "^4.0.0",
"lodash": "^4.17.19",
Expand Down
13 changes: 0 additions & 13 deletions @commitlint/prompt/src/index.js

This file was deleted.

17 changes: 17 additions & 0 deletions @commitlint/prompt/src/index.ts
@@ -0,0 +1,17 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import vorpal from 'vorpal';
import input from './input';

type Commit = (input: string) => void;

/**
* Entry point for commitizen
* @param _ inquirer instance passed by commitizen, unused
* @param commit callback to execute with complete commit message
* @return generated commit message
*/
export async function prompter(_: unknown, commit: Commit): Promise<void> {
const message = await input(vorpal);
commit(message);
}
94 changes: 0 additions & 94 deletions @commitlint/prompt/src/input.js

This file was deleted.

63 changes: 63 additions & 0 deletions @commitlint/prompt/src/input.ts
@@ -0,0 +1,63 @@
import load from '@commitlint/load';
import throat from 'throat';

import format from './library/format';
import getPrompt from './library/get-prompt';
import settings from './settings';
import {InputSetting, Prompter, Result} from './library/types';
import {getHasName, getMaxLength, getRules} from './library/utils';

export default input;

/**
* Get user input by interactive prompt based on
* conventional-changelog-lint rules.
* @param prompter
* @return commit message
*/
async function input(prompter: () => Prompter): Promise<string> {
const results: Result = {
type: null,
scope: null,
subject: null,
body: null,
footer: null,
};

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);

await Promise.all(
parts.map(
throat(1, async (input) => {
const inputRules = getRules(input, rules);
const inputSettings: InputSetting = settings[input];

if (headerParts.includes(input) && maxLength < Infinity) {
inputSettings.header = {
length: maxLength,
};
}

results[input] = await getPrompt(input, {
rules: inputRules,
settings: inputSettings,
results,
prompter,
});
})
)
).catch((err) => {
console.error(err);
return '';
});

// Return the results
return format(results);
}
12 changes: 0 additions & 12 deletions @commitlint/prompt/src/library/enum-rule-is-active.js

This file was deleted.

@@ -1,18 +1,18 @@
import chalk from 'chalk';
import {Result} from './types';

export default format;

/**
* Get formatted commit message
* @param {object} input object containing structured results
* @param {boolean} debug show debug information in commit message
* @return {string} formatted debug message
* @param input object containing structured results
* @param debug show debug information in commit message
* @return formatted debug message
*/
function format(input, debug = false) {
function format(input: Result, debug = false): string {
const results = debug
? Object.entries(input || {}).reduce((registry, item) => {
const [name, value] = item;
registry[name] =
? Object.entries(input).reduce<Result>((registry, [name, value]) => {
registry[name as 'type' | 'scope' | 'subject' | 'body' | 'footer'] =
value === null ? chalk.grey(`<${name}>`) : chalk.bold(value);
return registry;
}, {})
Expand Down
68 changes: 0 additions & 68 deletions @commitlint/prompt/src/library/get-forced-case-fn.js

This file was deleted.

0 comments on commit 8c41651

Please sign in to comment.