-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-cli.js
More file actions
55 lines (45 loc) · 1.36 KB
/
build-cli.js
File metadata and controls
55 lines (45 loc) · 1.36 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
import { build } from 'esbuild';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { promises as fs } from 'node:fs';
const __dirname = dirname(fileURLToPath(import.meta.url));
async function buildCli() {
try {
// Ensure the bin directory exists
await fs.mkdir(join(__dirname, 'bin'), { recursive: true });
// Build the CLI
await build({
entryPoints: [join(__dirname, 'src/cli.ts')],
bundle: true,
platform: 'node',
target: 'node18',
outfile: join(__dirname, 'bin/cli.js'),
format: 'esm',
external: [
'commander',
'node:*',
'node:events',
'node:fs',
'node:path',
'node:url',
'node:util',
'node:process'
],
});
// Fix shebang in the output file
const cliPath = join(__dirname, 'bin/cli.js');
let content = await fs.readFile(cliPath, 'utf-8');
// Remove any existing shebang lines
content = content.replace(/^#!.*\n/gm, '');
// Add single shebang at the start
content = `#!/usr/bin/env node\n${content}`;
await fs.writeFile(cliPath, content);
// Make the file executable
await fs.chmod(cliPath, 0o755);
console.log('CLI built successfully');
} catch (error) {
console.error('Error building CLI:', error);
process.exit(1);
}
}
buildCli();