load-oxfmt-config 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +190 -18
  2. package/dist/index.d.mts +6 -6
  3. package/package.json +13 -12
package/README.md CHANGED
@@ -5,7 +5,15 @@
5
5
  [![NPM DOWNLOADS](https://img.shields.io/npm/dy/load-oxfmt-config.svg)](https://www.npmjs.com/package/load-oxfmt-config)
6
6
  [![LICENSE](https://img.shields.io/github/license/ntnyq/load-oxfmt-config.svg)](https://github.com/ntnyq/load-oxfmt-config/blob/main/LICENSE)
7
7
 
8
- Load .oxfmtrc.json(c) for oxfmt.
8
+ > Load and resolve `.oxfmtrc.json(c)` configuration files for [oxfmt](https://oxc.rs/docs/guide/usage/formatter.html).
9
+
10
+ ## Features
11
+
12
+ - 🔍 **Auto-discovery** - Automatically searches for config files in current and parent directories
13
+ - 📦 **Multiple formats** - Supports both `.oxfmtrc.json` and `.oxfmtrc.jsonc` (JSON with comments)
14
+ - ⚡ **Built-in caching** - Caches both file resolution and parsed configs for optimal performance
15
+ - 🎯 **TypeScript support** - Fully typed with comprehensive type definitions
16
+ - 🛠️ **Flexible API** - Support explicit config paths or automatic discovery
9
17
 
10
18
  ## Install
11
19
 
@@ -23,41 +31,205 @@ pnpm add load-oxfmt-config
23
31
 
24
32
  ## Usage
25
33
 
34
+ ### Basic Usage
35
+
36
+ Load config from current directory or parent directories:
37
+
38
+ ```ts
39
+ import { loadOxfmtConfig } from 'load-oxfmt-config'
40
+
41
+ // Automatically searches for .oxfmtrc.json(c) in current and parent directories
42
+ const config = await loadOxfmtConfig()
43
+ console.log(config) // { printWidth: 80, ... }
44
+ ```
45
+
46
+ ### Specify Working Directory
47
+
48
+ ```ts
49
+ import { loadOxfmtConfig } from 'load-oxfmtConfig'
50
+
51
+ const config = await loadOxfmtConfig({
52
+ cwd: '/path/to/project',
53
+ })
54
+ ```
55
+
56
+ ### Explicit Config Path
57
+
58
+ ```ts
59
+ import { loadOxfmtConfig } from 'load-oxfmt-config'
60
+
61
+ // Relative path (resolved relative to cwd)
62
+ const config = await loadOxfmtConfig({
63
+ configPath: 'configs/.oxfmtrc.json',
64
+ cwd: '/path/to/project',
65
+ })
66
+
67
+ // Absolute path
68
+ const config = await loadOxfmtConfig({
69
+ configPath: '/absolute/path/to/.oxfmtrc.json',
70
+ })
71
+ ```
72
+
73
+ ### Disable Caching
74
+
26
75
  ```ts
27
76
  import { loadOxfmtConfig } from 'load-oxfmt-config'
28
77
 
78
+ // Force reload from disk, bypassing cache
29
79
  const config = await loadOxfmtConfig({
30
- cwd: '/configs',
80
+ useCache: false,
31
81
  })
82
+ ```
32
83
 
33
- console.log({ config })
84
+ ### Path Resolution Only
85
+
86
+ ```ts
87
+ import { resolveOxfmtrcPath } from 'load-oxfmt-config'
88
+
89
+ // Only resolve the config file path without loading it
90
+ const configPath = await resolveOxfmtrcPath(process.cwd())
91
+ console.log(configPath) // '/path/to/.oxfmtrc.json' or undefined
34
92
  ```
35
93
 
94
+ ## API
95
+
96
+ ### `loadOxfmtConfig(options?)`
97
+
98
+ Load and parse oxfmt configuration file.
99
+
100
+ **Parameters:**
101
+
102
+ - `options` - Optional configuration object
103
+
104
+ **Returns:** `Promise<FormatOptions>` - Parsed oxfmt configuration object. Returns empty object `{}` if no config file is found.
105
+
106
+ **Throws:** Error if config file exists but cannot be parsed.
107
+
108
+ ### `resolveOxfmtrcPath(cwd, configPath?)`
109
+
110
+ Resolve the absolute path to oxfmt config file.
111
+
112
+ **Parameters:**
113
+
114
+ - `cwd` - Starting directory for resolution
115
+ - `configPath` - Optional explicit path (absolute or relative to cwd)
116
+
117
+ **Returns:** `Promise<string | undefined>` - Absolute path to config file, or `undefined` if not found.
118
+
36
119
  ## Options
37
120
 
38
- ### configPath
121
+ All options are optional.
122
+
123
+ ### `cwd`
124
+
125
+ - **Type:** `string`
126
+ - **Default:** `process.cwd()`
127
+
128
+ Current working directory to start searching for config files. The loader will walk up from this directory to find a config file.
129
+
130
+ ### `configPath`
131
+
132
+ - **Type:** `string`
133
+ - **Default:** `undefined`
134
+
135
+ Explicit path to the config file:
136
+
137
+ - **Relative path:** Resolved relative to `cwd`
138
+ - **Absolute path:** Used as-is
139
+ - **When provided:** Skips auto-discovery and uses this path directly
140
+
141
+ ### `useCache`
142
+
143
+ - **Type:** `boolean`
144
+ - **Default:** `true`
145
+
146
+ Enable in-memory caching for both path resolution and parsed config contents. When enabled:
147
+
148
+ - Config file paths are cached to avoid repeated filesystem lookups
149
+ - Parsed config objects are cached to avoid re-parsing
150
+ - Subsequent calls with the same parameters return cached results instantly
151
+
152
+ Set to `false` to force reload from disk on every call.
153
+
154
+ ## Config File Discovery
155
+
156
+ When `configPath` is not provided, the loader automatically searches for config files:
157
+
158
+ 1. **Search order:** Starts from `cwd` and walks up to parent directories
159
+ 2. **Supported filenames:**
160
+ - `.oxfmtrc.json`
161
+ - `.oxfmtrc.jsonc`
162
+ 3. **Stops when:**
163
+ - A valid config file is found
164
+ - Reaches the filesystem root
165
+ 4. **Returns:** Empty object `{}` if no config file is found
166
+
167
+ ## Supported Config Formats
168
+
169
+ ### JSON (`.oxfmtrc.json`)
170
+
171
+ ```json
172
+ {
173
+ "printWidth": 100,
174
+ "tabWidth": 2,
175
+ "useTabs": false,
176
+ "semi": true,
177
+ "singleQuote": true
178
+ }
179
+ ```
180
+
181
+ ### JSONC (`.oxfmtrc.jsonc`)
182
+
183
+ JSON with comments support:
184
+
185
+ ```jsonc
186
+ {
187
+ // Formatting options
188
+ "printWidth": 100,
189
+ "tabWidth": 2,
190
+
191
+ /* Code style preferences */
192
+ "useTabs": false,
193
+ "semi": true,
194
+ "singleQuote": true
195
+ }
196
+ ```
197
+
198
+ ## Error Handling
199
+
200
+ ```ts
201
+ import { loadOxfmtConfig } from 'load-oxfmt-config'
202
+
203
+ try {
204
+ const config = await loadOxfmtConfig()
205
+ } catch (error) {
206
+ // Thrown when config file exists but contains invalid JSON
207
+ console.error('Failed to parse oxfmt config:', error.message)
208
+ }
209
+ ```
210
+
211
+ ## Caching Behavior
39
212
 
40
- **Type**: `string`\
41
- **Required**: `false`\
42
- **Default**: `undefined`
213
+ The caching system maintains two separate caches:
43
214
 
44
- Path to the oxfmt config file, resolved relative to `cwd`.
215
+ 1. **Path Resolution Cache:** Stores resolved config file paths
216
+ 2. **Config Content Cache:** Stores parsed configuration objects
45
217
 
46
- ### cwd
218
+ **Cache keys are based on:**
47
219
 
48
- **Type**: `string`\
49
- **Required**: `false`\
50
- **Default**: `process.cwd()`
220
+ - `cwd` + `configPath` for path resolution
221
+ - Resolved file path for config content
51
222
 
52
- Current working directory used when searching for config files.
223
+ **Cache invalidation:**
53
224
 
54
- ### useCache
225
+ - Failed operations automatically clear their cache entries
226
+ - Use `useCache: false` to bypass cache for specific calls
227
+ - Cache persists for the lifetime of the Node.js process
55
228
 
56
- **Type**: `boolean`\
57
- **Required**: `false`\
58
- **Default**: `true`
229
+ ## Related
59
230
 
60
- Enable in-memory caching for path resolution and parsed config contents.
231
+ - [oxfmt](https://oxc.rs/docs/guide/usage/formatter.html) - The Oxidation Compiler formatter
232
+ - [oxc](https://oxc.rs/) - The JavaScript Oxidation Compiler
61
233
 
62
234
  ## License
63
235
 
package/dist/index.d.mts CHANGED
@@ -3,16 +3,16 @@ import { FormatOptions } from "oxfmt";
3
3
  //#region src/types.d.ts
4
4
  interface Options {
5
5
  /**
6
- * Path to the configuration file
7
- */
6
+ * Path to the configuration file
7
+ */
8
8
  configPath?: string;
9
9
  /**
10
- * Current working directory
11
- */
10
+ * Current working directory
11
+ */
12
12
  cwd?: string;
13
13
  /**
14
- * Whether to use cache
15
- */
14
+ * Whether to use cache
15
+ */
16
16
  useCache?: boolean;
17
17
  }
18
18
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "load-oxfmt-config",
3
3
  "type": "module",
4
- "version": "0.0.7",
4
+ "version": "0.1.0",
5
5
  "description": "Load .oxfmtrc.json(c) for oxfmt.",
6
6
  "keywords": [
7
7
  "jsonc-parser",
@@ -38,39 +38,40 @@
38
38
  },
39
39
  "sideEffects": false,
40
40
  "peerDependencies": {
41
- "oxfmt": "^0.26.0"
41
+ "oxfmt": "0.x"
42
42
  },
43
43
  "dependencies": {
44
44
  "jsonc-parser": "^3.3.1"
45
45
  },
46
46
  "devDependencies": {
47
- "@ntnyq/eslint-config": "^5.9.0",
48
- "@ntnyq/prettier-config": "^3.0.1",
47
+ "@ntnyq/eslint-config": "^6.0.0-beta.6",
49
48
  "@ntnyq/tsconfig": "^3.1.0",
50
- "@types/node": "^25.0.9",
49
+ "@types/node": "^25.2.0",
50
+ "@typescript/native-preview": "^7.0.0-dev.20260202.1",
51
51
  "bumpp": "^10.4.0",
52
52
  "eslint": "^9.39.2",
53
53
  "husky": "^9.1.7",
54
54
  "nano-staged": "^0.9.0",
55
55
  "npm-run-all2": "^8.0.4",
56
- "oxfmt": "^0.26.0",
57
- "prettier": "^3.8.0",
58
- "tsdown": "^0.20.0-beta.3",
56
+ "oxfmt": "^0.28.0",
57
+ "tsdown": "^0.20.1",
59
58
  "typescript": "^5.9.3",
60
- "vitest": "^4.0.17"
59
+ "vitest": "^4.0.18"
61
60
  },
62
61
  "nano-staged": {
63
62
  "*.{js,ts,mjs,cjs,md,vue,yml,yaml,toml,json}": "eslint --fix",
64
- "*.{css,scss,html}": "prettier -uw"
63
+ "*": "oxfmt --no-error-on-unmatched-pattern"
65
64
  },
66
65
  "scripts": {
67
66
  "build": "tsdown",
68
67
  "dev": "tsdown --watch",
69
68
  "lint": "eslint",
69
+ "format": "oxfmt",
70
+ "format:check": "oxfmt --check",
70
71
  "release": "run-s release:check release:version",
71
- "release:check": "run-s lint typecheck test",
72
+ "release:check": "run-s format:check lint typecheck test",
72
73
  "release:version": "bumpp",
73
74
  "test": "vitest",
74
- "typecheck": "tsc --noEmit"
75
+ "typecheck": "tsgo --noEmit"
75
76
  }
76
77
  }