conventional-commits-cli
Version:
A CLI that enables automatic Conventional Git commits
99 lines (79 loc) • 3.12 kB
JavaScript
const chalk = require('chalk');
const boxen = require('boxen');
const yargs = require('yargs');
const shell = require('shelljs');
const prompt = require('prompt');
const inquirer = require('inquirer'); // Local Modules
const boxes = require('./deps/boxes'); // Local Consts
const types = ['build', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'style', 'test'];
const options = yargs.usage('Usage: -t <type>').option('t', {
alias: 'type',
describe: 'Commit type',
type: 'string',
demandOption: false
}).argv;
const msgBox = boxen(chalk.white.bold(`Welcome to Conventional Commits CLI!\n\nSelected type is: ${options.type}`), boxes.boxenOptions);
const errorBox = boxen(chalk.white.bold('Conventional Commits CLI\n\nERROR: wrong commit type\nSelect one of the following types:'), boxes.errorOptions); // Create a box using the boxen module
function createBox(text, box) {
if (box === 'error') {
console.log(boxen(chalk.white.bold(`CoCoCLI Error:\n${text}`), boxes.errorOptions));
} else if (box === 'msg') {
console.log(boxen(chalk.white.bold(`CoCoCLI Message:\n${text}`), boxes.boxenOptions));
}
} // Run Git Commit
function commitMaster(message, files, type) {
shell.exec(`git add ${files}`);
shell.exec(`git commit -m "${type}: ${message}"`);
shell.exec('git push origin master');
createBox(`Added ${files} and commited.`, 'msg');
}
if (!shell.which('git')) {
createBox('In order to use cococli you need to have Git installed.\nExiting...', 'error');
shell.exit(1);
}
if (types.includes(options.type)) {
console.log(msgBox);
createBox('Type the commit message & the files you want to add (seperated by spaces): ', 'msg');
prompt.start();
prompt.get(['message', 'files'], function (err, result) {
if (err) {
console.log(err);
return 1;
}
createBox('Pushing...');
createBox(`Commit Message: ${result.message}`, 'msg');
if (result.files === '*') {
console.log(' Files: ' + 'all');
commitMaster(result.message, result.files, options.type);
} else {
console.log(' Files: ' + result.files);
commitMaster(result.message, result.files, options.type);
}
});
} else {
console.log(errorBox);
inquirer.prompt([{
type: 'list',
name: 'theme',
message: 'Select the type of commit:',
choices: ['fix', 'feat', new inquirer.Separator(), 'docs', 'perf', new inquirer.Separator(), 'refactor', 'style', new inquirer.Separator(), 'test', new inquirer.Separator(), 'ci', 'build', new inquirer.Separator()]
}]).then(answers => {
prompt.start();
prompt.get(['message', 'files'], function (err, result) {
if (err) {
console.log(err);
return 1;
}
createBox('Pushing...');
createBox(`Commit Message: ${result.message}`, 'msg');
if (result.files === '*') {
console.log(' Files: ' + 'all');
commitMaster(result.message, result.files, answers.theme);
} else {
console.log(' Files: ' + result.files);
commitMaster(result.message, result.files, answers.theme);
}
});
});
}