util.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. // ------------------------------------------------------------------------------
  8. // Requirements
  9. // ------------------------------------------------------------------------------
  10. const cp = require('child_process')
  11. const fs = require('fs')
  12. const path = require('path')
  13. const BufferStream = require('./buffer-stream')
  14. // ------------------------------------------------------------------------------
  15. // Helpers
  16. // ------------------------------------------------------------------------------
  17. const FILE_NAME = 'test.txt'
  18. const NPM_RUN_ALL = path.resolve(__dirname, '../../bin/npm-run-all/index.js')
  19. const RUN_P = path.resolve(__dirname, '../../bin/run-p/index.js')
  20. const RUN_S = path.resolve(__dirname, '../../bin/run-s/index.js')
  21. /**
  22. * Spawns the given script with the given arguments.
  23. *
  24. * @param {string} filePath - The file path to be executed.
  25. * @param {string[]} args - The arguments to execute.
  26. * @param {Writable} [stdout] - The writable stream to receive stdout.
  27. * @param {Writable} [stderr] - The writable stream to receive stderr.
  28. * @returns {Promise<void>} The promise which becomes fulfilled if the child
  29. * process finished.
  30. */
  31. function spawn (filePath, args, stdout, stderr) {
  32. return new Promise((resolve, reject) => {
  33. const child = cp.spawn(
  34. process.execPath,
  35. [filePath].concat(args),
  36. { stdio: 'pipe' }
  37. )
  38. const out = new BufferStream()
  39. const error = new BufferStream()
  40. if (stdout != null) {
  41. child.stdout.pipe(stdout)
  42. }
  43. if (stderr != null) {
  44. child.stderr.pipe(stderr)
  45. } else {
  46. child.stderr.pipe(error)
  47. }
  48. child.stdout.pipe(out)
  49. child.on('close', (exitCode) => {
  50. if (exitCode) {
  51. reject(new Error(error.value || 'Exited with non-zero code.'))
  52. } else {
  53. resolve()
  54. }
  55. })
  56. child.on('error', reject)
  57. })
  58. }
  59. // ------------------------------------------------------------------------------
  60. // Public Interface
  61. // ------------------------------------------------------------------------------
  62. /**
  63. * Gets the result text from `test.txt`.
  64. *
  65. * @returns {string|null} The result text.
  66. */
  67. module.exports.result = function result () {
  68. try {
  69. return fs.readFileSync(FILE_NAME, { encoding: 'utf8' })
  70. } catch (err) {
  71. if (err.code === 'ENOENT') {
  72. return null
  73. }
  74. throw err
  75. }
  76. }
  77. /**
  78. * Appends text to `test.txt`.
  79. *
  80. * @param {string} content - A text to append.
  81. * @returns {void}
  82. */
  83. module.exports.appendResult = function appendResult (content) {
  84. fs.appendFileSync(FILE_NAME, content)
  85. }
  86. /**
  87. * Removes `test.txt`.
  88. *
  89. * @returns {void}
  90. */
  91. module.exports.removeResult = function removeResult () {
  92. try {
  93. fs.unlinkSync(FILE_NAME)
  94. } catch (err) {
  95. if (err.code === 'ENOENT') {
  96. return
  97. }
  98. throw err
  99. }
  100. }
  101. /**
  102. * Delay.
  103. *
  104. * @param {number} timeoutInMillis - The time to delay.
  105. * @returns {Promise<void>} The promise which fulfilled after the given time.
  106. */
  107. module.exports.delay = function delay (timeoutInMillis) {
  108. return new Promise(resolve => {
  109. setTimeout(resolve, timeoutInMillis)
  110. })
  111. }
  112. /**
  113. * Executes `npm-run-all` with the given arguments.
  114. *
  115. * @param {string[]} args - The arguments to execute.
  116. * @param {Writable} [stdout] - The writable stream to receive stdout.
  117. * @param {Writable} [stderr] - The writable stream to receive stderr.
  118. * @returns {Promise<void>} The promise which becomes fulfilled if the child
  119. * process finished.
  120. */
  121. module.exports.runAll = function runAll (args, stdout, stderr) {
  122. return spawn(NPM_RUN_ALL, args, stdout, stderr)
  123. }
  124. /**
  125. * Executes `run-p` with the given arguments.
  126. *
  127. * @param {string[]} args - The arguments to execute.
  128. * @param {Writable} [stdout] - The writable stream to receive stdout.
  129. * @param {Writable} [stderr] - The writable stream to receive stderr.
  130. * @returns {Promise<void>} The promise which becomes fulfilled if the child
  131. * process finished.
  132. */
  133. module.exports.runPar = function runPar (args, stdout, stderr) {
  134. return spawn(RUN_P, args, stdout, stderr)
  135. }
  136. /**
  137. * Executes `run-s` with the given arguments.
  138. *
  139. * @param {string[]} args - The arguments to execute.
  140. * @param {Writable} [stdout] - The writable stream to receive stdout.
  141. * @param {Writable} [stderr] - The writable stream to receive stderr.
  142. * @returns {Promise<void>} The promise which becomes fulfilled if the child
  143. * process finished.
  144. */
  145. module.exports.runSeq = function runSeq (args, stdout, stderr) {
  146. return spawn(RUN_S, args, stdout, stderr)
  147. }