generated from project-error/fivem-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.dev.js
More file actions
79 lines (66 loc) · 2.12 KB
/
build.dev.js
File metadata and controls
79 lines (66 loc) · 2.12 KB
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
const chokidar = require('chokidar');
const { build } = require('esbuild');
const path = require('path');
const fs = require('fs');
function getAllFiles(folder) {
let result = [];
const files = fs.readdirSync(folder);
for (const file of files) {
const filePath = path.join(folder, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
result = result.concat(getAllFiles(filePath));
} else if (file.endsWith('.ts')) {
result.push(filePath);
}
}
return result;
}
async function buildFolder(folder, outdir, target, format, platform) {
try {
const entryPoints = getAllFiles(folder);
await build({
entryPoints,
bundle: true,
outdir,
target,
format,
platform,
minify: false,
});
const folderName = folder.includes('server') ? 'server' : 'client';
console.log(
(await import('chalk')).default.green(
`[${folderName}]: Built successfully!`,
),
);
} catch (error) {
console.error(error);
process.exit(1);
}
}
async function watchFolder(folder, outDir, target, format, platform) {
const chalk = (await import('chalk')).default;
await buildFolder(folder, outDir, target, format, platform);
const watcher = chokidar.watch(`${folder}/**/*.ts`, {
ignoreInitial: true,
});
console.log(chalk.yellow(`[${folder}]: Watching for changes...`));
watcher.on('change', async (filePath) => {
console.log(chalk.yellow(`[${folder}]: File changed: ${filePath}`));
await buildFolder(folder, outDir, target, format, platform);
});
}
async function watchAll() {
const folders = ['server', 'client'];
for (const folder of folders) {
await watchFolder(
folder,
path.join(__dirname, 'dist', folder),
folder === 'client' ? ['chrome58'] : undefined,
folder === 'client' ? 'iife' : 'cjs',
folder === 'server' ? 'node' : undefined,
);
}
}
watchAll();