load-oxfmt-config 0.0.5 → 0.0.6
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.
- package/dist/index.d.mts +18 -3
- package/dist/index.mjs +49 -7
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -18,12 +18,27 @@ interface Options {
|
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region src/core.d.ts
|
|
20
20
|
/**
|
|
21
|
-
* Load oxfmt configuration
|
|
22
|
-
*
|
|
21
|
+
* Load oxfmt configuration: resolve the file path, then read and parse it.
|
|
22
|
+
* Caching is enabled by default; pass `useCache: false` to force a re-read.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Optional loader settings (cwd, configPath, useCache).
|
|
25
|
+
* @returns Parsed oxfmt FormatOptions or an empty object when missing.
|
|
26
|
+
* @throws {Error} when the config file exists but cannot be parsed.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const config = await loadOxfmtConfig({ cwd: '/project' })
|
|
31
|
+
* ```
|
|
23
32
|
*/
|
|
24
33
|
declare function loadOxfmtConfig(options?: Options): Promise<FormatOptions>;
|
|
25
34
|
/**
|
|
26
|
-
* Resolve the oxfmt config file path
|
|
35
|
+
* Resolve the oxfmt config file path.
|
|
36
|
+
* - If `configPath` is provided, absolute paths are returned as-is; relative paths are joined to cwd.
|
|
37
|
+
* - Otherwise, walk upward from cwd to find known filenames.
|
|
38
|
+
*
|
|
39
|
+
* @param cwd - Starting directory for resolution.
|
|
40
|
+
* @param configPath - Optional explicit path (absolute or relative to cwd).
|
|
41
|
+
* @returns Absolute path to the config file, or undefined when not found.
|
|
27
42
|
*/
|
|
28
43
|
declare function resolveOxfmtrcPath(cwd: string, configPath?: string): Promise<string | undefined>;
|
|
29
44
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { join } from "node:path";
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { isAbsolute, join } from "node:path";
|
|
3
3
|
import process from "node:process";
|
|
4
4
|
import { parse } from "jsonc-parser";
|
|
5
5
|
|
|
@@ -15,8 +15,17 @@ const OXFMT_CONFIG_FILES = [".oxfmtrc.json", ".oxfmtrc.jsonc"];
|
|
|
15
15
|
const resolveCache = /* @__PURE__ */ new Map();
|
|
16
16
|
const configCache = /* @__PURE__ */ new Map();
|
|
17
17
|
/**
|
|
18
|
-
* Load oxfmt configuration
|
|
19
|
-
*
|
|
18
|
+
* Load oxfmt configuration: resolve the file path, then read and parse it.
|
|
19
|
+
* Caching is enabled by default; pass `useCache: false` to force a re-read.
|
|
20
|
+
*
|
|
21
|
+
* @param options - Optional loader settings (cwd, configPath, useCache).
|
|
22
|
+
* @returns Parsed oxfmt FormatOptions or an empty object when missing.
|
|
23
|
+
* @throws {Error} when the config file exists but cannot be parsed.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const config = await loadOxfmtConfig({ cwd: '/project' })
|
|
28
|
+
* ```
|
|
20
29
|
*/
|
|
21
30
|
async function loadOxfmtConfig(options = {}) {
|
|
22
31
|
const useCache = options.useCache !== false;
|
|
@@ -34,16 +43,21 @@ async function loadOxfmtConfig(options = {}) {
|
|
|
34
43
|
return cachePromise(configCache, getConfigCacheKey(resolvedPath, resolveKey), loadTask);
|
|
35
44
|
}
|
|
36
45
|
/**
|
|
37
|
-
* Resolve the oxfmt config file path
|
|
46
|
+
* Resolve the oxfmt config file path.
|
|
47
|
+
* - If `configPath` is provided, absolute paths are returned as-is; relative paths are joined to cwd.
|
|
48
|
+
* - Otherwise, walk upward from cwd to find known filenames.
|
|
49
|
+
*
|
|
50
|
+
* @param cwd - Starting directory for resolution.
|
|
51
|
+
* @param configPath - Optional explicit path (absolute or relative to cwd).
|
|
52
|
+
* @returns Absolute path to the config file, or undefined when not found.
|
|
38
53
|
*/
|
|
39
54
|
async function resolveOxfmtrcPath(cwd, configPath) {
|
|
40
|
-
if (configPath) return join(cwd, configPath);
|
|
55
|
+
if (configPath) return isAbsolute(configPath) ? configPath : join(cwd, configPath);
|
|
41
56
|
let currentDir = cwd;
|
|
42
57
|
while (true) {
|
|
43
58
|
for (const filename of OXFMT_CONFIG_FILES) {
|
|
44
59
|
const configFilePath = join(currentDir, filename);
|
|
45
60
|
try {
|
|
46
|
-
const { stat } = await import("node:fs/promises");
|
|
47
61
|
if ((await stat(configFilePath)).isFile()) return configFilePath;
|
|
48
62
|
} catch {}
|
|
49
63
|
}
|
|
@@ -52,6 +66,14 @@ async function resolveOxfmtrcPath(cwd, configPath) {
|
|
|
52
66
|
currentDir = parentDir;
|
|
53
67
|
}
|
|
54
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Return a cached promise by key, creating and storing it on miss; failures clear the entry.
|
|
71
|
+
*
|
|
72
|
+
* @param cache - Map used to store inflight/resolved promises.
|
|
73
|
+
* @param key - Cache key.
|
|
74
|
+
* @param factory - Factory to create the promise when missing.
|
|
75
|
+
* @returns Cached or newly created promise.
|
|
76
|
+
*/
|
|
55
77
|
function cachePromise(cache, key, factory) {
|
|
56
78
|
const cached = cache.get(key);
|
|
57
79
|
if (cached) return cached;
|
|
@@ -62,12 +84,32 @@ function cachePromise(cache, key, factory) {
|
|
|
62
84
|
cache.set(key, task);
|
|
63
85
|
return task;
|
|
64
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Build a cache key for config content; prefixes missing entries with `missing:`.
|
|
89
|
+
*
|
|
90
|
+
* @param resolvedPath - Resolved config path or undefined when missing.
|
|
91
|
+
* @param resolveKey - Key used for path resolution caching.
|
|
92
|
+
* @returns Cache key for config content.
|
|
93
|
+
*/
|
|
65
94
|
function getConfigCacheKey(resolvedPath, resolveKey) {
|
|
66
95
|
return resolvedPath || `missing:${resolveKey}`;
|
|
67
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Build a cache key for path resolution (cwd + configPath).
|
|
99
|
+
*
|
|
100
|
+
* @param cwd - Current working directory.
|
|
101
|
+
* @param configPath - Optional config path.
|
|
102
|
+
* @returns Cache key for resolve cache.
|
|
103
|
+
*/
|
|
68
104
|
function getResolveCacheKey(cwd, configPath) {
|
|
69
105
|
return `${cwd}::${configPath || ""}`;
|
|
70
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Read and parse config file, supporting JSON and JSONC.
|
|
109
|
+
*
|
|
110
|
+
* @param resolvedPath - Absolute path to the config file.
|
|
111
|
+
* @returns Parsed FormatOptions object.
|
|
112
|
+
*/
|
|
71
113
|
async function readConfigFromFile(resolvedPath) {
|
|
72
114
|
const content = await readFile(resolvedPath, "utf-8");
|
|
73
115
|
if (resolvedPath.endsWith(".jsonc")) return parse(content);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "load-oxfmt-config",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"description": "Load .oxfmtrc.json(c) for oxfmt.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"jsonc-parser",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"sideEffects": false,
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"oxfmt": "^0.
|
|
41
|
+
"oxfmt": "^0.24.0"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"jsonc-parser": "^3.3.1"
|
|
@@ -47,15 +47,15 @@
|
|
|
47
47
|
"@ntnyq/eslint-config": "^5.9.0",
|
|
48
48
|
"@ntnyq/prettier-config": "^3.0.1",
|
|
49
49
|
"@ntnyq/tsconfig": "^3.1.0",
|
|
50
|
-
"@types/node": "^25.0.
|
|
50
|
+
"@types/node": "^25.0.6",
|
|
51
51
|
"bumpp": "^10.3.2",
|
|
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.
|
|
56
|
+
"oxfmt": "^0.24.0",
|
|
57
57
|
"prettier": "^3.7.4",
|
|
58
|
-
"tsdown": "^0.19.0
|
|
58
|
+
"tsdown": "^0.19.0",
|
|
59
59
|
"typescript": "^5.9.3",
|
|
60
60
|
"vitest": "^4.0.16"
|
|
61
61
|
},
|