@@ -27,7 +27,29 @@ function sendToESLintProcess(jsObj) {
2727 }
2828}
2929
30- // Function to check if Node.js is installed
30+ function isVersionGreater ( versionA , versionB ) {
31+ try {
32+ const partsA = versionA . split ( '.' ) . map ( Number ) ;
33+ const partsB = versionB . split ( '.' ) . map ( Number ) ;
34+
35+ for ( let i = 0 ; i < Math . max ( partsA . length , partsB . length ) ; i ++ ) {
36+ const partA = partsA [ i ] || 0 ;
37+ const partB = partsB [ i ] || 0 ;
38+
39+ if ( partA > partB ) {
40+ return true ;
41+ } else if ( partA < partB ) {
42+ return false ;
43+ }
44+ }
45+ } catch ( e ) {
46+ console . error ( "error comparing nodejs versions: " , versionA , versionB , e ) ;
47+ }
48+ return false ;
49+ }
50+
51+ // We always use phnode if the system node is lower than phnode version. our supported eslint versions 7-latest runs
52+ // on phnode 20. If user has the latest node, we will use that for future proofing.
3153function getNodeJSBinPath ( ) {
3254 return new Promise ( ( resolve ) => {
3355 if ( nodeBinPath ) {
@@ -39,8 +61,16 @@ function getNodeJSBinPath() {
3961 console . error ( 'System Node.js is not installed, using PHNode for ESLint' ) ;
4062 nodeBinPath = process . argv [ 0 ] ; // phnode itself
4163 } else {
42- console . log ( `Node.js is installed. Version: ${ stdout . trim ( ) } ` ) ;
43- nodeBinPath = "node" ; // system node
64+ const systemNodeVersion = stdout . trim ( ) . substring ( 1 ) ; // remove the 'v' prefix
65+ const currentNodeVersion = process . version . substring ( 1 ) ; // remove the 'v' prefix
66+
67+ if ( isVersionGreater ( systemNodeVersion , currentNodeVersion ) ) {
68+ console . log ( `System Node.js (${ systemNodeVersion } ) is newer than phnode(${ currentNodeVersion } ). Using system Node.js.` ) ;
69+ nodeBinPath = "node" ; // system node
70+ } else {
71+ console . log ( `phnode (${ currentNodeVersion } ) is same/newer than system nodejs. using phnode.` ) ;
72+ nodeBinPath = process . argv [ 0 ] ; // current node process path
73+ }
4474 }
4575 resolve ( nodeBinPath ) ;
4676 } ) ;
@@ -172,4 +202,6 @@ async function lintFile(text, fullFilePath, projectFullPath) {
172202 } ) ;
173203}
174204
205+ getNodeJSBinPath ( ) ;
206+
175207exports . lintFile = lintFile ;
0 commit comments