From cab988c7de74221262ff89698ac049c4bc413db5 Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Tue, 24 Oct 2023 22:32:36 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(core):=20Add=20`getWalletSetup?= =?UTF-8?q?Files`=20function=20(#949)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/src/utils/getWalletSetupFiles.ts | 31 +++++++++ .../test/utils/getWalletSetupFiles.test.ts | 68 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 packages/core/src/utils/getWalletSetupFiles.ts create mode 100644 packages/core/test/utils/getWalletSetupFiles.test.ts diff --git a/packages/core/src/utils/getWalletSetupFiles.ts b/packages/core/src/utils/getWalletSetupFiles.ts new file mode 100644 index 000000000..ec153c4e8 --- /dev/null +++ b/packages/core/src/utils/getWalletSetupFiles.ts @@ -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 +} diff --git a/packages/core/test/utils/getWalletSetupFiles.test.ts b/packages/core/test/utils/getWalletSetupFiles.test.ts new file mode 100644 index 000000000..4be0dfea7 --- /dev/null +++ b/packages/core/test/utils/getWalletSetupFiles.test.ts @@ -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') + }) +})