8000 #8: scope.exclude config knob for discoverFiles by devill · Pull Request #31 · habit-hooks/habit-hooks · GitHub
[go: up one dir, main page]

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
6 changes: 6 additions & 0 deletions docs/DECISIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,9 @@ phases (PR #13). Each call below is an _agent decision_.
only remaining `rules` usages are the tests that intentionally cover the alias
and its precedence. **Scheduled follow-up:** a later issue removes the `rules`
field from the schema/merge entirely (the hard removal).

- **File discovery lives in its own module.** _(agent decision, #8)_ `FILE_GLOBS`
and `discoverFiles` moved out of `runner.ts` into `src/discover.ts`. `runner.ts`
sat exactly at the `max-lines: 200` limit, and discovery is a distinct
responsibility from orchestration; the move keeps runner under the cap and gives
the `scope.exclude` knob a focused home. Reversible — it could be inlined back.
3 changes: 3 additions & 0 deletions habit-hooks.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default {
scope: {
exclude: ['tests/fixtures/**'],
},
smells: {
'non-essential-comment': {
exclude: ['tests/fixtures/**', 'habit-hooks.config.*'],
Expand Down
1 change: 1 addition & 0 deletions src/config/schema.ts
< 8000 /table>
1 change: 1 addition & 0 deletions src/config/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ScopeConfig {
autoBranchOffMain?: boolean;
branchBase?: string;
mainBranch?: string;
exclude?: string[];
}

export interface CommentCheckConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function validateScope(value: unknown): ScopeConfig | undefined {
validateOptionalBoolean(value.autoBranchOffMain, 'scope.autoBranchOffMain');
validateOptionalString(value.branchBase, 'scope.branchBase');
validateOptionalString(value.mainBranch, 'scope.mainBranch');
validateOptionalStringArray(value.exclude, 'scope.exclude');
return value as ScopeConfig;
}

Expand Down
41 changes: 41 additions & 0 deletions src/discover-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, relative } from 'node:path';
import { discoverFiles } from './discover.js';

function write(dir: string, rel: string): void {
const path = join(dir, rel);
mkdirSync(join(path, '..'), { recursive: true });
writeFileSync(path, 'export const x = 1;\n');
}

describe('discoverFiles', () => {
let cwd: string;

beforeEach(() => {
cwd = mkdtempSync(join(tmpdir(), 'hh-discover-'));
write(cwd, 'a.ts');
write(cwd, 'sub/b.ts');
write(cwd, 'fixtures/bad.ts');
write(cwd, 'node_modules/dep/c.ts');
});

afterEach(() => {
rmSync(cwd, { recursive: true, force: true });
});

it('ignores the built-in set and keeps everything else by default', async () => {
const files = await discoverFiles(cwd, 'typescript');
expect(files.map((file) => relative(cwd, file)).sort()).toEqual([
'a.ts',
'fixtures/bad.ts',
'sub/b.ts',
]);
});

it('also skips paths matched by scope.exclude', async () => {
const files = await discoverFiles(cwd, 'typescript', ['fixtures/**']);
expect(files.map((file) => relative(cwd, file)).sort()).toEqual(['a.ts', 'sub/b.ts']);
});
});
20 changes: 20 additions & 0 deletions src/discover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fg from 'fast-glob';
import type { Language } from './config/schema.js';

const FILE_GLOBS: Record<Language, string[]> = {
typescript: ['**/*.{ts,tsx,js,mjs,cjs}'],
python: ['**/*.py'],
};

export async function discoverFiles(
cwd: string,
language: Language,
exclude: string[] = [],
): Promise<string[]> {
return fg(FILE_GLOBS[language], {
cwd,
absolute: true,
ignore: ['**/node_modules/**', '**/dist/**', '**/coverage/**', ...exclude],
dot: false,
});
}
18 changes: 2 additions & 16 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, isAbsolute, relative, resolve } from 'node:path';
import fg from 'fast-glob';
import picomatch from 'picomatch';
import { discoverFiles } from './discover.js';
import { loadConfig, loadConfigFromPath, RULES_DEPRECATION } from './config/load.js';
import { buildRules } from './rules/registry.js';
import { lookupPrompt } from './prompts/registry.js';
Expand Down Expand Up @@ -44,20 +44,6 @@ interface RunContext {
configWarnings: string[];
}

const FILE_GLOBS: Record<Language, string[]> = {
typescript: ['**/*.{ts,tsx,js,mjs,cjs}'],
python: ['**/*.py'],
};

async function discoverFiles(cwd: string, language: Language): Promise<string[]> {
return fg(FILE_GLOBS[language], {
cwd,
absolute: true,
ignore: ['**/node_modules/**', '**/dist/**', '**/coverage/**'],
dot: false,
});
}

function buildMatcher(patterns: string[] | undefined): ((_path: string) => boolean) | null {
if (!patterns || patterns.length === 0) return null;
return picomatch(patterns);
Expand Down Expand Up @@ -120,7 +106,7 @@ async function buildContext(cwd: string, options: RunOptions): Promise<{ ctx: Ru
const { config, configDir } = await resolveConfig(cwd, options);
const rules = buildRules(config, configDir);
const language: Language = config.language ?? 'typescript';
const files = await discoverFiles(cwd, language);
const files = await discoverFiles(cwd, language, config.scope?.exclude);
const scope = resolveScope(options.scopeFlags ?? {}, config.scope, cwd);
const baseline = resolveBaseline(cwd, options);
const promptsDir = resolvePromptsDir(config, configDir);
Expand Down
0