-
-
Notifications
You must be signed in to change notification settings - Fork 195
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 d224dbe
Showing
2 changed files
with
99 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,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 | ||
} |
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,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') | ||
}) | ||
}) |