Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 947B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. }