fix: add function to retrieve short git commit hash and update timestamp format
This commit is contained in:
@@ -25,22 +25,39 @@
|
|||||||
* Errors are logged and the process exits with code 1 on failure.
|
* Errors are logged and the process exits with code 1 on failure.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { program } from "commander";
|
import { program } from "commander";
|
||||||
|
import { execSync } from "child_process";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成短时间戳(格式:YYMMDDHHMM)
|
* 获取当前 git 短 commit hash
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
function generateShortTimestamp() {
|
function getGitShortCommit() {
|
||||||
|
try {
|
||||||
|
return execSync("git rev-parse --short HEAD").toString().trim();
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("[WARN]: Failed to get git short commit, fallback to 'nogit'");
|
||||||
|
return "nogit";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成短时间戳(格式:YYMMDD)或带 commit(格式:YYMMDD.cc39b27)
|
||||||
|
* @param {boolean} withCommit 是否带 commit
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function generateShortTimestamp(withCommit = false) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const year = String(now.getFullYear()).slice(-2);
|
|
||||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||||
const day = String(now.getDate()).padStart(2, "0");
|
const day = String(now.getDate()).padStart(2, "0");
|
||||||
const hours = String(now.getHours()).padStart(2, "0");
|
if (withCommit) {
|
||||||
const minutes = String(now.getMinutes()).padStart(2, "0");
|
const gitShort = getGitShortCommit();
|
||||||
return `${year}${month}${day}${hours}${minutes}`;
|
return `${month}${day}.${gitShort}`;
|
||||||
|
}
|
||||||
|
return `${month}${day}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -205,8 +222,8 @@ async function main(versionArg) {
|
|||||||
const baseVersion = getBaseVersion(currentVersion);
|
const baseVersion = getBaseVersion(currentVersion);
|
||||||
|
|
||||||
if (versionArg.toLowerCase() === "autobuild") {
|
if (versionArg.toLowerCase() === "autobuild") {
|
||||||
const timestamp = generateShortTimestamp();
|
// 格式: 2.3.0+autobuild.250613.cc39b27
|
||||||
newVersion = `${baseVersion}+autobuild.${timestamp}`;
|
newVersion = `${baseVersion}+autobuild.${generateShortTimestamp(true)}`;
|
||||||
} else {
|
} else {
|
||||||
newVersion = `${baseVersion}-${versionArg.toLowerCase()}`;
|
newVersion = `${baseVersion}-${versionArg.toLowerCase()}`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user