UNPKG

@imraan-hendricks/tagmaster

Version:

Revolutionize your development workflow with TagMaster and experience bulletproof commits, accelerated releases, and seamless versioning!

3 lines (2 loc) 14.5 kB
"use strict";var e=require("readline"),n=require("child_process"),t=require("fs");function r(e,n,t,r){return new(t||(t=Promise))((function(o,c){function s(e){try{a(r.next(e))}catch(e){c(e)}}function i(e){try{a(r.throw(e))}catch(e){c(e)}}function a(e){var n;e.done?o(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(s,i)}a((r=r.apply(e,n||[])).next())}))}function o(e,n){var t,r,o,c,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return c={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function i(i){return function(a){return function(i){if(t)throw new TypeError("Generator is already executing.");for(;c&&(c=0,i[0]&&(s=0)),s;)try{if(t=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,r=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=n.call(e,s)}catch(e){i=[6,e],r=0}finally{t=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}}function c(e,n,t){if(t||2===arguments.length)for(var r,o=0,c=n.length;o<c;o++)!r&&o in n||(r||(r=Array.prototype.slice.call(n,0,o)),r[o]=n[o]);return e.concat(r||Array.prototype.slice.call(n))}function s(n){var t=e.createInterface({input:process.stdin,output:process.stdout});return new Promise((function(e){t.question(n,(function(n){t.close(),e(n)}))}))}function i(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return console.log("Write a short, imperative tense description of the change (max 94 chars):"),[4,s("Enter short description: ")];case 1:e=n.sent(),n.label=2;case 2:return e?[3,4]:(console.log("\nShort description is required!"),[4,s("Enter short description: ")]);case 3:return e=n.sent(),[3,2];case 4:return console.log(),[2,e]}}))}))}function a(e,n){return r(this,void 0,void 0,(function(){var t,r,c;return o(this,(function(o){switch(o.label){case 0:return console.log(e),n.forEach((function(e){var n=e.ref,t=e.action,r=e.description;return console.log("".concat(n,") ")+"".concat(t,": ").concat(r))})),[4,s("Enter your choice: ")];case 1:t=o.sent(),o.label=2;case 2:return r?[3,4]:(c=n.filter((function(e){return e.ref===t})),1===c.length?(r=c[0].action,[3,4]):[4,s("Please enter a valid option as your choice: ")]);case 3:return t=o.sent(),[3,2];case 4:return[2,r]}}))}))}function u(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return[4,a("Is this a breaking change?",[{ref:"y",action:"yes",description:"The change is not backwards-compatible"},{ref:"n",action:"no",description:"The change is backwards-compatible"}])];case 1:return e=n.sent(),console.log(e+"\n"),[2,e]}}))}))}function l(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return[4,a("Select a commit type:",[{ref:"1",action:"feat",description:"A new feature"},{ref:"2",action:"fix",description:"A bug fix"},{ref:"3",action:"docs",description:"Documentation changes"},{ref:"4",action:"style",description:"Code style changes"},{ref:"5",action:"refactor",description:"Code refactoring"},{ref:"6",action:"perf",description:"Performance improvements"},{ref:"7",action:"test",description:"Test related changes"},{ref:"8",action:"build",description:"Build system changes"},{ref:"9",action:"ci",description:"CI/CD related changes"},{ref:"10",action:"chore",description:"Other changes that don't modify src or test files"},{ref:"11",action:"revert",description:"Revert a previous commit"}])];case 1:return e=n.sent(),console.log(e+"\n"),[2,e]}}))}))}function f(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return[4,a("Does this change affect any open issues?",[{ref:"y",action:"yes",description:"It does affect an open issue"},{ref:"n",action:"no",description:"It does not affect an open issue"}])];case 1:return e=n.sent(),console.log(e+"\n"),[2,e]}}))}))}function d(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return[4,a("Do you agree with the above commit details?",[{ref:"y",action:"yes",description:"Yes, I agree with the commit details"},{ref:"n",action:"no",description:"No, I want to make changes"}])];case 1:return e=n.sent(),console.log(e+"\n"),"no"===e&&(console.log("Commit cancelled. No changes have been committed."),process.exit(0)),[2]}}))}))}function h(e){return r(this,void 0,void 0,(function(){var n;return o(this,(function(t){switch(t.label){case 0:return"yes"!==e?[3,5]:(console.log('Add issue references (e.g. "fix #123", "re #123":'),[4,s("Enter issue reference: ")]);case 1:n=t.sent(),t.label=2;case 2:return n?[3,4]:(console.log("\nIssue reference is required!"),[4,s("Enter issue reference: ")]);case 3:return n=t.sent(),[3,2];case 4:console.log(),t.label=5;case 5:return[2,n]}}))}))}function g(e){return r(this,void 0,void 0,(function(){var n;return o(this,(function(t){switch(t.label){case 0:return"yes"!==e?[3,5]:(console.log("Describe the breaking changes:"),[4,s("Enter description: ")]);case 1:n=t.sent(),t.label=2;case 2:return n?[3,4]:(console.log("\nBreaking change description is required!"),[4,s("Enter description: ")]);case 3:return n=t.sent(),[3,2];case 4:console.log(),t.label=5;case 5:return[2,n]}}))}))}function p(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return console.log("Provide a longer description of the change: (press enter to skip)"),[4,s("Enter long description: ")];case 1:return e=n.sent(),console.log(e?"":"Skipped!\n"),[2,e]}}))}))}function v(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return console.log("What is the scope of this change (e.g. component or file name): (press enter to skip)"),[4,s("Enter your scope: ")];case 1:return e=n.sent(),console.log(e?"":"Skipped!\n"),[2,e]}}))}))}function m(){return r(this,void 0,void 0,(function(){var e,t,r,c,s,a,m,b,y;return o(this,(function(o){switch(o.label){case 0:return[4,l()];case 1:return e=o.sent(),[4,v()];case 2:return t=o.sent(),[4,i()];case 3:return r=o.sent(),[4,p()];case 4:return c=o.sent(),[4,u()];case 5:return[4,g(s=o.sent())];case 6:return a=o.sent(),[4,f()];case 7:return[4,h(m=o.sent())];case 8:return b=o.sent(),function(e){var n=e.type,t=e.scope,r=e.shortDescription,o=e.longDescription,c=e.breakingChange,s=e.breakingChangeDescription,i=e.openIssue,a=e.openIssueReference;console.log("Commit Summary:"),console.log("---------------"),console.log("Type: ".concat(n)),t&&console.log("Scope: ".concat(t)),console.log("Description: ".concat(r)),o&&console.log("Long Description: ".concat(o)),console.log("Breaking Change: ".concat(c)),"yes"===c&&s&&console.log("Breaking Change Description: ".concat(s)),console.log("Open Issue: ".concat(i)),"yes"===i&&a&&console.log("Open issue reference: ".concat(a)),console.log()}(y={type:e,scope:t,shortDescription:r,longDescription:c,breakingChange:s,breakingChangeDescription:a,openIssue:m,openIssueReference:b}),[4,d()];case 9:return o.sent(),function(e){var t=e.type,r=e.scope,o=e.shortDescription,c=e.longDescription,s=e.breakingChange,i=e.breakingChangeDescription,a=e.openIssue,u=e.openIssueReference,l="".concat(t).concat(r?"(".concat(r,")"):"",": ").concat(o),f=c?"\n\n".concat(c):"",d="yes"===s?"\n\nBREAKING CHANGE: ".concat(i):"",h="yes"===a?"\n\n".concat(u):"",g='git commit -m "'.concat(l).concat(f).concat(d).concat(h,'"');n.exec(g,(function(e,n){e&&(console.error("Error executing command"),console.error("Feedback: ".concat(e.message)),console.error("Possible reasons for the error:"),console.error("- Ensure that you are in a valid Git repository."),console.error("- Make sure there are staged changes using the `git add` command."),process.exit(1)),console.log("Git command output:"),console.log(n)}))}(y),[2]}}))}))}function b(){return r(this,void 0,void 0,(function(){return o(this,(function(e){return[2,console.log("License feature coming soon!")]}))}))}function y(){return new Promise((function(e,t){n.exec("git tag",(function(n,r,o){return n?t("Error executing command: ".concat(n.message)):o?t("Command execution failed: ".concat(o)):void(r.split("\n").filter((function(e){return""!==e.trim()})).length>0?e(!0):e(!1))}))}))}var w="changelog.md";function x(e){return r(this,void 0,void 0,(function(){var t;return o(this,(function(r){return t=/warning: LF will be replaced by CRLF/,[2,new Promise((function(r,o){n.exec(e,(function(e,n,c){return e&&!t.test(c)?(console.error("Error executing command: ".concat(e.message)),o(new Error(e.message))):c&&!t.test(c)?(console.error("Command execution failed: ".concat(c)),o(new Error(c))):void r(n)}))}))]}))}))}function k(e){return r(this,void 0,void 0,(function(){var n;return o(this,(function(t){switch(t.label){case 0:return t.trys.push([0,7,,8]),[4,y()];case 1:return t.sent()?[4,x("git tag -d ".concat(e))]:[3,3];case 2:t.sent(),t.label=3;case 3:return[4,x("git add ".concat(w))];case 4:return t.sent(),[4,x('git commit --amend -m "chore(release): '.concat(e,'"'))];case 5:return t.sent(),[4,x("git tag ".concat(e))];case 6:return t.sent(),[3,8];case 7:return n=t.sent(),console.error(n),process.exit(1),[3,8];case 8:return[2]}}))}))}var E=(new Date).toLocaleDateString("en-US",{timeZone:"Africa/Johannesburg"}),C=function(e,n,t){var r=e.filter((function(e){return e.startsWith(n)})).map((function(e){return"- ".concat(e.split(": ")[1],"\n")}));return r.length<1?[]:c(["\n### ".concat(t,"\n")],r,!0)};function D(e){var s=e.lastTag,i=e.secondLastTag;return r(this,void 0,void 0,(function(){var e,a=this;return o(this,(function(u){return e="",e=null===i?'git log --pretty=format:"%s"':"git log ".concat(i,"..").concat(s,' --pretty=format:"%s"'),n.exec(e,(function(e,n){return r(a,void 0,void 0,(function(){var r,i,a,u,l;return o(this,(function(o){e&&(console.error("Error executing command"),console.error("Feedback: ".concat(e.message))),r=n.split("\n"),i="changelog.md",a=c(c(c(c(c(c(c(c(c(["## ".concat(E,": ").concat(s,"\n")],C(r,"feat","Features"),!0),C(r,"fix","Fixes"),!0),C(r,"refactor","Refactors"),!0),C(r,"test","Tests"),!0),C(r,"chore","Chores"),!0),C(r,"docs","Documentation"),!0),C(r,"style","Styles"),!0),C(r,"build","Builds"),!0),C(r,"ci","CI"),!0).join(""),u="";try{u=t.readFileSync(i,"utf8")}catch(e){console.log("The changelog.md' file does not currently exist, but a new one will be generated.")}return l="".concat(a,"\n<br />\n\n").concat(u),t.writeFileSync(i,l),console.log("Updated changelog file!"),[2]}))}))})),[2]}))}))}function I(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,y()];case 1:return t.sent()?[2,new Promise((function(e,t){n.exec("git describe --tags --abbrev=0",(function(n,r){if(n)return t(new Error("Failed to execute command: ".concat(n.message)));e(r.trim())}))}))]:[2,null];case 2:return e=t.sent(),console.log(e),process.exit(1),[3,3];case 3:return[2]}}))}))}function S(e,t){return r(this,void 0,void 0,(function(){var r,c;return o(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,I()];case 1:return r=o.sent(),[2,new Promise((function(o){var c="npm version ".concat(e).concat(t?" --preid=".concat(t):"");n.exec(c,(function(e,n){e&&(console.error("Error executing command"),console.error("Feedback: ".concat(e.message)),console.error("Possible reasons for the error:"),console.error("- Ensure that you are in a valid Git repository."),console.error("- Git working directory not clean."),process.exit(1)),o({lastTag:n.split("\n")[0],secondLastTag:r})}))}))];case 2:return c=o.sent(),console.error("Error retrieving last tag:",c),process.exit(1),[3,3];case 3:return[2]}}))}))}function T(e){return r(this,void 0,void 0,(function(){var n;return o(this,(function(t){switch(t.label){case 0:return e.includes("pre")?[4,a("Select a release type:",[{ref:"1",action:"standard",description:"Standard pre-release identifier"},{ref:"2",action:"alpha",description:"Alpha version for testing purposes"},{ref:"3",action:"beta",description:"Beta version for testing purposes"}])]:[2];case 1:return n=t.sent(),console.log(n+"\n"),[2,"standard"===n?void 0:n]}}))}))}function P(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return[4,a("Select a release type:",[{ref:"1",action:"major",description:"Introduces backward-incompatible changes"},{ref:"2",action:"minor",description:"Adds new features without breaking existing functionality"},{ref:"3",action:"patch",description:"Includes bug fixes and small improvements"},{ref:"4",action:"premajor",description:"Pre-release version with major changes"},{ref:"5",action:"preminor",description:"Pre-release version with minor changes"},{ref:"6",action:"prepatch",description:"Pre-release version with patch-level changes"},{ref:"7",action:"prerelease",description:"Pre-release version for testing purposes"}])];case 1:return e=n.sent(),console.log(e+"\n"),[2,e]}}))}))}function A(){return r(this,void 0,void 0,(function(){var e,n,t,r,c;return o(this,(function(o){switch(o.label){case 0:return[4,P()];case 1:return[4,T(e=o.sent())];case 2:return n=o.sent(),[4,S(e,n)];case 3:return t=o.sent(),r=t.lastTag,c=t.secondLastTag,[4,D({lastTag:r,secondLastTag:c})];case 4:return o.sent(),[4,k(r)];case 5:return o.sent(),[2]}}))}))}module.exports=function(){return r(this,void 0,void 0,(function(){var e;return o(this,(function(n){switch(n.label){case 0:return console.log("Welcome to TagMaster\n"),[4,a("Select an action: ",[{ref:"1",action:"commit",description:"Make a commit"},{ref:"2",action:"release",description:"Perform a release"},{ref:"3",action:"license",description:"Generate a license"}])];case 1:return e=n.sent(),console.log(e+"\n"),"commit"!==e?[3,3]:[4,m()];case 2:case 4:case 6:return[2,n.sent()];case 3:return"release"!==e?[3,5]:[4,A()];case 5:return"license"!==e?[3,7]:[4,b()];case 7:return console.error("TagMaster has encountered an issue and will exit to ensure stability."),process.exit(1),[2]}}))}))}; //# sourceMappingURL=index.js.map