-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathutils_code_server.ts
More file actions
165 lines (143 loc) · 4.36 KB
/
utils_code_server.ts
File metadata and controls
165 lines (143 loc) · 4.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { spawn, ChildProcess, execSync } from 'child_process'
import path from 'path'
import fs from 'fs-extra'
export interface CodeServerContext {
codeServerProcess: ChildProcess
codeServerPort: number
tempDir: string
}
/**
* @param tempDir - The temporary directory to use for the code-server instance
* @param placeFileWithPythonInterpreter - Whether to place a vscode/settings.json file in the temp directory that points to the python interpreter of the environmen the test is running in.
* @returns The code-server context
*/
export async function startCodeServer(
tempDir: string,
placeFileWithPythonInterpreter: boolean = false,
): Promise<CodeServerContext> {
// Find an available port
const codeServerPort = Math.floor(Math.random() * 10000) + 50000
// Create .vscode/settings.json with Python interpreter if requested
if (placeFileWithPythonInterpreter) {
const vscodeDir = path.join(tempDir, '.vscode')
await fs.ensureDir(vscodeDir)
// Get the current Python interpreter path
const pythonPath = execSync('which python', {
encoding: 'utf-8',
}).trim()
const settings = {
'python.defaultInterpreterPath': path.join(
__dirname,
'..',
'..',
'..',
'.venv',
'bin',
'python',
),
}
await fs.writeJson(path.join(vscodeDir, 'settings.json'), settings, {
spaces: 2,
})
console.log(
`Created .vscode/settings.json with Python interpreter: ${pythonPath}`,
)
}
// Get the extension version from package.json
const extensionDir = path.join(__dirname, '..')
const packageJson = JSON.parse(
fs.readFileSync(path.join(extensionDir, 'package.json'), 'utf-8'),
)
const version = packageJson.version
const extensionName = packageJson.name || 'sqlmesh'
// Look for the specific version .vsix file
const vsixFileName = `${extensionName}-${version}.vsix`
const vsixPath = path.join(extensionDir, vsixFileName)
if (!fs.existsSync(vsixPath)) {
throw new Error(
`Extension file ${vsixFileName} not found. Run "pnpm run vscode:package" first.`,
)
}
console.log(`Using extension: ${vsixFileName}`)
// Install the extension first
const extensionsDir = path.join(tempDir, 'extensions')
console.log('Installing extension...')
execSync(
`pnpm run code-server --user-data-dir "${tempDir}" --extensions-dir "${extensionsDir}" --install-extension "${vsixPath}"`,
{ stdio: 'inherit' },
)
// Start code-server instance
const codeServerProcess = spawn(
'pnpm',
[
'run',
'code-server',
'--bind-addr',
`127.0.0.1:${codeServerPort}`,
'--auth',
'none',
'--disable-telemetry',
'--disable-update-check',
'--disable-workspace-trust',
'--user-data-dir',
tempDir,
'--extensions-dir',
extensionsDir,
tempDir,
],
{
stdio: 'pipe',
cwd: path.join(__dirname, '..'),
},
)
// Wait for code-server to be ready
await new Promise<void>((resolve, reject) => {
let output = ''
const timeout = setTimeout(() => {
reject(new Error('Code-server failed to start within timeout'))
}, 30000)
codeServerProcess.stdout?.on('data', data => {
output += data.toString()
if (output.includes('HTTP server listening on')) {
clearTimeout(timeout)
resolve()
}
})
codeServerProcess.stderr?.on('data', data => {
console.error('Code-server stderr:', data.toString())
})
codeServerProcess.on('error', error => {
clearTimeout(timeout)
reject(error)
})
codeServerProcess.on('exit', code => {
if (code !== 0) {
clearTimeout(timeout)
reject(new Error(`Code-server exited with code ${code}`))
}
})
})
return { codeServerProcess, codeServerPort, tempDir }
}
export async function stopCodeServer(
context: CodeServerContext,
): Promise<void> {
const { codeServerProcess, tempDir } = context
// Clean up code-server process
codeServerProcess.kill('SIGTERM')
// Wait for process to exit
await new Promise<void>(resolve => {
codeServerProcess.on('exit', () => {
resolve()
})
// Force kill after 5 seconds
setTimeout(() => {
if (!codeServerProcess.killed) {
codeServerProcess.kill('SIGKILL')
}
resolve()
}, 5000)
})
// Clean up temporary directory
await fs.remove(tempDir)
}