source: trip-planner-front/node_modules/fs-extra/lib/ensure/file.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.1 KB
Line 
1'use strict'
2
3const u = require('universalify').fromCallback
4const path = require('path')
5const fs = require('graceful-fs')
6const mkdir = require('../mkdirs')
7const pathExists = require('../path-exists').pathExists
8
9function 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
31function 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
46module.exports = {
47 createFile: u(createFile),
48 createFileSync
49}
Note: See TracBrowser for help on using the repository browser.