index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const childProcess = require('child_process')
  2. const path = require('path')
  3. // For attrib command
  4. const params =
  5. {
  6. archive: 'a',
  7. hidden: 'h',
  8. readonly: 'r',
  9. system: 's',
  10. }
  11. function getArgs (file) {
  12. return [
  13. path.join(__dirname, 'hostscript.js'),
  14. file,
  15. '//nologo',
  16. '//E:jscript',
  17. ]
  18. }
  19. function getParseResult (result) {
  20. let json
  21. let error = null
  22. result.stdout = result.stdout.trim()
  23. if (result.stdout.length <= 0) {
  24. error = new Error('unknown error')
  25. } else {
  26. json = JSON.parse(result.stdout)
  27. if (json.error !== undefined) {
  28. error = new Error(json.error)
  29. json = undefined
  30. }
  31. }
  32. return { error: error, attrs: json }
  33. }
  34. function setArgs (file, attrs) {
  35. const args = []
  36. for (let i in attrs) {
  37. if (attrs.hasOwnProperty(i) === true && params.hasOwnProperty(i) === true) {
  38. args.push((attrs[i] === true ? '+' : '-') + params[i])
  39. }
  40. }
  41. args.push(file)
  42. return args
  43. }
  44. function setParseResult (result) {
  45. // `result.stdout` is empty when successful
  46. if (result.stdout.length <= 0) {
  47. return null
  48. } else {
  49. return new Error(result.stdout)
  50. }
  51. }
  52. function shell (command, args, callback) {
  53. let instance = childProcess.spawn(command, args)
  54. let stderr = ''
  55. let stdout = ''
  56. instance.stderr.on('data', function (data) {
  57. stderr += data.toString()
  58. })
  59. instance.stdout.on('data', function (data) {
  60. stdout += data.toString()
  61. })
  62. instance.on('exit', function (status) {
  63. this.removeAllListeners()
  64. // Pass an Object so that it's similar to spawnSync()
  65. // eslint-disable-next-line standard/no-callback-literal
  66. callback({ status: status, stdout: stdout, stderr: stderr })
  67. })
  68. }
  69. function shellSync (command, args) {
  70. let result = childProcess.spawnSync(command, args, {encoding: 'utf8'})
  71. // Consistent with shell()
  72. if (result.stderr === null) result.stderr = ''
  73. if (result.stdout === null) result.stdout = ''
  74. return result
  75. }
  76. // ::: PUBLIC FUNCTIONS
  77. function get (file, callback) {
  78. shell('cscript', getArgs(file), function (result) {
  79. result = getParseResult(result)
  80. callback(result.error, result.attrs)
  81. })
  82. }
  83. function getSync (file) {
  84. let result = shellSync('cscript', getArgs(file))
  85. result = getParseResult(result)
  86. if (result.error !== null) {
  87. throw result.error
  88. }
  89. return result.attrs
  90. }
  91. function set (file, attrs, callback) {
  92. shell('attrib', setArgs(file, attrs), function (result) {
  93. callback(setParseResult(result))
  94. })
  95. }
  96. function setSync (file, attrs, callback) {
  97. let result = shellSync('attrib', setArgs(file, attrs))
  98. result = setParseResult(result)
  99. if (result !== null) {
  100. throw result
  101. }
  102. }
  103. module.exports = {
  104. get,
  105. getSync,
  106. set,
  107. setSync,
  108. }