@@ -5,26 +5,28 @@ const { existsSync } = require('fs')
55const { win32 : path } = require ( 'path' )
66const { regSearchKeys, execFile } = require ( './util' )
77
8- function VisualStudioFinder ( nodeSemver , configMsvsVersion ) {
9- this . nodeSemver = nodeSemver
10- this . configMsvsVersion = configMsvsVersion
11- this . errorLog = [ ]
12- this . validVersions = [ ]
13- }
8+ class VisualStudioFinder {
9+ static findVisualStudio = ( ...args ) => new VisualStudioFinder ( ...args ) . findVisualStudio ( )
10+
11+ log = log . withPrefix ( 'find VS' )
1412
15- VisualStudioFinder . prototype = {
16- log : log . withPrefix ( 'find VS' ) ,
13+ regSearchKeys = regSearchKeys
1714
18- regSearchKeys,
15+ constructor ( nodeSemver , configMsvsVersion ) {
16+ this . nodeSemver = nodeSemver
17+ this . configMsvsVersion = configMsvsVersion
18+ this . errorLog = [ ]
19+ this . validVersions = [ ]
20+ }
1921
2022 // Logs a message at verbose level, but also saves it to be displayed later
2123 // at error level if an error occurs. This should help diagnose the problem.
22- addLog : function addLog ( message ) {
24+ addLog ( message ) {
2325 this . log . verbose ( message )
2426 this . errorLog . push ( message )
25- } ,
27+ }
2628
27- findVisualStudio : async function findVisualStudio ( ) {
29+ async findVisualStudio ( ) {
2830 this . configVersionYear = null
2931 this . configPath = null
3032 if ( this . configMsvsVersion ) {
@@ -65,16 +67,16 @@ VisualStudioFinder.prototype = {
6567 }
6668
6769 return this . fail ( )
68- } ,
70+ }
6971
70- succeed : function succeed ( info ) {
72+ succeed ( info ) {
7173 this . log . info ( `using VS${ info . versionYear } (${ info . version } ) found at:` +
7274 `\n"${ info . path } "` +
7375 '\nrun with --verbose for detailed information' )
7476 return info
75- } ,
77+ }
7678
77- fail : function fail ( ) {
79+ fail ( ) {
7880 if ( this . configMsvsVersion && this . envVcInstallDir ) {
7981 this . errorLog . push (
8082 'msvs_version does not match this VS Command Prompt or the' ,
@@ -109,11 +111,11 @@ VisualStudioFinder.prototype = {
109111
110112 this . log . error ( `\n${ errorLog } \n\n${ infoLog } \n` )
111113 throw new Error ( 'Could not find any Visual Studio installation to use' )
112- } ,
114+ }
113115
114116 // Invoke the PowerShell script to get information about Visual Studio 2017
115117 // or newer installations
116- findVisualStudio2017OrNewer : async function findVisualStudio2017OrNewer ( ) {
118+ async findVisualStudio2017OrNewer ( ) {
117119 const ps = path . join ( process . env . SystemRoot , 'System32' ,
118120 'WindowsPowerShell' , 'v1.0' , 'powershell.exe' )
119121 const csFile = path . join ( __dirname , 'Find-VisualStudio.cs' )
@@ -128,11 +130,11 @@ VisualStudioFinder.prototype = {
128130 this . log . silly ( 'Running' , ps , psArgs )
129131 const [ err , stdout , stderr ] = await execFile ( ps , psArgs , { encoding : 'utf8' } )
130132 return this . parseData ( err , stdout , stderr )
131- } ,
133+ }
132134
133135 // Parse the output of the PowerShell script and look for an installation
134136 // of Visual Studio 2017 or newer to use
135- parseData : function parseData ( err , stdout , stderr ) {
137+ parseData ( err , stdout , stderr ) {
136138 this . log . silly ( 'PS stderr = %j' , stderr )
137139
138140 const failPowershell = ( ) => {
@@ -220,10 +222,10 @@ VisualStudioFinder.prototype = {
220222 this . addLog (
221223 'could not find a version of Visual Studio 2017 or newer to use' )
222224 return null
223- } ,
225+ }
224226
225227 // Helper - process version information
226- getVersionInfo : function getVersionInfo ( info ) {
228+ getVersionInfo ( info ) {
227229 const match = / ^ ( \d + ) \. ( \d + ) \. .* / . exec ( info . version )
228230 if ( ! match ) {
229231 this . log . silly ( '- failed to parse version:' , info . version )
@@ -249,14 +251,14 @@ VisualStudioFinder.prototype = {
249251 }
250252 this . log . silly ( '- unsupported version:' , ret . versionMajor )
251253 return { }
252- } ,
254+ }
253255
254- msBuildPathExists : function msBuildPathExists ( path ) {
256+ msBuildPathExists ( path ) {
255257 return existsSync ( path )
256- } ,
258+ }
257259
258260 // Helper - process MSBuild information
259- getMSBuild : function getMSBuild ( info , versionYear ) {
261+ getMSBuild ( info , versionYear ) {
260262 const pkg = 'Microsoft.VisualStudio.VC.MSBuild.Base'
261263 const msbuildPath = path . join ( info . path , 'MSBuild' , 'Current' , 'Bin' , 'MSBuild.exe' )
262264 const msbuildPathArm64 = path . join ( info . path , 'MSBuild' , 'Current' , 'Bin' , 'arm64' , 'MSBuild.exe' )
@@ -280,10 +282,10 @@ VisualStudioFinder.prototype = {
280282 return msbuildPath
281283 }
282284 return null
283- } ,
285+ }
284286
285287 // Helper - process toolset information
286- getToolset : function getToolset ( info , versionYear ) {
288+ getToolset ( info , versionYear ) {
287289 const pkg = 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64'
288290 const express = 'Microsoft.VisualStudio.WDExpress'
289291
@@ -304,10 +306,10 @@ VisualStudioFinder.prototype = {
304306 }
305307 this . log . silly ( '- invalid versionYear:' , versionYear )
306308 return null
307- } ,
309+ }
308310
309311 // Helper - process Windows SDK information
310- getSDK : function getSDK ( info ) {
312+ getSDK ( info ) {
311313 const win8SDK = 'Microsoft.VisualStudio.Component.Windows81SDK'
312314 const win10SDKPrefix = 'Microsoft.VisualStudio.Component.Windows10SDK.'
313315 const win11SDKPrefix = 'Microsoft.VisualStudio.Component.Windows11SDK.'
@@ -339,10 +341,10 @@ VisualStudioFinder.prototype = {
339341 return '8.1'
340342 }
341343 return null
342- } ,
344+ }
343345
344346 // Find an installation of Visual Studio 2015 to use
345- findVisualStudio2015 : async function findVisualStudio2015 ( ) {
347+ async findVisualStudio2015 ( ) {
346348 if ( this . nodeSemver . major >= 19 ) {
347349 this . addLog (
348350 'not looking for VS2015 as it is only supported up to Node.js 18' )
@@ -355,10 +357,10 @@ VisualStudioFinder.prototype = {
355357 versionYear : 2015 ,
356358 toolset : 'v140'
357359 } )
358- } ,
360+ }
359361
360362 // Find an installation of Visual Studio 2013 to use
361- findVisualStudio2013 : async function findVisualStudio2013 ( ) {
363+ async findVisualStudio2013 ( ) {
362364 if ( this . nodeSemver . major >= 9 ) {
363365 this . addLog (
364366 'not looking for VS2013 as it is only supported up to Node.js 8' )
@@ -371,10 +373,10 @@ VisualStudioFinder.prototype = {
371373 versionYear : 2013 ,
372374 toolset : 'v120'
373375 } )
374- } ,
376+ }
375377
376378 // Helper - common code for VS2013 and VS2015
377- findOldVS : async function findOldVS ( info ) {
379+ async findOldVS ( info ) {
378380 const regVC7 = [ 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7' ,
379381 'HKLM\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7' ]
380382 const regMSBuild = 'HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions'
@@ -408,14 +410,14 @@ VisualStudioFinder.prototype = {
408410 this . addLog ( '- not found' )
409411 return null
410412 }
411- } ,
413+ }
412414
413415 // After finding a usable version of Visual Studio:
414416 // - add it to validVersions to be displayed at the end if a specific
415417 // version was requested and not found;
416418 // - check if this is the version that was requested.
417419 // - check if this matches the Visual Studio Command Prompt
418- checkConfigVersion : function checkConfigVersion ( versionYear , vsPath ) {
420+ checkConfigVersion ( versionYear , vsPath ) {
419421 this . validVersions . push ( versionYear )
420422 this . validVersions . push ( vsPath )
421423
@@ -438,10 +440,4 @@ VisualStudioFinder.prototype = {
438440 }
439441}
440442
441- const findVisualStudio = async ( nodeSemver , configMsvsVersion ) => new VisualStudioFinder ( nodeSemver , configMsvsVersion ) . findVisualStudio ( )
442-
443- module . exports = findVisualStudio
444- module . exports . test = {
445- VisualStudioFinder,
446- findVisualStudio
447- }
443+ module . exports = VisualStudioFinder
0 commit comments