util.js 1.5 KB

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