util.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const util = require('util')
  2. const path = require('path')
  3. const fs = require('fs')
  4. const readFile = util.promisify(fs.readFile)
  5. const writeFile = util.promisify(fs.writeFile)
  6. const copyFile = util.promisify(fs.copyFile)
  7. const fileExists = util.promisify(fs.exists)
  8. const mkdirp = util.promisify(require('mkdirp'))
  9. const concurrently = require('concurrently')
  10. const { watch } = require('gulp')
  11. exports.whenFirstModified = whenFirstModified
  12. exports.watch = betterWatch
  13. exports.watchTask = watchTask
  14. exports.shellTask = shellTask
  15. exports.promisifyVinyl = promisifyVinyl
  16. exports.readFile = betterReadFile
  17. exports.writeFile = betterWriteFile
  18. exports.writeFileSync = betterWriteFileSync
  19. exports.copyFile = betterCopyFile
  20. exports.fileExists = fileExists
  21. exports.mkdirp = mkdirp
  22. function betterWatch() {
  23. let watcher = watch.apply(null, arguments)
  24. return new Promise((resolve) => {
  25. process.on('SIGINT', function() {
  26. watcher.close()
  27. resolve()
  28. })
  29. })
  30. }
  31. // function firstModifiedTask() { // TODO: general taskify (with description?)
  32. // let args = arguments
  33. // return function() {
  34. // return whenFirstModified.apply(null, args) // can just use bind?
  35. // }
  36. // }
  37. function whenFirstModified(glob, options) {
  38. return new Promise((resolve) => {
  39. let watcher = watch(glob, options || {}, function(cb) {
  40. watcher.close()
  41. resolve()
  42. cb()
  43. })
  44. })
  45. }
  46. function watchTask() { // TODO: use in more places
  47. let watchArgs = arguments
  48. return function() {
  49. return betterWatch.apply(null, watchArgs)
  50. }
  51. }
  52. function shellTask(...tasks) {
  53. let func = function() {
  54. return concurrently(tasks) // good for labeling each line of output
  55. }
  56. func.displayName = tasks.join(' ')
  57. return func
  58. }
  59. /*
  60. turns a vinyl stream object into a promise
  61. */
  62. function promisifyVinyl(vinyl) {
  63. return new Promise(function(resolve, reject) {
  64. vinyl.on('end', resolve) // TODO: handle error?
  65. })
  66. }
  67. function betterReadFile(destPath) {
  68. return readFile(destPath, { encoding: 'utf8' })
  69. }
  70. function betterWriteFile(destPath, content) {
  71. return mkdirp(path.dirname(destPath)).then(function() {
  72. return writeFile(destPath, content, { encoding: 'utf8' })
  73. })
  74. }
  75. function betterWriteFileSync(destPath, content) {
  76. mkdirp.sync(path.dirname(destPath))
  77. writeFile(destPath, content, { encoding: 'utf8' })
  78. }
  79. function betterCopyFile(srcPath, destPath) {
  80. return mkdirp(path.dirname(destPath)).then(function() {
  81. return copyFile(srcPath, destPath)
  82. })
  83. }