From 889a4c63f310e599ec2dbb20da1b0a2a55778c17 Mon Sep 17 00:00:00 2001 From: voldemort <5692900+yell0wsuit@users.noreply.github.com> Date: Sun, 20 Jul 2025 17:21:02 +0700 Subject: [PATCH] sync version: only update the "version" instead of parsing whole file --- syncVersion.js | 52 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/syncVersion.js b/syncVersion.js index 7acba5d..6faa01a 100644 --- a/syncVersion.js +++ b/syncVersion.js @@ -5,6 +5,32 @@ import { fileURLToPath } from "url"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); +const updateVersionWithRegex = async (filePath, newVersion) => { + try { + const content = await fs.readFile(filePath, "utf-8"); + + // Regex to match "version": "any.version.number" + const versionRegex = /("version"\s*:\s*")([^"]+)(")/; + + if (!versionRegex.test(content)) { + console.warn(`âš ī¸ No version field found in ${filePath}`); + return false; + } + + const updatedContent = content.replace(versionRegex, `$1${newVersion}$3`); + + if (content !== updatedContent) { + await fs.writeFile(filePath, updatedContent); + return true; + } + + return false; + } catch (err) { + console.error(`❌ Failed to update ${filePath}: ${err.message}`); + return false; + } +}; + const syncVersion = async () => { const rootPkgPath = path.join(__dirname, "package.json"); const frontendPkgPath = path.join(__dirname, "frontend", "package.json"); @@ -20,26 +46,20 @@ const syncVersion = async () => { return; } - // Update frontend/package.json if exists - try { - const frontendPkgRaw = await fs.readFile(frontendPkgPath, "utf-8"); - const frontendPkg = JSON.parse(frontendPkgRaw); - frontendPkg.version = version; - await fs.writeFile(frontendPkgPath, JSON.stringify(frontendPkg, null, 2)); + // Update frontend/package.json using regex + const frontendUpdated = await updateVersionWithRegex(frontendPkgPath, version); + if (frontendUpdated) { console.log(`🔄 Updated frontend/package.json version to ${version}`); - } catch { - console.log("â„šī¸ frontend/package.json not found or unreadable, skipping version update."); + } else { + console.log("â„šī¸ frontend/package.json not found or no changes needed."); } - // Update src/manifest.json version - try { - const manifestRaw = await fs.readFile(manifestPath, "utf-8"); - const manifest = JSON.parse(manifestRaw); - manifest.version = version; - await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2)); + // Update src/manifest.json using regex + const manifestUpdated = await updateVersionWithRegex(manifestPath, version); + if (manifestUpdated) { console.log(`🔄 Updated src/manifest.json version to ${version}`); - } catch (err) { - console.error(`❌ Failed to update src/manifest.json version: ${err.message}`); + } else { + console.log("â„šī¸ src/manifest.json not found or no changes needed."); } };