@@ -5,26 +5,28 @@ const { existsSync } = require('fs')
5
5
const { win32 : path } = require ( 'path' )
6
6
const { regSearchKeys, execFile } = require ( './util' )
7
7
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' )
14
12
15
- VisualStudioFinder . prototype = {
16
- log : log . withPrefix ( 'find VS' ) ,
13
+ regSearchKeys = regSearchKeys
17
14
18
- regSearchKeys,
15
+ constructor ( nodeSemver , configMsvsVersion ) {
16
+ this . nodeSemver = nodeSemver
17
+ this . configMsvsVersion = configMsvsVersion
18
+ this . errorLog = [ ]
19
+ this . validVersions = [ ]
20
+ }
19
21
20
22
// Logs a message at verbose level, but also saves it to be displayed later
21
23
// at error level if an error occurs. This should help diagnose the problem.
22
- addLog : function addLog ( message ) {
24
+ addLog ( message ) {
23
25
this . log . verbose ( message )
24
26
this . errorLog . push ( message )
25
- } ,
27
+ }
26
28
27
- findVisualStudio : async function findVisualStudio ( ) {
29
+ async findVisualStudio ( ) {
28
30
this . configVersionYear = null
29
31
this . configPath = null
30
32
if ( this . configMsvsVersion ) {
@@ -65,16 +67,16 @@ VisualStudioFinder.prototype = {
65
67
}
66
68
67
69
return this . fail ( )
68
- } ,
70
+ }
69
71
70
- succeed : function succeed ( info ) {
72
+ succeed ( info ) {
71
73
this . log . info ( `using VS${ info . versionYear } (${ info . version } ) found at:` +
72
74
`\n"${ info . path } "` +
73
75
'\nrun with --verbose for detailed information' )
74
76
return info
75
- } ,
77
+ }
76
78
77
- fail : function fail ( ) {
79
+ fail ( ) {
78
80
if ( this . configMsvsVersion && this . envVcInstallDir ) {
79
81
this . errorLog . push (
80
82
'msvs_version does not match this VS Command Prompt or the' ,
@@ -109,11 +111,11 @@ VisualStudioFinder.prototype = {
109
111
110
112
this . log . error ( `\n${ errorLog } \n\n${ infoLog } \n` )
111
113
throw new Error ( 'Could not find any Visual Studio installation to use' )
112
- } ,
114
+ }
113
115
114
116
// Invoke the PowerShell script to get information about Visual Studio 2017
115
117
// or newer installations
116
- findVisualStudio2017OrNewer : async function findVisualStudio2017OrNewer ( ) {
118
+ async findVisualStudio2017OrNewer ( ) {
117
119
const ps = path . join ( process . env . SystemRoot , 'System32' ,
118
120
'WindowsPowerShell' , 'v1.0' , 'powershell.exe' )
119
121
const csFile = path . join ( __dirname , 'Find-VisualStudio.cs' )
@@ -128,11 +130,11 @@ VisualStudioFinder.prototype = {
128
130
this . log . silly ( 'Running' , ps , psArgs )
129
131
const [ err , stdout , stderr ] = await execFile ( ps , psArgs , { encoding : 'utf8' } )
130
132
return this . parseData ( err , stdout , stderr )
131
- } ,
133
+ }
132
134
133
135
// Parse the output of the PowerShell script and look for an installation
134
136
// of Visual Studio 2017 or newer to use
135
- parseData : function parseData ( err , stdout , stderr ) {
137
+ parseData ( err , stdout , stderr ) {
136
138
this . log . silly ( 'PS stderr = %j' , stderr )
137
139
138
140
const failPowershell = ( ) => {
@@ -220,10 +222,10 @@ VisualStudioFinder.prototype = {
220
222
this . addLog (
221
223
'could not find a version of Visual Studio 2017 or newer to use' )
222
224
return null
223
- } ,
225
+ }
224
226
225
227
// Helper - process version information
226
- getVersionInfo : function getVersionInfo ( info ) {
228
+ getVersionInfo ( info ) {
227
229
const match = / ^ ( \d + ) \. ( \d + ) \. .* / . exec ( info . version )
228
230
if ( ! match ) {
229
231
this . log . silly ( '- failed to parse version:' , info . version )
@@ -249,14 +251,14 @@ VisualStudioFinder.prototype = {
249
251
}
250
252
this . log . silly ( '- unsupported version:' , ret . versionMajor )
251
253
return { }
252
- } ,
254
+ }
253
255
254
- msBuildPathExists : function msBuildPathExists ( path ) {
256
+ msBuildPathExists ( path ) {
255
257
return existsSync ( path )
256
- } ,
258
+ }
257
259
258
260
// Helper - process MSBuild information
259
- getMSBuild : function getMSBuild ( info , versionYear ) {
261
+ getMSBuild ( info , versionYear ) {
260
262
const pkg = 'Microsoft.VisualStudio.VC.MSBuild.Base'
261
263
const msbuildPath = path . join ( info . path , 'MSBuild' , 'Current' , 'Bin' , 'MSBuild.exe' )
262
264
const msbuildPathArm64 = path . join ( info . path , 'MSBuild' , 'Current' , 'Bin' , 'arm64' , 'MSBuild.exe' )
@@ -280,10 +282,10 @@ VisualStudioFinder.prototype = {
280
282
return msbuildPath
281
283
}
282
284
return null
283
- } ,
285
+ }
284
286
285
287
// Helper - process toolset information
286
- getToolset : function getToolset ( info , versionYear ) {
288
+ getToolset ( info , versionYear ) {
287
289
const pkg = 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64'
288
290
const express = 'Microsoft.VisualStudio.WDExpress'
289
291
@@ -304,10 +306,10 @@ VisualStudioFinder.prototype = {
304
306
}
305
307
this . log . silly ( '- invalid versionYear:' , versionYear )
306
308
return null
307
- } ,
309
+ }
308
310
309
311
// Helper - process Windows SDK information
310
- getSDK : function getSDK ( info ) {
312
+ getSDK ( info ) {
311
313
const win8SDK = 'Microsoft.VisualStudio.Component.Windows81SDK'
312
314
const win10SDKPrefix = 'Microsoft.VisualStudio.Component.Windows10SDK.'
313
315
const win11SDKPrefix = 'Microsoft.VisualStudio.Component.Windows11SDK.'
@@ -339,10 +341,10 @@ VisualStudioFinder.prototype = {
339
341
return '8.1'
340
342
}
341
343
return null
342
- } ,
344
+ }
343
345
344
346
// Find an installation of Visual Studio 2015 to use
345
- findVisualStudio2015 : async function findVisualStudio2015 ( ) {
347
+ async findVisualStudio2015 ( ) {
346
348
if ( this . nodeSemver . major >= 19 ) {
347
349
this . addLog (
348
350
'not looking for VS2015 as it is only supported up to Node.js 18' )
@@ -355,10 +357,10 @@ VisualStudioFinder.prototype = {
355
357
versionYear : 2015 ,
356
358
toolset : 'v140'
357
359
} )
358
- } ,
360
+ }
359
361
360
362
// Find an installation of Visual Studio 2013 to use
361
- findVisualStudio2013 : async function findVisualStudio2013 ( ) {
363
+ async findVisualStudio2013 ( ) {
362
364
if ( this . nodeSemver . major >= 9 ) {
363
365
this . addLog (
364
366
'not looking for VS2013 as it is only supported up to Node.js 8' )
@@ -371,10 +373,10 @@ VisualStudioFinder.prototype = {
371
373
versionYear : 2013 ,
372
374
toolset : 'v120'
373
375
} )
374
- } ,
376
+ }
375
377
376
378
// Helper - common code for VS2013 and VS2015
377
- findOldVS : async function findOldVS ( info ) {
379
+ async findOldVS ( info ) {
378
380
const regVC7 = [ 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7' ,
379
381
'HKLM\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7' ]
380
382
const regMSBuild = 'HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions'
@@ -408,14 +410,14 @@ VisualStudioFinder.prototype = {
408
410
this . addLog ( '- not found' )
409
411
return null
410
412
}
411
- } ,
413
+ }
412
414
413
415
// After finding a usable version of Visual Studio:
414
416
// - add it to validVersions to be displayed at the end if a specific
415
417
// version was requested and not found;
416
418
// - check if this is the version that was requested.
417
419
// - check if this matches the Visual Studio Command Prompt
418
- checkConfigVersion : function checkConfigVersion ( versionYear , vsPath ) {
420
+ checkConfigVersion ( versionYear , vsPath ) {
419
421
this . validVersions . push ( versionYear )
420
422
this . validVersions . push ( vsPath )
421
423
@@ -438,10 +440,4 @@ VisualStudioFinder.prototype = {
438
440
}
439
441
}
440
442
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