Skip to content

Fix #issue 2 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/prompts/scope-maker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,20 @@ describe('scopeMaker', () => {
const scopeConfig = scopeMaker([], { 'scope-enum': [2, 'always', ['foo', 'bar']] })[0] as ListQuestion;

if (scopeConfig.choices) {
expect(scopeConfig.choices).toEqual(['foo', 'bar']);
expect(scopeConfig.choices).toEqual([
{
name: 'foo',
value: 'foo'
},
{
name: 'bar',
value: 'bar'
},
{
name: ':skip',
value: ''
}
]);
}
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/prompts/scope-maker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Rules } from '@commitlint/load';
import { DistinctQuestion } from 'inquirer';
import { ChoiceOptions } from 'inquirer';
import { whenFactory } from '../when';
import { caseValidator, emptyValidator, maxLengthValidator, minLengthValidator, validate } from '../validators';
import { wordCaseFilter } from '../filters';
Expand Down Expand Up @@ -37,11 +37,11 @@ export function validatorFactory(rules: Rules) {
}

export function choicesFactory(rules: Rules) {
let choices: string[] | undefined;
let choices: ChoiceOptions[] | undefined;
if (rules['scope-enum']) {
const [, , scopeEnum] = rules['scope-enum'];
if (scopeEnum && scopeEnum.length > 0) {
choices = scopeEnum;
choices = [...scopeEnum.map(scope => ({ name: scope, value: scope })), { name: ':skip', value: '' }];
}
}

Expand Down
87 changes: 74 additions & 13 deletions src/prompts/type-maker.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Level, Rules } from '@commitlint/load';
import { types } from 'conventional-commit-types';

import { filterFactory, choicesFactory, validatorFactory, typeMaker } from './type-maker';

jest.mock('conventional-commit-types');
Expand Down Expand Up @@ -36,28 +38,87 @@ describe('type-maker', () => {
});

describe('choicesFactory', () => {
describe('should return undefined if type-enum undefined', () => {
const result = choicesFactory({}, {});
describe('should return commitTypes as choices if type-enum undefined', () => {
const result = choicesFactory({}, types);

expect(result).toBeUndefined();
expect(result).toEqual([
{
name: 'feat: A new feature',
short: 'feat',
value: 'feat'
},
{
name: 'fix: A bug fix',
short: 'fix',
value: 'fix'
},
{
name: 'docs: Documentation only changes',
short: 'docs',
value: 'docs'
},
{
name:
'style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)',
short: 'style',
value: 'style'
},
{
name: 'refactor: A code change that neither fixes a bug nor adds a feature',
short: 'refactor',
value: 'refactor'
},
{
name: 'perf: A code change that improves performance',
short: 'perf',
value: 'perf'
},
{
name: 'test: Adding missing tests or correcting existing tests',
short: 'test',
value: 'test'
},
{
name:
'build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)',
short: 'build',
value: 'build'
},
{
name:
'ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)',
short: 'ci',
value: 'ci'
},
{
name: "chore: Other changes that don't modify src or test files",
short: 'chore',
value: 'chore'
},
{
name: 'revert: Reverts a previous commit',
short: 'revert',
value: 'revert'
}
]);
});

describe('should return choices if type-enum exits', () => {
const result = choicesFactory({ 'type-enum': [Level.Error, 'always', ['foo', 'bar', 'baz']] }, {});

expect(result).toEqual([
{
name: 'foo ',
name: 'foo: ',
short: 'foo',
value: 'foo'
},
{
name: 'bar ',
name: 'bar: ',
short: 'bar',
value: 'bar'
},
{
name: 'baz ',
name: 'baz: ',
short: 'baz',
value: 'baz'
}
Expand All @@ -75,17 +136,17 @@ describe('type-maker', () => {

expect(result).toEqual([
{
name: 'foo Fooey',
name: 'foo: Fooey',
short: 'foo',
value: 'foo'
},
{
name: 'bar Barey',
name: 'bar: Barey',
short: 'bar',
value: 'bar'
},
{
name: 'baz Bazey',
name: 'baz: Bazey',
short: 'baz',
value: 'baz'
}
Expand All @@ -105,22 +166,22 @@ describe('type-maker', () => {

expect(result).toEqual([
{
name: 'foo Fooey',
name: 'foo : Fooey',
short: 'foo',
value: 'foo'
},
{
name: 'bar Barey',
name: 'bar : Barey',
short: 'bar',
value: 'bar'
},
{
name: 'baz Bazey',
name: 'baz : Bazey',
short: 'baz',
value: 'baz'
},
{
name: 'very-long Longey',
name: 'very-long: Longey',
short: 'very-long',
value: 'very-long'
}
Expand Down
11 changes: 9 additions & 2 deletions src/prompts/type-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ export function choicesFactory(rules: Rules, commitTypes: CommitType) {
if (typeEnum && typeEnum.length > 0) {
const longest = getLongest(typeEnum);
choices = typeEnum.map(value => ({
name: `${value.padEnd(longest)} ${commitTypes[value]?.description ?? ''}`,
name: `${value.padEnd(longest)}: ${commitTypes[value]?.description ?? ''}`,
value: value,
short: value
}));
}

return choices;
return (
choices ||
Object.keys(commitTypes).map(commitType => ({
name: `${commitType}: ${commitTypes[commitType].description ?? ''}`,
value: commitType,
short: commitType
}))
);
}

export function typeMaker(questions: Question[], rules: Rules): Question[] {
Expand Down