Created
November 25, 2023 12:14
-
-
Save shawn-sandy/e35526281238376b235b944b4f9c5cbc to your computer and use it in GitHub Desktop.
cli tools
This file contains 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
import inquirer from "inquirer"; | |
import { exec } from "child_process"; | |
// Define the questions | |
const questions = [ | |
{ | |
type: "list", | |
name: "type", | |
message: "Select the type of change that you're committing:", | |
choices: ["feat", "fix", "docs", "style", "refactor", "test", "chore"], | |
}, | |
{ | |
type: "input", | |
name: "scope", | |
message: | |
"What is the scope of this change (e.g., component or file name)? (press enter to skip)", | |
}, | |
{ | |
type: "input", | |
name: "description", | |
message: "Write a short, imperative tense description of the change:", | |
validate: function (input) { | |
if (!input) { | |
return "Description is required."; | |
} | |
return true; | |
}, | |
}, | |
]; | |
// Use inquirer to ask the questions | |
inquirer.prompt(questions).then((answers) => { | |
// Execute git add command | |
exec("git add .", (addError, addStdout, addStderr) => { | |
if (addError) { | |
console.error(`exec error: ${addError}`); | |
return; | |
} | |
// console.log(`Files added: ${addStdout}`); | |
console.error(`Add stderr: ${addStderr}`); | |
// Construct the commit message | |
const commitMessage = `${answers.type}${ | |
answers.scope ? "(" + answers.scope + ")" : "" | |
}: ${answers.description}`; | |
console.log(`Committing with message: "${commitMessage}"`); | |
// Execute git commit command | |
exec( | |
`git commit -m "${commitMessage}"`, | |
(commitError, commitStdout, commitStderr) => { | |
if (commitError) { | |
console.error(`exec error: ${commitError}`); | |
return; | |
} | |
// console.log(`Commit stdout: ${commitStdout}`); | |
console.error(`Commit stderr: ${commitStderr}`); | |
} | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment