diff --git a/src/model/AssetMap.ts b/src/model/AssetMap.ts index 844ce40..bee260c 100644 --- a/src/model/AssetMap.ts +++ b/src/model/AssetMap.ts @@ -9,6 +9,15 @@ toNetwork: string; type: MapType; + constructor({ fromAddress, fromNetwork, toAddress, toNetwork, type }: AssetMap) { + super({}); + this.fromAddress = fromAddress; + this.fromNetwork = fromNetwork; + this.toAddress = toAddress; + this.toNetwork = toNetwork; + this.type = type; + } + deserialise(): string { let parsed = JSON.parse(JSON.stringify(this)); parsed = sortObjectKeys(parsed); diff --git a/src/repo/index.ts b/src/repo/index.ts index 70750b5..a65b501 100644 --- a/src/repo/index.ts +++ b/src/repo/index.ts @@ -1,5 +1,5 @@ -import { GITHUB_USER_CONTENT_BASE_URL, REPO_CLONE_URL } from '../utils/constants'; +import { DEFAULT_REPO_DISK_LOCATION, GITHUB_USER_CONTENT_BASE_URL, REPO_CLONE_URL } from '../utils/constants'; import { getDirectories, readAndParseJson } from '../utils/filesystem'; import { push } from '../utils/git'; import fs from 'fs'; @@ -116,4 +116,20 @@ export function getAssetMaps(networkCode: string, address: string): AssetMap[] { return readAndParseJson(assetMapInfoFile) .map(map => new AssetMap(map)); +} + +export function addAssetMap(map: AssetMap, repoPath: string = DEFAULT_REPO_DISK_LOCATION) { + const assetMapInfoFile = path.join(repoPath, getDirPathForTokenlist(map.fromNetwork, map.fromAddress), 'maps.json'); + + if(!fs.existsSync(assetMapInfoFile)) { + fs.writeFileSync(assetMapInfoFile, JSON.stringify([map], null, 2)); + return; + } + + const assetMaps = readAndParseJson(assetMapInfoFile) + .map(map => new AssetMap(map)); + + assetMaps.push(map); + + fs.writeFileSync(assetMapInfoFile, JSON.stringify(assetMaps, null, 2)); } \ No newline at end of file