|
| 1 | +const { spawn, exec } = require('child_process'); |
| 2 | +const readline = require('readline'); |
| 3 | +const path = require('path'); |
| 4 | +const {OPERATION_RESPONSE, OPERATION_QUIT, OPERATION_LINT_TEXT} |
| 5 | + = require("./constants"); |
| 6 | + |
| 7 | +let requestID = 1; |
| 8 | +let queuedReq = new Map(); |
| 9 | +function newRequestCallback(resolve, reject) { |
| 10 | + const newRequestID = requestID++; |
| 11 | + queuedReq.set(newRequestID, {resolve, reject}); |
| 12 | + return newRequestID; |
| 13 | +} |
| 14 | + |
| 15 | +let eslintServiceProcess, nodeBinPath, currentProjectPath; |
| 16 | + |
| 17 | +function sendToESLintProcess(jsObj) { |
| 18 | + try{ |
| 19 | + if(!eslintServiceProcess) { |
| 20 | + console.error('sendToESLintProcess: eslintServiceProcess not found'); |
| 21 | + return; |
| 22 | + } |
| 23 | + eslintServiceProcess.stdin.write(JSON.stringify(jsObj) + "\n"); |
| 24 | + } catch (e) { |
| 25 | + console.error('sendToESLintProcess: send error', e); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Function to check if Node.js is installed |
| 30 | +function getNodeJSBinPath() { |
| 31 | + return new Promise((resolve) => { |
| 32 | + if(nodeBinPath){ |
| 33 | + resolve(nodeBinPath); |
| 34 | + return; |
| 35 | + } |
| 36 | + exec('node -v', (error, stdout, stderr) => { |
| 37 | + if (error) { |
| 38 | + console.error('System Node.js is not installed, using PHNode for ESLint'); |
| 39 | + nodeBinPath = process.argv[0]; // phnode itself |
| 40 | + } else { |
| 41 | + console.log(`Node.js is installed. Version: ${stdout.trim()}`); |
| 42 | + nodeBinPath = "node"; // system node |
| 43 | + } |
| 44 | + resolve(nodeBinPath); |
| 45 | + }); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +async function createESLintService(projectFullPath) { |
| 50 | + const nodeJSBinPath = await getNodeJSBinPath(); |
| 51 | + return new Promise(resolve => { |
| 52 | + const args = [ path.join(__dirname, "runner.js"), |
| 53 | + path.join(projectFullPath, "node_modules", "eslint"), projectFullPath]; |
| 54 | + if(eslintServiceProcess) { |
| 55 | + sendToESLintProcess({ |
| 56 | + operation: OPERATION_QUIT |
| 57 | + }); |
| 58 | + } |
| 59 | + eslintServiceProcess = spawn(nodeJSBinPath, args, { |
| 60 | + stdio: ['pipe', 'pipe', 'pipe'] |
| 61 | + }); |
| 62 | + eslintServiceProcess.on('spawn', () => { |
| 63 | + console.log('ESLint process started successfully'); |
| 64 | + resolve(); |
| 65 | + }); |
| 66 | + const eslineServiceHandler = eslintServiceProcess; |
| 67 | + const rl = readline.createInterface({ |
| 68 | + input: eslintServiceProcess.stdout, |
| 69 | + output: process.stdout, |
| 70 | + terminal: false |
| 71 | + }); |
| 72 | + rl.on('line', (line) => { |
| 73 | + if(!line.trim()){ |
| 74 | + return; // empty line |
| 75 | + } |
| 76 | + // {operation, text, fullFilePath, requestID} |
| 77 | + const eslintResponse = JSON.parse(line); |
| 78 | + if(eslintResponse.operation === OPERATION_RESPONSE) { |
| 79 | + const sentRequestID = eslintResponse.requestID; |
| 80 | + const promiseResoover = queuedReq.get(sentRequestID); |
| 81 | + if(!promiseResoover){ |
| 82 | + console.error("ESLint service: request ID not found to process request!!: ", eslintResponse); |
| 83 | + return; |
| 84 | + } |
| 85 | + delete eslintResponse.requestID; |
| 86 | + delete eslintResponse.operation; |
| 87 | + promiseResoover.resolve(eslintResponse); |
| 88 | + } else { |
| 89 | + console.error("ESLint service: Unknown operation: ", eslintResponse); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + eslintServiceProcess.on('error', (error) => { |
| 94 | + console.error(`ESLint Runner Process Error: ${error}`); |
| 95 | + }); |
| 96 | + |
| 97 | + eslintServiceProcess.on('close', (code) => { |
| 98 | + if(eslineServiceHandler === eslintServiceProcess) { |
| 99 | + eslintServiceProcess = null; |
| 100 | + } |
| 101 | + console.log(`ESLint Runner process exited with code ${code}`); |
| 102 | + }); |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +async function lintFile(text, fullFilePath, projectFullPath) { |
| 107 | + if(currentProjectPath !== projectFullPath) { |
| 108 | + // on project change, we should create a new es linter |
| 109 | + currentProjectPath = projectFullPath; |
| 110 | + if(eslintServiceProcess) { |
| 111 | + sendToESLintProcess({ |
| 112 | + operation: OPERATION_QUIT |
| 113 | + }); |
| 114 | + eslintServiceProcess = null; |
| 115 | + } |
| 116 | + } |
| 117 | + if(!eslintServiceProcess){ |
| 118 | + await createESLintService(projectFullPath); |
| 119 | + } |
| 120 | + return new Promise((resolve, reject) => { |
| 121 | + sendToESLintProcess({ |
| 122 | + requestID: newRequestCallback(resolve, reject), |
| 123 | + operation: OPERATION_LINT_TEXT, |
| 124 | + text, |
| 125 | + fullFilePath |
| 126 | + }); |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +exports.lintFile = lintFile; |
0 commit comments