load-oxfmt-config 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-PRESENT ntnyq <https://github.com/ntnyq>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # load-oxfmt-config
2
+
3
+ [![CI](https://github.com/ntnyq/load-oxfmt-config/workflows/CI/badge.svg)](https://github.com/ntnyq/load-oxfmt-config/actions)
4
+ [![NPM VERSION](https://img.shields.io/npm/v/load-oxfmt-config.svg)](https://www.npmjs.com/package/load-oxfmt-config)
5
+ [![NPM DOWNLOADS](https://img.shields.io/npm/dy/load-oxfmt-config.svg)](https://www.npmjs.com/package/load-oxfmt-config)
6
+ [![LICENSE](https://img.shields.io/github/license/ntnyq/load-oxfmt-config.svg)](https://github.com/ntnyq/load-oxfmt-config/blob/main/LICENSE)
7
+
8
+ Load .oxfmtrc.json(c) for oxfmt.
9
+
10
+ ## Install
11
+
12
+ ```shell
13
+ npm install load-oxfmt-config
14
+ ```
15
+
16
+ ```shell
17
+ yarn add load-oxfmt-config
18
+ ```
19
+
20
+ ```shell
21
+ pnpm add load-oxfmt-config
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```ts
27
+ import { loadOxfmtConfig } from 'load-oxfmt-config'
28
+
29
+ const config = loadOxfmtConfig({
30
+ cwd: '/configs',
31
+ })
32
+
33
+ console.log({ config })
34
+ ```
35
+
36
+ ## Options
37
+
38
+ - `configPath?: string` Path to the oxfmt config file, resolved relative to `cwd`.
39
+ - `cwd?: string` Current working directory used when searching for config files. Defaults to `process.cwd()`.
40
+ - `useCache?: boolean` Enable in-memory caching for path resolution and parsed config contents. Defaults to `true`.
41
+
42
+ ## License
43
+
44
+ [MIT](./LICENSE) License © 2025-PRESENT [ntnyq](https://github.com/ntnyq)
@@ -0,0 +1,166 @@
1
+ //#region src/types.d.ts
2
+ interface Options {
3
+ /**
4
+ * Path to the configuration file
5
+ */
6
+ configPath?: string;
7
+ /**
8
+ * Current working directory
9
+ */
10
+ cwd?: string;
11
+ /**
12
+ * Whether to use cache
13
+ */
14
+ useCache?: boolean;
15
+ }
16
+ /**
17
+ * Oxfmt configuration interface
18
+ */
19
+ interface OxfmtConfig {
20
+ /**
21
+ * Include parentheses around a sole arrow function parameter.
22
+ * @default `always`
23
+ */
24
+ arrowParens?: 'always' | 'avoid';
25
+ /**
26
+ * Put the `>` of a multi-line JSX element at the end of the last line\ninstead of being alone on the next line.
27
+ * @default false
28
+ */
29
+ bracketSameLine?: boolean;
30
+ /**
31
+ * Print spaces between brackets in object literals.
32
+ * @default true
33
+ */
34
+ bracketSpacing?: boolean;
35
+ /**
36
+ * Control whether to format embedded parts in the file.
37
+ * @default `off`
38
+ */
39
+ embeddedLanguageFormatting?: 'auto' | 'off';
40
+ /**
41
+ * Which end of line characters to apply.
42
+ * @default `lf`
43
+ */
44
+ endOfLine?: 'cr' | 'crlf' | 'lf';
45
+ /**
46
+ * Experimental: Sort `package.json` keys.
47
+ * @default true
48
+ */
49
+ experimentalSortPackageJson?: boolean;
50
+ /**
51
+ * Ignore files matching these glob patterns. Current working directory is used as the root.
52
+ */
53
+ ignorePatterns?: string[];
54
+ /**
55
+ * Whether to insert a final newline at the end of the file.
56
+ * @default true
57
+ */
58
+ insertFinalNewline?: boolean;
59
+ /**
60
+ * Use single quotes instead of double quotes in JSX.
61
+ * @default false
62
+ */
63
+ jsxSingleQuote?: boolean;
64
+ /**
65
+ * How to wrap object literals when they could fit on one line or span multiple lines.
66
+ * @default `preserve`
67
+ * NOTE: In addition to Prettier's `preserve` and `collapse`, we also support `always`
68
+ */
69
+ objectWrap?: 'always' | 'never' | 'preserve';
70
+ /**
71
+ * The line length that the printer will wrap on.
72
+ * @default 100
73
+ */
74
+ printWidth?: number;
75
+ /**
76
+ * Change when properties in objects are quoted.
77
+ * @default `as-needed`
78
+ */
79
+ quoteProps?: 'as-needed' | 'consistent' | 'preserve';
80
+ /**
81
+ * Print semicolons at the ends of statements.
82
+ * @default true
83
+ */
84
+ semi?: boolean;
85
+ /**
86
+ * Put each attribute on a new line in JSX.
87
+ * @default false
88
+ */
89
+ singleAttributePerLine?: boolean;
90
+ /**
91
+ * Use single quotes instead of double quotes.
92
+ * @default false
93
+ */
94
+ singleQuote?: boolean;
95
+ /**
96
+ * Number of spaces per indentation level.
97
+ * @default 2
98
+ */
99
+ tabWidth?: number;
100
+ /**
101
+ * Print trailing commas wherever possible.
102
+ * @default `all`
103
+ */
104
+ trailingComma?: 'all' | 'es5' | 'none';
105
+ /**
106
+ * Use tabs for indentation or spaces.
107
+ * @default false
108
+ */
109
+ useTabs?: boolean;
110
+ /**
111
+ * Experimental: Sort import statements. Disabled by default.
112
+ */
113
+ experimentalSortImports?: {
114
+ /**
115
+ * Custom groups configuration for organizing imports.\nEach array element represents a group, and multiple group names in the same array are treated as one.\nAccepts both `string` and `string[]` as group elements.
116
+ */
117
+ groups?: Array<string | string[]>;
118
+ /**
119
+ * Ignore case when sorting.
120
+ * @default true
121
+ */
122
+ ignoreCase?: boolean;
123
+ /**
124
+ * Glob patterns to identify internal imports.
125
+ */
126
+ internalPattern?: string[];
127
+ /**
128
+ * Add newlines between import groups.
129
+ * @default true
130
+ */
131
+ newlinesBetween?: boolean;
132
+ /**
133
+ * Sort order. (Default: `asc`)
134
+ * @default `asc`
135
+ */
136
+ order?: 'asc' | 'desc';
137
+ /**
138
+ * Partition imports by comments.
139
+ * @default false
140
+ */
141
+ partitionByComment?: boolean;
142
+ /**
143
+ * Partition imports by newlines.
144
+ * @default false
145
+ */
146
+ partitionByNewline?: boolean;
147
+ /**
148
+ * Sort side-effect imports.
149
+ * @default false
150
+ */
151
+ sortSideEffects?: boolean;
152
+ };
153
+ }
154
+ //#endregion
155
+ //#region src/core.d.ts
156
+ /**
157
+ * Load oxfmt configuration by resolving the config file path and parsing its contents.
158
+ * Uses in-memory caches by default; set `useCache` to false to force re-read.
159
+ */
160
+ declare function loadOxfmtConfig(options?: Options): Promise<OxfmtConfig>;
161
+ /**
162
+ * Resolve the oxfmt config file path, searching upward from the provided cwd when no explicit path is given.
163
+ */
164
+ declare function resolveOxfmtrcPath(cwd: string, configPath?: string): Promise<string | undefined>;
165
+ //#endregion
166
+ export { Options, OxfmtConfig, loadOxfmtConfig, resolveOxfmtrcPath };
package/dist/index.mjs ADDED
@@ -0,0 +1,78 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import process from "node:process";
4
+ import { parse } from "jsonc-parser";
5
+
6
+ //#region src/constants.ts
7
+ /**
8
+ * Supported configuration files for oxfmt.
9
+ * @see https://oxc.rs/docs/guide/usage/formatter/config.html#oxfmtrc-json-c
10
+ */
11
+ const OXFMT_CONFIG_FILES = [".oxfmtrc.json", ".oxfmtrc.jsonc"];
12
+
13
+ //#endregion
14
+ //#region src/core.ts
15
+ const resolveCache = /* @__PURE__ */ new Map();
16
+ const configCache = /* @__PURE__ */ new Map();
17
+ /**
18
+ * Load oxfmt configuration by resolving the config file path and parsing its contents.
19
+ * Uses in-memory caches by default; set `useCache` to false to force re-read.
20
+ */
21
+ async function loadOxfmtConfig(options = {}) {
22
+ const useCache = options.useCache !== false;
23
+ const cwd = options.cwd || process.cwd();
24
+ const resolveKey = getResolveCacheKey(cwd, options.configPath);
25
+ const resolvedPath = useCache ? await cachePromise(resolveCache, resolveKey, () => resolveOxfmtrcPath(cwd, options.configPath)) : await resolveOxfmtrcPath(cwd, options.configPath);
26
+ if (!resolvedPath) {
27
+ if (!useCache) return {};
28
+ return cachePromise(configCache, getConfigCacheKey(resolvedPath, resolveKey), () => Promise.resolve({}));
29
+ }
30
+ const loadTask = () => readConfigFromFile(resolvedPath).catch((err) => {
31
+ throw new Error(`Failed to parse oxfmt configuration file at ${resolvedPath}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });
32
+ });
33
+ if (!useCache) return loadTask();
34
+ return cachePromise(configCache, getConfigCacheKey(resolvedPath, resolveKey), loadTask);
35
+ }
36
+ /**
37
+ * Resolve the oxfmt config file path, searching upward from the provided cwd when no explicit path is given.
38
+ */
39
+ async function resolveOxfmtrcPath(cwd, configPath) {
40
+ if (configPath) return join(cwd, configPath);
41
+ let currentDir = cwd;
42
+ while (true) {
43
+ for (const filename of OXFMT_CONFIG_FILES) {
44
+ const configFilePath = join(currentDir, filename);
45
+ try {
46
+ const { stat } = await import("node:fs/promises");
47
+ if ((await stat(configFilePath)).isFile()) return configFilePath;
48
+ } catch {}
49
+ }
50
+ const parentDir = join(currentDir, "..");
51
+ if (parentDir === currentDir) break;
52
+ currentDir = parentDir;
53
+ }
54
+ }
55
+ function cachePromise(cache, key, factory) {
56
+ const cached = cache.get(key);
57
+ if (cached) return cached;
58
+ const task = factory().catch((err) => {
59
+ cache.delete(key);
60
+ throw err;
61
+ });
62
+ cache.set(key, task);
63
+ return task;
64
+ }
65
+ function getConfigCacheKey(resolvedPath, resolveKey) {
66
+ return resolvedPath || `missing:${resolveKey}`;
67
+ }
68
+ function getResolveCacheKey(cwd, configPath) {
69
+ return `${cwd}::${configPath || ""}`;
70
+ }
71
+ async function readConfigFromFile(resolvedPath) {
72
+ const content = await readFile(resolvedPath, "utf-8");
73
+ if (resolvedPath.endsWith(".jsonc")) return parse(content);
74
+ return JSON.parse(content);
75
+ }
76
+
77
+ //#endregion
78
+ export { loadOxfmtConfig, resolveOxfmtrcPath };
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "load-oxfmt-config",
3
+ "type": "module",
4
+ "version": "0.0.0",
5
+ "packageManager": "pnpm@10.26.1",
6
+ "description": "Load .oxfmtrc.json(c) for oxfmt.",
7
+ "keywords": [
8
+ "jsonc-parser",
9
+ "load-config",
10
+ "load-oxfmt-config",
11
+ "load-oxfmtrc",
12
+ "oxfmt",
13
+ "oxfmtrc"
14
+ ],
15
+ "license": "MIT",
16
+ "author": {
17
+ "name": "ntnyq",
18
+ "email": "ntnyq13@gmail.com"
19
+ },
20
+ "homepage": "https://github.com/ntnyq/load-oxfmt-config#readme",
21
+ "repository": "ntnyq/load-oxfmt-config",
22
+ "bugs": {
23
+ "url": "https://github.com/ntnyq/load-oxfmt-config/issues"
24
+ },
25
+ "exports": {
26
+ "./package.json": "./package.json",
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "default": "./dist/index.js"
30
+ }
31
+ },
32
+ "main": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "sideEffects": false,
41
+ "scripts": {
42
+ "build": "tsdown",
43
+ "dev": "tsdown --watch",
44
+ "lint": "eslint",
45
+ "prepare": "husky",
46
+ "prepublishOnly": "pnpm run build",
47
+ "release": "run-s release:check release:version",
48
+ "release:check": "run-s lint typecheck test",
49
+ "release:version": "bumpp",
50
+ "test": "vitest",
51
+ "typecheck": "tsc --noEmit"
52
+ },
53
+ "dependencies": {
54
+ "jsonc-parser": "^3.3.1"
55
+ },
56
+ "devDependencies": {
57
+ "@ntnyq/eslint-config": "^5.8.0",
58
+ "@ntnyq/prettier-config": "^3.0.1",
59
+ "@ntnyq/tsconfig": "^3.0.0",
60
+ "@types/node": "^25.0.3",
61
+ "bumpp": "^10.3.2",
62
+ "eslint": "^9.39.2",
63
+ "husky": "^9.1.7",
64
+ "nano-staged": "^0.9.0",
65
+ "npm-run-all2": "^8.0.4",
66
+ "prettier": "^3.7.4",
67
+ "tsdown": "^0.18.2",
68
+ "typescript": "^5.9.3",
69
+ "vitest": "^4.0.16"
70
+ },
71
+ "nano-staged": {
72
+ "*.{js,ts,mjs,cjs,md,vue,yml,yaml,toml,json}": "eslint --fix",
73
+ "*.{css,scss,html}": "prettier -uw"
74
+ }
75
+ }