feat(commands): add --sudo option to commit command (#16)

This commit is contained in:
2024-01-27 16:15:31 +02:00
parent ebfa3859fc
commit 30a7e0b286

View File

@@ -93,8 +93,8 @@ const CheckForUpdates = async () => {
const cachedVersion = fs.existsSync(path.join(tmpdir(), "rcz-server-version")) const cachedVersion = fs.existsSync(path.join(tmpdir(), "rcz-server-version"))
? (await fs.promises.readFile(path.join(tmpdir(), "rcz-server-version"))) ? (await fs.promises.readFile(path.join(tmpdir(), "rcz-server-version")))
.toString() .toString()
.trim() .trim()
: null; : null;
const localVersion = execSync("rcz --version").toString().trim(); const localVersion = execSync("rcz --version").toString().trim();
@@ -131,6 +131,7 @@ program
.description("Create a conventional commit") .description("Create a conventional commit")
.option("-S, --sign", "sign the commit") .option("-S, --sign", "sign the commit")
.option("--amend", "amend commit message to the last commit") .option("--amend", "amend commit message to the last commit")
.option("--sudo", "remove any validation")
.action(async (options) => { .action(async (options) => {
await CheckForUpdates(); await CheckForUpdates();
@@ -138,6 +139,7 @@ program
const sign = config?.autoSignCommits || options.sign ? true : false; const sign = config?.autoSignCommits || options.sign ? true : false;
const amend = options.amend ? true : false; const amend = options.amend ? true : false;
const sudo = options.sudo ? true : false;
intro("Creating a conventional commit"); intro("Creating a conventional commit");
@@ -149,11 +151,11 @@ program
const stageAll = amend const stageAll = amend
? null ? null
: await confirm({ : await confirm({
message: "Stage all changes?", message: "Stage all changes?",
initialValue: (await simpleGit().diff(["--cached"])).toString() initialValue: (await simpleGit().diff(["--cached"])).toString()
? false ? false
: true, : true,
}); });
if (isCancel(stageAll)) { if (isCancel(stageAll)) {
cancel("Commit creation cancelled"); cancel("Commit creation cancelled");
@@ -166,7 +168,7 @@ program
).match(/\d+/gm) || [] ).match(/\d+/gm) || []
).reduce((partialSum, num) => partialSum + Number(num), 0); ).reduce((partialSum, num) => partialSum + Number(num), 0);
if (changedLines > 250) { if (changedLines > 250 && !sudo) {
const proceedCommitting = await confirm({ const proceedCommitting = await confirm({
message: message:
"You are about to commit changes to more than 250 lines, are you sure you want to proceed?", "You are about to commit changes to more than 250 lines, are you sure you want to proceed?",
@@ -240,6 +242,10 @@ program
"Input a scope (subsystem which change is relevant to e.g. router, forms, core) or leave empty", "Input a scope (subsystem which change is relevant to e.g. router, forms, core) or leave empty",
placeholder: "router", placeholder: "router",
validate: (value) => { validate: (value) => {
if (sudo) {
return
}
if (config?.scopes && value) { if (config?.scopes && value) {
if (!config?.scopes.includes(value)) if (!config?.scopes.includes(value))
return "This scope is not allowed by local configuration"; return "This scope is not allowed by local configuration";
@@ -260,6 +266,10 @@ program
message: `Briefly describe made changes in imperative tense, maximum length 50`, message: `Briefly describe made changes in imperative tense, maximum length 50`,
placeholder: "warn upon suspicious router requests", placeholder: "warn upon suspicious router requests",
validate: (value) => { validate: (value) => {
if (sudo) {
return
}
if (value.length > 50) { if (value.length > 50) {
return "Your message is too long"; return "Your message is too long";
} }
@@ -321,11 +331,9 @@ program
process.exit(0); process.exit(0);
} }
const commitMessage = `${type.toString()}${ const commitMessage = `${type.toString()}${scope ? `(${scope.toString()})` : ``
scope ? `(${scope.toString()})` : `` }${isBreaking ? "!" : ""}: ${message.toString()}${resolvesIssue ? ` (${issue?.toString()})` : ``
}${isBreaking ? "!" : ""}: ${message.toString()}${ }${body ? `\n\n${body}` : ``}${footer ? `\n\n${footer}` : ``}`;
resolvesIssue ? ` (${issue?.toString()})` : ``
}${body ? `\n\n${body}` : ``}${footer ? `\n\n${footer}` : ``}`;
if (stageAll) { if (stageAll) {
await simpleGit() await simpleGit()
@@ -344,8 +352,7 @@ program
note(commitMessage); note(commitMessage);
outro( outro(
`Finished ${ `Finished ${amend ? "amending" : "creating"
amend ? "amending" : "creating"
} a conventional commit, feel free to push` } a conventional commit, feel free to push`
); );
}); });
@@ -467,8 +474,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -487,8 +493,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -507,8 +512,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -527,8 +531,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -616,11 +619,9 @@ program
// /(build|feat|docs)(\((commands|changelog)\))?!?: .* ?(\(..*\))?((\n\n..*)?(\n\n..*)?)?/gm // /(build|feat|docs)(\((commands|changelog)\))?!?: .* ?(\(..*\))?((\n\n..*)?(\n\n..*)?)?/gm
const testRegex = new RegExp( const testRegex = new RegExp(
`(${ `(${config?.types?.map((type) => type.value).join("|") ||
config?.types?.map((type) => type.value).join("|") || "feat|fix|build|ci|docs|perf|refactor|chore"
"feat|fix|build|ci|docs|perf|refactor|chore" })(\\((${config?.scopes ? [...config?.scopes, "release"].join("|") : "..*"
})(\\((${
config?.scopes ? [...config?.scopes, "release"].join("|") : "..*"
})\\))?!?: .* ?(\\(..*\\))?((\n\n..*)?(\n\n..*)?)?`, })\\))?!?: .* ?(\\(..*\\))?((\n\n..*)?(\n\n..*)?)?`,
"gm" "gm"
); );