sync version: only update the "version" instead of parsing whole file

This commit is contained in:
voldemort 2025-07-20 17:21:02 +07:00
parent 918269f42e
commit 889a4c63f3

View File

@ -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.");
}
};