Skip to content
Open
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
4 changes: 3 additions & 1 deletion .git-cz.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"disableEmoji": false,
"format": "{type}{scope}: {emoji}{subject}"
"format": "{type}{scope}: {emoji}{subject}",
"customScopeInput": true,
"branchScopePattern": "NFS-\\d+"
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ module.exports = {
format: '{type}{scope}: {emoji}{subject}',
list: ['test', 'feat', 'fix', 'chore', 'docs', 'refactor', 'style', 'ci', 'perf'],
maxMessageLength: 64,
maxScopeLength: 10,
minMessageLength: 3,
questions: ['type', 'scope', 'subject', 'body', 'breaking', 'issues', 'lerna'],
scopes: [],
customScopeInput: true
types: {
chore: {
description: 'Build process or auxiliary tool changes',
Expand Down
27 changes: 27 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ const main = async () => {
}
}

const branchScope = (() => {
const pattern = state && state.config && state.config.branchScopePattern;

if (!pattern) return undefined;

const rx = typeof pattern === 'string' ? new RegExp('(' + pattern + ')', 'i') : pattern;

try {
const devNull = process.platform === 'win32' ? ' nul' : '/dev/null';
const branch = execSync('git rev-parse --abbrev-ref HEAD 2>' + devNull)
.toString()
.trim();

const m = branch.match(rx);
return m ? String(m[1]).toUpperCase() : undefined;
} catch (e) {
return undefined;
}
})();

if (branchScope && !cliAnswers.scope) {
cliAnswers.scope = branchScope;
if (state && state.answers) {
state.answers.scope = branchScope;
}
}

if (cliOptions.nonInteractive) {
await runNonInteractiveMode(state, cliAnswers);
} else {
Expand Down
5 changes: 5 additions & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const list = [
// https://github.com/angular/angular/blob/master/CONTRIBUTING.md#scope
const scopes = [];

const customScopeInput = true;

const questions = [
'type',
'scope',
Expand All @@ -86,8 +88,11 @@ module.exports = {
format,
list,
maxMessageLength: 64,
maxScopeLength: 10,
minMessageLength: 3,
customScopeInput,
questions,
scopes,
branchScopePattern: null,
types
};
68 changes: 53 additions & 15 deletions lib/questions/scope.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fuzzy = require('fuzzy');
const { execSync } = require('child_process');

/**
* Searches for the scopes containing the given substring.
Expand All @@ -7,30 +8,67 @@ const fuzzy = require('fuzzy');
* @param {string[]} scopes Scopes list.
*/
const findScope = function (substring, scopes) {
return Promise.resolve(fuzzy.filter(substring || '', scopes).map(({original: scope}) => scope));
return Promise.resolve(fuzzy.filter(substring || '', scopes).map(({ original: scope }) => scope));
};

exports.createQuestion = (state) => {
const {scopes} = state.config;
const { scopes, customScopeInput } = state.config;

if (!scopes) {
return null;
}
const branchScope = (() => {
const pattern = state && state.config && state.config.branchScopePattern;

if (!Array.isArray(scopes)) {
throw new TypeError('scopes must be an array of strings.');
}
if (!pattern) return undefined;

if (scopes.length < 1) {
return null;
}
const rx = typeof pattern === 'string' ? new RegExp('(' + pattern + ')', 'i') : pattern;

const question = {
message: 'Select the scope this component affects:',
try {
const devNull = process.platform === 'win32' ? ' nul' : '/dev/null';
const branch = execSync('git rev-parse --abbrev-ref HEAD 2>' + devNull)
.toString()
.trim();

const m = branch.match(rx);
return m ? String(m[1]).toUpperCase() : undefined;
} catch (e) {
return undefined;
}
})();

let question = {
name: 'scope',
source: (_answers, input) => findScope(input, scopes),
type: 'autocomplete'
};

if (!customScopeInput) {
if (!scopes) {
return null;
}

if (!Array.isArray(scopes)) {
throw new TypeError('scopes must be an array of strings.');
}

if (scopes.length < 1) {
return null;
}

question = {
...question,
message: 'Select the scope this component affects:',
// If we found a scope in the branch name, use it as the default selection
default: branchScope,
source: (_answers, input) => findScope(input, scopes),
type: 'autocomplete'
}
}
else {
question = {
...question,
message: 'Specify the scope this component affects',
type: 'limitedInput',
maxLength: state.config.maxScopeLength,
default: branchScope,
}
}

return question;
};
6 changes: 4 additions & 2 deletions lib/questions/subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ exports.createQuestion = (state) => {
},
leadingLabel: (answers) => {
let scope = '';

const effectiveScope = (answers && answers.scope) || state.answers.scope;

if (answers.scope && answers.scope !== 'none') {
scope = `(${answers.scope})`;
if (effectiveScope && effectiveScope !== 'none') {
scope = `(${effectiveScope})`;
}

return `${state.answers.type || answers.type}${scope}:`;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"eslint-plugin-filenames": "1.3.2",
"eslint-plugin-import": "2.31.0",
"eslint-plugin-jest": "27.9.0",
"eslint-plugin-jsdoc": "41.1.2",
"eslint-plugin-jsdoc": "^56.1.2",
"eslint-plugin-no-use-extend-native": "0.5.0",
"eslint-plugin-promise": "6.6.0",
"eslint-plugin-unicorn": "23.0.0",
Expand Down
22 changes: 21 additions & 1 deletion test/formatCommitMessage.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable sort-keys */
const {expect} = require('chai');
const { expect } = require('chai');
const formatCommitMessage = require('../lib/formatCommitMessage');

const defaultConfig = {
Expand All @@ -11,9 +11,12 @@ const defaultConfig = {
commitMessageFormat: '<type><(scope)>: <emoji><subject>',
list: ['test', 'feat', 'fix', 'chore', 'docs', 'refactor', 'style', 'ci', 'perf'],
maxMessageLength: 64,
maxScopeLength: 10,
minMessageLength: 3,
questions: ['type', 'scope', 'subject', 'body', 'breaking', 'issues', 'lerna'],
scopes: [],
customScopeInput: false,
branchScopePattern: null,
types: {
chore: {
description: 'Build process or auxiliary tool changes',
Expand Down Expand Up @@ -145,4 +148,21 @@ describe('formatCommitMessage()', () => {

expect(message).equal('First commit :(init)feat [skip ci]');
});

it('does include custom scope, if custom scope enable in config', () => {
const message = formatCommitMessage({
...defaultState,
answers: {
...defaultState.answers,
scope: 'custom-scope'
},
config: {
...defaultConfig,
disableEmoji: true,
customScopeInput: true
}
});

expect(message).equal('feat(custom-scope): First commit');
});
});
Loading