cscript.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* eslint-disable global-require */
  2. var ifAsync
  3. var fs
  4. var execFile
  5. var debug
  6. var init
  7. /*
  8. * when we mock stuff we need a way to reset
  9. */
  10. function resetDependencies() {
  11. init = false
  12. ifAsync = require('if-async')
  13. fs = require('fs')
  14. execFile = require('../lib/execFile.js')({bufferStdout: true, bufferStderr: true})
  15. debug = require('debug')('regedit:cscript')
  16. }
  17. resetDependencies()
  18. /*
  19. * expected output for failed where.exe execution, exported for testing purposes
  20. */
  21. var CSCRIPT_NOT_FOUND = module.exports.CSCRIPT_NOT_FOUND = 'INFO: Could not find files for the given pattern(s).'
  22. /*
  23. * expected output for cscript.exe, exported for testing purposes
  24. */
  25. var CSCRIPT_EXPECTED_OUTPUT = module.exports.CSCRIPT_EXPECTED_OUTPUT = 'Microsoft (R) Windows Script Host Version'
  26. var cscript = 'cscript.exe'
  27. module.exports.path = function () {
  28. // this sucks...
  29. if (init === false) {
  30. throw new Error('must initialize first')
  31. }
  32. debug(cscript)
  33. return cscript
  34. }
  35. module.exports.init = function (callback) {
  36. debug('init()')
  37. if (init) {
  38. debug('already initialized')
  39. return setImmediate(callback)
  40. }
  41. var functor =
  42. ifAsync(spawnCScriptSucceeded)
  43. .or(whereCScriptSucceeded)
  44. .or(fsStatCScriptSucceeded)
  45. .then(function (cb) {
  46. init = true
  47. cb()
  48. })
  49. .else(callbackWithError)
  50. functor(function (err) {
  51. if (err) {
  52. return callback(err)
  53. }
  54. callback()
  55. })
  56. }
  57. module.exports._mock = function(_fs, _execFile, _init) {
  58. fs = _fs
  59. execFile = _execFile
  60. init = _init
  61. }
  62. module.exports._mockReset = resetDependencies
  63. function spawnCScriptSucceeded (callback) {
  64. debug('spawnCScriptSucceeded()')
  65. execFile('cscript.exe', function (err, stdout, stderr) {
  66. if (err) {
  67. // where command not found on this system
  68. if (err.code === 'ENOENT') {
  69. return callback(null, false)
  70. }
  71. return callback(err)
  72. }
  73. cscript = 'cscript.exe'
  74. callback(null, stdout.indexOf(CSCRIPT_EXPECTED_OUTPUT) > -1)
  75. })
  76. }
  77. function whereCScriptSucceeded (callback) {
  78. debug('whereCScriptSucceeded()')
  79. execFile('where cscript.exe', function (err, stdout, stderr) {
  80. if (err) {
  81. // where command not found on this system
  82. if (err.code === 'ENOENT') {
  83. return callback(null, false)
  84. }
  85. return callback(err)
  86. }
  87. if (typeof stdout !== 'string') {
  88. return callback(null, false)
  89. }
  90. if (stdout.indexOf(CSCRIPT_NOT_FOUND) > -1) {
  91. return callback(null, false)
  92. }
  93. cscript = stdout.trim()
  94. callback(null, true)
  95. })
  96. }
  97. function fsStatCScriptSucceeded(callback) {
  98. debug('fsStatCScriptSucceeded()')
  99. fs.stat('c:\\windows\\system32\\cscript.exe', function(err, stat) {
  100. if (err) {
  101. if (err.code === 'ENOENT') {
  102. return callback(null, false)
  103. }
  104. return callback(err)
  105. }
  106. cscript = 'c:\\windows\\system32\\cscript.exe'
  107. callback(null, true)
  108. })
  109. }
  110. function callbackWithError(cb) {
  111. cb(new Error('cscript not found'))
  112. }
  113. function once (cb) {
  114. var fired = false
  115. return function () {
  116. if (fired) {
  117. return cb()
  118. }
  119. fired = true
  120. cb.apply(null, arguments)
  121. }
  122. }