Skip to content

Commit 36e1773

Browse files
committed
refactor: migrate sound data generation logic from build.ts to apps/backend/scripts/build.ts, enhancing project structure and maintainability
1 parent 3ca4d60 commit 36e1773

File tree

3 files changed

+92
-79
lines changed

3 files changed

+92
-79
lines changed

apps/backend/scripts/build.ts

Lines changed: 91 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,94 @@
1-
import { $, build } from 'bun';
2-
3-
await $`rm -rf dist`;
4-
5-
const optionalRequirePackages = [
6-
'class-transformer',
7-
'class-transformer/storage',
8-
'class-validator',
9-
'@nestjs/microservices',
10-
'@nestjs/websockets',
11-
'@fastify/static',
12-
];
13-
14-
const result = await build({
15-
entrypoints: ['./src/main.ts'],
16-
outdir: './dist',
17-
target: 'bun',
18-
minify: false,
19-
sourcemap: 'linked',
20-
external: optionalRequirePackages.filter((pkg) => {
21-
try {
22-
require(pkg);
23-
return false;
24-
} catch (_) {
25-
return true;
1+
import * as Bun from 'bun';
2+
import { existsSync, mkdirSync, writeFileSync } from 'fs';
3+
import { resolve } from 'path';
4+
5+
import { getLatestVersionSoundList } from '@nbw/sounds';
6+
7+
const writeSoundList = async () => {
8+
function writeJSONFile(
9+
dir: string,
10+
fileName: string,
11+
data: Record<string, any> | string[],
12+
) {
13+
const path = resolve(dir, fileName);
14+
const jsonString = JSON.stringify(data, null, 0);
15+
writeFileSync(path, jsonString);
16+
}
17+
18+
const dataDir = resolve(__dirname, '..', 'public', 'data');
19+
20+
const soundListPath = 'soundList.json';
21+
22+
// Try to create the output directory if it doesn't exist
23+
try {
24+
mkdirSync(dataDir, { recursive: true });
25+
} catch (err) {
26+
if (err instanceof Error && err.message.includes('EEXIST')) {
27+
console.log('Sound data files already exist; skipping generation.');
28+
process.exit(0);
2629
}
27-
}),
28-
splitting: true,
29-
});
30+
}
31+
32+
// If the files already exist, exit early
33+
const files = [soundListPath].map((fileName) => resolve(dataDir, fileName));
34+
35+
if (files.every((file) => existsSync(file))) {
36+
console.log('Sound data files already exist; skipping generation.');
37+
process.exit(0);
38+
}
39+
40+
console.log('Generating sound data files...');
41+
42+
// Write list of sounds in the latest MC version to a JSON file
43+
// Filter the list to only include sounds that match the chosen patterns
44+
// (defined in the shared/ module)
45+
getLatestVersionSoundList().then((soundList) => {
46+
writeJSONFile(dataDir, soundListPath, soundList);
47+
});
48+
};
49+
50+
const build = async () => {
51+
await Bun.$`rm -rf dist`;
52+
53+
const optionalRequirePackages = [
54+
'class-transformer',
55+
'class-transformer/storage',
56+
'class-validator',
57+
'@nestjs/microservices',
58+
'@nestjs/websockets',
59+
'@fastify/static',
60+
];
61+
62+
const result = await Bun.build({
63+
entrypoints: ['./src/main.ts'],
64+
outdir: './dist',
65+
target: 'bun',
66+
minify: false,
67+
sourcemap: 'linked',
68+
external: optionalRequirePackages.filter((pkg) => {
69+
try {
70+
require(pkg);
71+
return false;
72+
} catch (_) {
73+
return true;
74+
}
75+
}),
76+
splitting: true,
77+
});
78+
79+
if (!result.success) {
80+
console.log(result.logs[0]);
81+
process.exit(1);
82+
}
3083

31-
if (!result.success) {
32-
console.log(result.logs[0]);
33-
process.exit(1);
34-
}
84+
console.log('Built successfully!');
85+
};
3586

36-
console.log('Built successfully!');
87+
build()
88+
.then(() => {
89+
writeSoundList();
90+
})
91+
.catch((err) => {
92+
console.error(err);
93+
process.exit(1);
94+
});

build.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555
"test": "cd ./apps/backend && bun test",
5656
"cy:open": "bun run test:cy",
5757
"test:cy": "cd ./tests && bun run cy:open",
58-
"prettier": "prettier --write .",
59-
"build:data": "bun ./build.ts"
58+
"prettier": "prettier --write ."
6059
},
6160
"keywords": [],
6261
"author": "",

0 commit comments

Comments
 (0)