find-vs2017.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var log = require('npmlog')
  2. , execFile = require('child_process').execFile
  3. , path = require('path')
  4. function findVS2017(callback) {
  5. var ps = path.join(process.env.SystemRoot, 'System32', 'WindowsPowerShell',
  6. 'v1.0', 'powershell.exe')
  7. var csFile = path.join(__dirname, 'Find-VS2017.cs')
  8. var psArgs = ['-ExecutionPolicy', 'Unrestricted', '-NoProfile',
  9. '-Command', '&{Add-Type -Path \'' + csFile + '\';' +
  10. '[VisualStudioConfiguration.Main]::Query()}']
  11. log.silly('find vs2017', 'Running', ps, psArgs)
  12. var child = execFile(ps, psArgs, { encoding: 'utf8' },
  13. function (err, stdout, stderr) {
  14. log.silly('find vs2017', 'PS err:', err)
  15. log.silly('find vs2017', 'PS stdout:', stdout)
  16. log.silly('find vs2017', 'PS stderr:', stderr)
  17. if (err)
  18. return callback(new Error('Could not use PowerShell to find VS2017'))
  19. var vsSetup
  20. try {
  21. vsSetup = JSON.parse(stdout)
  22. } catch (e) {
  23. log.silly('find vs2017', e)
  24. return callback(new Error('Could not use PowerShell to find VS2017'))
  25. }
  26. log.silly('find vs2017', 'vsSetup:', vsSetup)
  27. if (vsSetup && vsSetup.log)
  28. log.verbose('find vs2017', vsSetup.log.trimRight())
  29. if (!vsSetup || !vsSetup.path || !vsSetup.sdk) {
  30. return callback(new Error('No usable installation of VS2017 found'))
  31. }
  32. log.verbose('find vs2017', 'using installation:', vsSetup.path)
  33. callback(null, { "path": vsSetup.path, "sdk": vsSetup.sdk })
  34. })
  35. child.stdin.end()
  36. }
  37. module.exports = findVS2017