Repository navigation
feat!: --stage-record-file now requires files list; add config file support
#21
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
37c3ae3
feat: Add config file support and improve monorepo compatibility
frankieyan 11cb3e3
feat: Add git path normalization for monorepo support
frankieyan 64d13f9
fix: Return copy of DEFAULT_CONFIG and add config validation
frankieyan 4be2e9a
docs: Exclude deleted files from pre-commit hook example
frankieyan 034defd
refactor: Consolidate path normalization into source-files.mts
frankieyan dfdfe3c
feat: Add filterByGlob to validate file paths against sourceGlob
frankieyan f1908a0
feat: Add validateFilesExist to error on missing files
frankieyan d0a4f4c
fix: Log warning when config file parsing fails
frankieyan c397cb3
fix: Throw errors for invalid config files instead of silently using …
frankieyan 34a1041
fix: Reject array configs in isValidConfig
frankieyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { existsSync, readFileSync } from 'node:fs' | ||
| import { join } from 'node:path' | ||
|
|
||
| type Config = { | ||
| recordsFile: string | ||
| sourceGlob: string | ||
| } | ||
|
|
||
| const DEFAULT_CONFIG: Config = { | ||
| recordsFile: '.react-compiler.rec.json', | ||
| sourceGlob: 'src/**/*.{js,jsx,ts,tsx}', | ||
| } | ||
|
|
||
| const CONFIG_FILE_NAME = '.react-compiler-tracker.config.json' | ||
|
|
||
| function isValidConfig(config: unknown): config is Partial<Config> { | ||
| if (typeof config !== 'object' || config === null || Array.isArray(config)) { | ||
| return false | ||
| } | ||
| const obj = config as Record<string, unknown> | ||
| if (obj.recordsFile !== undefined && typeof obj.recordsFile !== 'string') { | ||
| return false | ||
| } | ||
| if (obj.sourceGlob !== undefined && typeof obj.sourceGlob !== 'string') { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| function loadConfig(): Config { | ||
| const configPath = join(process.cwd(), CONFIG_FILE_NAME) | ||
|
|
||
| if (!existsSync(configPath)) { | ||
| return { ...DEFAULT_CONFIG } | ||
| } | ||
|
|
||
| try { | ||
| const configContent = readFileSync(configPath, 'utf8') | ||
| const parsed: unknown = JSON.parse(configContent) | ||
| if (!isValidConfig(parsed)) { | ||
| throw new Error(`Invalid config file at ${configPath}`) | ||
| } | ||
|
|
||
| return { | ||
| ...DEFAULT_CONFIG, | ||
| ...parsed, | ||
| } | ||
| } catch (error) { | ||
| throw new Error(`Failed to parse config at ${configPath}: ${error}`) | ||
| } | ||
| } | ||
|
|
||
| export { type Config, DEFAULT_CONFIG, loadConfig } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { existsSync, readFileSync } from 'node:fs' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { DEFAULT_CONFIG, loadConfig } from './config.mjs' | ||
|
|
||
| vi.mock('node:fs', () => ({ | ||
| existsSync: vi.fn(), | ||
| readFileSync: vi.fn(), | ||
| })) | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe('loadConfig', () => { | ||
| it('returns defaults when no config file exists', () => { | ||
| vi.mocked(existsSync).mockReturnValue(false) | ||
|
|
||
| const result = loadConfig() | ||
|
|
||
| expect(result).toEqual(DEFAULT_CONFIG) | ||
| }) | ||
|
|
||
| it('loads and merges user config correctly', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true) | ||
| vi.mocked(readFileSync).mockReturnValue( | ||
| JSON.stringify({ | ||
| recordsFile: 'custom-records.json', | ||
| sourceGlob: 'app/**/*.{ts,tsx}', | ||
| }), | ||
| ) | ||
|
|
||
| const result = loadConfig() | ||
|
|
||
| expect(result).toEqual({ | ||
| recordsFile: 'custom-records.json', | ||
| sourceGlob: 'app/**/*.{ts,tsx}', | ||
| }) | ||
| }) | ||
|
|
||
| it('handles partial config (only some fields specified)', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true) | ||
| vi.mocked(readFileSync).mockReturnValue( | ||
| JSON.stringify({ | ||
| sourceGlob: 'packages/frontend/**/*.tsx', | ||
| }), | ||
| ) | ||
|
|
||
| const result = loadConfig() | ||
|
|
||
| expect(result).toEqual({ | ||
| recordsFile: DEFAULT_CONFIG.recordsFile, | ||
| sourceGlob: 'packages/frontend/**/*.tsx', | ||
| }) | ||
| }) | ||
|
|
||
| it('throws when config file is invalid JSON', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true) | ||
| // Trailing comma - a common mistake when editing JSON by hand | ||
| vi.mocked(readFileSync).mockReturnValue(`{ | ||
| "recordsFile": ".react-compiler.rec.json", | ||
| }`) | ||
|
|
||
| expect(() => loadConfig()).toThrow('Failed to parse config') | ||
| }) | ||
|
|
||
| it('throws when config has invalid field types', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true) | ||
| vi.mocked(readFileSync).mockReturnValue( | ||
| JSON.stringify({ | ||
| recordsFile: { path: '.react-compiler.rec.json' }, | ||
| }), | ||
| ) | ||
|
|
||
| expect(() => loadConfig()).toThrow('Invalid config file') | ||
| }) | ||
|
|
||
| it('throws when config file contains an array instead of object', () => { | ||
| vi.mocked(existsSync).mockReturnValue(true) | ||
| vi.mocked(readFileSync).mockReturnValue(JSON.stringify([{ recordsFile: 'records.json' }])) | ||
|
|
||
| expect(() => loadConfig()).toThrow('Invalid config file') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 Perhaps we should just use zod here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if we have more config options to add in the future?