From 9311be80b2b47f3623902f04e17a540772b6b781 Mon Sep 17 00:00:00 2001 From: Olivers Vitins Date: Mon, 21 Aug 2023 18:03:36 +0300 Subject: [PATCH] feat(commands): add commit message validation command (#4) --- src/index.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/index.ts b/src/index.ts index 71c4a13..868168f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -407,4 +407,37 @@ program ); }); +program + .command("validate") + .description("Validate whether a string fits given commit conventions") + .argument("[message]", "string for validation") + .action(async (string: string) => { + try { + const message = string || fs.readFileSync(0, "utf-8"); + + 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" + })(\\((${ + config?.scopes?.join("|") || "..*" + })\\))?!?: .* ?(\\(..*\\))?((\n\n..*)?(\n\n..*)?)?`, + "gm" + ); + + console.log( + testRegex.test(message) + ? "[rcz]: valid message" + : "[rcz]: invalid message" + ); + } catch (err) { + console.log("[rcz]: no stdin found"); + } + }); + program.parse();