-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
58 lines (47 loc) · 1.79 KB
/
build.js
File metadata and controls
58 lines (47 loc) · 1.79 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
import { execSync } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import pc from 'picocolors';
const CONFIG = {
entry: 'index.js',
outDir: 'dist',
buildDir: 'build',
finalName: 'webstray',
targets: 'node20-win-x64,node20-linux-x64,node20-macos-x64,node20-macos-arm64',
};
const run = async () => {
try {
console.log(pc.cyan('Starting build process...'));
await fs.emptyDir(CONFIG.outDir);
await fs.emptyDir(CONFIG.buildDir);
console.log(pc.green('Folders "dist" and "build" have been cleared.'));
console.log(pc.yellow('Bundling with esbuild...'));
const bundlePath = path.join(CONFIG.outDir, 'bundle.js');
execSync(
`npx esbuild ${CONFIG.entry} --bundle --platform=node --format=esm --outfile=${bundlePath}`,
{ stdio: 'inherit' },
);
console.log(pc.yellow('\nPackaging into binaries with pkg...'));
execSync(
`npx @yao-pkg/pkg ${bundlePath} --targets ${CONFIG.targets} --out-path ${CONFIG.buildDir} --compress GZip --public --no-bytecode`,
{ stdio: 'inherit' },
);
console.log(pc.yellow('\nRenaming files...'));
const files = await fs.readdir(CONFIG.buildDir);
for (const file of files) {
if (file.startsWith('bundle')) {
const oldPath = path.join(CONFIG.buildDir, file);
let newFileName = file.replace('bundle', 'webstray');
await fs.rename(oldPath, path.join(CONFIG.buildDir, newFileName));
console.log(`${pc.gray(' - ')}${file} → ${pc.white(newFileName)}`);
}
}
await fs.remove(CONFIG.outDir);
console.log(pc.green('\nBuild completed successfully!'));
console.log(pc.cyan(`All files are located in ./${CONFIG.buildDir}`));
} catch (err) {
console.error(pc.red('\nBuild error:'), err.message);
process.exit(1);
}
};
await run();