2025-07-20 15:03:43 +07:00
|
|
|
|
import fs from "fs/promises";
|
|
|
|
|
import path from "path";
|
|
|
|
|
import { fileURLToPath } from "url";
|
|
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
|
2025-07-20 17:21:02 +07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-20 15:03:43 +07:00
|
|
|
|
const syncVersion = async () => {
|
|
|
|
|
const rootPkgPath = path.join(__dirname, "package.json");
|
|
|
|
|
const frontendPkgPath = path.join(__dirname, "frontend", "package.json");
|
|
|
|
|
const manifestPath = path.join(__dirname, "src", "manifest.json");
|
|
|
|
|
|
|
|
|
|
// Read root package.json version
|
|
|
|
|
const rootPkgRaw = await fs.readFile(rootPkgPath, "utf-8");
|
|
|
|
|
const rootPkg = JSON.parse(rootPkgRaw);
|
|
|
|
|
const version = rootPkg.version;
|
|
|
|
|
|
|
|
|
|
if (!version) {
|
|
|
|
|
console.warn("⚠️ No version field found in root package.json, skipping sync.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-20 17:21:02 +07:00
|
|
|
|
// Update frontend/package.json using regex
|
|
|
|
|
const frontendUpdated = await updateVersionWithRegex(frontendPkgPath, version);
|
|
|
|
|
if (frontendUpdated) {
|
2025-07-20 15:03:43 +07:00
|
|
|
|
console.log(`🔄 Updated frontend/package.json version to ${version}`);
|
2025-07-20 17:21:02 +07:00
|
|
|
|
} else {
|
|
|
|
|
console.log("ℹ️ frontend/package.json not found or no changes needed.");
|
2025-07-20 15:03:43 +07:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-20 17:21:02 +07:00
|
|
|
|
// Update src/manifest.json using regex
|
|
|
|
|
const manifestUpdated = await updateVersionWithRegex(manifestPath, version);
|
|
|
|
|
if (manifestUpdated) {
|
2025-07-20 15:03:43 +07:00
|
|
|
|
console.log(`🔄 Updated src/manifest.json version to ${version}`);
|
2025-07-20 17:21:02 +07:00
|
|
|
|
} else {
|
|
|
|
|
console.log("ℹ️ src/manifest.json not found or no changes needed.");
|
2025-07-20 15:03:43 +07:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default syncVersion;
|