Skip to content

Commit

Permalink
Merge pull request #366 from yamadashy/feat/config-trailing-commas
Browse files Browse the repository at this point in the history
feat: Add support for JSON5 in configuration files
  • Loading branch information
yamadashy authored Feb 22, 2025
2 parents 34c53c8 + e995a7d commit 3295800
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ Here's an explanation of the configuration options:
| `security.enableSecurityCheck` | Whether to perform security checks on files | `true` |
| `tokenCount.encoding` | Token count encoding for AI model context limits (e.g., `o200k_base`, `cl100k_base`) | `"o200k_base"` |
The configuration file supports [JSON5](https://json5.org/) syntax, which allows:
- Comments (both single-line and multi-line)
- Trailing commas in objects and arrays
- Unquoted property names
- More relaxed string syntax
Example configuration:
```json5
Expand All @@ -607,7 +613,7 @@ Example configuration:
"showLineNumbers": false,
"copyToClipboard": true,
"topFilesLength": 5,
"includeEmptyDirectories": false
"includeEmptyDirectories": false,
},
"include": [
"**/*"
Expand All @@ -619,14 +625,14 @@ Example configuration:
"customPatterns": [
"additional-folder",
"**/*.log"
]
],
},
"security": {
"enableSecurityCheck": true
},
"tokenCount": {
"encoding": "o200k_base"
}
},
}
```
Expand Down
6 changes: 5 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
},
"json": {
"parser": {
"allowComments": true
"allowComments": true,
"allowTrailingCommas": true
},
"formatter": {
"enabled": false
}
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"iconv-lite": "^0.6.3",
"istextorbinary": "^9.5.0",
"jschardet": "^3.1.4",
"json5": "^2.2.3",
"log-update": "^6.1.0",
"minimatch": "^10.0.1",
"picocolors": "^1.1.1",
Expand Down
8 changes: 4 additions & 4 deletions repomix.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
"removeEmptyLines": false,
"topFilesLength": 5,
"showLineNumbers": false,
"includeEmptyDirectories": true
"includeEmptyDirectories": true,
},
"include": [],
"ignore": {
"useGitignore": true,
"useDefaultPatterns": true,
// ignore is specified in .repomixignore
"customPatterns": []
"customPatterns": [],
},
"security": {
"enableSecurityCheck": true
"enableSecurityCheck": true,
},
"tokenCount": {
"encoding": "o200k_base"
"encoding": "o200k_base",
}
}
6 changes: 3 additions & 3 deletions src/config/configLoad.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'node:fs/promises';
import path from 'node:path';
import JSON5 from 'json5';
import pc from 'picocolors';
import stripJsonComments from 'strip-json-comments';
import { RepomixError, rethrowValidationErrorIfZodError } from '../shared/errorHandle.js';
import { logger } from '../shared/logger.js';
import {
Expand Down Expand Up @@ -70,12 +70,12 @@ export const loadFileConfig = async (rootDir: string, argConfigPath: string | nu
const loadAndValidateConfig = async (filePath: string): Promise<RepomixConfigFile> => {
try {
const fileContent = await fs.readFile(filePath, 'utf-8');
const config = JSON.parse(stripJsonComments(fileContent));
const config = JSON5.parse(fileContent);
return repomixConfigFileSchema.parse(config);
} catch (error) {
rethrowValidationErrorIfZodError(error, 'Invalid config schema');
if (error instanceof SyntaxError) {
throw new RepomixError(`Invalid JSON in config file ${filePath}: ${error.message}`);
throw new RepomixError(`Invalid JSON5 in config file ${filePath}: ${error.message}`);
}
if (error instanceof Error) {
throw new RepomixError(`Error loading config from ${filePath}: ${error.message}`);
Expand Down
31 changes: 31 additions & 0 deletions tests/config/configLoad.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,37 @@ describe('configLoad', () => {
ignore: { useGitignore: true },
});
});

test('should parse config file with JSON5 features', async () => {
const configWithJSON5Features = `{
// Output configuration
output: {
filePath: 'test-output.txt',
style: 'plain',
},
/* Ignore configuration */
ignore: {
useGitignore: true, // Use .gitignore file
customPatterns: [
'*.log',
'*.tmp',
'*.temp', // Trailing comma
],
},
}`;

vi.mocked(fs.readFile).mockResolvedValue(configWithJSON5Features);
vi.mocked(fs.stat).mockResolvedValue({ isFile: () => true } as Stats);

const result = await loadFileConfig(process.cwd(), 'test-config.json');
expect(result).toEqual({
output: { filePath: 'test-output.txt', style: 'plain' },
ignore: {
useGitignore: true,
customPatterns: ['*.log', '*.tmp', '*.temp'],
},
});
});
});

describe('mergeConfigs', () => {
Expand Down

0 comments on commit 3295800

Please sign in to comment.