Skip to content

Commit

Permalink
feat: ✨ add command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
JsonMa committed Nov 25, 2021
1 parent 04d5948 commit bafc956
Show file tree
Hide file tree
Showing 6 changed files with 3,300 additions and 34 deletions.
44 changes: 44 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node

'use strict';

const branchName = require('current-git-branch');
const { Command, Option } = require('commander');
const path = require('path');
const {
validateBranchName,
} = require('./lib/validateBranchName');
const currentBranchName = branchName();
const SUCCESS_CODE = 0;
const FAILED_CODE = 1;
// const REGEXP = [
// '^(master|develop){1}$|^(feature|fix|hotfix|release)\/.+$',
// '(feature|release|hotfix)\/(JIRA-\d+',
// '(feature|release|hotfix)\/(JIRA-\d+\/)?[a-z-]+',
// '^(feature|fix|hotfix|release)\/.+',
// ];
const pkgJson = require(path.join(__dirname, './package.json'));
const program = new Command();
program.description('Git branch name validate tool');
program.version(pkgJson.version, '-v, --version', 'validate-branch-name current version');
program.option('-t, --test <branch>', 'test target branch, using current git brach by default');
program.addOption(new Option('-r, --regexp <regexp>', 'choose regular expression to test branch name'));
program.parse(process.argv);

const options = program.opts();
const branch = options.test || currentBranchName;

// validate whether it is a git repository
if (!options.branch && !currentBranchName) {
console.error('\x1b[31m%s\x1b[0m', 'Error: not a git repository\n');
process.exitCode = FAILED_CODE;
return;
}
try {
console.log(options.regexp);
const result = validateBranchName(branch, options.regexp);
process.exitCode = result ? SUCCESS_CODE : FAILED_CODE;
} catch (error) {
console.error('\x1b[31m%s\x1b[0m', error.message + '\n');
process.exitCode = FAILED_CODE;
}
27 changes: 5 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
#!/usr/bin/env node

'use strict';

const branchName = require('current-git-branch');
const {
validateBranchName,
} = require('./lib/validateBranchName');
const currentBranchName = branchName();
const SUCCESS_CODE = 0;
const FAILED_CODE = 1;
const { validateBranchName } = require('./lib/validateBranchName');

// validate whether it is a git repository
if (!currentBranchName) {
console.error('\x1b[31m%s\x1b[0m', 'Error: not a git repository\n');
process.exitCode = FAILED_CODE;
return;
}
try {
const result = validateBranchName(currentBranchName);
process.exitCode = result ? SUCCESS_CODE : FAILED_CODE;
} catch (error) {
console.error('\x1b[31m%s\x1b[0m', error.message + '\n');
process.exitCode = FAILED_CODE;
}
module.exports = branch => {
const branchName = branch || require('current-git-branch');
return validateBranchName(branchName, null, false);
};
32 changes: 22 additions & 10 deletions lib/validateBranchName.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,34 @@ const {
* validate branch name
*
* @param {String} branchName - branch name
* @param {String} pattern - pattern
* @param {Bollean} showLog - whether to show console log
* @return {Boolean} result
*/
function validateBranchName(branchName) {
function validateBranchName(branchName, pattern, showLog = true) {
const {
pattern,
pattern: configPattern,
errorMsg,
} = getConf();
const validBranchNameRegExp = new RegExp(pattern, 'g');
const validBranchNameRegExp = new RegExp(pattern || configPattern, 'g');
const result = validBranchNameRegExp.test(branchName);
if (!result) {
console.error(
'\x1b[31m%s\x1b[0m',
`${errorMsg} \n` +
`Branch Name: "${branchName}" \n` +
`Pattern:"${validBranchNameRegExp.toString()}" \n`
);
if (showLog) {
if (!result) {
console.error(
'\x1b[31m%s\x1b[0m',
'Result: "failed" \n' +
`Error Msg: ${errorMsg} \n` +
`Branch Name: "${branchName}" \n` +
`Pattern:"${validBranchNameRegExp.toString()}" \n`
);
} else {
console.info(
'\x1b[32m%s\x1b[0m',
'Result: "passed"\n' +
`Branch Name: "${branchName}" \n` +
`Pattern:"${validBranchNameRegExp.toString()}" \n`
);
}
}
return result;
}
Expand Down
Loading

0 comments on commit bafc956

Please sign in to comment.