diff --git a/lib/fs.js b/lib/fs.js index 6d094da..646e6e9 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -36,54 +36,20 @@ function existsSync(path) { return true; } -async function _mkdirs(path) { - const parent = dirname(path); - - try { - await fsPromises.access(path); - } catch (err) { - if (err.code !== 'ENOENT') throw err; - await _mkdirs(parent); - } - - try { - await fsPromises.mkdir(path); - } catch (err) { - if (err.code !== 'EEXIST') throw err; - } -} - function mkdirs(path, callback) { if (!path) throw new TypeError('path is required!'); - return Promise.resolve(_mkdirs(path)).asCallback(callback); + return Promise.resolve(fsPromises.mkdir(path, { recursive: true })).asCallback(callback); } function mkdirsSync(path) { if (!path) throw new TypeError('path is required!'); - const parent = dirname(path); - - if (!fs.existsSync(parent)) mkdirsSync(parent); - fs.mkdirSync(path); + fs.mkdirSync(path, { recursive: true }); } function checkParent(path) { - return Promise.resolve(_mkdirs(dirname(path))); -} - -function checkParentSync(path) { - if (!path) throw new TypeError('path is required!'); - - const parent = dirname(path); - - if (fs.existsSync(parent)) return; - - try { - mkdirsSync(parent); - } catch (err) { - if (err.code !== 'EEXIST') throw err; - } + return Promise.resolve(fsPromises.mkdir(dirname(path), { recursive: true })); } function writeFile(path, data, options, callback) { @@ -100,7 +66,7 @@ function writeFile(path, data, options, callback) { function writeFileSync(path, data, options) { if (!path) throw new TypeError('path is required!'); - checkParentSync(path); + fs.mkdirSync(dirname(path), { recursive: true }); fs.writeFileSync(path, data, options); } @@ -118,7 +84,7 @@ function appendFile(path, data, options, callback) { function appendFileSync(path, data, options) { if (!path) throw new TypeError('path is required!'); - checkParentSync(path); + fs.mkdirSync(dirname(path), { recursive: true }); fs.appendFileSync(path, data, options); } @@ -466,7 +432,7 @@ function ensureWriteStream(path, options, callback) { function ensureWriteStreamSync(path, options) { if (!path) throw new TypeError('path is required!'); - checkParentSync(path); + fs.mkdirSync(dirname(path), { recursive: true }); return fs.createWriteStream(path, options); }