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

file.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict'
  2. const u = require('universalify').fromCallback
  3. const path = require('path')
  4. const fs = require('graceful-fs')
  5. const mkdir = require('../mkdirs')
  6. const pathExists = require('../path-exists').pathExists
  7. function createFile (file, callback) {
  8. function makeFile () {
  9. fs.writeFile(file, '', err => {
  10. if (err) return callback(err)
  11. callback()
  12. })
  13. }
  14. fs.stat(file, (err, stats) => { // eslint-disable-line handle-callback-err
  15. if (!err && stats.isFile()) return callback()
  16. const dir = path.dirname(file)
  17. pathExists(dir, (err, dirExists) => {
  18. if (err) return callback(err)
  19. if (dirExists) return makeFile()
  20. mkdir.mkdirs(dir, err => {
  21. if (err) return callback(err)
  22. makeFile()
  23. })
  24. })
  25. })
  26. }
  27. function createFileSync (file) {
  28. let stats
  29. try {
  30. stats = fs.statSync(file)
  31. } catch (e) {}
  32. if (stats && stats.isFile()) return
  33. const dir = path.dirname(file)
  34. if (!fs.existsSync(dir)) {
  35. mkdir.mkdirsSync(dir)
  36. }
  37. fs.writeFileSync(file, '')
  38. }
  39. module.exports = {
  40. createFile: u(createFile),
  41. createFileSync
  42. }