Skip to content

Commit

Permalink
update to tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 16, 2024
1 parent 82a1ccf commit 8db24e9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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',
Expand Down
43 changes: 0 additions & 43 deletions .github/actions/get_test_infos/discover_test_dirs.js

This file was deleted.

46 changes: 46 additions & 0 deletions .github/actions/get_test_infos/discover_test_dirs.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
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<boolean> {
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;
}
}
21 changes: 0 additions & 21 deletions .github/actions/get_test_infos/get_test_infos.js

This file was deleted.

2 changes: 1 addition & 1 deletion .github/actions/get_test_infos/get_test_infos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'}"
Expand Down
25 changes: 25 additions & 0 deletions .github/actions/get_test_infos/get_test_infos.ts
Original file line number Diff line number Diff line change
@@ -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));
}
File renamed without changes.

0 comments on commit 8db24e9

Please sign in to comment.