From 68bb1a54f79cd9f731c6bdbfde2a26f207514f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E4=BA=91?= Date: Mon, 28 Apr 2025 21:39:03 +0800 Subject: [PATCH] Update build.js --- build/build.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/build/build.js b/build/build.js index ccda31e4..67ae1be3 100644 --- a/build/build.js +++ b/build/build.js @@ -1,6 +1,7 @@ const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); +const { execSync } = require('child_process'); // 在文件开头添加全局变量 const pathingDirsWithoutIcon = new Set(); @@ -12,6 +13,17 @@ function calculateSHA1(filePath) { return hashSum.digest('hex'); } +function getGitTimestamp(filePath) { + try { + // 获取最后一次提交时间(ISO 格式) + const iso = execSync(`git log -1 --format=\"%ci\" -- "${filePath}"`, { encoding: 'utf8' }).trim(); + return new Date(iso); + } catch (e) { + console.warn(`无法通过 Git 获取时间: ${filePath}`, e); + return null; + } +} + function convertNewlines(text) { return text.replace(/\\n/g, '\n'); } @@ -72,10 +84,12 @@ function extractInfoFromPathingFile(filePath, parentFolders) { const contentObj = JSON.parse(content); - // 提取版本字段,若不存在则使用创建时间 - const version = contentObj.info && contentObj.info.version - ? contentObj.info.version - : : `${formatTime(stats.birthtime)}_${formatTime(stats.mtime)}`; + // 提取版本字段,若不存在则使用上传时间,还不存在就使用 SHA + let version = contentObj.info && contentObj.info.version; + if (!version) { + const gitDate = getGitTimestamp(filePath); + version = gitDate ? formatTime(gitDate) : calculateSHA1(filePath).substring(0, 7); + } let tags = parentFolders.slice(2) .filter(tag => !tag.includes('@'))