Skip to content

Commit

Permalink
✨ feat(core): Add isDirEmpty util
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Jan 22, 2024
1 parent a37baa5 commit f2fb5a9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/utils/isDirEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from 'fs-extra'

export async function isDirEmpty(dirPath: string) {
const files = await fs.readdir(dirPath)

return files.length === 0
}
39 changes: 39 additions & 0 deletions packages/core/test/utils/isDirEmpty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import path from 'node:path'
import { fs, vol } from 'memfs'
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { isDirEmpty } from '../../src/utils/isDirEmpty'

const ROOT_DIR = '/tmp'
const FILE_PATH = path.join(ROOT_DIR, 'file.txt')

vi.mock('fs-extra', async () => {
return {
default: fs.promises
}
})

describe('isDirEmpty', () => {
afterAll(() => {
vi.resetAllMocks()
})

beforeEach(() => {
vol.mkdirSync(ROOT_DIR)
})

afterEach(() => {
vol.reset() // Clear the in-memory file system after each test
})

it('returns `true` if the directory is empty', async () => {
const isEmpty = await isDirEmpty(ROOT_DIR)
expect(isEmpty).toEqual(true)
})

it('returns `false` if the directory contains files', async () => {
fs.writeFileSync(FILE_PATH, 'Hello there! 👋')

const isEmpty = await isDirEmpty(ROOT_DIR)
expect(isEmpty).toEqual(false)
})
})

0 comments on commit f2fb5a9

Please sign in to comment.