Skip to content

Commit

Permalink
Merge pull request #11 from KillahB33/package-update
Browse files Browse the repository at this point in the history
handling package update because of json issue
  • Loading branch information
KillahB33 authored Oct 4, 2024
2 parents 517e890 + 3d9cd0a commit d742d32
Showing 1 changed file with 15 additions and 46 deletions.
61 changes: 15 additions & 46 deletions web/pages/api/github-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,6 @@ export async function getDownloadUrl(
return null;
}

function waitForFile(filePath: string, maxAttempts: number = 10, interval: number = 1000): Promise<void> {
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",
Expand All @@ -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) {
Expand Down

0 comments on commit d742d32

Please sign in to comment.