aboutsummaryrefslogtreecommitdiff
path: root/cli.ts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xcli.ts22
1 files changed, 13 insertions, 9 deletions
diff --git a/cli.ts b/cli.ts
index 24011c5..832f300 100755
--- a/cli.ts
+++ b/cli.ts
@@ -40,7 +40,7 @@ if (import.meta.main) {
}
async function main(): Promise<void> {
- const args = parseArgs(Deno.args, {
+ const args = parseArgs(preserveNegativeNumbers(Deno.args), {
alias: { h: "help", v: "version" },
boolean: ["help", "version", "create", "sort-desc"],
collect: ["compare", "sort"],
@@ -236,11 +236,7 @@ function formatFromDate(dateStr: string, changeset?: string): void {
const [, year, month, day] = match;
const date = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
- const changesetNum = changeset ? parseInt(changeset) : 0;
-
- if (isNaN(changesetNum) || changesetNum < 0)
- throw new Error("Changeset must be a non-negative number");
-
+ const changesetNum = changeset ? Number(changeset) : 0; /*** fromDate validates it ***/
const version = ChronVer.fromDate(date, changesetNum);
console.log(version.toString());
} catch(error) {
@@ -325,6 +321,16 @@ function parseVersion(version: string): void {
}
}
+/*** "-1" would otherwise parse as a flag, so move negative numbers after "--" to keep them as values ***/
+export function preserveNegativeNumbers(args: string[]): string[] {
+ const isNegativeNumber = (arg: string) => /^-\d/.test(arg);
+
+ if (args.includes("--") || !args.some(isNegativeNumber))
+ return args;
+
+ return [...args.filter(arg => !isNegativeNumber(arg)), "--", ...args.filter(isNegativeNumber)];
+}
+
function showHelp(): void {
console.log(`
__
@@ -382,8 +388,6 @@ function sortVersions(versions: string[], descending = false): void {
console.log(`📊 Sorted (${direction}):`);
- console.log(versions);
-
sorted.forEach((version, index) => {
const prefix = descending ? "🔽" : "🔼";
console.log(`${prefix} ${index + 1}. ${version}`);
@@ -401,7 +405,7 @@ function validateVersion(version: string): void {
if (isValid) {
console.log(`✅ Valid: ${version}`);
const parsed = new ChronVer(version);
- console.log(` Components: ${parsed.year}-${parsed.month.toString().padStart(2, "0")}-${parsed.day.toString().padStart(2, "0")}`);
+ console.log(` Components: ${parsed.getDateString().replaceAll(".", "-")}`);
if (parsed.changeset > 0)
console.log(` Changeset: ${parsed.changeset}`);