Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ findFile(files, (file) => file.endsWith('.css'));
findFile(files, 'index.abcdef12.js', { ignoreHash: false });
```

### findFiles

Finds all matching paths with the same matcher and options as `findFile`. Returns an empty array when no files match.

```ts
import { findFiles } from '@rstackjs/test-utils';

const cssFiles = findFiles(files, '.css');
// ['/dist/styles.css']
```

### getDistFiles

Recursively reads UTF-8 files from a dist directory. Source map files are excluded by default.
Expand Down
14 changes: 14 additions & 0 deletions src/findFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,17 @@ export const findFile = (

throw new Error(`Unable to find file matching "${matcher.toString()}"`);
};

/** Find all file paths that match the provided matcher. */
export const findFiles = (
files: Record<string, string>,
matcher: FileMatcher,
options: FindFileOptions = {},
): string[] => {
const { ignoreHash = true } = options;
const matcherFn = toMatcher(matcher);

return Object.keys(files).filter((file) =>
matcherFn(ignoreHash ? file.replace(HASH_PATTERN, '') : file),
);
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
type FileMatcher,
findFile,
type FindFileOptions,
findFiles,
} from './findFile.js';
export { getDistFiles } from './getDistFiles.js';
export { getFileContent } from './getFileContent.js';
Expand Down
29 changes: 29 additions & 0 deletions tests/findFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, test } from 'rstack/test';
import { findFiles } from '../src/index';

const files = {
'/dist/index.abcdef12.js': 'console.log("index")',
'/dist/nested/index.12345678.js': 'console.log("nested")',
'/dist/styles.css': '.root {}',
};

test('should return all matching paths for string, regex and function matchers', () => {
const expected = [
'/dist/index.abcdef12.js',
'/dist/nested/index.12345678.js',
];

expect(findFiles(files, 'index.js')).toEqual(expected);
expect(findFiles(files, /index\.js$/g)).toEqual(expected);
expect(findFiles(files, (file) => file.endsWith('index.js'))).toEqual(
expected,
);
expect(findFiles(files, 'missing.js')).toEqual([]);
});

test('should match original paths when ignoreHash is false', () => {
expect(findFiles(files, 'index.js', { ignoreHash: false })).toEqual([]);
expect(findFiles(files, 'index.abcdef12.js', { ignoreHash: false })).toEqual([
'/dist/index.abcdef12.js',
]);
});