|
| 1 | +import { spawn } from 'child_process'; |
| 2 | +import path from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Creates a new instance of TypeScript Server. |
| 7 | + * @returns {Object} An object containing methods to interact with TypeScript Server. |
| 8 | + */ |
| 9 | +function createTSServerInstance() { |
| 10 | + let tsserverProcess = null; |
| 11 | + let seqNumber = 0; |
| 12 | + const pendingCommands = new Map(); |
| 13 | + |
| 14 | + /** |
| 15 | + * Initializes the TypeScript Server process. |
| 16 | + */ |
| 17 | + function initTSServer() { |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 20 | + const tsserverPath = path.join(__dirname, '..', '..', 'node_modules', 'typescript', 'bin', 'tsserver'); |
| 21 | + const nodePath = '/home/charly/.nvm/versions/node/v20.10.0/bin/node'; |
| 22 | + tsserverProcess = spawn(nodePath, [tsserverPath]); |
| 23 | + tsserverProcess.stdout.setEncoding('utf8'); |
| 24 | + |
| 25 | + tsserverProcess.stdout.on('data', (data) => { |
| 26 | + const lines = data.split('\n'); |
| 27 | + for (const line of lines) { |
| 28 | + if (line.trim().startsWith('{')) { |
| 29 | + const message = JSON.parse(line.trim()); |
| 30 | + if (message.type === 'event' && message.event === 'typingsInstallerPid') { |
| 31 | + // Server is ready |
| 32 | + resolve(); |
| 33 | + } |
| 34 | + onData(line); |
| 35 | + } |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + tsserverProcess.stderr.on('data', (data) => { |
| 40 | + console.error(`stderr: ${data}`); |
| 41 | + }); |
| 42 | + |
| 43 | + tsserverProcess.on('close', (code) => { |
| 44 | + console.log(`tsserver process exited with code ${code}`); |
| 45 | + }); |
| 46 | + |
| 47 | + // Add a timeout for server initialization |
| 48 | + setTimeout(() => { |
| 49 | + reject(new Error('Timeout waiting for tsserver to be ready')); |
| 50 | + }, 10000); // 10 seconds timeout |
| 51 | + }); |
| 52 | + } |
| 53 | + /** |
| 54 | + * Handles incoming data from the TypeScript Server. |
| 55 | + * @param {string} line - A line of data received from tsserver. |
| 56 | + */ |
| 57 | + function onData(line) { |
| 58 | + try { |
| 59 | + console.log(line); |
| 60 | + const response = JSON.parse(line); |
| 61 | + // Check if it's a command response and has a matching sequence number |
| 62 | + if (response.request_seq !== undefined && pendingCommands.has(response.request_seq)) { |
| 63 | + const { resolve } = pendingCommands.get(response.request_seq); |
| 64 | + pendingCommands.delete(response.request_seq); |
| 65 | + resolve(response); |
| 66 | + } else if (response.type === 'event') { |
| 67 | + // Handle or log event messages from tsserver |
| 68 | + console.log('Event from tsserver:', response); |
| 69 | + } |
| 70 | + } catch (e) { |
| 71 | + console.error('Error parsing line from tsserver:', e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sends a command to the TypeScript Server. |
| 77 | + * @param {Object} command - The command object to send. |
| 78 | + * @param {number} [timeout=5000] - The timeout in milliseconds for the command. |
| 79 | + * @returns {Promise<Object>} A promise that resolves with the response from tsserver. |
| 80 | + */ |
| 81 | + function sendCommand(command, timeout = 5000) { |
| 82 | + return new Promise((resolve, reject) => { |
| 83 | + if (!tsserverProcess) { |
| 84 | + reject(new Error('tsserver is not initialized')); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const seq = ++seqNumber; |
| 89 | + pendingCommands.set(seq, { resolve, reject }); |
| 90 | + |
| 91 | + const timeoutId = setTimeout(() => { |
| 92 | + if (pendingCommands.has(seq)) { |
| 93 | + pendingCommands.delete(seq); |
| 94 | + reject(new Error('tsserver response timeout')); |
| 95 | + } |
| 96 | + }, timeout); |
| 97 | + |
| 98 | + command.seq = seq; |
| 99 | + command.type = 'request'; |
| 100 | + console.log(command); |
| 101 | + |
| 102 | + if (tsserverProcess.stdin.writable) { |
| 103 | + tsserverProcess.stdin.write(`${JSON.stringify(command)}\n`); |
| 104 | + } else { |
| 105 | + clearTimeout(timeoutId); |
| 106 | + reject(new Error('tsserver stdin not writable')); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sends an 'open file' command to the TypeScript Server. |
| 113 | + * @param {string} filePath - The path to the TypeScript file to open. |
| 114 | + * @param {number} timeout - The timeout in milliseconds for the command. |
| 115 | + * @returns {Promise<Object>} A promise that resolves with the response from tsserver. |
| 116 | + */ |
| 117 | + function openFile(filePath, timeout) { |
| 118 | + const command = { |
| 119 | + command: 'open', |
| 120 | + arguments: { file: filePath } |
| 121 | + }; |
| 122 | + return sendCommand(command, timeout); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Kills the TypeScript Server process. |
| 127 | + */ |
| 128 | + function killTSServer() { |
| 129 | + if (tsserverProcess) { |
| 130 | + tsserverProcess.kill(); |
| 131 | + tsserverProcess = null; |
| 132 | + console.log('tsserver process terminated'); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return { init: initTSServer, openFile, killServer: killTSServer }; |
| 137 | +} |
| 138 | + |
| 139 | +export default createTSServerInstance; |
0 commit comments