Skip to content

Commit 447c38b

Browse files
committed
Add CommonJS wrapper for Windows path handling
1 parent ab616d4 commit 447c38b

2 files changed

Lines changed: 48 additions & 10 deletions

File tree

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
@echo off
22
REM OpenCode launcher for Node.js on Windows
3+
REM Uses the Node.js wrapper script to handle path conversions
34

45
setlocal
56

67
REM Get the directory where this script is located
7-
set SCRIPT_DIR=%~dp0
8+
set "SCRIPT_DIR=%~dp0"
89

9-
REM Remove trailing backslash
10-
set SCRIPT_DIR=%SCRIPT_DIR:~0,-1%
11-
12-
REM Convert Windows path to file:// URL for loader
13-
set LOADER_PATH=file:///%SCRIPT_DIR:\=/%/loader.mjs
14-
set SHIM_PATH=file:///%SCRIPT_DIR:\=/%/global-bun-shim.mjs
15-
16-
REM Build the node command with all arguments
17-
node --loader "%LOADER_PATH%" --conditions=browser --import "%SHIM_PATH%" --import tsx/esm "%SCRIPT_DIR%\src\index.ts" %*
10+
REM Run the Node.js wrapper which handles all the path conversions
11+
node "%SCRIPT_DIR%opencode-node.cjs" %*
1812

1913
endlocal
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* OpenCode launcher for Windows
5+
* Handles path conversions for Windows file:// URLs
6+
*/
7+
8+
const { spawn } = require('child_process');
9+
const path = require('path');
10+
const { pathToFileURL } = require('url');
11+
12+
// Get the script directory
13+
const scriptDir = __dirname;
14+
15+
// Convert paths to file:// URLs
16+
const loaderPath = pathToFileURL(path.join(scriptDir, 'loader.mjs')).href;
17+
const shimPath = pathToFileURL(path.join(scriptDir, 'global-bun-shim.mjs')).href;
18+
const indexPath = path.join(scriptDir, 'src', 'index.ts');
19+
20+
// Build the arguments
21+
const args = [
22+
'--loader', loaderPath,
23+
'--conditions=browser',
24+
'--import', shimPath,
25+
'--import', 'tsx/esm',
26+
indexPath,
27+
...process.argv.slice(2) // Forward all arguments
28+
];
29+
30+
// Spawn Node.js
31+
const child = spawn('node', args, {
32+
stdio: 'inherit',
33+
windowsHide: false
34+
});
35+
36+
// Forward exit code
37+
child.on('exit', (code) => {
38+
process.exit(code || 0);
39+
});
40+
41+
child.on('error', (err) => {
42+
console.error('Failed to start OpenCode:', err);
43+
process.exit(1);
44+
});

0 commit comments

Comments
 (0)