-
Notifications
You must be signed in to change notification settings - Fork 5
/
manifest-generator.js
118 lines (89 loc) · 2.39 KB
/
manifest-generator.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
116
117
118
const fs = require("fs");
const path = require("path");
async function traverseFiles(curPath, cb) {
const dir = await fs.promises.opendir(curPath);
for await (const dirent of dir) {
const absPath = path.join(curPath, dirent.name);
if (dirent.isDirectory()) {
await traverseFiles(absPath, cb);
} else if (dirent.isFile()) {
cb(dirent, absPath);
}
}
}
async function main() {
const projectPath = path.resolve(__dirname, "..");
const assetsPath = path.join(projectPath, "Audio");
const manifest = {
name: "Hubs Sound Pack",
searchPlaceholder: "Search sounds...",
assets: [],
tags: []
};
await traverseFiles(assetsPath, (dirent, absPath) => {
if (path.extname(absPath) !== ".mp3") {
return;
}
const relativePath = path.relative(assetsPath, absPath).replace(/\\/g, "/");
const dirPath = path.parse(relativePath).dir;
const parts = dirPath.split("/");
const tags = [];
if (parts.length === 1) {
tags.push("Full_Mix");
}
while (parts.length > 0) {
tags.push(parts.join("/"));
parts.pop();
}
const fileName = dirent.name.replace(".mp3", "");
manifest.assets.push({
id: fileName,
label: fileName.replace(/_/g, " "),
type: "Audio",
url: relativePath,
tags
})
});
const uniqueTags = new Set();
for (const asset of manifest.assets) {
for (const tag of asset.tags) {
uniqueTags.add(tag);
}
}
const tagTree = [];
for (const tag of uniqueTags) {
const parts = tag.split("/");
let curPart = parts.shift().replace(/_/g, " ");
let curArray = tagTree;
while (curPart) {
let curNode = curArray.find(n => n.label === curPart);
if (!curNode) {
curNode = {
label: curPart,
value: tag
};
curArray.push(curNode);
}
curPart = parts.shift();
if (curPart) {
if (!curNode.children) {
curNode.children = [];
}
curArray = curNode.children;
}
}
}
manifest.tags = tagTree;
const manifestPath = path.join(assetsPath, "asset-manifest.json");
const json = JSON.stringify(manifest, null, 2);
await fs.promises.writeFile(manifestPath, json, "utf8");
console.log(`Wrote manifest to ${manifestPath}`);
}
main()
.then(() => {
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
});