0
# Directory Configuration
1
2
Advanced configuration building for specific directories, supporting both file paths and file:// URLs. Handles config reading, path resolution, and pattern building for monorepo structures.
3
4
## Capabilities
5
6
### matchToFlatDir Function
7
8
Builds ESLint flat config for a specific directory path, automatically reading configuration files and adjusting file patterns with directory prefixes.
9
10
```javascript { .api }
11
/**
12
* Builds ESLint flat config for a specific directory path
13
* @param {string|URL} cwd - Current working directory or file URL
14
* @param {string} path - Relative path to directory
15
* @param {Object} [config] - Optional existing config object, reads from directory if not provided
16
* @param {Object} [overrides] - Optional override functions for dependency injection
17
* @returns {Promise<Array<Object>>} Promise resolving to array of ESLint flat config objects
18
*/
19
async function matchToFlatDir(cwd, path, config, overrides);
20
```
21
22
**Parameters:**
23
- `cwd` (string|URL): Current working directory or file:// URL (supports import.meta.url)
24
- `path` (string): Relative path to the directory containing ESLint configuration
25
- `config` (Object, optional): Existing configuration object. If not provided, reads from directory
26
- `overrides` (Object, optional): Override functions for dependency injection (currently no public override options available)
27
28
**Returns:**
29
Promise resolving to array of ESLint flat config objects with properly prefixed file patterns.
30
31
**Usage Examples:**
32
33
**With File Path:**
34
```javascript
35
const { matchToFlatDir } = require('@putout/eslint-flat');
36
37
// Using __dirname (CommonJS)
38
const config = await matchToFlatDir(__dirname, './packages/putout');
39
40
// The function will:
41
// 1. Read eslint config from ./packages/putout directory
42
// 2. Prefix all file patterns with **/packages/putout/
43
// 3. Return flat config array
44
```
45
46
**With File URL (ESM):**
47
```javascript
48
import { matchToFlatDir } from '@putout/eslint-flat';
49
50
// Using import.meta.url (ESM)
51
const config = await matchToFlatDir(import.meta.url, './packages/putout');
52
```
53
54
**With Custom Configuration:**
55
```javascript
56
const { matchToFlatDir } = require('@putout/eslint-flat');
57
58
// Provide config directly instead of reading from directory
59
const customConfig = {
60
match: {
61
'bin/*.js': {
62
'n/hashbang': 'off',
63
},
64
'lib/**/*.js': {
65
'complexity': ['error', 10],
66
},
67
},
68
};
69
70
const result = await matchToFlatDir(__dirname, './my-package', customConfig);
71
// Result will have patterns like:
72
// - files: ['**/my-package/bin/*.js']
73
// - files: ['**/my-package/lib/**/*.js']
74
```
75
76
**In Monorepo ESLint Config:**
77
```javascript
78
const { safeAlign } = require('eslint-plugin-putout/config');
79
const { matchToFlatDir } = require('@putout/eslint-flat');
80
81
module.exports = [
82
...safeAlign,
83
// Include configuration from specific package
84
...await matchToFlatDir(__dirname, './packages/putout'),
85
...await matchToFlatDir(__dirname, './packages/eslint-flat'),
86
];
87
```
88
89
## Configuration File Resolution
90
91
The function automatically tries to read ESLint configuration in this order:
92
93
1. **Modern flat config files:**
94
- `eslint.config.js`
95
- `eslint.config.cjs`
96
- `eslint.config.mjs`
97
98
2. **Legacy configuration:**
99
- `.eslintrc.json` (converted using ESLint's FlatCompat)
100
101
## Configuration Formats
102
103
**Match Object Format (Simplified):**
104
If the config exports a `match` property, it uses the simplified object format:
105
106
```javascript
107
// In ./packages/my-package/eslint.config.js
108
module.exports = {
109
match: {
110
'src/*.js': {
111
'no-console': 'warn',
112
},
113
'test/*.js': {
114
'no-console': 'off',
115
},
116
},
117
};
118
```
119
120
**Standard Flat Config Format:**
121
If no `match` property, processes as standard ESLint flat config:
122
123
```javascript
124
// In ./packages/my-package/eslint.config.js
125
module.exports = [
126
{
127
files: ['src/*.js'],
128
rules: {
129
'no-console': 'warn',
130
},
131
},
132
{
133
files: ['test/*.js'],
134
rules: {
135
'no-console': 'off',
136
},
137
ignores: ['test/fixtures/**'],
138
},
139
];
140
```
141
142
## Path Handling
143
144
**File Pattern Processing:**
145
- Automatically prefixes file patterns with `**/directory-path/`
146
- Handles both simple patterns and complex glob patterns
147
- Preserves function-based file patterns without modification
148
149
**Ignore Pattern Processing:**
150
- Processes `ignores` arrays by prefixing with directory path
151
- Preserves function-based ignore patterns
152
- Only adds `ignores` property if patterns exist
153
154
**URL Support:**
155
- Supports both regular directory paths and file:// URLs
156
- Automatically converts file:// URLs to file system paths
157
- Useful for ESM modules using import.meta.url
158
159
## Error Handling
160
161
- Returns empty array `[]` if no configuration files are found
162
- Gracefully handles missing directories
163
- Uses try-catch internally to prevent errors from propagating
164
- Supports dependency injection via overrides for testing