From 8db24e9336805d24dc15ac52a39261b5c41ed5b5 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Mon, 16 Sep 2024 10:15:33 -0600 Subject: [PATCH] update to tsx --- ...ir_to_test_info.js => dir_to_test_info.ts} | 15 ++++-- .../get_test_infos/discover_test_dirs.js | 43 ----------------- .../get_test_infos/discover_test_dirs.ts | 46 +++++++++++++++++++ .../actions/get_test_infos/get_test_infos.js | 21 --------- .../actions/get_test_infos/get_test_infos.sh | 2 +- .../actions/get_test_infos/get_test_infos.ts | 25 ++++++++++ .../get_test_infos/{index.js => index.ts} | 0 7 files changed, 84 insertions(+), 68 deletions(-) rename .github/actions/get_test_infos/{dir_to_test_info.js => dir_to_test_info.ts} (74%) delete mode 100644 .github/actions/get_test_infos/discover_test_dirs.js create mode 100644 .github/actions/get_test_infos/discover_test_dirs.ts delete mode 100644 .github/actions/get_test_infos/get_test_infos.js create mode 100644 .github/actions/get_test_infos/get_test_infos.ts rename .github/actions/get_test_infos/{index.js => index.ts} (100%) diff --git a/.github/actions/get_test_infos/dir_to_test_info.js b/.github/actions/get_test_infos/dir_to_test_info.ts similarity index 74% rename from .github/actions/get_test_infos/dir_to_test_info.js rename to .github/actions/get_test_infos/dir_to_test_info.ts index f8f19e01db..418ff798d2 100644 --- a/.github/actions/get_test_infos/dir_to_test_info.js +++ b/.github/actions/get_test_infos/dir_to_test_info.ts @@ -1,6 +1,15 @@ import { basename } from 'path'; -export function dirToTestInfo(dir) { +type TestInfo = { + path: string; + name: string; + type: Type; + displayPath: string; +}; + +type Type = 'ex' | 'e2e' | 'prop' | ''; + +export function dirToTestInfo(dir: string): TestInfo { const path = dir; const name = basename(dir); const type = getType(dir); @@ -9,14 +18,14 @@ export function dirToTestInfo(dir) { return { path, name, type, displayPath }; } -function getType(dir) { +function getType(dir: string): Type { if (dir.includes('examples/')) return 'ex'; if (dir.includes('/end_to_end/')) return 'e2e'; if (dir.includes('/property/')) return 'prop'; return ''; } -function generateDisplayPath(path) { +function generateDisplayPath(path: string): string { const replacements = { examples: 'ex', property: 'prop', diff --git a/.github/actions/get_test_infos/discover_test_dirs.js b/.github/actions/get_test_infos/discover_test_dirs.js deleted file mode 100644 index 629c2cbbd6..0000000000 --- a/.github/actions/get_test_infos/discover_test_dirs.js +++ /dev/null @@ -1,43 +0,0 @@ -import { promises as fs } from 'fs'; -import path from 'path'; - -// Discover directories containing a package.json file with a test script -export async function discoverTestDirs(dirsToSearch) { - const directories = []; - await findDirectories(dirsToSearch, directories); - return directories; -} - -// Recursively find directories and check for package.json with a test script -async function findDirectories(currentDir, directories) { - const files = await fs.readdir(currentDir, { withFileTypes: true }); - - for (const file of files) { - const fullPath = path.join(currentDir, file.name); - - if (file.isDirectory() && !fullPath.includes('node_modules')) { - // Check for package.json and if it contains a test script - const packageJsonPath = path.join(fullPath, 'package.json'); - const hasTestScript = await checkForTestScript(packageJsonPath); - - if (hasTestScript) { - directories.push(fullPath); - } - - // Recurse into subdirectory - await findDirectories(fullPath, directories); - } - } -} - -// Check if package.json contains a test script -async function checkForTestScript(packageJsonPath) { - try { - const packageJson = await fs.readFile(packageJsonPath, 'utf-8'); - const packageData = JSON.parse(packageJson); - return packageData.scripts && packageData.scripts.test; - } catch (err) { - // Return false if the file doesn't exist or there's a JSON error - return false; - } -} diff --git a/.github/actions/get_test_infos/discover_test_dirs.ts b/.github/actions/get_test_infos/discover_test_dirs.ts new file mode 100644 index 0000000000..171857176d --- /dev/null +++ b/.github/actions/get_test_infos/discover_test_dirs.ts @@ -0,0 +1,46 @@ +import { promises as fs } from 'fs'; +import path from 'path'; + +// Recursively find directories and check for package.json with a test script +export async function discoverTestDirs(dirToSearch: string): Promise { + const files = await fs.readdir(dirToSearch, { withFileTypes: true }); + + return files.reduce( + async (accPromise, file) => { + const acc = await accPromise; + + const fullPath = path.join(dirToSearch, file.name); + + if (file.isDirectory() && !fullPath.includes('node_modules')) { + // Check for package.json and if it contains a test script + const packageJsonPath = path.join(fullPath, 'package.json'); + const hasTestScript = await checkForTestScript(packageJsonPath); + + // Recurse into subdirectory + return [ + ...acc, + ...(hasTestScript ? [fullPath] : []), + ...(await discoverTestDirs(fullPath)) + ]; + } + + return []; + }, + Promise.resolve([] as string[]) + ); +} + +// Check if package.json exists and contains a test script +async function checkForTestScript(packageJsonPath: string): Promise { + try { + const packageJson = await fs.readFile(packageJsonPath, 'utf-8'); + const packageData = JSON.parse(packageJson); + return ( + packageData.scripts !== undefined && + packageData.scripts.test !== undefined + ); + } catch { + // Return false if the file doesn't exist or there's a JSON error + return false; + } +} diff --git a/.github/actions/get_test_infos/get_test_infos.js b/.github/actions/get_test_infos/get_test_infos.js deleted file mode 100644 index 10c4b30197..0000000000 --- a/.github/actions/get_test_infos/get_test_infos.js +++ /dev/null @@ -1,21 +0,0 @@ -import { discoverTestDirs } from './discover_test_dirs.js'; -import { dirToTestInfo } from './dir_to_test_info.js'; - -export async function getTestInfos(dirs, excludeDirs) { - const allDirs = await dirs - .reduce(async (accPromise, dir) => { - const acc = await accPromise; - const discoveredDirs = await discoverTestDirs(dir); - return [...acc, ...discoveredDirs]; - }, Promise.resolve([])) - .filter((dir) => dir && !isExcluded(dir, excludeDirs)) - .sort(); - - const testInfos = allDirs.map((dir) => dirToTestInfo(dir)); - - return testInfos; -} - -function isExcluded(dir, excludeDirs) { - return excludeDirs.some((exclude) => dir.includes(exclude)); -} diff --git a/.github/actions/get_test_infos/get_test_infos.sh b/.github/actions/get_test_infos/get_test_infos.sh index 5ddea5ee03..5ec4c7e873 100755 --- a/.github/actions/get_test_infos/get_test_infos.sh +++ b/.github/actions/get_test_infos/get_test_infos.sh @@ -9,7 +9,7 @@ directories=$(IFS=, ; echo "${DIRECTORIES[*]}") exclude_dirs=$(IFS=, ; echo "${EXCLUDE_DIRS[*]}") # Get test infos -json_result=$(node .github/actions/get_test_infos/index.js "$directories" "$exclude_dirs") +json_result=$(npx tsx .github/actions/get_test_infos/index.js "$directories" "$exclude_dirs") # Format the result result="${json_result//'%'/'%25'}" diff --git a/.github/actions/get_test_infos/get_test_infos.ts b/.github/actions/get_test_infos/get_test_infos.ts new file mode 100644 index 0000000000..28f8250d44 --- /dev/null +++ b/.github/actions/get_test_infos/get_test_infos.ts @@ -0,0 +1,25 @@ +import { discoverTestDirs } from './discover_test_dirs.js'; +import { dirToTestInfo } from './dir_to_test_info.js'; + +export async function getTestInfos(dirs: string[], excludeDirs: string[]) { + const allDirs = ( + await dirs.reduce( + async (accPromise, dir) => { + const acc = await accPromise; + const discoveredDirs = await discoverTestDirs(dir); + return [...acc, ...discoveredDirs]; + }, + Promise.resolve([] as string[]) + ) + ) + .filter((dir) => dir && !isExcluded(dir, excludeDirs)) + .sort(); + + const testInfos = allDirs.map((dir) => dirToTestInfo(dir)); + + return testInfos; +} + +function isExcluded(dir, excludeDirs) { + return excludeDirs.some((exclude) => dir.includes(exclude)); +} diff --git a/.github/actions/get_test_infos/index.js b/.github/actions/get_test_infos/index.ts similarity index 100% rename from .github/actions/get_test_infos/index.js rename to .github/actions/get_test_infos/index.ts