|
| 1 | +import { spawn, ChildProcess, execSync } from 'child_process' |
| 2 | +import path from 'path' |
| 3 | +import fs from 'fs-extra' |
| 4 | + |
| 5 | +export interface CodeServerContext { |
| 6 | + codeServerProcess: ChildProcess |
| 7 | + codeServerPort: number |
| 8 | + tempDir: string |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * @param tempDir - The temporary directory to use for the code-server instance |
| 13 | + * @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. |
| 14 | + * @returns The code-server context |
| 15 | + */ |
| 16 | +export async function startCodeServer( |
| 17 | + tempDir: string, |
| 18 | + placeFileWithPythonInterpreter: boolean = false, |
| 19 | +): Promise<CodeServerContext> { |
| 20 | + // Find an available port |
| 21 | + const codeServerPort = Math.floor(Math.random() * 10000) + 50000 |
| 22 | + |
| 23 | + // Create .vscode/settings.json with Python interpreter if requested |
| 24 | + if (placeFileWithPythonInterpreter) { |
| 25 | + const vscodeDir = path.join(tempDir, '.vscode') |
| 26 | + await fs.ensureDir(vscodeDir) |
| 27 | + |
| 28 | + // Get the current Python interpreter path |
| 29 | + const pythonPath = execSync('which python', { |
| 30 | + encoding: 'utf-8', |
| 31 | + }).trim() |
| 32 | + |
| 33 | + const settings = { |
| 34 | + 'python.defaultInterpreterPath': path.join( |
| 35 | + __dirname, |
| 36 | + '..', |
| 37 | + '..', |
| 38 | + '..', |
| 39 | + '.venv', |
| 40 | + 'bin', |
| 41 | + 'python', |
| 42 | + ), |
| 43 | + } |
| 44 | + |
| 45 | + await fs.writeJson(path.join(vscodeDir, 'settings.json'), settings, { |
| 46 | + spaces: 2, |
| 47 | + }) |
| 48 | + console.log( |
| 49 | + `Created .vscode/settings.json with Python interpreter: ${pythonPath}`, |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + // Get the extension version from package.json |
| 54 | + const extensionDir = path.join(__dirname, '..') |
| 55 | + const packageJson = JSON.parse( |
| 56 | + fs.readFileSync(path.join(extensionDir, 'package.json'), 'utf-8'), |
| 57 | + ) |
| 58 | + const version = packageJson.version |
| 59 | + const extensionName = packageJson.name || 'sqlmesh' |
| 60 | + |
| 61 | + // Look for the specific version .vsix file |
| 62 | + const vsixFileName = `${extensionName}-${version}.vsix` |
| 63 | + const vsixPath = path.join(extensionDir, vsixFileName) |
| 64 | + |
| 65 | + if (!fs.existsSync(vsixPath)) { |
| 66 | + throw new Error( |
| 67 | + `Extension file ${vsixFileName} not found. Run "pnpm run vscode:package" first.`, |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + console.log(`Using extension: ${vsixFileName}`) |
| 72 | + |
| 73 | + // Install the extension first |
| 74 | + const extensionsDir = path.join(tempDir, 'extensions') |
| 75 | + console.log('Installing extension...') |
| 76 | + execSync( |
| 77 | + `pnpm run code-server --user-data-dir "${tempDir}" --extensions-dir "${extensionsDir}" --install-extension "${vsixPath}"`, |
| 78 | + { stdio: 'inherit' }, |
| 79 | + ) |
| 80 | + |
| 81 | + // Start code-server instance |
| 82 | + const codeServerProcess = spawn( |
| 83 | + 'pnpm', |
| 84 | + [ |
| 85 | + 'run', |
| 86 | + 'code-server', |
| 87 | + '--bind-addr', |
| 88 | + `127.0.0.1:${codeServerPort}`, |
| 89 | + '--auth', |
| 90 | + 'none', |
| 91 | + '--disable-telemetry', |
| 92 | + '--disable-update-check', |
| 93 | + '--disable-workspace-trust', |
| 94 | + '--user-data-dir', |
| 95 | + tempDir, |
| 96 | + '--extensions-dir', |
| 97 | + extensionsDir, |
| 98 | + tempDir, |
| 99 | + ], |
| 100 | + { |
| 101 | + stdio: 'pipe', |
| 102 | + cwd: path.join(__dirname, '..'), |
| 103 | + }, |
| 104 | + ) |
| 105 | + |
| 106 | + // Wait for code-server to be ready |
| 107 | + await new Promise<void>((resolve, reject) => { |
| 108 | + let output = '' |
| 109 | + const timeout = setTimeout(() => { |
| 110 | + reject(new Error('Code-server failed to start within timeout')) |
| 111 | + }, 30000) |
| 112 | + |
| 113 | + codeServerProcess.stdout?.on('data', data => { |
| 114 | + output += data.toString() |
| 115 | + if (output.includes('HTTP server listening on')) { |
| 116 | + clearTimeout(timeout) |
| 117 | + resolve() |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + codeServerProcess.stderr?.on('data', data => { |
| 122 | + console.error('Code-server stderr:', data.toString()) |
| 123 | + }) |
| 124 | + |
| 125 | + codeServerProcess.on('error', error => { |
| 126 | + clearTimeout(timeout) |
| 127 | + reject(error) |
| 128 | + }) |
| 129 | + |
| 130 | + codeServerProcess.on('exit', code => { |
| 131 | + if (code !== 0) { |
| 132 | + clearTimeout(timeout) |
| 133 | + reject(new Error(`Code-server exited with code ${code}`)) |
| 134 | + } |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + return { codeServerProcess, codeServerPort, tempDir } |
| 139 | +} |
| 140 | + |
| 141 | +export async function stopCodeServer( |
| 142 | + context: CodeServerContext, |
| 143 | +): Promise<void> { |
| 144 | + const { codeServerProcess, tempDir } = context |
| 145 | + |
| 146 | + // Clean up code-server process |
| 147 | + codeServerProcess.kill('SIGTERM') |
| 148 | + |
| 149 | + // Wait for process to exit |
| 150 | + await new Promise<void>(resolve => { |
| 151 | + codeServerProcess.on('exit', () => { |
| 152 | + resolve() |
| 153 | + }) |
| 154 | + // Force kill after 5 seconds |
| 155 | + setTimeout(() => { |
| 156 | + if (!codeServerProcess.killed) { |
| 157 | + codeServerProcess.kill('SIGKILL') |
| 158 | + } |
| 159 | + resolve() |
| 160 | + }, 5000) |
| 161 | + }) |
| 162 | + |
| 163 | + // Clean up temporary directory |
| 164 | + await fs.remove(tempDir) |
| 165 | +} |
0 commit comments