63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
/*
|
|
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;
|