refactor: split commands into files (#18)

This commit is contained in:
2024-03-06 22:17:32 +02:00
parent 6e63867ca6
commit 778175a2f3
6 changed files with 771 additions and 667 deletions

62
src/commands/validate.ts Normal file
View File

@@ -0,0 +1,62 @@
/*
Copyright 2024 Resultium LLC
This file is part of RCZ.
RCZ is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RCZ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
*/
import { Command } from "commander";
import { getConfig } from "../utils/functions";
import { readFileSync } from "fs";
const command = new Command("validate")
.description("Validate whether a string fits given commit conventions")
.argument("[message]", "string for validation")
.option("-C, --code-only", "return code only")
.action(async (string: string, options) => {
try {
const message = string || readFileSync(0, "utf-8");
const codeOnly = options.codeOnly ? true : false;
const config = await getConfig();
// Regex for testing:
// /(build|feat|docs)(\((commands|changelog)\))?!?: .* ?(\(..*\))?((\n\n..*)?(\n\n..*)?)?/gm
const testRegex = new RegExp(
`(${
config?.types?.map((type) => type.value).join("|") ||
"feat|fix|build|ci|docs|perf|refactor|chore"
})(\\((${
config?.scopes ? [...config?.scopes, "release"].join("|") : "..*"
})\\))?!?: .* ?(\\(..*\\))?((\n\n..*)?(\n\n..*)?)?`,
"gm",
);
if (codeOnly) {
console.log(testRegex.test(message) ? 0 : 1);
} else {
console.log(
testRegex.test(message)
? "[rcz]: valid message"
: "[rcz]: invalid message",
);
}
} catch (err) {
console.log("[rcz]: no stdin found");
}
});
export default command;