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