From 2ba23ac64ea64309056dc8a114bd7227e09021f4 Mon Sep 17 00:00:00 2001 From: Amadeo Pellicce Date: Sat, 12 Nov 2022 16:28:09 +0100 Subject: [PATCH] adding safeguard in case maps file does not exist --- src/repo/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/repo/index.ts b/src/repo/index.ts index 3271ae6..46594cb 100644 --- a/src/repo/index.ts +++ b/src/repo/index.ts @@ -126,15 +126,18 @@ export function addAssetMap(map: AssetMap, repoPath: string = DEFAULT_REPO_DISK_ const assetDir = path.join(repoPath, getDirPathForTokenlist(map.fromNetwork, map.fromAddress)); const assetMapInfoFile = path.join(assetDir, 'maps.json'); - if(fs.existsSync(assetDir) && !fs.existsSync(assetMapInfoFile)) { - fs.writeFileSync(assetMapInfoFile, JSON.stringify([map.deserialise()]), null, 2); + if(!fs.existsSync(assetDir)) { + throw new Error(`Cannot add map to ${assetDir} as it does not exist`) + } + + if(!fs.existsSync(assetMapInfoFile)) { + fs.writeFileSync(assetMapInfoFile, JSON.stringify([map.deserialise()], null, 2)); return; } const assetMaps = readAndParseJson(assetMapInfoFile) .map(map => new AssetMap(map)); - const existingMap = assetMaps.find( _map => _map.fromAddress === map.fromAddress && _map.fromNetwork === map.fromNetwork