├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 20 15 | - 18 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if a directory is empty. 3 | 4 | @param path - The path to the directory to check. 5 | @returns A promise that resolves to true if the directory is empty, false otherwise. 6 | @throws If the path is not a directory or does not exist. 7 | 8 | @example 9 | ``` 10 | import {isDirectoryEmpty} from 'is-directory-empty'; 11 | 12 | console.log(await isDirectoryEmpty('./some-directory')); 13 | //=> true 14 | ``` 15 | */ 16 | export function isDirectoryEmpty(path: string): Promise; 17 | 18 | /** 19 | Synchronously check if a directory is empty. 20 | 21 | @param path - The path to the directory to check. 22 | @returns True if the directory is empty, false otherwise. 23 | @throws If the path is not a directory or does not exist. 24 | 25 | @example 26 | ``` 27 | import {isDirectoryEmptySync} from 'is-directory-empty'; 28 | 29 | console.log(isDirectoryEmptySync('./some-directory')); 30 | //=> true 31 | ``` 32 | */ 33 | export function isDirectoryEmptySync(path: string): boolean; 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import fsPromises from 'node:fs/promises'; 3 | 4 | export async function isDirectoryEmpty(path) { 5 | const directoryHandle = await fsPromises.opendir(path); 6 | 7 | const {done} = await directoryHandle[Symbol.asyncIterator]().next(); 8 | 9 | if (!done) { 10 | await directoryHandle.close(); 11 | } 12 | 13 | return done; 14 | } 15 | 16 | export function isDirectoryEmptySync(path) { 17 | const directoryHandle = fs.opendirSync(path); 18 | 19 | try { 20 | return directoryHandle.readSync() === null; 21 | } finally { 22 | directoryHandle.closeSync(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-directory-empty", 3 | "version": "1.0.0", 4 | "description": "Check if a directory is empty", 5 | "license": "MIT", 6 | "repository": "sindresorhus/is-directory-empty", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsc index.d.ts" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "directory", 31 | "folder", 32 | "empty", 33 | "fs", 34 | "filesystem", 35 | "check", 36 | "detect", 37 | "is", 38 | "path", 39 | "contents", 40 | "read" 41 | ], 42 | "devDependencies": { 43 | "ava": "^6.1.3", 44 | "tempy": "^3.1.0", 45 | "typescript": "^5.5.4", 46 | "xo": "^0.59.2" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-directory-empty 2 | 3 | > Check if a directory is empty 4 | 5 | It uses the fastest way to check. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install is-directory-empty 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import {isDirectoryEmpty} from 'is-directory-empty'; 17 | 18 | console.log(await isDirectoryEmpty('./some-directory')); 19 | //=> true 20 | ``` 21 | 22 | ## API 23 | 24 | ### `isDirectoryEmpty(path: string): Promise` 25 | ### `isDirectoryEmptySync(path: string): boolean` 26 | 27 | Returns a boolean indicating whether the directory at the given path is empty. 28 | 29 | Throws if the path is not a directory or does not exist. 30 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import fsPromises from 'node:fs/promises'; 3 | import path from 'node:path'; 4 | import test from 'ava'; 5 | import {temporaryDirectory} from 'tempy'; 6 | import {isDirectoryEmpty, isDirectoryEmptySync} from './index.js'; 7 | 8 | test.beforeEach(t => { 9 | t.context.temporaryDirectory = temporaryDirectory(); 10 | }); 11 | 12 | test.afterEach.always(async t => { 13 | await fsPromises.rm(t.context.temporaryDirectory, {recursive: true}); 14 | }); 15 | 16 | test('isDirectoryEmpty - returns true for empty directory', async t => { 17 | const result = await isDirectoryEmpty(t.context.temporaryDirectory); 18 | t.true(result); 19 | }); 20 | 21 | test('isDirectoryEmpty - returns false for non-empty directory', async t => { 22 | await fsPromises.writeFile(path.join(t.context.temporaryDirectory, 'test.txt'), 'test'); 23 | const result = await isDirectoryEmpty(t.context.temporaryDirectory); 24 | t.false(result); 25 | }); 26 | 27 | test('isDirectoryEmpty - throws error for non-existent directory', async t => { 28 | const nonExistentDirectory = temporaryDirectory(); 29 | await fsPromises.rm(nonExistentDirectory, {recursive: true, force: true}); 30 | await t.throwsAsync(isDirectoryEmpty(nonExistentDirectory), {code: 'ENOENT'}); 31 | }); 32 | 33 | test('isDirectoryEmptySync - returns true for empty directory', t => { 34 | const result = isDirectoryEmptySync(t.context.temporaryDirectory); 35 | t.true(result); 36 | }); 37 | 38 | test('isDirectoryEmptySync - returns false for non-empty directory', t => { 39 | fs.writeFileSync(path.join(t.context.temporaryDirectory, 'test.txt'), 'test'); 40 | const result = isDirectoryEmptySync(t.context.temporaryDirectory); 41 | t.false(result); 42 | }); 43 | 44 | test('isDirectoryEmptySync - throws error for non-existent directory', t => { 45 | const nonExistentDirectory = temporaryDirectory(); 46 | fs.rmSync(nonExistentDirectory, {recursive: true}); 47 | t.throws(() => { 48 | isDirectoryEmptySync(nonExistentDirectory); 49 | }, {code: 'ENOENT'}); 50 | }); 51 | --------------------------------------------------------------------------------