Skip to content

Commit 45e8ca0

Browse files
authored
fix(core): handle non-source files better (#35)
* fix(core): handle non-source files better * pr changes
1 parent b40b9ad commit 45e8ca0

File tree

4 files changed

+98
-38
lines changed

4 files changed

+98
-38
lines changed

libs/core/src/assets.spec.ts

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('findNonSourceAffectedFiles', () => {
1313

1414
it('should return relevant files', () => {
1515
const cwd = '/project';
16-
const changedFilePath = '/project/src/file.ts';
16+
const changedFilePaths = ['/project/src/file.ts'];
1717
const excludeFolderPaths = ['node_modules', 'dist', '.git'];
1818

1919
(fastFindInFiles as jest.Mock).mockReturnValue([
@@ -26,14 +26,50 @@ describe('findNonSourceAffectedFiles', () => {
2626

2727
const result = findNonSourceAffectedFiles(
2828
cwd,
29-
changedFilePath,
29+
changedFilePaths,
3030
excludeFolderPaths
3131
);
3232

3333
expect(result).toEqual([{ filePath: 'src/file.ts', changedLines: [1] }]);
3434
expect(fastFindInFiles).toHaveBeenCalledWith({
3535
directory: cwd,
36-
needle: path.basename(changedFilePath),
36+
needle: new RegExp(
37+
changedFilePaths
38+
.map((changedFilePath) => path.basename(changedFilePath))
39+
.join('|')
40+
.replaceAll('.', '\\.')
41+
),
42+
excludeFolderPaths: excludeFolderPaths.map((folder) =>
43+
path.join(cwd, folder)
44+
),
45+
});
46+
});
47+
48+
it('should aggregate changedFilePaths to regExp needle', () => {
49+
const cwd = '/project';
50+
const changedFilePaths = ['/project/src/file.ts', '/project/src/file2.ts'];
51+
const excludeFolderPaths = ['node_modules', 'dist', '.git'];
52+
(fastFindInFiles as jest.Mock).mockReturnValue([
53+
{
54+
filePath: '/project/src/file.ts',
55+
queryHits: [{ lineNumber: 1, line: `"file.ts"` }],
56+
},
57+
]);
58+
(existsSync as jest.Mock).mockReturnValue(true);
59+
const result = findNonSourceAffectedFiles(
60+
cwd,
61+
changedFilePaths,
62+
excludeFolderPaths
63+
);
64+
expect(result).toEqual([{ filePath: 'src/file.ts', changedLines: [1] }]);
65+
expect(fastFindInFiles).toHaveBeenCalledWith({
66+
directory: cwd,
67+
needle: new RegExp(
68+
changedFilePaths
69+
.map((changedFilePath) => path.basename(changedFilePath))
70+
.join('|')
71+
.replaceAll('.', '\\.')
72+
),
3773
excludeFolderPaths: excludeFolderPaths.map((folder) =>
3874
path.join(cwd, folder)
3975
),
@@ -42,22 +78,27 @@ describe('findNonSourceAffectedFiles', () => {
4278

4379
it('should return empty array if no relevant files found', () => {
4480
const cwd = '/project';
45-
const changedFilePath = '/project/src/file.ts';
81+
const changedFilePaths = ['/project/src/file.ts'];
4682
const excludeFolderPaths = ['node_modules', 'dist', '.git'];
4783

4884
(fastFindInFiles as jest.Mock).mockReturnValue([]);
4985
(existsSync as jest.Mock).mockReturnValue(true);
5086

5187
const result = findNonSourceAffectedFiles(
5288
cwd,
53-
changedFilePath,
89+
changedFilePaths,
5490
excludeFolderPaths
5591
);
5692

5793
expect(result).toEqual([]);
5894
expect(fastFindInFiles).toHaveBeenCalledWith({
5995
directory: cwd,
60-
needle: path.basename(changedFilePath),
96+
needle: new RegExp(
97+
changedFilePaths
98+
.map((changedFilePath) => path.basename(changedFilePath))
99+
.join('|')
100+
.replaceAll('.', '\\.')
101+
),
61102
excludeFolderPaths: excludeFolderPaths.map((folder) =>
62103
path.join(cwd, folder)
63104
),
@@ -66,7 +107,7 @@ describe('findNonSourceAffectedFiles', () => {
66107

67108
it("should still work even if found file didn't have a match", () => {
68109
const cwd = '/project';
69-
const changedFilePath = '/project/src/file.ts';
110+
const changedFilePaths = ['/project/src/file.ts'];
70111
const excludeFolderPaths = ['node_modules', 'dist', '.git'];
71112

72113
(fastFindInFiles as jest.Mock).mockReturnValue([
@@ -79,14 +120,19 @@ describe('findNonSourceAffectedFiles', () => {
79120

80121
const result = findNonSourceAffectedFiles(
81122
cwd,
82-
changedFilePath,
123+
changedFilePaths,
83124
excludeFolderPaths
84125
);
85126

86127
expect(result).toEqual([]);
87128
expect(fastFindInFiles).toHaveBeenCalledWith({
88129
directory: cwd,
89-
needle: path.basename(changedFilePath),
130+
needle: new RegExp(
131+
changedFilePaths
132+
.map((changedFilePath) => path.basename(changedFilePath))
133+
.join('|')
134+
.replaceAll('.', '\\.')
135+
),
90136
excludeFolderPaths: excludeFolderPaths.map((folder) =>
91137
path.join(cwd, folder)
92138
),

libs/core/src/assets.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,46 @@ import { existsSync } from 'fs';
55

66
export function findNonSourceAffectedFiles(
77
cwd: string,
8-
changedFilePath: string,
8+
changedFilePaths: string[],
99
excludeFolderPaths: (string | RegExp)[]
1010
): ChangedFiles[] {
11-
const fileName = basename(changedFilePath);
11+
if (changedFilePaths.length === 0) return [];
12+
13+
const fileNames = changedFilePaths.map((path) => basename(path));
1214

1315
const files = fastFindInFiles({
1416
directory: cwd,
15-
needle: fileName,
17+
needle: new RegExp(fileNames.join('|').replaceAll('.', '\\.')),
1618
excludeFolderPaths: excludeFolderPaths.map((path) =>
1719
typeof path === 'string' ? join(cwd, path) : path
1820
),
1921
});
2022

21-
const relevantFiles = filterRelevantFiles(cwd, files, changedFilePath);
23+
const relevantFiles = filterRelevantFiles(cwd, files, changedFilePaths);
2224

2325
return relevantFiles;
2426
}
2527

2628
function filterRelevantFiles(
2729
cwd: string,
2830
files: FastFindInFiles[],
29-
changedFilePath: string
31+
changedFilePaths: string[]
3032
): ChangedFiles[] {
31-
const fileName = basename(changedFilePath);
32-
const regExp = new RegExp(`['"\`](?<relFilePath>.*${fileName})['"\`]`);
33+
return changedFilePaths.flatMap((changedFilePath) => {
34+
const fileName = basename(changedFilePath);
35+
const regExp = new RegExp(`['"\`](?<relFilePath>.*${fileName})['"\`]`);
3336

34-
return files
35-
.map(({ filePath: foundFilePath, queryHits }) => ({
36-
filePath: relative(cwd, foundFilePath),
37-
changedLines: queryHits
38-
.filter(({ line }) =>
39-
isRelevantLine(line, regExp, cwd, foundFilePath, changedFilePath)
40-
)
41-
.map(({ lineNumber }) => lineNumber),
42-
}))
43-
.filter(({ changedLines }) => changedLines.length > 0);
37+
return files
38+
.map(({ filePath: foundFilePath, queryHits }) => ({
39+
filePath: relative(cwd, foundFilePath),
40+
changedLines: queryHits
41+
.filter(({ line }) =>
42+
isRelevantLine(line, regExp, cwd, foundFilePath, changedFilePath)
43+
)
44+
.map(({ lineNumber }) => lineNumber),
45+
}))
46+
.filter(({ changedLines }) => changedLines.length > 0);
47+
});
4448
}
4549

4650
function isRelevantLine(

libs/core/src/true-affected.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,37 @@ export const trueAffected = async ({
9999
({ filePath }) => project.getSourceFile(resolve(cwd, filePath)) != null
100100
);
101101

102-
const nonSourceChangedFiles = changedFiles
102+
const nonSourceChangedFilesPaths = changedFiles
103103
.filter(
104104
({ filePath }) =>
105105
!filePath.match(/.*\.(ts|js)x?$/g) &&
106106
!filePath.endsWith(lockFileName) &&
107107
project.getSourceFile(resolve(cwd, filePath)) == null
108108
)
109-
.flatMap(({ filePath: changedFilePath }) => {
110-
logger.debug(
111-
`Finding non-source affected files for ${chalk.bold(changedFilePath)}`
112-
);
109+
.map(({ filePath }) => filePath);
113110

114-
return findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths);
115-
});
111+
let nonSourceChangedFiles: ChangedFiles[] = [];
116112

117-
if (nonSourceChangedFiles.length > 0) {
113+
if (nonSourceChangedFilesPaths.length > 0) {
118114
logger.debug(
119-
`Found ${chalk.bold(
120-
nonSourceChangedFiles.length
121-
)} non-source affected files`
115+
`Finding non-source affected files for ${chalk.bold(
116+
nonSourceChangedFilesPaths.join(', ')
117+
)}`
122118
);
119+
120+
nonSourceChangedFiles = findNonSourceAffectedFiles(
121+
cwd,
122+
nonSourceChangedFilesPaths,
123+
ignoredPaths
124+
);
125+
126+
if (nonSourceChangedFiles.length > 0) {
127+
logger.debug(
128+
`Found ${chalk.bold(
129+
nonSourceChangedFiles.length
130+
)} non-source affected files`
131+
);
132+
}
123133
}
124134

125135
let changedFilesByLockfile: ChangedFiles[] = [];

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"importHelpers": true,
1111
"target": "es2015",
1212
"module": "esnext",
13-
"lib": ["es2020", "dom"],
13+
"lib": ["es2023", "dom"],
1414
"skipLibCheck": true,
1515
"skipDefaultLibCheck": true,
1616
"baseUrl": ".",

0 commit comments

Comments
 (0)