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.

49 lines
1.1 KiB

4 years ago
  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. }