Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): add find in file logic #978

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
105 changes: 105 additions & 0 deletions packages/utils/src/lib/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { bold, gray } from 'ansis';
import { type Options, bundleRequire } from 'bundle-require';
import * as fs from 'node:fs';
import { mkdir, readFile, readdir, rm, stat } from 'node:fs/promises';
import path from 'node:path';
import * as readline from 'node:readline';
import type { SourceFileLocation } from '@code-pushup/models';
import { formatBytes } from './formatting.js';
import { logMultipleResults } from './log-results.js';
import { ui } from './logging.js';
Expand Down Expand Up @@ -93,6 +96,7 @@
pattern?: string | RegExp;
fileTransform?: (filePath: string) => Promise<T> | T;
};

export async function crawlFileSystem<T = string>(
options: CrawlFileSystemOptions<T>,
): Promise<T[]> {
Expand Down Expand Up @@ -159,3 +163,104 @@
export function projectToFilename(project: string): string {
return project.replace(/[/\\\s]+/g, '-').replace(/@/g, '');
}

export type LineHit = {

Check warning on line 167 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Types coverage

Missing types documentation for LineHit
startColumn: number;
endColumn: number;
};

export type FileHit = Pick<SourceFileLocation, 'file'> &

Check warning on line 172 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Types coverage

Missing types documentation for FileHit
Exclude<SourceFileLocation['position'], undefined>;

const escapeRegExp = (str: string): string =>

Check warning on line 175 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Variables coverage

Missing variables documentation for escapeRegExp
str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

Check warning on line 176 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> ESLint | Prefer using the `String.raw` tag to avoid escaping `\`.

`String.raw` should be used to avoid escaping `\`.
const ensureGlobalRegex = (pattern: RegExp): RegExp =>

Check failure on line 177 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Function coverage

Function ensureGlobalRegex is not called in any test case.

Check warning on line 177 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Variables coverage

Missing variables documentation for ensureGlobalRegex
new RegExp(
pattern.source,
pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`,
);

Check warning on line 181 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 178-181 are not covered in any test case.

const findAllMatches = (

Check warning on line 183 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Variables coverage

Missing variables documentation for findAllMatches
line: string,
searchPattern: string | RegExp | ((line: string) => LineHit[] | null),
): LineHit[] => {
if (typeof searchPattern === 'string') {
return [...line.matchAll(new RegExp(escapeRegExp(searchPattern), 'g'))].map(
({ index = 0 }) => ({
startColumn: index,
endColumn: index + searchPattern.length,
}),
);
}

Check failure on line 194 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.

if (searchPattern instanceof RegExp) {
return [...line.matchAll(ensureGlobalRegex(searchPattern))].map(
({ index = 0, 0: match }) => ({
startColumn: index,
endColumn: index + match.length,
}),
);
}

return searchPattern(line) || [];

Check warning on line 205 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 195-205 are not covered in any test case.
};

/**
* Reads a file line-by-line and checks if it contains the search pattern.
* @param file - The file path to check.
* @param searchPattern - The pattern to match.
* @param options - Additional options. If true, the search will stop after the first hit.
* @returns Promise<FileHit[]> - List of hits with matching details.
*/
export async function findInFile(
file: string,
searchPattern: string | RegExp | ((line: string) => LineHit[] | null),
options?: { bail?: boolean },
): Promise<FileHit[]> {
const { bail = false } = options || {};
const hits: FileHit[] = [];

return new Promise((resolve, reject) => {
const stream = fs.createReadStream(file, { encoding: 'utf8' });
const rl = readline.createInterface({ input: stream });
// eslint-disable-next-line functional/no-let
let lineNumber = 0;
// eslint-disable-next-line functional/no-let
let isResolved = false;

rl.on('line', line => {
lineNumber++;
const matches = findAllMatches(line, searchPattern);

matches.forEach(({ startColumn, endColumn }) => {
// eslint-disable-next-line functional/immutable-data
hits.push({
file,
startLine: lineNumber,
startColumn,
endLine: lineNumber,
endColumn,
});

if (bail && !isResolved) {

Check failure on line 245 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.

Check failure on line 245 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
isResolved = true;
stream.destroy();
resolve(hits);
}

Check warning on line 249 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 246-249 are not covered in any test case.
});
});
rl.once('close', () => {
if (!isResolved) {
isResolved = true;
}
resolve(hits); // Resolve only once after closure
});

rl.once('error', error => {
if (!isResolved) {
isResolved = true;
reject(error);
}

Check warning on line 263 in packages/utils/src/lib/file-system.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 260-263 are not covered in any test case.
});
});
}
41 changes: 41 additions & 0 deletions packages/utils/src/lib/file-system.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
crawlFileSystem,
ensureDirectoryExists,
filePathToCliArg,
findInFile,
findLineNumberInText,
findNearestFile,
logMultipleFileResults,
Expand Down Expand Up @@ -263,3 +264,43 @@ describe('projectToFilename', () => {
expect(projectToFilename(project)).toBe(file);
});
});

describe('findInFile', () => {
const file = 'file.txt';
const content =
'line 1 - even:false\nline 2 - even:true\nline 3 - even:false\nline 4 - even:true\nline 5 - even:false\n';
const filePath = path.join(MEMFS_VOLUME, file);

beforeEach(() => {
vol.reset();
vol.fromJSON({ [file]: content }, MEMFS_VOLUME);
});

it('should find pattern in a file if a string is given', async () => {
const result = await findInFile(filePath, 'line 3');
expect(result).toStrictEqual([
{
file: filePath,
endColumn: 6,
endLine: 3,
startColumn: 0,
startLine: 3,
},
]);
});

// @TODO any second test will fail
// Error: EBADF: bad file descriptor, close
it.todo('should find pattern in a file if a RegEx is given', async () => {
const result = await findInFile('file.txt', new RegExp('line 3', 'g'));
expect(result).toStrictEqual([
{
file: 'file.txt',
endColumn: 6,
endLine: 3,
startColumn: 0,
startLine: 3,
},
]);
});
});
Loading