make-empty-dir
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -0,3 +1,7 @@ | ||
declare function makeEmptyDir (dir: string, opts?: { recursive?: boolean }): Promise<'created' | 'emptied'> | ||
declare namespace makeEmptyDir { | ||
function sync(dir: string, opts?: { recursive?: boolean }): 'created' | 'emptied' | ||
} | ||
export = makeEmptyDir | ||
declare function makeEmptyDir (dir: string, opts?: { recursive?: boolean }): Promise<'created' | 'emptied'> |
31
index.js
'use strict' | ||
const fs = require('fs').promises | ||
const fs = require('fs') | ||
const path = require('path') | ||
@@ -8,6 +8,6 @@ const rimraf = require('@zkochan/rimraf') | ||
if (opts && opts.recursive) { | ||
await fs.mkdir(path.dirname(dir), { recursive: true }) | ||
await fs.promises.mkdir(path.dirname(dir), { recursive: true }) | ||
} | ||
try { | ||
await fs.mkdir(dir) | ||
await fs.promises.mkdir(dir) | ||
return 'created' | ||
@@ -24,3 +24,3 @@ } catch (err) { | ||
async function removeContentsOfDir (dir) { | ||
const items = await fs.readdir(dir) | ||
const items = await fs.promises.readdir(dir) | ||
for (const item of items) { | ||
@@ -30,1 +30,24 @@ await rimraf(path.join(dir, item)) | ||
} | ||
module.exports.sync = function makeEmptyDirSync (dir, opts) { | ||
if (opts && opts.recursive) { | ||
fs.mkdirSync(path.dirname(dir), { recursive: true }) | ||
} | ||
try { | ||
fs.mkdirSync(dir) | ||
return 'created' | ||
} catch (err) { | ||
if (err.code === 'EEXIST') { | ||
removeContentsOfDirSync(dir) | ||
return 'emptied' | ||
} | ||
throw err | ||
} | ||
} | ||
function removeContentsOfDirSync (dir) { | ||
const items = fs.readdirSync(dir) | ||
for (const item of items) { | ||
rimraf.sync(path.join(dir, item)) | ||
} | ||
} |
{ | ||
"name": "make-empty-dir", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Ensures that a directory is empty", | ||
@@ -10,9 +10,6 @@ "main": "index.js", | ||
], | ||
"scripts": { | ||
"test": "node test" | ||
}, | ||
"engines": { | ||
"node": ">=12.10" | ||
}, | ||
"repository": "https://github.com/zkochan/packages/tree/master/make-empty-dir", | ||
"repository": "https://github.com/zkochan/packages/tree/main/make-empty-dir", | ||
"keywords": [ | ||
@@ -30,10 +27,13 @@ "directory", | ||
"license": "MIT", | ||
"homepage": "https://github.com/zkochan/packages/tree/master/make-empty-dir#readme", | ||
"homepage": "https://github.com/zkochan/packages/tree/main/make-empty-dir#readme", | ||
"devDependencies": { | ||
"tape": "^5.1.1", | ||
"tempy": "^1.0.0" | ||
"tape": "^5.3.2", | ||
"tempy": "^1.0.1" | ||
}, | ||
"dependencies": { | ||
"@zkochan/rimraf": "^2.1.0" | ||
"@zkochan/rimraf": "^2.1.3" | ||
}, | ||
"scripts": { | ||
"test": "node test" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
4438
51
5
Updated@zkochan/rimraf@^2.1.3