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.
40 lines
966 B
40 lines
966 B
'use strict' |
|
|
|
const u = require('universalify').fromCallback |
|
const fs = require('graceful-fs') |
|
const path = require('path') |
|
const mkdir = require('../mkdirs') |
|
const pathExists = require('../path-exists').pathExists |
|
|
|
function outputFile (file, data, encoding, callback) { |
|
if (typeof encoding === 'function') { |
|
callback = encoding |
|
encoding = 'utf8' |
|
} |
|
|
|
const dir = path.dirname(file) |
|
pathExists(dir, (err, itDoes) => { |
|
if (err) return callback(err) |
|
if (itDoes) return fs.writeFile(file, data, encoding, callback) |
|
|
|
mkdir.mkdirs(dir, err => { |
|
if (err) return callback(err) |
|
|
|
fs.writeFile(file, data, encoding, callback) |
|
}) |
|
}) |
|
} |
|
|
|
function outputFileSync (file, data, encoding) { |
|
const dir = path.dirname(file) |
|
if (fs.existsSync(dir)) { |
|
return fs.writeFileSync.apply(fs, arguments) |
|
} |
|
mkdir.mkdirsSync(dir) |
|
fs.writeFileSync.apply(fs, arguments) |
|
} |
|
|
|
module.exports = { |
|
outputFile: u(outputFile), |
|
outputFileSync |
|
}
|
|
|