-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.js
92 lines (74 loc) · 2.22 KB
/
esbuild.config.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
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const { build } = require("esbuild");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const { readFileSync, copyFileSync, existsSync } = require("fs");
const path = require("path");
const bundleDirName = "bundle";
const commandSeparator = "&&";
(async () => {
console.clear();
await createBundle();
await delay(3000);
await setUpBundledProjectEnvironment();
console.log("Done.");
})();
async function createBundle() {
console.log("Building...");
await exec("npm run build");
await delay(3000);
console.log("Bundling...");
build({
entryPoints: ["build/index.js"],
outfile: "bundle/index.js",
mainFields: ["module", "main"],
bundle: true,
platform: "node",
target: "node18",
legalComments: "none",
}).catch(() => process.exit(1));
await exec("rimraf ./build");
}
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
async function setUpBundledProjectEnvironment() {
console.log("Setting up environment...");
console.log("Installing Dependencies...");
const commands = [
`cd ${bundleDirName}`,
`npm init -y`,
`npm install ${getDependencies()}`,
];
await exec(commands.join(` ${commandSeparator} `));
console.log("Adding .env file...");
moveEnvToProject();
console.log("Adding FFMPEG...");
moveFFMPEG();
}
function moveEnvToProject() {
const srcPath = path.join(__dirname, ".env");
const destPath = path.join(__dirname, bundleDirName, ".env");
if (!existsSync(srcPath)) {
throw new Error("Missing the .env file in the project...");
}
copyFileSync(srcPath, destPath);
}
function moveFFMPEG() {
const srcPath = path.join(
__dirname,
"node_modules",
"ffmpeg-static",
"ffmpeg.exe"
);
const destPath = path.join(__dirname, bundleDirName, "ffmpeg.exe");
if (!existsSync(srcPath)) {
throw new Error("Package ffmpeg-static is missing");
}
copyFileSync(srcPath, destPath);
}
function getDependencies() {
const package = readFileSync("package.json", "utf8");
const packageJson = JSON.parse(package);
const dependencies = Object.keys(packageJson.dependencies);
return dependencies.join(" ");
}