-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssetLoader.js
115 lines (106 loc) · 3.42 KB
/
AssetLoader.js
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
const fs = require("node:fs");
const path = require("node:path");
class AssetLoader {
assetDirs = ["./src/assets"];
// TODO: caching
constructor(...sourceDirectories) {
if (sourceDirectories.length != 0) this.assetDirs = sourceDirectories;
}
load() {
return new Promise(async (res) => {
var mapDirectories = (
directory,
parentObj = this,
currDirName = null
) => {
return new Promise(async (r) => {
var files = (await this.readDir(directory)).flat(1);
var topLevel = false;
if (!currDirName) {
currDirName = "all";
topLevel = true;
}
if (!parentObj[currDirName])
parentObj[currDirName] = {
assetMap: new Map(),
getPath: function (p) {
if (p.includes("/")) {
let dirs = p.split("/");
let getProp = (ds, o = this) => {
if (ds.length == 1) {
return o[ds[0]];
} else {
let d = ds.shift();
return getProp(ds, o[d]);
}
};
return getProp(dirs.slice(0, dirs.length - 1)).getPath(
dirs[dirs.length - 1]
);
}
return this.assetMap.get(path.parse(p).name);
},
get: function (path) {
let fp = this.getPath(path);
if (!fp) return null;
return fs.readFileSync(fp);
},
getAsync: function (path) {
let fp = this.getPath(path);
if (!fp) return null;
return new Promise((res, rej) => {
fs.readFile(fp, (err, data) => {
if (err) return rej(err);
return res(data);
});
});
},
};
for (let i = 0; i < files.length; i++) {
let file = files[i];
let p = path.parse(file);
let ext = p.ext;
if (!ext) {
let obj = parentObj[currDirName];
await mapDirectories(directory + "/" + file, obj, file);
if (topLevel) {
Object.defineProperty(parentObj, file, {
get: () => {
return parentObj[currDirName][file];
},
});
}
continue;
}
let name = p.name;
parentObj[currDirName].assetMap.set(name, directory + "/" + file);
}
r(parentObj[currDirName]);
});
};
let dirs = [];
for (const d of this.assetDirs) {
dirs.push(mapDirectories(d));
}
await Promise.allSettled(dirs);
res(this);
});
}
readDir(directory) {
return new Promise((res, rej) => {
fs.readdir(directory, (err, files) => {
if (files) return res(files);
rej(err);
});
});
}
}
/*const loader = new AssetLoader();
(async () => {
await loader.load(); // gotta call this only once
console.log(loader.images.get("Spotify/Overlay")); // same as the following examples
console.log(loader.images.Spotify.get("Overlay.png"));
console.log(loader.all.images.get("Spotify/Overlay"));
console.log(loader.all.get("images/Spotify/Overlay.png"));
})();*/
module.exports = AssetLoader;