diff --git a/.gitgo b/.gitgo index 8f0b165..41c9e19 100644 --- a/.gitgo +++ b/.gitgo @@ -6,11 +6,11 @@ }, "commit_guidelines": [ "fix:", - " feat:", - " chore:" + "feat:", + "chore:" ], "custom_guidelines": false, - "selected_commit_type": "", + "selected_commit_type": "✨ Adding a new user-facing feature", "emojis": { "initial_commit": "tada", "feature": "sparkles", diff --git a/bin/index.js b/bin/index.js index 13c8e0f..28b7f0b 100755 --- a/bin/index.js +++ b/bin/index.js @@ -5,7 +5,7 @@ const figlet = require('figlet'); const cowsay = require('cowsay'); const files = require('../lib/files.js'); const program = require('commander'); -const { getQuestions, getConfigQuestions } = require('../lib/inquirer.js'); +const { getQuestions, getConfigQuestions, displaySuggestions } = require('../lib/inquirer.js'); clear(); @@ -55,4 +55,27 @@ program } }); +program +.command('display') +.alias('d') +.action(function () { + // displays Gitg0 on start + if (files.directoryExists('.git')) { + console.log(figlet.textSync('Gitg0', { + horizontalLayout: 'default', + verticalLayout: 'default', + }), '\n'); + // asks task based questions + displaySuggestions(); + } else { + // checks if the directory is a git based repo or not + console.log(cowsay.say({ + text: 'Not a git repository!', + T: 'U ' + } + )); + process.exit(); + } +}); + program.parse(process.argv); diff --git a/lib/funcs/commit-gen.js b/lib/funcs/commit-gen.js index 74415f0..8a55629 100644 --- a/lib/funcs/commit-gen.js +++ b/lib/funcs/commit-gen.js @@ -1,211 +1,209 @@ const emoji = require('node-emoji'); - +const fs = require('fs'); const reader = require('./jsonReader'); stopwords = ['a', 'the']; -function suggestCommitMsg() { - reader.jsonReader('../../.gitgo', (err, conf) => { - if (err) { - console.log(err) - return - } - - var issue_labels = conf.current_issue.labels; - var issue_title = conf.current_issue.title; - var issue_number = conf.current_issue.number; - var commitGuidelines = conf.commit_guidelines; - var customBoolean = conf. custom_guidelines; - var commitTypeContent = String(conf.selected_commit_type.match(/([A-Za-z\s\-]+)\w+/g)).trim(); - var commitTypeEmoji; - var emojiBool = conf.use_emojis; - var commitGuideNum; - var commitMsg; - var prefix; +module.exports = { + suggestCommitMsg:() => { + reader.jsonReader('./.gitgo', (err, conf) => { + if (err) { + console.log(err) + return + } + var issue_labels = conf.current_issue.labels; + var issue_title = conf.current_issue.title; + var issue_number = conf.current_issue.number; + var commitGuidelines = conf.commit_guidelines; + var customBoolean = conf. custom_guidelines; + var commitTypeContent = String(conf.selected_commit_type.match(/([A-Za-z\s\-]+)\w+/g)).trim(); + var commitTypeEmoji; + var emojiBool = conf.use_emojis; + var commitGuideNum; + var commitMsg; + var prefix; + switch(commitTypeContent) + { + case 'Initial commit': + { + commitTypeEmoji = conf.emojis.initial_commit; + } + break; + case 'Adding a new user-facing feature': + { + commitTypeEmoji = conf.emojis.feature; + prefix = 'feat'; + } + break; + case 'Improving UI': + { + commitTypeEmoji = conf.emojis.ui; + prefix = 'feat'; + } + break; + case 'Refactoring or improving code': + { + commitTypeEmoji = conf.emojis.code_quality; + prefix = 'chore'; + } + break; + case 'Improving performance': + { + commitTypeEmoji = conf.emojis.performance; + prefix = 'chore'; + } + break; + case 'Improving security': + { + commitTypeEmoji = conf.emojis.security; + prefix = 'chore'; + } + break; + case 'Updating configs': + { + commitTypeEmoji = conf.emojis.config; + prefix = 'chore'; + } + break; + case 'Improving accessibility': + { + commitTypeEmoji = conf.emojis.accessibility; + prefix = 'chore'; + } + break; + case 'Improving dev tools': + { + commitTypeEmoji = conf.emojis.dev_tools; + prefix = 'feat'; + } + break; + case 'Writing docs': + { + commitTypeEmoji = conf.emojis.docs; + prefix = 'chore'; + } + break; + case 'New release': + { + commitTypeEmoji = conf.emojis.release; + prefix = 'chore'; + } + break; + case 'Fixing a bug': + { + commitTypeEmoji = conf.emojis.bug_fix; + prefix = 'fix'; + } + break; + case 'Fixing a crash': + { + commitTypeEmoji = conf.emojis.crash; + prefix = 'fix'; + } + break; + case 'Removing code/files': + { + commitTypeEmoji = conf.emojis.cleanup; + prefix = 'chore'; + } + break; + case 'WIP': + { + commitTypeEmoji = conf.emojis.wip; + prefix = 'chore'; + } + break; + // default: + // { + // commitTypeEmoji = conf.emojis.feature; + // } + + } + commitTypeEmoji = emoji.get(':'+commitTypeEmoji+':'); - - switch(commitTypeContent) - { - case 'Initial commit': - { - commitTypeEmoji = conf.emojis.initial_commit; - } - break; - case 'Adding a new user-facing feature': - { - commitTypeEmoji = conf.emojis.feature; - prefix = 'feat'; - } - break; - case 'Improving UI': - { - commitTypeEmoji = conf.emojis.ui; - prefix = 'feat'; - } - break; - case 'Refactoring or improving code': - { - commitTypeEmoji = conf.emojis.code_quality; - prefix = 'chore'; - } - break; - case 'Improving performance': - { - commitTypeEmoji = conf.emojis.performance; - prefix = 'chore'; - } - break; - case 'Improving security': - { - commitTypeEmoji = conf.emojis.security; - prefix = 'chore'; - } - break; - case 'Updating configs': - { - commitTypeEmoji = conf.emojis.config; - prefix = 'chore'; - } - break; - case 'Improving accessibility': - { - commitTypeEmoji = conf.emojis.accessibility; - prefix = 'chore'; - } - break; - case 'Improving dev tools': - { - commitTypeEmoji = conf.emojis.dev_tools; - prefix = 'feat'; - } - break; - case 'Writing docs': - { - commitTypeEmoji = conf.emojis.docs; - prefix = 'chore'; - } - break; - case 'New release': - { - commitTypeEmoji = conf.emojis.release; - prefix = 'chore'; - } - break; - case 'Fixing a bug': - { - commitTypeEmoji = conf.emojis.bug_fix; - prefix = 'fix'; - } - break; - case 'Fixing a crash': - { - commitTypeEmoji = conf.emojis.crash; - prefix = 'fix'; - } - break; - case 'Removing code/files': - { - commitTypeEmoji = conf.emojis.cleanup; - prefix = 'chore'; - } - break; - case 'WIP': - { - commitTypeEmoji = conf.emojis.wip; - prefix = 'chore'; - } + switch(commitGuidelines[0]) + { + case `fix:`: + commitGuideNum = 1; break; - // default: - // { - // commitTypeEmoji = conf.emojis.feature; - // } - - } - commitTypeEmoji = emoji.get(':'+commitTypeEmoji+':'); - - switch(commitGuidelines[0]) - { - case `fix:`: - commitGuideNum = 1; - break; - case `fix #`: - commitGuideNum = 2; - break; - case `...(fix #)`: - commitGuideNum = 3; + case `fix #`: + commitGuideNum = 2; break; - default: commitGuideNum = 4; - } + case `...(fix #)`: + commitGuideNum = 3; + break; + default: commitGuideNum = 4; + } - if (customBoolean == false) - { - switch(commitGuideNum) + if (customBoolean == false) { - case 1: - { - if (emojiBool == true) - { - commitMsg = `${commitTypeEmoji} ${prefix}: ${issue_title}`; - console.log(commitMsg); - } - else + switch(commitGuideNum) + { + case 1: { - commitMsg = `${prefix}: ${issue_title}`; - console.log(commitMsg); - } + if (emojiBool == true) + { + commitMsg = `${commitTypeEmoji} ${prefix}: ${issue_title}`; + conf.current_commit_message = commitMsg; + } + else + { + commitMsg = `${prefix}: ${issue_title}`; + conf.current_commit_message = commitMsg; + } - } - break; + } + break; - case 2: - { - if (emojiBool == true) + case 2: { - commitMsg = `${commitTypeEmoji} fix #${issue_number}`; - console.log(commitMsg); + if (emojiBool == true) + { + commitMsg = `${commitTypeEmoji} fix #${issue_number}`; + conf.current_commit_message = commitMsg; + } + else + { + commitMsg = `fix #${issue_number}`; + conf.current_commit_message = commitMsg; + } } - else + break; + case 3: { - commitMsg = `fix #${issue_number}`; - console.log(commitMsg); + if (emojiBool == true) + { + commitMsg = `${commitTypeEmoji} ${issue_title} fix #${issue_number}`; + conf.current_commit_message = commitMsg; + } + else + { + commitMsg = `${issue_title} fix #${issue_number}`; + conf.current_commit_message = commitMsg; + } } + } + } + else + { + prefix = issue_labels.length>0? issue_labels[0]+":": ''; + if (emojiBool == true) + { + commitMsg = `${commitTypeEmoji} ${prefix} ${issue_title}`; + conf.current_commit_message = commitMsg; } - break; - case 3: + else { - if (emojiBool == true) - { - commitMsg = `${commitTypeEmoji} ${issue_title} fix #${issue_number}`; - console.log(commitMsg); - } - else - { - commitMsg = `${issue_title} fix #${issue_number}`; - console.log(commitMsg); - } + commitMsg = `${prefix} ${issue_title}`; + conf.current_commit_message = commitMsg; } + } - } - else - { - prefix = issue_labels.length>0? issue_labels[0]+":": ''; - if (emojiBool == true) - { - commitMsg = `${commitTypeEmoji} ${prefix} ${issue_title}`; - console.log(commitMsg); - } - else - { - commitMsg = `${prefix} ${issue_title}`; - console.log(commitMsg); - } - - } - - }); -}; - - -suggestCommitMsg() \ No newline at end of file + fs.writeFile('./.gitgo', JSON.stringify(conf, null, 2), (err) => { + if (err) console.log('Error writing file:', err) + }) + }); + } +} diff --git a/lib/funcs/generate.js b/lib/funcs/generate.js index df8dca3..963b30d 100644 --- a/lib/funcs/generate.js +++ b/lib/funcs/generate.js @@ -1,66 +1,74 @@ const reader = require('./jsonReader'); const cowsay = require('cowsay'); - +const fs = require('fs'); var retext = require('retext'); var pos = require('retext-pos'); var keywords = require('retext-keywords'); var toString = require('nlcst-to-string'); var branch_names = []; -function branchName() { - reader.jsonReader('./.gitgo', (err, conf) => { - if (err) { - console.log(err) - return - } - issue_title = conf.current_issue.title; - retext() - .use(pos) - .use(keywords) - .process(conf.current_issue.title, done) +module.exports = { + branchName:() => { + reader.jsonReader('./.gitgo', (err, conf) => { + if (err) { + console.log(err) + return + } + issue_title = conf.current_issue.title; + retext() + .use(pos) + .use(keywords) + .process(conf.current_issue.title, done) - function done(err, file) { - if (err) throw err; - var keywords_list = [] - file.data.keywords.forEach(function (keyword) { - keywords_list.push(toString(keyword.matches[0].node)) - }) + function done(err, file) { + if (err) throw err; + var keywords_list = [] + file.data.keywords.forEach(function (keyword) { + keywords_list.push(toString(keyword.matches[0].node)) + }) - var key_phrases = [] - file.data.keyphrases.forEach(function (phrase) { - key_phrases.push(phrase.matches[0].nodes.map(stringify).join('')); - function stringify(value) { - return toString(value) - }; + var key_phrases = [] + file.data.keyphrases.forEach(function (phrase) { + key_phrases.push(phrase.matches[0].nodes.map(stringify).join('')); + function stringify(value) { + return toString(value) + }; - key_phrases.forEach(function (phrase) { - phrase = phrase.split(" ").join("-").toLowerCase(); - branch_names.push(phrase); - }); - }) + key_phrases.forEach(function (phrase) { + phrase = phrase.split(" ").join("-").toLowerCase(); + branch_names.push(phrase); + }); + }) - if (branch_names.length == 0) { - keyword_bn = keywords_list.join("-"); - branch_names.push(keyword_bn); - } + if (branch_names.length == 0) { + keyword_bn = keywords_list.join("-"); + branch_names.push(keyword_bn); + } - for (var item = 0; item < branch_names.length; item++) { - if (conf.existing_branches.includes(branch_names[item])) { - branch_names.pop(branch_names[item]) + for (var item = 0; item < branch_names.length; item++) { + if (conf.existing_branches.includes(branch_names[item])) { + branch_names.pop(branch_names[item]) + } } - } - if (branch_names.length === 0) { - console.log(cowsay.say({ - text: "Couldn't figure out a branch name for you :(", - T: 'U ' + if (branch_names.length === 0) { + console.log(cowsay.say({ + text: "Couldn't figure out a branch name for you :(", + T: 'U ' + } + )) + } else { + reader.jsonReader('./.gitgo', (err, conf) => { + if (err) { + console.log('Error reading file:', err) + return + } + conf.current_branch = branch_names; + fs.writeFile('./.gitgo', JSON.stringify(conf, null, 2), (err) => { + if (err) console.log('Error writing file:', err) + }) + }); } - )) - } else { - console.log("Suggested branch name") - console.log(branch_names); } - } - }); + }); + } } - -branchName(); diff --git a/lib/inquirer.js b/lib/inquirer.js index ce35561..fe811e6 100644 --- a/lib/inquirer.js +++ b/lib/inquirer.js @@ -172,5 +172,58 @@ module.exports = { // Something else went wrong } }); - } + }, + + displaySuggestions: async () => { + jsonReader('./.gitgo', (err, conf) => { + if (err) { + console.log('Error reading file:', err) + return + } + branch = conf['current_branch']; + commitMsg = conf['current_commit_message']; + suggestedBranch = "Suggested branch name(press enter to continue or type an alternate): " + branch + "\n"; + suggestedCommitMsg = "Suggested commit message(press enter to continue or \"e\" to type an alternate): " + commitMsg + "\n"; + + inquirer.prompt([ + { + type: 'string', message: suggestedBranch, name: 'branch' + } + ]) + .then(answers => { + if (answers['branch'] != "") { + conf.current_branch = answers['branch']; + } + inquirer.prompt([{ + type: 'string', message: suggestedCommitMsg, name: 'commit' + }]).then( + ans => { + if (ans['commit'] === "e") { + inquirer.prompt([{ + type: 'editor', message: commitMsg + "\n", name: 'commitCustom' + }]).then( + ans2 => { + console.log(ans2['commitCustom']); + conf.current_commit_message = ans2.commitCustom; + fs.writeFile('./.gitgo', JSON.stringify(conf, null, 2), (err) => { + if (err) console.log('Error writing file:', err) + }) + } + ) + } + fs.writeFile('./.gitgo', JSON.stringify(conf, null, 2), (err) => { + if (err) console.log('Error writing file:', err) + }) + } + ) + }) + .catch(error => { + if (error.isTtyError) { + // Prompt couldn't render in the current environment + } else { + // Something else went wrong + } + }); + }) + }, }; diff --git a/lib/issue.js b/lib/issue.js index a148c79..2260138 100644 --- a/lib/issue.js +++ b/lib/issue.js @@ -3,6 +3,8 @@ const gitRemoteOriginUrl = require('git-remote-origin-url'); const reader = require('./funcs/jsonReader'); var emoji = require('node-emoji'); const chalk = require('chalk'); +const { suggestCommitMsg } = require('./funcs/commit-gen'); +const { branchName } = require('./funcs/generate'); var titleOfIssue; let labelsOfIssue = []; const fs = require('fs'); @@ -54,14 +56,8 @@ module.exports = { }) }) setTimeout(function () { - reader.jsonReader('./.gitgo', (err, conf) => { - if (err) { - console.log(err) - return - } - // display current issue details - console.log(conf.current_issue) - }); + suggestCommitMsg(); + branchName(); }, 1000); }); }