Skip to content

Commit

Permalink
Refactor outputFile API to async/await (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Oct 24, 2023
1 parent 47bc43d commit 426bb46
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions lib/output-file/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
'use strict'

const u = require('universalify').fromCallback
const fs = require('graceful-fs')
const u = require('universalify').fromPromise
const fs = require('../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'
}

async function outputFile (file, data, encoding = 'utf-8') {
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)
if (!(await pathExists(dir))) {
await mkdir.mkdirs(dir)
}

fs.writeFile(file, data, encoding, callback)
})
})
return fs.writeFile(file, data, encoding)
}

function outputFileSync (file, ...args) {
const dir = path.dirname(file)
if (fs.existsSync(dir)) {
return fs.writeFileSync(file, ...args)
if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
}
mkdir.mkdirsSync(dir)

fs.writeFileSync(file, ...args)
}

Expand Down

0 comments on commit 426bb46

Please sign in to comment.