11const { spawn, exec } = require ( 'child_process' ) ;
22const readline = require ( 'readline' ) ;
33const path = require ( 'path' ) ;
4- const { OPERATION_RESPONSE , OPERATION_QUIT , OPERATION_LINT_TEXT }
4+ const fsPromises = require ( 'fs' ) . promises ;
5+ const { OPERATION_RESPONSE , OPERATION_QUIT , OPERATION_LINT_TEXT , OPERATION_GET_LOADED_VERSION }
56 = require ( "./constants" ) ;
67
78let requestID = 1 ;
@@ -77,14 +78,15 @@ async function createESLintService(projectFullPath) {
7778 const eslintResponse = JSON . parse ( line ) ;
7879 if ( eslintResponse . operation === OPERATION_RESPONSE ) {
7980 const sentRequestID = eslintResponse . requestID ;
80- const promiseResoover = queuedReq . get ( sentRequestID ) ;
81- if ( ! promiseResoover ) {
81+ const promiseResolver = queuedReq . get ( sentRequestID ) ;
82+ if ( ! promiseResolver ) {
8283 console . error ( "ESLint service: request ID not found to process request!!: " , eslintResponse ) ;
8384 return ;
8485 }
86+ queuedReq . delete ( sentRequestID ) ;
8587 delete eslintResponse . requestID ;
8688 delete eslintResponse . operation ;
87- promiseResoover . resolve ( eslintResponse ) ;
89+ promiseResolver . resolve ( eslintResponse ) ;
8890 } else {
8991 console . error ( "ESLint service: Unknown operation: " , eslintResponse ) ;
9092 }
@@ -103,7 +105,50 @@ async function createESLintService(projectFullPath) {
103105 } ) ;
104106}
105107
108+ async function _getESLintLoadedVersion ( ) {
109+ if ( ! eslintServiceProcess ) {
110+ return null ;
111+ }
112+ return new Promise ( ( resolve , reject ) => {
113+ sendToESLintProcess ( {
114+ requestID : newRequestCallback ( resolve , reject ) ,
115+ operation : OPERATION_GET_LOADED_VERSION
116+ } ) ;
117+ } ) ;
118+ }
119+
120+ // if the user changed his eslint version while we were caching an old eslint version, we need to update.
121+ // Function to observe version changes in ESLint
122+ async function observeVersionChanges ( projectFullPath ) {
123+ try {
124+ if ( ! eslintServiceProcess ) {
125+ return ;
126+ }
127+ const currentlyLoadedVersion = await _getESLintLoadedVersion ( ) ;
128+ if ( ! currentlyLoadedVersion || ! currentlyLoadedVersion . esLintVersion ) {
129+ return ;
130+ }
131+
132+ const ESLintModulePath = path . join ( projectFullPath , "node_modules" , "eslint" ) ;
133+ const packageJsonPath = path . join ( ESLintModulePath , 'package.json' ) ;
134+ const packageJson = await fsPromises . readFile ( packageJsonPath , 'utf8' ) ;
135+ const packageData = JSON . parse ( packageJson ) ;
136+ const currentEsLintModuleVersion = packageData . version ;
137+ if ( currentEsLintModuleVersion && currentlyLoadedVersion . esLintVersion !== currentEsLintModuleVersion ) {
138+ console . log ( `ESLint runner: ESLint version has changed from ${ currentlyLoadedVersion . esLintVersion } ` +
139+ ` to ${ currentEsLintModuleVersion } . Restarting with new version...` ) ;
140+ sendToESLintProcess ( {
141+ operation : OPERATION_QUIT
142+ } ) ;
143+ eslintServiceProcess = null ;
144+ }
145+ } catch ( error ) {
146+ console . error ( 'ESLint runner: Error reading ESLint version:' , error . message ) ;
147+ }
148+ }
149+
106150async function lintFile ( text , fullFilePath , projectFullPath ) {
151+ observeVersionChanges ( projectFullPath ) ;
107152 if ( currentProjectPath !== projectFullPath ) {
108153 // on project change, we should create a new es linter
109154 currentProjectPath = projectFullPath ;
0 commit comments