util.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 mkdir = util.promisify(fs.mkdir)
  9. const mkdirSync = fs.mkdirSync
  10. const concurrently = require('concurrently')
  11. const { watch } = require('gulp')
  12. exports.watch = betterWatch
  13. exports.watchTask = watchTask
  14. exports.shellTask = shellTask
  15. exports.promisifyVinyl = promisifyVinyl
  16. exports.readFile = betterReadFile
  17. exports.readFileSync = betterReadFileSync
  18. exports.writeFile = betterWriteFile
  19. exports.writeFileSync = betterWriteFileSync
  20. exports.copyFile = betterCopyFile
  21. exports.fileExists = fileExists
  22. function betterWatch() { // i cant believe gulp doesnt do this
  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 watchTask() { // TODO: use in more places
  32. let watchArgs = arguments
  33. return function() {
  34. return betterWatch.apply(null, watchArgs)
  35. }
  36. }
  37. function shellTask(...tasks) {
  38. let func = function() {
  39. return concurrently(tasks) // good for labeling each line of output
  40. }
  41. func.displayName = tasks.join(' ')
  42. return func
  43. }
  44. /*
  45. turns a vinyl stream object into a promise
  46. */
  47. function promisifyVinyl(vinyl) {
  48. return new Promise(function(resolve, reject) {
  49. vinyl.on('end', resolve) // TODO: handle error?
  50. })
  51. }
  52. function betterReadFile(destPath) {
  53. return readFile(destPath, { encoding: 'utf8' })
  54. }
  55. function betterReadFileSync(destPath) {
  56. return fs.readFileSync(destPath, { encoding: 'utf8' })
  57. }
  58. function betterWriteFile(destPath, content) {
  59. return mkdir(path.dirname(destPath), { recursive: true }).then(function() {
  60. return writeFile(destPath, content, { encoding: 'utf8' })
  61. })
  62. }
  63. function betterWriteFileSync(destPath, content) {
  64. mkdirSync(path.dirname(destPath), { recursive: true })
  65. return fs.writeFileSync(destPath, content, { encoding: 'utf8' })
  66. }
  67. function betterCopyFile(srcPath, destPath) {
  68. return mkdir(path.dirname(destPath), { recursive: true }).then(function() {
  69. return copyFile(srcPath, destPath)
  70. })
  71. }
  72. exports.mapHash = function(input, func) {
  73. const output = {}
  74. for (const key in input) {
  75. if (hasOwnProperty.call(input, key)) {
  76. output[key] = func(input[key], key)
  77. }
  78. }
  79. return output
  80. }
  81. exports.mapHashViaPair = function(input, func) {
  82. const output = {}
  83. for (const key in input) {
  84. if (hasOwnProperty.call(input, key)) {
  85. let pair = func(input[key], key)
  86. output[pair[0]] = pair[1]
  87. }
  88. }
  89. return output
  90. }
  91. exports.arrayToHash = function(a, func) {
  92. let output = {}
  93. for (let i = 0; i < a.length; i++) {
  94. let pair = func(a[i], i)
  95. output[pair[0]] = pair[1]
  96. }
  97. return output
  98. }