Skip to content

Commit

Permalink
✨ feat(core): Add getWalletSetupFiles function
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Oct 24, 2023
1 parent a3c535e commit d224dbe
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/core/src/utils/getWalletSetupFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fs from 'fs-extra'

const SETUP_FILE_FILTER_REGEX = /\.setup\.(js|ts)$/

export async function getWalletSetupFiles(walletSetupDirPath: string) {
await fs.access(walletSetupDirPath).catch((e) => {
// TODO: This should utilize the `e.code`.
// TODO: See: https://nodejs.org/api/fs.html#fsexistspath-callback
// TODO: See fix: https://stackoverflow.com/a/49562477
// TODO: Replace ALL occurrences of `fs.exists` with `fs.access`.
if (e instanceof Error && e.message.includes('ENOENT')) {
throw new Error(`[GetWalletSetupFiles] Wallet setup directory does not exist at ${walletSetupDirPath}`)
}

throw e
})

const setupFilesFilter = (file: string) => file.match(SETUP_FILE_FILTER_REGEX)
const fileList = (await fs.readdir(walletSetupDirPath)).filter(setupFilesFilter)

if (!fileList.length) {
throw new Error(
[
`[GetWalletSetupFiles] No wallet setup files found at ${walletSetupDirPath}`,
'Remember that all wallet setup files must end with `.setup.{js,ts}` extension!'
].join('\n')
)
}

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

const ROOT_DIR = '/tmp'
const DUMMY_CONTENT = 'Hello world! 👋'

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

async function createFiles(fileNames: string[]) {
for (const fileName of fileNames) {
const filePath = path.join(ROOT_DIR, fileName)
await fs.promises.writeFile(filePath, DUMMY_CONTENT)
}
}

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

beforeEach(async () => {
vol.mkdirSync(ROOT_DIR)
await createFiles([
'quack',
'quack.js',
'quack.ts',
'quack.tsx',
'quack.setup',
'quack.setup.js',
'quack.setup.ts',
'quack.setup.tsx'
])
})

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

it('throws if the target directory does not exist', async () => {
const nonExistentDirPath = path.join(ROOT_DIR, 'non-existent-dir')
await expect(getWalletSetupFiles(nonExistentDirPath)).rejects.toThrowError(
`[GetWalletSetupFiles] Wallet setup directory does not exist at ${nonExistentDirPath}`
)
})

it('throws if no setup files are found in the target directory', async () => {
const emptyDirPath = path.join(ROOT_DIR, 'empty-dir')
vol.mkdirSync(emptyDirPath)

await expect(getWalletSetupFiles(emptyDirPath)).rejects.toThrowError(
`[GetWalletSetupFiles] No wallet setup files found at ${emptyDirPath}`
)
})

it('returns only valid setup files', async () => {
const setupFiles = await getWalletSetupFiles(ROOT_DIR)

expect(setupFiles).toHaveLength(2)
expect(setupFiles).toContain('quack.setup.js')
expect(setupFiles).toContain('quack.setup.ts')
})
})

0 comments on commit d224dbe

Please sign in to comment.