forked from tinymanorg/asa-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
103 lines (81 loc) · 2.49 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
import path from "path";
import { readdir, mkdir, rm } 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_ID_MATCHER = /\d+/;
const currentDirname = path.dirname(__filename);
const buildDirectory = path.join(currentDirname, "build");
try {
if (existsSync(buildDirectory)) {
await rm(buildDirectory, { recursive: true });
}
await mkdir(buildDirectory);
const files = await readdir(currentDirname);
for (const file of files) {
try {
const folderFiles = await readdir(path.join(currentDirname, file));
if (folderFiles.some((folderFile) => folderFile.includes("icon.png"))) {
copyDirectorySync(file, buildDirectory);
}
} catch (error) {
console.error(error);
throw error;
}
}
} 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) {
let targetDirName = createTargetDirectoryName(path.basename(source));
var files = [];
// Check if folder needs to be created or integrated
var targetFolder = path.join(target, targetDirName);
if (!existsSync(targetFolder)) {
console.log(`┏━━━ Creating ${targetFolder} ━━━━━━━━━━━━`);
mkdirSync(targetFolder);
}
// Copy source
if (lstatSync(source).isDirectory()) {
console.log(
`
Copying contents of ${source} into ${targetFolder} ━━━━━━━━━━━━`
);
files = readdirSync(source);
files.forEach(function (file) {
var 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} ━━━━━━━━━━━━
`);
}
}