Skip to content

Commit 7bac72c

Browse files
fix: Add ESM banner for __dirname/__filename compatibility
esbuild was tree-shaking the ESM polyfill for __dirname and __filename, causing "ReferenceError: __dirname is not defined" in compiled CLI files. - Add ESM banner to esbuild.config.js that injects the polyfill - Remove redundant manual polyfills from source files - Add claude-sm-danger wrapper for --dangerously-skip-permissions
1 parent 906df8f commit 7bac72c

6 files changed

Lines changed: 55 additions & 10 deletions

File tree

bin/claude-smd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Claude-SM-Danger CLI Launcher (ESM)
4+
* Delegates to built CLI in dist without requiring tsx.
5+
*/
6+
import('../dist/cli/claude-sm-danger.js');

esbuild.config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ import { glob } from 'glob';
44

55
// Get all TypeScript files except tests
66
const entryPoints = glob.sync('src/**/*.ts', {
7-
ignore: ['**/*.test.ts', '**/*.spec.ts', '**/__tests__/**']
7+
ignore: ['**/*.test.ts', '**/*.spec.ts', '**/__tests__/**'],
88
});
99

10+
// ESM polyfill for __dirname and __filename
11+
const esmBanner = `import { fileURLToPath as __fileURLToPath } from 'url';
12+
import { dirname as __pathDirname } from 'path';
13+
const __filename = __fileURLToPath(import.meta.url);
14+
const __dirname = __pathDirname(__filename);`;
15+
1016
// Build configuration
1117
const buildConfig = {
1218
entryPoints,
@@ -19,6 +25,9 @@ const buildConfig = {
1925
logLevel: 'info',
2026
preserveSymlinks: false,
2127
splitting: false,
28+
banner: {
29+
js: esmBanner,
30+
},
2231
};
2332

2433
// Build function
@@ -37,4 +46,4 @@ if (import.meta.url === `file://${process.argv[1]}`) {
3746
build();
3847
}
3948

40-
export { buildConfig, build };
49+
export { buildConfig, build };

src/cli/claude-sm-danger.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* claude-sm-danger: Claude-SM wrapper with --dangerously-skip-permissions
5+
* Shorthand for: claude-sm --dangerously-skip-permissions [args]
6+
*/
7+
8+
import { spawn } from 'child_process';
9+
import * as path from 'path';
10+
11+
// __filename and __dirname are provided by esbuild banner for ESM compatibility
12+
13+
// Get the claude-sm script path
14+
const claudeSmPath = path.join(__dirname, 'claude-sm.js');
15+
16+
// Prepend the danger flag to all args
17+
const args = ['--dangerously-skip-permissions', ...process.argv.slice(2)];
18+
19+
// Spawn claude-sm with the danger flag
20+
const child = spawn('node', [claudeSmPath, ...args], {
21+
stdio: 'inherit',
22+
env: process.env,
23+
});
24+
25+
child.on('exit', (code) => {
26+
process.exit(code || 0);
27+
});
28+
29+
child.on('error', (err) => {
30+
console.error('Failed to launch claude-sm:', err.message);
31+
process.exit(1);
32+
});

src/cli/claude-sm.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { v4 as uuidv4 } from 'uuid';
1414
import chalk from 'chalk';
1515
import { initializeTracing, trace } from '../core/trace/index.js';
1616

17+
// __filename and __dirname are provided by esbuild banner for ESM compatibility
18+
1719
interface ClaudeConfig {
1820
instanceId: string;
1921
worktreePath?: string;

src/cli/commands/shell.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ import {
1515
chmodSync,
1616
appendFileSync,
1717
} from 'fs';
18-
import { join, dirname } from 'path';
19-
import { fileURLToPath } from 'url';
18+
import { join } from 'path';
2019

21-
const __filename = fileURLToPath(import.meta.url);
22-
const __dirname = dirname(__filename);
20+
// __filename and __dirname are provided by esbuild banner for ESM compatibility
2321

2422
function getShellType(): 'zsh' | 'bash' | 'unknown' {
2523
const shell = process.env.SHELL || '';

src/cli/commands/sweep.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import {
1919
copyFileSync,
2020
chmodSync,
2121
} from 'fs';
22-
import { join, dirname } from 'path';
23-
import { fileURLToPath } from 'url';
22+
import { join } from 'path';
2423
import { spawn, execSync } from 'child_process';
2524

26-
const __filename = fileURLToPath(import.meta.url);
27-
const __dirname = dirname(__filename);
25+
// __filename and __dirname are provided by esbuild banner for ESM compatibility
2826

2927
interface SweepStatus {
3028
installed: boolean;

0 commit comments

Comments
 (0)