Skip to content

Commit a58ca3b

Browse files
authored
Merge pull request #143 from salesforcecli/er/exclude-node_modules-from-yaml-inspection
W-18506859: exclude node_modules dir from yaml files inspection
2 parents a8165cd + 2677e67 commit a58ca3b

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

src/flags.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ export function makeFlags<T extends Record<string, FlaggablePrompt>>(flaggablePr
7777
) as FlagsOfPrompts<T>;
7878
}
7979

80-
async function traverseForFiles(dir: string, suffixes: string[]): Promise<string[]> {
80+
export async function traverseForFiles(dir: string, suffixes: string[], excludeDirs?: string[]): Promise<string[]> {
8181
const files = await readdir(dir, { withFileTypes: true });
8282
const results: string[] = [];
8383

8484
for (const file of files) {
8585
const fullPath = join(dir, file.name);
8686

87-
if (file.isDirectory()) {
87+
if (file.isDirectory() && !excludeDirs?.includes(file.name)) {
8888
// eslint-disable-next-line no-await-in-loop
89-
results.push(...(await traverseForFiles(fullPath, suffixes)));
89+
results.push(...(await traverseForFiles(fullPath, suffixes, excludeDirs)));
9090
} else if (suffixes.some((suffix) => file.name.endsWith(suffix))) {
9191
results.push(fullPath);
9292
}
@@ -127,7 +127,7 @@ export const promptForAiEvaluationDefinitionApiName = async (
127127
};
128128

129129
export const promptForYamlFile = async (flagDef: FlaggablePrompt): Promise<string> => {
130-
const yamlFiles = await traverseForFiles(process.cwd(), ['.yml', '.yaml']);
130+
const yamlFiles = await traverseForFiles(process.cwd(), ['.yml', '.yaml'], ['node_modules']);
131131
return autocomplete({
132132
message: flagDef.message,
133133
// eslint-disable-next-line @typescript-eslint/require-await

test/flags.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2025, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
import { join } from 'node:path';
9+
import { mkdir, writeFile, rm } from 'node:fs/promises';
10+
import { expect } from 'chai';
11+
import { traverseForFiles } from '../src/flags.js';
12+
13+
describe('traverseForFiles', () => {
14+
const testDir = join(process.cwd(), 'test-temp');
15+
const testFiles = [
16+
'file1.yml',
17+
'file2.yaml',
18+
join('subdir', 'file3.yml'),
19+
join('subdir', 'file4.yaml'),
20+
join('node_modules', 'file5.yml'),
21+
join('excluded', 'file6.yaml'),
22+
] as const;
23+
24+
before(async () => {
25+
// Create directory structure and test files
26+
await mkdir(testDir, { recursive: true });
27+
await mkdir(join(testDir, 'subdir'), { recursive: true });
28+
await mkdir(join(testDir, 'node_modules'), { recursive: true });
29+
await mkdir(join(testDir, 'excluded'), { recursive: true });
30+
31+
// Create test files
32+
await Promise.all(testFiles.map((file) => writeFile(join(testDir, file), 'test content')));
33+
});
34+
35+
after(async () => {
36+
// Clean up test files
37+
await rm(testDir, { recursive: true, force: true });
38+
});
39+
40+
it('should find all yaml files when no excludeDirs is provided', async () => {
41+
const results = await traverseForFiles(testDir, ['.yml', '.yaml']);
42+
expect(results).to.have.lengthOf(6);
43+
expect(results).to.include(join(testDir, 'file1.yml'));
44+
expect(results).to.include(join(testDir, 'file2.yaml'));
45+
expect(results).to.include(join(testDir, 'subdir', 'file3.yml'));
46+
expect(results).to.include(join(testDir, 'subdir', 'file4.yaml'));
47+
expect(results).to.include(join(testDir, 'node_modules', 'file5.yml'));
48+
expect(results).to.include(join(testDir, 'excluded', 'file6.yaml'));
49+
});
50+
51+
it('should exclude specified directories', async () => {
52+
const results = await traverseForFiles(testDir, ['.yml', '.yaml'], ['node_modules', 'excluded']);
53+
expect(results).to.have.lengthOf(4);
54+
expect(results).to.include(join(testDir, 'file1.yml'));
55+
expect(results).to.include(join(testDir, 'file2.yaml'));
56+
expect(results).to.include(join(testDir, 'subdir', 'file3.yml'));
57+
expect(results).to.include(join(testDir, 'subdir', 'file4.yaml'));
58+
expect(results).to.not.include(join(testDir, 'node_modules', 'file5.yml'));
59+
expect(results).to.not.include(join(testDir, 'excluded', 'file6.yaml'));
60+
});
61+
62+
it('should handle empty excludeDirs array', async () => {
63+
const results = await traverseForFiles(testDir, ['.yml', '.yaml'], []);
64+
expect(results).to.have.lengthOf(6);
65+
});
66+
});

0 commit comments

Comments
 (0)