-
Notifications
You must be signed in to change notification settings - Fork 9
/
cli.js
executable file
·37 lines (33 loc) · 1.26 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/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 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 (!branch) {
console.error('\x1b[31m%s\x1b[0m', 'Error: not a git repository\n');
process.exitCode = FAILED_CODE;
return;
}
try {
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;
}