1 | 'use strict'
|
---|
2 |
|
---|
3 | const u = require('universalify').fromCallback
|
---|
4 | const fs = require('graceful-fs')
|
---|
5 | const path = require('path')
|
---|
6 | const mkdir = require('../mkdirs')
|
---|
7 | const pathExists = require('../path-exists').pathExists
|
---|
8 |
|
---|
9 | function outputFile (file, data, encoding, callback) {
|
---|
10 | if (typeof encoding === 'function') {
|
---|
11 | callback = encoding
|
---|
12 | encoding = 'utf8'
|
---|
13 | }
|
---|
14 |
|
---|
15 | const dir = path.dirname(file)
|
---|
16 | pathExists(dir, (err, itDoes) => {
|
---|
17 | if (err) return callback(err)
|
---|
18 | if (itDoes) return fs.writeFile(file, data, encoding, callback)
|
---|
19 |
|
---|
20 | mkdir.mkdirs(dir, err => {
|
---|
21 | if (err) return callback(err)
|
---|
22 |
|
---|
23 | fs.writeFile(file, data, encoding, callback)
|
---|
24 | })
|
---|
25 | })
|
---|
26 | }
|
---|
27 |
|
---|
28 | function outputFileSync (file, ...args) {
|
---|
29 | const dir = path.dirname(file)
|
---|
30 | if (fs.existsSync(dir)) {
|
---|
31 | return fs.writeFileSync(file, ...args)
|
---|
32 | }
|
---|
33 | mkdir.mkdirsSync(dir)
|
---|
34 | fs.writeFileSync(file, ...args)
|
---|
35 | }
|
---|
36 |
|
---|
37 | module.exports = {
|
---|
38 | outputFile: u(outputFile),
|
---|
39 | outputFileSync
|
---|
40 | }
|
---|