Skip to content

Commit

Permalink
Merge pull request tinymanorg#141 from tinymanorg/feat/generate-json-…
Browse files Browse the repository at this point in the history
…for-logos

Generate JSON file that map asset ids to S3 urls of logos
  • Loading branch information
edizcelik authored Dec 20, 2021
2 parents a8317b8 + 52a99c5 commit 17ab6d1
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import { readdir, mkdir, rm } from "fs/promises";
import { readdir, mkdir, rm, appendFile } from "fs/promises";
import {
existsSync,
mkdirSync,
Expand All @@ -13,9 +13,11 @@ const __filename = fileURLToPath(import.meta.url);

const algoIconsFolderName = "ALGO";

const ASSET_LOGO_BASE_URL = "https://asa-icons.s3.eu-central-1.amazonaws.com";
const ASSET_ID_MATCHER = /\d+/;
const currentDirname = path.dirname(__filename);
const buildDirectory = path.join(currentDirname, "build");
const ASSET_LOGO_MAP = new Map();

try {
if (existsSync(buildDirectory)) {
Expand All @@ -31,14 +33,27 @@ try {
const folderFiles = await readdir(path.join(currentDirname, file));

if (folderFiles.some((folderFile) => folderFile.includes("icon.png"))) {
copyDirectorySync(file, buildDirectory);
const targetDirName = createTargetDirectoryName(path.basename(file));

copyDirectorySync(file, buildDirectory, targetDirName);

ASSET_LOGO_MAP.set(targetDirName, getAssetLogo(targetDirName));
}
}
} catch (error) {
console.error(error);
throw error;
}
}

// Create JSON file and save it under /build directory
console.log(`┏━━━ Creating logos.json ━━━━━━━━━━━━`);

const assetLogos = Object.fromEntries(ASSET_LOGO_MAP);
const assetLogosJSON = JSON.stringify(assetLogos, null, "\t");

await appendFile(path.join(buildDirectory, "logos.json"), assetLogosJSON);
console.log(`━━━━━━━━━━━━ Finished ━━━━━━━━━━━━`);
} catch (error) {
console.error(error);
throw error;
Expand All @@ -62,8 +77,7 @@ function createTargetDirectoryName(source) {
return sourceDirName;
}

function copyDirectorySync(source, target) {
let targetDirName = createTargetDirectoryName(path.basename(source));
function copyDirectorySync(source, target, targetDirName) {
let files = [];
const targetFolder = path.join(target, targetDirName);

Expand Down Expand Up @@ -100,3 +114,10 @@ function copyDirectorySync(source, target) {
`);
}
}

/**
* Generates URL for the asset's logo
*/
function getAssetLogo(assetId) {
return `${ASSET_LOGO_BASE_URL}/${assetId}/icon.png`;
}

0 comments on commit 17ab6d1

Please sign in to comment.