forked from tinymanorg/asa-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
123 lines (96 loc) · 3.28 KB
/
build.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import path from "path";
import { readdir, mkdir, rm, appendFile } from "fs/promises";
import {
existsSync,
mkdirSync,
lstatSync,
readdirSync,
copyFileSync,
} from "fs";
import { fileURLToPath } from "url";
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)) {
await rm(buildDirectory, { recursive: true });
}
await mkdir(buildDirectory);
const files = await readdir(currentDirname);
for (const file of files) {
try {
if (lstatSync(file).isDirectory()) {
const folderFiles = await readdir(path.join(currentDirname, file));
if (folderFiles.some((folderFile) => folderFile.includes("icon.png"))) {
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;
}
/**
* @param {string} source
* @returns Directory name that includes only asset ids
*/
function createTargetDirectoryName(source) {
let sourceDirName = source;
if (sourceDirName.match(ASSET_ID_MATCHER)) {
sourceDirName = sourceDirName.match(ASSET_ID_MATCHER)[0];
} else if (
sourceDirName.toLowerCase() === algoIconsFolderName.toLowerCase()
) {
sourceDirName = "0";
}
return sourceDirName;
}
function copyDirectorySync(source, target, targetDirName) {
let files = [];
const targetFolder = path.join(target, targetDirName);
if (!existsSync(targetFolder)) {
console.log(`┏━━━ Creating ${targetFolder} ━━━━━━━━━━━━`);
mkdirSync(targetFolder);
}
if (lstatSync(source).isDirectory()) {
console.log(
`
Copying contents of ${source} into ${targetFolder} ━━━━━━━━━━━━`
);
files = readdirSync(source);
files.forEach(function (file) {
const curSource = path.join(source, file);
if (lstatSync(curSource).isDirectory()) {
copyDirectorySync(curSource, targetFolder);
} else {
console.log(`••••• Copying ${curSource}`);
copyFileSync(curSource, path.join(targetFolder, file));
}
});
console.log(`
━━━━━━━━━━━━ Finished copying ${source} ━━━━━━━━━━━━
`);
}
}
/**
* Generates URL for the asset's logo
*/
function getAssetLogo(assetId) {
return `${ASSET_LOGO_BASE_URL}/${assetId}/icon.png`;
}