diff --git a/lib/funcs/commit-gen.js b/lib/funcs/commit-gen.js index f671868..a65a5e4 100644 --- a/lib/funcs/commit-gen.js +++ b/lib/funcs/commit-gen.js @@ -11,10 +11,7 @@ const suggestCommitMsgCb = (conf) => { const issue_number = conf.current_issue.number; const commitGuidelines = conf.commit_guidelines; const customBoolean = conf.custom_guidelines; - const commitTypeContent = conf.selected_commit_type - .match(/([A-Za-z\s\-]+)\w+/g) - .join() - .trim(); + const commitTypeContent = conf.selected_commit_type.match(/([A-Za-z\s\-]+)\w+/g).join().trim(); const emojiBool = conf.use_emojis; var commitTypeEmoji; var commitGuideNum; @@ -22,145 +19,181 @@ const suggestCommitMsgCb = (conf) => { var commitPrefix; const messageBlock = { - "Initial commit": { - emoji: conf.emojis.initial_commit, - prefix: "", - }, - "Adding a new user-facing feature": { - emoji: conf.emojis.feature, - prefix: "feat", - }, - "Improving UI": { - emoji: conf.emojis.ui, - prefix: "feat", - }, - "Refactoring or improving code": { - emoji: conf.emojis.code_quality, - prefix: "chore", - }, - "Improving performance": { - emoji: conf.emojis.security, - prefix: "chore", - }, - "Updating configs": { - emoji: conf.emojis.config, - prefix: "chore", - }, - "Improving accessibility": { - emoji: conf.emojis.accessibility, - prefix: "chore", - }, - "Improving dev tools": { - emoji: conf.emojis.dev_tools, - prefix: "feat", - }, - "Writing docs": { - emoji: conf.emojis.docs, - prefix: "chore", - }, - "New release": { - emoji: conf.emojis.release, - prefix: "chore", - }, - "Fixing a bug": { - emoji: conf.emojis.bug_fix, - prefix: "fix", - }, - "Fixing a crash": { - emoji: conf.emojis.crash, - prefix: "fix", - }, - "Removing code/files": { - emoji: conf.emojis.cleanup, - prefix: "chore", - }, - WIP: { - emoji: conf.emojis.wip, - prefix: "chore", - }, - }; + "Initial commit" : + { + emoji : conf.emojis.initial_commit, + prefix : '' + }, + "Adding a new user-facing feature" : + { + emoji : conf.emojis.feature, + prefix : 'feat' + }, + "Improving UI" : + { + emoji : conf.emojis.ui, + prefix : 'feat' + }, + "Refactoring or improving code" : + { + emoji : conf.emojis.code_quality, + prefix : 'chore' + }, + "Improving performance" : + { + emoji : conf.emojis.security, + prefix : 'chore' + }, + "Updating configs" : + { + emoji : conf.emojis.config, + prefix : 'chore' + }, + "Improving accessibility" : + { + emoji : conf.emojis.accessibility, + prefix : 'chore' + }, + "Improving dev tools" : + { + emoji : conf.emojis.dev_tools, + prefix : 'feat' + }, + "Writing docs" : + { + emoji : conf.emojis.docs, + prefix : 'chore' + }, + "New release" : + { + emoji : conf.emojis.release, + prefix : 'chore' + }, + "Fixing a bug" : + { + emoji : conf.emojis.bug_fix, + prefix : 'fix' + }, + "Fixing a crash" : + { + emoji : conf.emojis.crash, + prefix : 'fix' + }, + "Removing code/files" : + { + emoji : conf.emojis.cleanup, + prefix : 'chore' + }, + "WIP" : + { + emoji : conf.emojis.wip, + prefix : 'chore' + }, + } var details = messageBlock[commitTypeContent]; commitPrefix = details.prefix; - commitTypeEmoji = emoji.get(":" + details.emoji + ":"); + commitTypeEmoji = emoji.get(':'+details.emoji+':'); - switch (commitGuidelines[0]) { - case `fix:`: - commitGuideNum = 1; - break; - case `fix #`: - commitGuideNum = 2; + switch(commitGuidelines[0]) + { + case `fix:`: + commitGuideNum = 1; 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) { - case 1: - { - if (emojiBool == true) { - commitMsg = `${commitTypeEmoji} ${commitPrefix}: ${issue_title}`; - conf.current_commit_message = commitMsg; - } else { - commitMsg = `${commitPrefix}: ${issue_title}`; - conf.current_commit_message = commitMsg; + if (customBoolean == false) + { + switch(commitGuideNum) + { + case 1: + { + if (emojiBool == true) + { + commitMsg = `${commitTypeEmoji} ${commitPrefix}: ${issue_title}`; + conf.current_commit_message = commitMsg; + + } + else + { + commitMsg = `${commitPrefix}: ${issue_title}`; + conf.current_commit_message = commitMsg; + + } + } + break; + case 2: + { + if (emojiBool == true) + { + commitMsg = `${commitTypeEmoji} fix #${issue_number} ${issue_title}`; + conf.current_commit_message = commitMsg; + + } + else + { + commitMsg = `fix #${issue_number} ${issue_title}`; + conf.current_commit_message = commitMsg; + + } + } + break; + case 3: + { + 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 + { + var customPrefix = issue_labels.length>0? issue_labels[0]+":": ''; + if (emojiBool == true) + { + commitMsg = `${commitTypeEmoji} ${customPrefix} ${issue_title}`; + conf.current_commit_message = commitMsg; + } - } - break; - - case 2: - { - if (emojiBool == true) { - commitMsg = `${commitTypeEmoji} fix #${issue_number} ${issue_title}`; - conf.current_commit_message = commitMsg; - } else { - commitMsg = `fix #${issue_number} ${issue_title}`; - conf.current_commit_message = commitMsg; + else + { + commitMsg = `${customPrefix} ${issue_title}`; + conf.current_commit_message = commitMsg; + } - } - break; - case 3: { - 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 { - var customPrefix = issue_labels.length > 0 ? issue_labels[0] + ":" : ""; - if (emojiBool == true) { - commitMsg = `${commitTypeEmoji} ${customPrefix} ${issue_title}`; - conf.current_commit_message = commitMsg; - } else { - commitMsg = `${customPrefix} ${issue_title}`; - conf.current_commit_message = commitMsg; - } } fs.writeFile("./.gitgo", JSON.stringify(conf, null, 2), (err) => { if (err) console.log("Error writing file:", err); }); - return commitMsg; -}; +} module.exports = { - suggestCommitMsg: () => { - setTimeout(() => { - reader.jsonReader("./.gitgo", (err, conf) => { - if (err) { - console.log(err); - return; - } - const retVal = suggestCommitMsgCb(conf); - }); - }, 4000); + suggestCommitMsg:() => { + setTimeout(()=>{ + reader.jsonReader('./.gitgo', (err, conf) => { + if (err) { + console.log(err) + return + } + const retVal = suggestCommitMsgCb(conf); + }) + }, 6000); }, - suggestCommitMsgCb, -}; + suggestCommitMsgCb +} diff --git a/lib/funcs/generate.js b/lib/funcs/generate.js index 753d6b0..1f61602 100644 --- a/lib/funcs/generate.js +++ b/lib/funcs/generate.js @@ -5,69 +5,79 @@ var retext = require("retext"); var pos = require("retext-pos"); var keywords = require("retext-keywords"); var toString = require("nlcst-to-string"); -var branch_names = []; -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)); - }); +const suggestBranchNmCb = (conf) => +{ + var branch_names = []; + issue_title = conf.current_issue.title; + retext().use(pos).use(keywords).process(conf.current_issue.title, done); - var key_phrases = []; - file.data.keyphrases.forEach(function (phrase) { - key_phrases.push(phrase.matches[0].nodes.map(stringify).join("")); + 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 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]); + } + } + 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); + }); + }); + } + } + return branch_names; +} - for (var item = 0; item < branch_names.length; item++) { - if (conf.existing_branches.includes(branch_names[item])) { - branch_names.pop(branch_names[item]); + +module.exports = { + branchName:() => { + setTimeout(()=>{ + reader.jsonReader('./.gitgo', (err, conf) => { + if (err) { + console.log(err) + return } - } - 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); - }); - }); - } - } - }); + const retVal = suggestBranchNmCb(conf); + }) + }, 6000); }, -}; + suggestBranchNmCb +} diff --git a/tests/generate.test.js b/tests/generate.test.js new file mode 100644 index 0000000..b30cb72 --- /dev/null +++ b/tests/generate.test.js @@ -0,0 +1,11 @@ +const reader = require("../lib/funcs/jsonReader"); +const {suggestBranchNmCb} = require('../lib/funcs/generate') + +test('Suggests Commit Message Callback', () => { + reader.jsonReader('../.gitgo', (err, conf) => { + if (err) { + return + } + expect(suggestBranchNmCb(conf).not.toBe(undefined)) + }) +}) \ No newline at end of file