From 3c38a835b9e164f6aee56219949da600b363dfdb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 19 Sep 2024 14:10:06 +0200 Subject: [PATCH] improve format --- src/semver.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/semver.ts b/src/semver.ts index 2e11c44..ccc4253 100644 --- a/src/semver.ts +++ b/src/semver.ts @@ -66,13 +66,10 @@ export async function bumpVersion( } if (opts.suffix) { - const date = new Date(); - // YYMMDD-HHMMSS: 2024819-135530 - const dateStr = `${date.getFullYear()}${date.getMonth()}${date.getDate()}-${date.getHours()}${date.getMinutes()}${date.getSeconds()}`; const suffix = typeof opts.suffix === "string" ? `-${opts.suffix}` - : `+${dateStr}-${commits[0].shortHash}`; + : `+${fmtDate(new Date())}-${commits[0].shortHash}`; pkg.version = config.newVersion = config.newVersion.split("-")[0] + suffix; } @@ -88,3 +85,14 @@ export async function bumpVersion( return pkg.version; } + +function fmtDate(d: Date): string { + // YYMMDD-HHMMSS: 2024819-135530 + const date = joinNumbers([d.getFullYear(), d.getMonth() + 1, d.getDate()]); + const time = joinNumbers([d.getHours(), d.getMinutes(), d.getSeconds()]); + return `${date}-${time}`; +} + +function joinNumbers(items: number[]): string { + return items.map((i) => (i + "").padStart(2, "0")).join(""); +}