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 c8ed551
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/utils/getWalletSetupFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'fs-extra'

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

export async function getWalletSetupFiles(walletSetupDirPath: string) {
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
}
61 changes: 61 additions & 0 deletions packages/core/test/utils/getWalletSetupFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 no setup files are found', 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 c8ed551

Please sign in to comment.