Created
September 12, 2023 12:02
-
-
Save fs-doc/918502bacd9cd01bb753562b52085b3f to your computer and use it in GitHub Desktop.
Simple Commit NotePad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Basic Commit Notepad | |
| */ | |
| // $ node committer.js [branch] | |
| const { exec } = require("child_process"); | |
| const branch = process.argv[2] ? process.argv[2] : 'master'; //Master by default | |
| const now = new Date(); | |
| const date = `${now.getDate()}.${now.getMonth() + 1}.${now.getFullYear()}`; | |
| let commitMessage = ''; | |
| console.log(`Commit notepad started on [${branch}] branch. Type your note and press enter to add it to the commit message. | |
| Type "commit" to commit and "commit-push" to commit and push. Type "exit" to stop execution.`); | |
| //Listen for input | |
| process.stdin.on("data", data => { | |
| const input = data.toString().trim(); | |
| switch (input) { | |
| case 'exit': | |
| process.exit(); | |
| break; | |
| case 'commit': | |
| executeShell(); | |
| break; | |
| case 'clear': | |
| commitMessage = ''; | |
| console.log('Commit message cleared'); | |
| break; | |
| case 'status': | |
| console.log('Commit message so far: ' + commitMessage); | |
| break; | |
| case 'help': | |
| console.log('Type "commit" to commit, "commit-push" to commit and then push to master. Type "exit" to stop execution.'); | |
| break; | |
| case 'commit-push': | |
| executeShell(true); | |
| break; | |
| default: | |
| console.log('Commit message updated: + "'+data.toString().trim()+'"'); | |
| commitMessage += data.toString().trim()+'. '; | |
| break; | |
| } | |
| }); | |
| //Execute shell command | |
| const executeShell = (push) =>{ | |
| if(!commitMessage){ | |
| console.log('ERROR: No commit message. Please type a commit message and try again.'); | |
| return; | |
| } | |
| if(push){ | |
| console.log('Committing and pushing to master... Please wait'); | |
| }else{ | |
| console.log('Committing... Please wait'); | |
| } | |
| exec(`npm run build && git add . && git commit -m "[master] ${date} - ${String(commitMessage.trim())}" ${push && ' && git push'}`, (error, stdout, stderr) => { | |
| if (error) { | |
| console.log(`ERROR: ${error.message}`); | |
| return; | |
| } | |
| if (stderr) { | |
| console.log(`ERROR: ${stderr}`); | |
| return; | |
| } | |
| if(stdout){ | |
| console.log(`${stdout}`); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment