file.js
924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var path = require('path')
var fs = require('graceful-fs')
var mkdir = require('../mkdirs')
function createFile (file, callback) {
function makeFile () {
fs.writeFile(file, '', function (err) {
if (err) return callback(err)
callback()
})
}
fs.exists(file, function (fileExists) {
if (fileExists) return callback()
var dir = path.dirname(file)
fs.exists(dir, function (dirExists) {
if (dirExists) return makeFile()
mkdir.mkdirs(dir, function (err) {
if (err) return callback(err)
makeFile()
})
})
})
}
function createFileSync (file) {
if (fs.existsSync(file)) return
var dir = path.dirname(file)
if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
}
fs.writeFileSync(file, '')
}
module.exports = {
createFile: createFile,
createFileSync: createFileSync,
// alias
ensureFile: createFile,
ensureFileSync: createFileSync
}