From 07ca291d173c4ca15a4c0c569f5d845829150757 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 20 Jun 2019 17:50:44 +0700 Subject: [PATCH] Throw ENOTDIR error when dir doesn't exist for makeFile async --- lib/ensure/file.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/ensure/file.js b/lib/ensure/file.js index fa0f40be..49b17c94 100644 --- a/lib/ensure/file.js +++ b/lib/ensure/file.js @@ -8,9 +8,21 @@ const pathExists = require('../path-exists').pathExists function createFile (file, callback) { function makeFile () { - fs.writeFile(file, '', err => { + const dir = path.dirname(file) + + fs.stat(dir, (err, stats) => { if (err) return callback(err) - callback() + if (!stats.isDirectory()) { + // This is just to cause an internal ENOTDIR error to be thrown + fs.readdir(dir, err => { + if (err) return callback(err) + }) + } else { + fs.writeFile(file, '', err => { + if (err) return callback(err) + callback() + }) + } }) }