From 1d1eb3920cd1094df1fada3a4d749267aa4d21e6 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Molteni Date: Mon, 28 Oct 2024 11:38:52 -0300 Subject: [PATCH] Create output folder for has-content-changed --- .../src/logic/has-content-changed-task.ts | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/consumer-server/src/logic/has-content-changed-task.ts b/consumer-server/src/logic/has-content-changed-task.ts index d199cfd..c1dc640 100644 --- a/consumer-server/src/logic/has-content-changed-task.ts +++ b/consumer-server/src/logic/has-content-changed-task.ts @@ -2,7 +2,7 @@ import { Entity } from '@dcl/schemas' import * as fs from 'fs' import * as path from 'path' import fetch from 'node-fetch' -import { ILoggerComponent } from '@well-known-components/interfaces' // Assuming you're using the node-fetch package +import { ILoggerComponent } from '@well-known-components/interfaces' async function getActiveEntity(ids: string, contentServer: string): Promise { const url = `${contentServer}/entities/active` @@ -110,6 +110,8 @@ async function downloadFilesFromManifestSuccesfully( env: string, logger: ILoggerComponent.ILogger ): Promise { + fs.mkdirSync(outputFolder, { recursive: true }) + const baseUrl = `https://ab-cdn.decentraland.${env}/${version}/${previousHash}/` for (const file of hashesToDownload) { @@ -138,14 +140,17 @@ async function downloadFilesFromManifestSuccesfully( } // Helper function to delete all files in the output folder -async function DeleteFilesInOutputFolder(outputFolder: string): Promise { +async function DeleteFilesInOutputFolder(outputFolder: string, logger: ILoggerComponent.ILogger): Promise { if (fs.existsSync(outputFolder)) { - const files = fs.readdirSync(outputFolder) - - for (const file of files) { - const filePath = path.join(outputFolder, file) - fs.unlinkSync(filePath) // Delete each file + // Delete the directory and all of its contents + try { + fs.rmSync(outputFolder, { recursive: true, force: true }) + logger.log(`HasContentChanged: Directory ${path} deleted successfully`) + } catch (err) { + logger.log(`HasContentChanged: Error deleting ${path} ${err}`) } + } else { + logger.log(`HasContentChanged: Directory ${path} does not exist`) } } @@ -190,7 +195,7 @@ export async function hasContentChange( return false } else { logger.info(`HasContentChanged Error: Some files failed to download`) - await DeleteFilesInOutputFolder(outputFolder) + await DeleteFilesInOutputFolder(outputFolder, logger) } } else { logger.info(`HasContentChanged Error: Not all entities contained in old manifest`)