From 3d9cd0a066a6b230a15ebf1fb4afb52c191095a4 Mon Sep 17 00:00:00 2001 From: KillahBee Date: Fri, 4 Oct 2024 14:24:21 -0400 Subject: [PATCH] handling package update because of json issue --- web/pages/api/github-util.ts | 61 +++++++++--------------------------- 1 file changed, 15 insertions(+), 46 deletions(-) diff --git a/web/pages/api/github-util.ts b/web/pages/api/github-util.ts index 0d3f8ac..5a62089 100644 --- a/web/pages/api/github-util.ts +++ b/web/pages/api/github-util.ts @@ -56,26 +56,6 @@ export async function getDownloadUrl( return null; } -function waitForFile(filePath: string, maxAttempts: number = 10, interval: number = 1000): Promise { - return new Promise((resolve, reject) => { - let attempts = 0; - const checkFile = async () => { - try { - await fs.access(filePath, fs.constants.F_OK); - resolve(); - } catch (err) { - if (attempts < maxAttempts) { - attempts++; - setTimeout(checkFile, interval); - } else { - reject(new Error(`File ${filePath} not found after ${maxAttempts} attempts`)); - } - } - }; - checkFile(); - }); -} - export async function extractAndClean( assetUrl: string, baseModPath: string = "/app/user/mods", @@ -96,32 +76,21 @@ export async function extractAndClean( await fs.promises.writeFile(archiveFilePath, assetResponse.data); // Extract the archive - fullArchive(archiveFilePath, archiveDir).then(async function () { - // Clean up - fs.unlinkSync(archiveFilePath); - // Update package.json with the GitHub URL - const packageJsonPath = path.join(modPath, "package.json"); - let packageJson: { [key: string]: any } = {}; - - // Wait for the package.json file to exist - await waitForFile(packageJsonPath); - - if (fs.existsSync(packageJsonPath)) { - const fileContent = await fs.readFile(packageJsonPath, "utf-8"); - packageJson = JSON.parse(fileContent); - console.log("Existing package.json read"); - } else { - console.log("package.json not found"); - throw new Error("package.json not found"); - } - - // Update or add the githubUrl property - packageJson.githubUrl = assetUrl; - - // Write the updated or new package.json - fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); - console.log("package.json updated/created successfully"); - }); + await fullArchive(archiveFilePath, archiveDir); + + // Clean up + fs.unlinkSync(archiveFilePath); + // Update package.json with the GitHub URL + const packageJsonPath = path.join(modPath, "package.json"); + + const fileBuffer = await fs.promises.readFile(packageJsonPath, 'utf-8'); + const file = JSON.parse(fileBuffer); + + file.githubUrl = assetUrl; + + // Write the updated or new package.json + await fs.promises.writeFile(packageJsonPath, JSON.stringify(file, null, 2)); + console.log("package.json updated successfully"); return modName; } catch (error) {