util.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 mkdirp = util.promisify(require('mkdirp'))
  8. const concurrently = require('concurrently')
  9. exports.shellTask = shellTask
  10. exports.promisifyVinyl = promisifyVinyl
  11. exports.readFile = betterReadFile
  12. exports.writeFile = betterWriteFile
  13. exports.writeFileSync = betterWriteFileSync
  14. exports.copyFile = betterCopyFile
  15. function shellTask(...tasks) {
  16. let func = function() {
  17. return concurrently(tasks) // good for labeling each line of output
  18. }
  19. func.displayName = tasks.join(' ')
  20. return func
  21. }
  22. /*
  23. turns a vinyl stream object into a promise
  24. */
  25. function promisifyVinyl(vinyl) {
  26. return new Promise(function(resolve, reject) {
  27. vinyl.on('end', resolve) // TODO: handle error?
  28. })
  29. }
  30. function betterReadFile(destPath, content) {
  31. return readFile(destPath, { encoding: 'utf8' })
  32. }
  33. function betterWriteFile(destPath, content) {
  34. return mkdirp(path.dirname(destPath)).then(function() {
  35. return writeFile(destPath, content, { encoding: 'utf8' })
  36. })
  37. }
  38. function betterWriteFileSync(destPath, content) {
  39. mkdirp.sync(path.dirname(destPath))
  40. writeFile(destPath, content, { encoding: 'utf8' })
  41. }
  42. function betterCopyFile(srcPath, destPath) {
  43. return mkdirp(path.dirname(destPath)).then(function() {
  44. return copyFile(srcPath, destPath)
  45. })
  46. }