You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
947 B

4 years ago
  1. 'use strict'
  2. const u = require('universalify').fromCallback
  3. const fs = require('graceful-fs')
  4. const path = require('path')
  5. const mkdir = require('../mkdirs')
  6. const pathExists = require('../path-exists').pathExists
  7. function outputFile (file, data, encoding, callback) {
  8. if (typeof encoding === 'function') {
  9. callback = encoding
  10. encoding = 'utf8'
  11. }
  12. const dir = path.dirname(file)
  13. pathExists(dir, (err, itDoes) => {
  14. if (err) return callback(err)
  15. if (itDoes) return fs.writeFile(file, data, encoding, callback)
  16. mkdir.mkdirs(dir, err => {
  17. if (err) return callback(err)
  18. fs.writeFile(file, data, encoding, callback)
  19. })
  20. })
  21. }
  22. function outputFileSync (file, ...args) {
  23. const dir = path.dirname(file)
  24. if (fs.existsSync(dir)) {
  25. return fs.writeFileSync(file, ...args)
  26. }
  27. mkdir.mkdirsSync(dir)
  28. fs.writeFileSync(file, ...args)
  29. }
  30. module.exports = {
  31. outputFile: u(outputFile),
  32. outputFileSync
  33. }