-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(core): Add
getWalletSetupFiles
function
- Loading branch information
1 parent
a3c535e
commit c8ed551
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |