-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.js
67 lines (60 loc) · 1.78 KB
/
utils.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
const fs = require('fs');
const path = require('path');
const jsoncParser = require('jsonc-parser');
const locales = ['ko', 'ja', 'zh-CN', 'zh-TW'];
function traverseDirectory(dir) {
const files = [];
if (!fs.existsSync(dir)) return [];
fs.readdirSync(dir).forEach((file) => {
const fullPath = path.join(dir, file);
if (fs.lstatSync(fullPath).isDirectory()) {
files.push(...traverseDirectory(fullPath));
} else {
files.push(fullPath);
}
});
return files;
}
function getBgmDatabase(dir) {
const bgmCollection = [];
traverseDirectory(dir)
.filter((fullPath) => path.extname(fullPath) === '.json')
.forEach((fullPath) => {
bgmCollection.push(...JSON.parse(fs.readFileSync(fullPath, 'utf8')));
});
return bgmCollection;
}
function mergeLocaleData(db) {
locales.forEach((locale) => {
const localeDb = getBgmDatabase(`./locale/${locale}`);
localeDb.forEach((song) => {
const { filename } = song;
const entryInDb = db.find((dbSong) => dbSong.filename === filename);
if (entryInDb) {
entryInDb.locale = entryInDb.locale || {};
entryInDb.locale[locale] = song;
}
});
});
}
function getPlaylists(dir) {
const playlists = [];
traverseDirectory(dir)
.filter((fullPath) => path.extname(fullPath) === '.jsonc')
.forEach((fullPath) => {
playlists.push(jsoncParser.parse(fs.readFileSync(fullPath, 'utf8')));
});
return playlists;
}
function mergeBgmDatabase(dir, playlistDir) {
const db = getBgmDatabase(dir);
mergeLocaleData(db);
fs.writeFileSync('bgm.min.json', JSON.stringify(db));
const playlists = getPlaylists(playlistDir);
fs.writeFileSync('playlist.min.json', JSON.stringify(playlists));
}
module.exports = {
traverseDirectory,
getBgmDatabase,
mergeBgmDatabase,
};