|
1 | | -import { $ } from 'bun'; |
2 | 1 | import { createHash } from 'crypto'; |
3 | 2 | import { readFileSync, writeFileSync } from 'fs'; |
4 | 3 |
|
5 | 4 | const VERSION = process.env.VERSION ?? 'dev'; |
6 | | - |
7 | | -const targets = [ |
8 | | - { bunTarget: 'bun-linux-x64', platform: 'linux-x64', output: 'minimax-linux-x64' }, |
9 | | - { bunTarget: 'bun-linux-x64-musl', platform: 'linux-x64-musl', output: 'minimax-linux-x64-musl' }, |
10 | | - { bunTarget: 'bun-linux-arm64', platform: 'linux-arm64', output: 'minimax-linux-arm64' }, |
11 | | - { bunTarget: 'bun-linux-arm64-musl', platform: 'linux-arm64-musl', output: 'minimax-linux-arm64-musl' }, |
12 | | - { bunTarget: 'bun-darwin-x64', platform: 'darwin-x64', output: 'minimax-darwin-x64' }, |
13 | | - { bunTarget: 'bun-darwin-arm64', platform: 'darwin-arm64', output: 'minimax-darwin-arm64' }, |
14 | | - { bunTarget: 'bun-windows-x64', platform: 'windows-x64', output: 'minimax-windows-x64.exe' }, |
15 | | -]; |
| 5 | +const OUT = 'dist/minimax.mjs'; |
16 | 6 |
|
17 | 7 | function sha256(path: string): string { |
18 | 8 | return createHash('sha256').update(readFileSync(path)).digest('hex'); |
19 | 9 | } |
20 | 10 |
|
21 | | -console.log(`Building minimax-cli ${VERSION}...\n`); |
22 | | - |
23 | | -const manifest: { |
24 | | - version: string; |
25 | | - platforms: Record<string, { file: string; checksum: string }>; |
26 | | -} = { version: VERSION, platforms: {} }; |
27 | | - |
28 | | -// Platform standalones |
29 | | -for (const { bunTarget, platform, output } of targets) { |
30 | | - const outPath = `dist/${output}`; |
31 | | - process.stdout.write(` ${output}...`); |
32 | | - |
33 | | - await $`bun build src/main.ts \ |
34 | | - --compile \ |
35 | | - --minify \ |
36 | | - --target ${bunTarget} \ |
37 | | - --outfile ${outPath} \ |
38 | | - --define "process.env.CLI_VERSION='${VERSION}'"`.quiet(); |
39 | | - |
40 | | - manifest.platforms[platform] = { file: output, checksum: sha256(outPath) }; |
41 | | - console.log(' ✓'); |
42 | | -} |
43 | | - |
44 | | -// Node.js .mjs bundle (much smaller, requires node >= 18) |
45 | | -process.stdout.write(' minimax.mjs...'); |
46 | | -const mjsPath = 'dist/minimax.mjs'; |
47 | | - |
48 | | -await $`bun build src/main.ts \ |
49 | | - --outfile ${mjsPath} \ |
50 | | - --target node \ |
51 | | - --minify \ |
52 | | - --define "process.env.CLI_VERSION='${VERSION}'"`.quiet(); |
| 11 | +console.log(`Building minimax-cli ${VERSION}...`); |
53 | 12 |
|
54 | | -// Prepend shebang so the file is directly executable |
55 | | -const mjsContent = readFileSync(mjsPath); |
56 | | -writeFileSync(mjsPath, Buffer.concat([Buffer.from('#!/usr/bin/env node\n'), mjsContent])); |
| 13 | +await Bun.build({ |
| 14 | + entrypoints: ['src/main.ts'], |
| 15 | + outdir: 'dist', |
| 16 | + naming: 'minimax.mjs', |
| 17 | + target: 'node', |
| 18 | + minify: true, |
| 19 | + define: { 'process.env.CLI_VERSION': JSON.stringify(VERSION) }, |
| 20 | +}); |
57 | 21 |
|
58 | | -manifest.platforms['node'] = { file: 'minimax.mjs', checksum: sha256(mjsPath) }; |
59 | | -console.log(' ✓'); |
| 22 | +// Prepend shebang |
| 23 | +const content = readFileSync(OUT); |
| 24 | +writeFileSync(OUT, Buffer.concat([Buffer.from('#!/usr/bin/env node\n'), content])); |
60 | 25 |
|
61 | | -writeFileSync('dist/manifest.json', JSON.stringify(manifest, null, 2)); |
62 | | -console.log(' manifest.json ✓'); |
63 | | -console.log(`\nDone. ${targets.length + 1} builds in dist/`); |
| 26 | +writeFileSync('dist/manifest.json', JSON.stringify({ version: VERSION, checksum: sha256(OUT) }, null, 2)); |
| 27 | +console.log(`Done. dist/minimax.mjs ${(content.length / 1024).toFixed(0)}KB`); |
0 commit comments