forked from tpd94/CDRM-Extension
		
	sync version: only update the "version" instead of parsing whole file
This commit is contained in:
		
							parent
							
								
									918269f42e
								
							
						
					
					
						commit
						889a4c63f3
					
				@ -5,6 +5,32 @@ import { fileURLToPath } from "url";
 | 
				
			|||||||
const __filename = fileURLToPath(import.meta.url);
 | 
					const __filename = fileURLToPath(import.meta.url);
 | 
				
			||||||
const __dirname = path.dirname(__filename);
 | 
					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 syncVersion = async () => {
 | 
				
			||||||
    const rootPkgPath = path.join(__dirname, "package.json");
 | 
					    const rootPkgPath = path.join(__dirname, "package.json");
 | 
				
			||||||
    const frontendPkgPath = path.join(__dirname, "frontend", "package.json");
 | 
					    const frontendPkgPath = path.join(__dirname, "frontend", "package.json");
 | 
				
			||||||
@ -20,26 +46,20 @@ const syncVersion = async () => {
 | 
				
			|||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Update frontend/package.json if exists
 | 
					    // Update frontend/package.json using regex
 | 
				
			||||||
    try {
 | 
					    const frontendUpdated = await updateVersionWithRegex(frontendPkgPath, version);
 | 
				
			||||||
        const frontendPkgRaw = await fs.readFile(frontendPkgPath, "utf-8");
 | 
					    if (frontendUpdated) {
 | 
				
			||||||
        const frontendPkg = JSON.parse(frontendPkgRaw);
 | 
					 | 
				
			||||||
        frontendPkg.version = version;
 | 
					 | 
				
			||||||
        await fs.writeFile(frontendPkgPath, JSON.stringify(frontendPkg, null, 2));
 | 
					 | 
				
			||||||
        console.log(`🔄 Updated frontend/package.json version to ${version}`);
 | 
					        console.log(`🔄 Updated frontend/package.json version to ${version}`);
 | 
				
			||||||
    } catch {
 | 
					    } else {
 | 
				
			||||||
        console.log("ℹ️ frontend/package.json not found or unreadable, skipping version update.");
 | 
					        console.log("ℹ️ frontend/package.json not found or no changes needed.");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Update src/manifest.json version
 | 
					    // Update src/manifest.json using regex
 | 
				
			||||||
    try {
 | 
					    const manifestUpdated = await updateVersionWithRegex(manifestPath, version);
 | 
				
			||||||
        const manifestRaw = await fs.readFile(manifestPath, "utf-8");
 | 
					    if (manifestUpdated) {
 | 
				
			||||||
        const manifest = JSON.parse(manifestRaw);
 | 
					 | 
				
			||||||
        manifest.version = version;
 | 
					 | 
				
			||||||
        await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
 | 
					 | 
				
			||||||
        console.log(`🔄 Updated src/manifest.json version to ${version}`);
 | 
					        console.log(`🔄 Updated src/manifest.json version to ${version}`);
 | 
				
			||||||
    } catch (err) {
 | 
					    } else {
 | 
				
			||||||
        console.error(`❌ Failed to update src/manifest.json version: ${err.message}`);
 | 
					        console.log("ℹ️ src/manifest.json not found or no changes needed.");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user