-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathclaude-runtime.mjs
More file actions
executable file
·140 lines (124 loc) · 4.54 KB
/
Copy pathclaude-runtime.mjs
File metadata and controls
executable file
·140 lines (124 loc) · 4.54 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
/**
* Claude Runtime Switcher
*
* Experimental tool to switch Claude CLI between Node.js and Bun runtime.
* This modifies the Claude CLI script's shebang line to use either node or bun.
*
* Usage:
* ./claude-runtime.mjs --to-bun # Switch Claude to use Bun
* ./claude-runtime.mjs --to-node # Switch Claude to use Node.js
* ./claude-runtime.mjs --status # Check current runtime
*/
// Use use-m to dynamically import modules for cross-runtime compatibility
if (typeof use === 'undefined') {
globalThis.use = (await eval(await (await fetch('https://unpkg.com/use-m/use.js')).text())).use;
}
const yargsModule = await use('yargs@17.7.2');
const yargs = yargsModule.default || yargsModule;
const { hideBin } = await use('yargs@17.7.2/helpers');
// Import Claude library functions
const claudeLib = await import('./claude.lib.mjs');
const { handleClaudeRuntimeSwitch } = claudeLib;
// Configure command line arguments
const argv = yargs(hideBin(process.argv))
.usage('Usage: $0 [options]')
.option('to-bun', {
type: 'boolean',
description: 'Switch Claude CLI to run with Bun instead of Node.js',
conflicts: ['to-node'],
})
.option('to-node', {
type: 'boolean',
description: 'Switch Claude CLI to run with Node.js instead of Bun',
conflicts: ['to-bun'],
})
.option('status', {
type: 'boolean',
description: 'Check current Claude runtime configuration',
})
.help('h')
.alias('h', 'help')
.strict().argv;
// Main execution
async function main() {
// Translate options to match what handleClaudeRuntimeSwitch expects
const options = {
'force-claude-bun-run': argv.toBun,
'force-claude-nodejs-run': argv.toNode,
};
if (argv.status) {
// Check current status
const { execSync } = await import('child_process');
const { $ } = await use('command-stream');
try {
// Find Claude CLI location
const whichResult = await $`which claude`;
let claudePath = '';
for await (const chunk of whichResult.stream()) {
if (chunk.type === 'stdout') {
claudePath = chunk.data.toString().trim();
}
}
if (!claudePath) {
console.log('❌ Claude CLI not found in PATH');
process.exit(1);
}
console.log(`📍 Claude CLI location: ${claudePath}`);
// Read the shebang line
const fs = (await use('fs')).promises;
const content = await fs.readFile(claudePath, 'utf8');
const firstLine = content.split('\n')[0];
console.log(`📜 Shebang line: ${firstLine}`);
if (firstLine.includes('bun')) {
console.log('🚀 Current runtime: Bun');
} else if (firstLine.includes('node')) {
console.log('🟢 Current runtime: Node.js');
} else {
console.log('❓ Current runtime: Unknown');
}
// Check if runtimes are available
try {
execSync('which bun', { stdio: 'ignore' });
console.log('✅ Bun is available');
} catch {
console.log('❌ Bun is not installed');
}
try {
execSync('which node', { stdio: 'ignore' });
console.log('✅ Node.js is available');
} catch {
console.log('❌ Node.js is not installed');
}
} catch (error) {
console.error(`Error checking status: ${error.message}`);
process.exit(1);
}
} else if (argv.toBun || argv.toNode) {
// Perform runtime switch
await handleClaudeRuntimeSwitch(options);
if (argv.toBun) {
console.log('\n✅ Claude CLI has been switched to Bun runtime');
console.log(' You can now use Claude with improved performance');
console.log(' To switch back, run: ./claude-runtime.mjs --to-node');
} else {
console.log('\n✅ Claude CLI has been restored to Node.js runtime');
console.log(' This is the default and most compatible configuration');
console.log(' To switch to Bun, run: ./claude-runtime.mjs --to-bun');
}
} else {
// No action specified
console.log('Claude Runtime Switcher - Experimental Tool\n');
console.log('Usage:');
console.log(' ./claude-runtime.mjs --to-bun # Switch to Bun runtime');
console.log(' ./claude-runtime.mjs --to-node # Switch to Node.js runtime');
console.log(' ./claude-runtime.mjs --status # Check current runtime\n');
console.log('⚠️ WARNING: This is experimental and may break Claude CLI');
console.log(' Always keep a backup or know how to reinstall Claude');
}
}
// Run main function
main().catch(error => {
console.error(`❌ Error: ${error.message}`);
process.exit(1);
});