locter 2.2.1 → 3.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/README.MD +188 -6
- package/dist/index.d.mts +244 -0
- package/dist/index.mjs +688 -672
- package/dist/index.mjs.map +1 -1
- package/package.json +27 -34
- package/dist/index.cjs +0 -743
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/loader/built-in/conf/index.d.ts +0 -1
- package/dist/loader/built-in/conf/module.d.ts +0 -6
- package/dist/loader/built-in/index.d.ts +0 -3
- package/dist/loader/built-in/json/index.d.ts +0 -1
- package/dist/loader/built-in/json/module.d.ts +0 -5
- package/dist/loader/built-in/module/index.d.ts +0 -3
- package/dist/loader/built-in/module/module.d.ts +0 -15
- package/dist/loader/built-in/module/type.d.ts +0 -8
- package/dist/loader/built-in/module/utils.d.ts +0 -9
- package/dist/loader/built-in/yaml/index.d.ts +0 -1
- package/dist/loader/built-in/yaml/module.d.ts +0 -5
- package/dist/loader/constants.d.ts +0 -6
- package/dist/loader/helpers.d.ts +0 -6
- package/dist/loader/index.d.ts +0 -3
- package/dist/loader/module.d.ts +0 -18
- package/dist/loader/singleton.d.ts +0 -2
- package/dist/loader/type.d.ts +0 -8
- package/dist/locator/async.d.ts +0 -3
- package/dist/locator/index.d.ts +0 -4
- package/dist/locator/sync.d.ts +0 -3
- package/dist/locator/types.d.ts +0 -15
- package/dist/locator/utils.d.ts +0 -7
- package/dist/utils/error.d.ts +0 -1
- package/dist/utils/file-name.d.ts +0 -2
- package/dist/utils/file-path.d.ts +0 -1
- package/dist/utils/has-property.d.ts +0 -2
- package/dist/utils/index.d.ts +0 -8
- package/dist/utils/object.d.ts +0 -2
- package/dist/utils/runtime.d.ts +0 -2
- package/dist/utils/to-array.d.ts +0 -1
- package/dist/utils/typescript.d.ts +0 -1
package/README.MD
CHANGED
|
@@ -50,8 +50,8 @@ import { locateMany } from 'locter';
|
|
|
50
50
|
console.log(files);
|
|
51
51
|
/*
|
|
52
52
|
[
|
|
53
|
-
{
|
|
54
|
-
{
|
|
53
|
+
{ directory: '/cwd/files', name: 'example', extension: '.js', path: '/cwd/files/example.js' },
|
|
54
|
+
{ directory: '/cwd/files', name: 'example', extension: '.ts', path: '/cwd/files/example.ts' }
|
|
55
55
|
]
|
|
56
56
|
*/
|
|
57
57
|
|
|
@@ -62,9 +62,9 @@ import { locateMany } from 'locter';
|
|
|
62
62
|
console.log(files);
|
|
63
63
|
/*
|
|
64
64
|
[
|
|
65
|
-
{
|
|
66
|
-
{
|
|
67
|
-
{
|
|
65
|
+
{ directory: '/cwd/files', name: 'example', extension: '.js', path: '/cwd/files/example.js' },
|
|
66
|
+
{ directory: '/cwd/files', name: 'example', extension: '.ts', path: '/cwd/files/example.ts' },
|
|
67
|
+
{ directory: '/cwd/files', name: 'example-long', extension: '.ts', path: '/cwd/files/example-long.ts' }
|
|
68
68
|
]
|
|
69
69
|
*/
|
|
70
70
|
})
|
|
@@ -86,13 +86,51 @@ import { locate } from 'locter';
|
|
|
86
86
|
|
|
87
87
|
console.log(file);
|
|
88
88
|
/*
|
|
89
|
-
{
|
|
89
|
+
{ directory: '/cwd/files', name: 'example', extension: '.js', path: '/cwd/files/example.js' }
|
|
90
90
|
*/
|
|
91
91
|
})
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
A synchronous variant is also available: `locateSync`
|
|
95
95
|
|
|
96
|
+
**Walking up**
|
|
97
|
+
|
|
98
|
+
`locateUp` walks from a starting directory toward the filesystem root and returns the first match (useful for discovering config files at the repo root from any sub-directory):
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { locateUp } from 'locter';
|
|
102
|
+
|
|
103
|
+
(async () => {
|
|
104
|
+
const info = await locateUp('trapi.config.{ts,mts,cts,mjs,cjs,js,json}', {
|
|
105
|
+
cwd: process.cwd(),
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
console.log(info);
|
|
109
|
+
/*
|
|
110
|
+
{ directory: '/repo', name: 'trapi.config', extension: '.ts', path: '/repo/trapi.config.ts' }
|
|
111
|
+
or `undefined` if nothing matched
|
|
112
|
+
*/
|
|
113
|
+
})
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Pass `stopAt: '/repo'` to cap the walk at a known ceiling — inclusive, so the ceiling directory itself is still searched.
|
|
117
|
+
|
|
118
|
+
A synchronous variant `locateUpSync` is also available.
|
|
119
|
+
|
|
120
|
+
**Options**
|
|
121
|
+
|
|
122
|
+
`locate` / `locateMany` (and their sync variants) accept an options object:
|
|
123
|
+
|
|
124
|
+
| Option | Type | Default | Notes |
|
|
125
|
+
|-------------------|-------------------------|-----------------|--------------------------------------------------------|
|
|
126
|
+
| `cwd` | `string \| string[]` | `process.cwd()` | Working directory (or directories) the pattern is resolved against |
|
|
127
|
+
| `ignore` | `string \| string[]` | `[]` | Patterns to exclude |
|
|
128
|
+
| `onlyFiles` | `boolean` | `true` | Match files only |
|
|
129
|
+
| `onlyDirectories` | `boolean` | `false` | Match directories only |
|
|
130
|
+
| `dot` | `boolean` | `false` | Match dotfiles (e.g. `.env`, `.npmrc`) for wildcard patterns |
|
|
131
|
+
|
|
132
|
+
`locateUp` / `locateUpSync` take the same options except `cwd` must be a single `string`, and additionally accept `stopAt?: string` (inclusive ceiling for the walk).
|
|
133
|
+
|
|
96
134
|
### Loader
|
|
97
135
|
|
|
98
136
|
The `load` method can be used to load a file/module in an asynchronous fashion.
|
|
@@ -143,6 +181,31 @@ Two loaders are predefined from scratch and already registered:
|
|
|
143
181
|
- **ModuleLoader**: This loader allows to load modules with
|
|
144
182
|
`.js`, `.mjs`, `.mts`, `.cjs`, `.cts`, `.ts` file extensions independent of the environment (cjs or esm).
|
|
145
183
|
|
|
184
|
+
**Reading a `package.json` field**
|
|
185
|
+
|
|
186
|
+
`loadPackageField` reads a top-level field from the nearest `package.json` — handy for the common `pkg.<your-config-key>` config-loading fallback (vite's `pkg.vite`, eslint's `pkg.eslintConfig`, etc.):
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { loadPackageField } from 'locter';
|
|
190
|
+
|
|
191
|
+
(async () => {
|
|
192
|
+
const cwd = process.cwd();
|
|
193
|
+
|
|
194
|
+
const value = await loadPackageField<{ entry: string }>('myapp', { cwd });
|
|
195
|
+
// → the value of the `myapp` field, or undefined if package.json
|
|
196
|
+
// or the field is absent.
|
|
197
|
+
|
|
198
|
+
// Walk parent directories until a package.json is found:
|
|
199
|
+
const parentValue = await loadPackageField('myapp', {
|
|
200
|
+
cwd,
|
|
201
|
+
walkUp: true,
|
|
202
|
+
stopAt: '/repo', // optional ceiling, inclusive
|
|
203
|
+
});
|
|
204
|
+
})
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Throws `LocterLoadError` if the resolved `package.json` is malformed. A sync variant `loadPackageFieldSync` is also available.
|
|
208
|
+
|
|
146
209
|
To register loader for other file types, the function `registerLoader` can be used.
|
|
147
210
|
|
|
148
211
|
```typescript
|
|
@@ -158,6 +221,125 @@ registerLoader(['.ext'], {
|
|
|
158
221
|
})
|
|
159
222
|
```
|
|
160
223
|
|
|
224
|
+
### Errors
|
|
225
|
+
|
|
226
|
+
`load` / `loadSync` (and every built-in loader) throw a typed subclass of
|
|
227
|
+
`LocterError`, so callers can distinguish failure modes without substring
|
|
228
|
+
matching on `.message`:
|
|
229
|
+
|
|
230
|
+
| Class | When it's thrown |
|
|
231
|
+
|--------------------------------|---------------------------------------------------------|
|
|
232
|
+
| `LocterNotFoundError` | File or module does not exist (`ENOENT`, `MODULE_NOT_FOUND`, `ERR_MODULE_NOT_FOUND`) |
|
|
233
|
+
| `LocterLoadError` | A loader threw — parse error, runtime/eval error, etc. |
|
|
234
|
+
| `LocterUnknownExtensionError` | No rule matched the file's extension |
|
|
235
|
+
| `LocterError` | Base class; matches all of the above |
|
|
236
|
+
|
|
237
|
+
Each error exposes the offending `path` and preserves the underlying error
|
|
238
|
+
on `cause`:
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import { load, LocterNotFoundError, LocterLoadError } from 'locter';
|
|
242
|
+
|
|
243
|
+
try {
|
|
244
|
+
await load('config.json');
|
|
245
|
+
} catch (err) {
|
|
246
|
+
if (err instanceof LocterNotFoundError) {
|
|
247
|
+
console.error(`Config file not found: ${err.path}`);
|
|
248
|
+
} else if (err instanceof LocterLoadError) {
|
|
249
|
+
console.error(`Failed to parse config: ${err.cause}`);
|
|
250
|
+
} else {
|
|
251
|
+
throw err;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
`LocterError` extends [`BaseError`](https://github.com/Tada5hi/ebec) from `@ebec/core`, so each instance also carries a `code` (auto-derived from the class name unless overridden — e.g. `'ENOENT'` for `LocterNotFoundError`s thrown by the JSON loader) and a `toJSON()` for structured logging.
|
|
257
|
+
|
|
258
|
+
Each error class also publishes its `Symbol.for(...)` marker (`LOCTER_ERROR_MARKER`, `LOCTER_NOT_FOUND_ERROR_MARKER`, …), which makes `instanceof` work across realms — useful when consumers end up with duplicate copies of locter in their dependency tree.
|
|
259
|
+
|
|
260
|
+
### Migrating from 2.x
|
|
261
|
+
|
|
262
|
+
The 3.0 release reshapes a few public surfaces. The list below is exhaustive — no other public APIs changed.
|
|
263
|
+
|
|
264
|
+
```diff
|
|
265
|
+
- import { load, locate } from 'locter';
|
|
266
|
+
+ import { load, locate, LocterNotFoundError, LocterLoadError } from 'locter';
|
|
267
|
+
|
|
268
|
+
// 1. LocatorOptions.path → cwd
|
|
269
|
+
- await locate('config.json', { path: process.cwd() });
|
|
270
|
+
+ await locate('config.json', { cwd: process.cwd() });
|
|
271
|
+
|
|
272
|
+
// 2. LocatorInfo.path now holds the FULL file path (it was the
|
|
273
|
+
// containing directory in 2.x). buildFilePath(info) still works in
|
|
274
|
+
// both versions but is now a near-identity for LocatorInfo inputs.
|
|
275
|
+
- const filePath = buildFilePath(info); // 2.x: composed from .path + .name + .extension
|
|
276
|
+
+ const filePath = info.path; // 3.x: just the path field
|
|
277
|
+
+ const dir = info.directory; // 3.x: containing directory (the old `info.path`)
|
|
278
|
+
|
|
279
|
+
// 3. load() / loadSync() (and every built-in loader) now throw
|
|
280
|
+
// LocterError subclasses instead of generic Error / ebec BaseError.
|
|
281
|
+
try {
|
|
282
|
+
await load('config.json');
|
|
283
|
+
- } catch (err) {
|
|
284
|
+
- if (err.message.includes('ENOENT')) { /* … */ }
|
|
285
|
+
+ } catch (err) {
|
|
286
|
+
+ if (err instanceof LocterNotFoundError) { /* … */ }
|
|
287
|
+
+ else if (err instanceof LocterLoadError) { /* parse error: err.cause */ }
|
|
288
|
+
+ else throw err;
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Additionally:
|
|
293
|
+
|
|
294
|
+
- The `ebec` runtime dependency is replaced by `@ebec/core`. If you used to import `BaseError` from `ebec`, switch to `@ebec/core`.
|
|
295
|
+
- The package is now ESM-only (no `require('locter')`). This change shipped in the 2.x→3.x toolchain modernization commit; mentioned here for completeness.
|
|
296
|
+
|
|
297
|
+
### Runtime environments
|
|
298
|
+
|
|
299
|
+
Locter ships with two runtime detection helpers:
|
|
300
|
+
|
|
301
|
+
- `isJestRuntimeEnvironment()` — true when running under Jest. Built-in: the
|
|
302
|
+
`ModuleLoader` falls back to `require()` under Jest to avoid a known
|
|
303
|
+
segmentation fault ([nodejs/node#35889](https://github.com/nodejs/node/issues/35889)).
|
|
304
|
+
- `isVitestRuntimeEnvironment()` — true when running under Vitest (checks
|
|
305
|
+
`process.env.VITEST === 'true'`).
|
|
306
|
+
|
|
307
|
+
#### Using locter with Vitest
|
|
308
|
+
|
|
309
|
+
Locter's built-in `await import(id)` runs inside `node_modules/locter` and
|
|
310
|
+
therefore bypasses Vitest's `vite-node` module graph by default. For most use
|
|
311
|
+
cases this is fine, but it produces **duplicate module instances** when test
|
|
312
|
+
code statically imports a module and locter dynamically loads the same module
|
|
313
|
+
(or a transitive dependency of it). This breaks class identity for libraries
|
|
314
|
+
that rely on it (e.g. TypeORM entity metadata).
|
|
315
|
+
|
|
316
|
+
Use `setModuleLoader` to inject an `import` call from user space so Vitest can
|
|
317
|
+
rewrite it:
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
// vitest setup file
|
|
321
|
+
import { setModuleLoader } from 'locter';
|
|
322
|
+
|
|
323
|
+
setModuleLoader({
|
|
324
|
+
load: (id) => import(id),
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Alternatively, the same effect can be achieved by inlining locter in
|
|
329
|
+
`vitest.config.ts`:
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
export default defineConfig({
|
|
333
|
+
test: {
|
|
334
|
+
server: {
|
|
335
|
+
deps: {
|
|
336
|
+
inline: [/locter/],
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
```
|
|
342
|
+
|
|
161
343
|
|
|
162
344
|
## License
|
|
163
345
|
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { BaseError, ErrorOptions } from "@ebec/core";
|
|
2
|
+
import { createJiti } from "jiti";
|
|
3
|
+
|
|
4
|
+
//#region src/errors/base.d.ts
|
|
5
|
+
type LocterErrorOptions = ErrorOptions & {
|
|
6
|
+
path?: string;
|
|
7
|
+
};
|
|
8
|
+
type LocterErrorInput = string | LocterErrorOptions;
|
|
9
|
+
declare const LOCTER_ERROR_MARKER: unique symbol;
|
|
10
|
+
declare class LocterError extends BaseError {
|
|
11
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
12
|
+
readonly path?: string;
|
|
13
|
+
constructor(input?: LocterErrorInput);
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/errors/load.d.ts
|
|
17
|
+
declare const LOCTER_LOAD_ERROR_MARKER: unique symbol;
|
|
18
|
+
declare class LocterLoadError extends LocterError {
|
|
19
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
20
|
+
constructor(input?: LocterErrorInput);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/errors/not-found.d.ts
|
|
24
|
+
declare const LOCTER_NOT_FOUND_ERROR_MARKER: unique symbol;
|
|
25
|
+
declare class LocterNotFoundError extends LocterError {
|
|
26
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
27
|
+
constructor(input?: LocterErrorInput);
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/errors/unknown-extension.d.ts
|
|
31
|
+
declare const LOCTER_UNKNOWN_EXTENSION_ERROR_MARKER: unique symbol;
|
|
32
|
+
declare class LocterUnknownExtensionError extends LocterError {
|
|
33
|
+
static [Symbol.hasInstance](input: unknown): boolean;
|
|
34
|
+
constructor(input?: LocterErrorInput);
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/errors/wrap.d.ts
|
|
38
|
+
declare function wrapLoaderError(input: unknown, path: string): LocterError;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/loader/type.d.ts
|
|
41
|
+
type Loader = {
|
|
42
|
+
execute: (input: string) => Promise<any>;
|
|
43
|
+
executeSync: (input: string) => any;
|
|
44
|
+
};
|
|
45
|
+
type Rule = {
|
|
46
|
+
test: RegExp | string[];
|
|
47
|
+
loader: Loader | string;
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/loader/built-in/conf/module.d.ts
|
|
51
|
+
declare class ConfLoader implements Loader {
|
|
52
|
+
execute(input: string): Promise<Record<string, any>>;
|
|
53
|
+
executeSync(input: string): Record<string, any>;
|
|
54
|
+
parse(contents: string): Record<string, any>;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/loader/built-in/json/module.d.ts
|
|
58
|
+
declare class JSONLoader implements Loader {
|
|
59
|
+
execute(input: string): Promise<any>;
|
|
60
|
+
executeSync(input: string): any;
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/locator/types.d.ts
|
|
64
|
+
type LocatorInfo = {
|
|
65
|
+
directory: string;
|
|
66
|
+
name: string;
|
|
67
|
+
extension?: string;
|
|
68
|
+
path: string;
|
|
69
|
+
};
|
|
70
|
+
type LocatorOptions = {
|
|
71
|
+
cwd: string[];
|
|
72
|
+
ignore: string[];
|
|
73
|
+
onlyFiles: boolean;
|
|
74
|
+
onlyDirectories: boolean;
|
|
75
|
+
dot: boolean;
|
|
76
|
+
};
|
|
77
|
+
type LocatorOptionsInput = Partial<Omit<LocatorOptions, 'cwd' | 'ignore'>> & {
|
|
78
|
+
cwd?: string | string[];
|
|
79
|
+
ignore?: string | string[];
|
|
80
|
+
};
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/locator/async.d.ts
|
|
83
|
+
declare function locateMany(pattern: string | string[], options?: LocatorOptionsInput): Promise<LocatorInfo[]>;
|
|
84
|
+
declare function locate(pattern: string | string[], options?: LocatorOptionsInput): Promise<LocatorInfo | undefined>;
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/locator/sync.d.ts
|
|
87
|
+
declare function locateManySync(pattern: string | string[], options?: LocatorOptionsInput): LocatorInfo[];
|
|
88
|
+
declare function locateSync(pattern: string | string[], options?: LocatorOptionsInput): LocatorInfo | undefined;
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/locator/up.d.ts
|
|
91
|
+
type LocatorUpOptionsInput = Omit<LocatorOptionsInput, 'cwd'> & {
|
|
92
|
+
cwd?: string;
|
|
93
|
+
stopAt?: string;
|
|
94
|
+
};
|
|
95
|
+
declare function locateUp(pattern: string | string[], options?: LocatorUpOptionsInput): Promise<LocatorInfo | undefined>;
|
|
96
|
+
declare function locateUpSync(pattern: string | string[], options?: LocatorUpOptionsInput): LocatorInfo | undefined;
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/locator/utils.d.ts
|
|
99
|
+
declare function buildLocatorPatterns(pattern: string | string[]): string[];
|
|
100
|
+
declare function buildLocatorOptions(options?: LocatorOptionsInput): LocatorOptions;
|
|
101
|
+
declare function pathToLocatorInfo(input: string, skipResolve?: boolean): LocatorInfo;
|
|
102
|
+
declare function isLocatorInfo(input: unknown): input is LocatorInfo;
|
|
103
|
+
declare function buildFilePath(input: LocatorInfo | string): string;
|
|
104
|
+
declare function buildFilePathWithoutExtension(input: LocatorInfo | string): string;
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/loader/built-in/module/type.d.ts
|
|
107
|
+
type LoaderFilterFn = (key: string, value: unknown) => boolean;
|
|
108
|
+
type ModuleExport = {
|
|
109
|
+
key: string;
|
|
110
|
+
value: any;
|
|
111
|
+
};
|
|
112
|
+
type ModuleLoadOptions = {
|
|
113
|
+
withFilePrefix?: boolean;
|
|
114
|
+
};
|
|
115
|
+
type ModuleLoadFn = (id: string) => unknown | Promise<unknown>;
|
|
116
|
+
type ModuleLoadSyncFn = (id: string) => unknown;
|
|
117
|
+
type ModuleLoaderOptions = {
|
|
118
|
+
/**
|
|
119
|
+
* Custom asynchronous module loader. When set, this replaces the
|
|
120
|
+
* built-in `await import(id)` call. Useful for test runners (e.g. Vitest)
|
|
121
|
+
* where dynamic imports inside `node_modules/locter` would otherwise
|
|
122
|
+
* escape the runner's module graph — defining this in user space lets
|
|
123
|
+
* the runner rewrite the `import()`.
|
|
124
|
+
*/
|
|
125
|
+
load?: ModuleLoadFn;
|
|
126
|
+
/**
|
|
127
|
+
* Custom synchronous module loader. When set, this replaces the built-in
|
|
128
|
+
* `require(id)` call.
|
|
129
|
+
*/
|
|
130
|
+
loadSync?: ModuleLoadSyncFn;
|
|
131
|
+
};
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/loader/built-in/module/module.d.ts
|
|
134
|
+
type Jiti = ReturnType<typeof createJiti>;
|
|
135
|
+
declare class ModuleLoader implements Loader {
|
|
136
|
+
protected instance: Jiti;
|
|
137
|
+
protected loadFn?: ModuleLoadFn;
|
|
138
|
+
protected loadSyncFn?: ModuleLoadSyncFn;
|
|
139
|
+
constructor(options?: ModuleLoaderOptions);
|
|
140
|
+
configure(options: ModuleLoaderOptions): void;
|
|
141
|
+
execute(input: string): Promise<any>;
|
|
142
|
+
executeSync(input: string): any;
|
|
143
|
+
load(data: LocatorInfo | string, options?: ModuleLoadOptions): Promise<unknown>;
|
|
144
|
+
loadSync(data: LocatorInfo | string, options?: ModuleLoadOptions): unknown;
|
|
145
|
+
private build;
|
|
146
|
+
}
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/loader/built-in/module/utils.d.ts
|
|
149
|
+
type ESModule = {
|
|
150
|
+
[key: string]: any;
|
|
151
|
+
__esModule: boolean;
|
|
152
|
+
};
|
|
153
|
+
declare function isESModule(input: unknown): input is ESModule;
|
|
154
|
+
declare function toModuleRecord(data: unknown): any;
|
|
155
|
+
declare function getModuleExport(data: Record<string, any>, filterFn: LoaderFilterFn): ModuleExport | undefined;
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/loader/built-in/yaml/module.d.ts
|
|
158
|
+
declare class YAMLLoader implements Loader {
|
|
159
|
+
execute(input: string): Promise<any>;
|
|
160
|
+
executeSync(input: string): any;
|
|
161
|
+
}
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/loader/helpers.d.ts
|
|
164
|
+
declare function registerLoader(rule: Rule): void;
|
|
165
|
+
declare function registerLoader(test: string[] | RegExp, loader: Loader): void;
|
|
166
|
+
declare function load(input: LocatorInfo | string): Promise<any>;
|
|
167
|
+
declare function loadSync(input: LocatorInfo | string): any;
|
|
168
|
+
/**
|
|
169
|
+
* Override the built-in module loader's `import` / `require` calls.
|
|
170
|
+
*
|
|
171
|
+
* Useful for test runners (e.g. Vitest) where the dynamic `import()` inside
|
|
172
|
+
* `node_modules/locter` would bypass the runner's module graph. Calling this
|
|
173
|
+
* from user code (e.g. a Vitest setup file) lets the runner rewrite the
|
|
174
|
+
* `import()` so loaded modules share identity with statically imported ones.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* // vitest setup file
|
|
179
|
+
* import { setModuleLoader } from 'locter';
|
|
180
|
+
*
|
|
181
|
+
* setModuleLoader({
|
|
182
|
+
* load: (id) => import(id),
|
|
183
|
+
* });
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
declare function setModuleLoader(options: ModuleLoaderOptions): void;
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/loader/module.d.ts
|
|
189
|
+
declare class LoaderManager implements Loader {
|
|
190
|
+
protected loaders: Record<string, Loader>;
|
|
191
|
+
protected rules: Rule[];
|
|
192
|
+
constructor();
|
|
193
|
+
register(rule: Rule): void;
|
|
194
|
+
register(test: string[] | RegExp, loader: Loader): void;
|
|
195
|
+
execute(input: string): Promise<any>;
|
|
196
|
+
executeSync(input: string): any;
|
|
197
|
+
protected unknownExtensionError(input: string): LocterUnknownExtensionError;
|
|
198
|
+
findLoader(input: string): Loader | string | undefined;
|
|
199
|
+
/**
|
|
200
|
+
* Resolve loader by id.
|
|
201
|
+
*
|
|
202
|
+
* @param id
|
|
203
|
+
*/
|
|
204
|
+
resolve(id: string | Loader): Loader;
|
|
205
|
+
normalizePath(input: string): string;
|
|
206
|
+
}
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/loader/package-field.d.ts
|
|
209
|
+
type LoadPackageFieldOptions = {
|
|
210
|
+
cwd?: string;
|
|
211
|
+
walkUp?: boolean;
|
|
212
|
+
stopAt?: string;
|
|
213
|
+
};
|
|
214
|
+
declare function loadPackageField<T = unknown>(field: string, options?: LoadPackageFieldOptions): Promise<T | undefined>;
|
|
215
|
+
declare function loadPackageFieldSync<T = unknown>(field: string, options?: LoadPackageFieldOptions): T | undefined;
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region src/utils/file-name.d.ts
|
|
218
|
+
declare function getFileNameExtension(input: string, allowed?: string[]): string | undefined;
|
|
219
|
+
declare function removeFileNameExtension(input: string, extensions?: string[]): string;
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/utils/has-property.d.ts
|
|
222
|
+
declare function hasOwnProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
|
|
223
|
+
declare function hasStringProperty<X extends Record<string, any>, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, string>;
|
|
224
|
+
//#endregion
|
|
225
|
+
//#region src/utils/object.d.ts
|
|
226
|
+
declare function isObject(item: unknown): item is Record<string, any>;
|
|
227
|
+
declare function isSafeObjectKey(key: string): boolean;
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/utils/to-array.d.ts
|
|
230
|
+
declare function toArray(input: string | string[]): string[];
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region src/utils/file-path.d.ts
|
|
233
|
+
declare function isFilePath(input: string): boolean | "";
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/utils/runtime.d.ts
|
|
236
|
+
declare function isJestRuntimeEnvironment(): boolean;
|
|
237
|
+
declare function isVitestRuntimeEnvironment(): boolean;
|
|
238
|
+
declare function isTsNodeRuntimeEnvironment(): boolean;
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/utils/typescript.d.ts
|
|
241
|
+
declare function isTypeScriptError(input: unknown): input is Error;
|
|
242
|
+
//#endregion
|
|
243
|
+
export { ConfLoader, JSONLoader, LOCTER_ERROR_MARKER, LOCTER_LOAD_ERROR_MARKER, LOCTER_NOT_FOUND_ERROR_MARKER, LOCTER_UNKNOWN_EXTENSION_ERROR_MARKER, LoadPackageFieldOptions, LoaderFilterFn, LoaderManager, LocatorInfo, LocatorOptions, LocatorOptionsInput, LocatorUpOptionsInput, LocterError, LocterErrorInput, LocterErrorOptions, LocterLoadError, LocterNotFoundError, LocterUnknownExtensionError, ModuleExport, ModuleLoadFn, ModuleLoadOptions, ModuleLoadSyncFn, ModuleLoader, ModuleLoaderOptions, YAMLLoader, buildFilePath, buildFilePathWithoutExtension, buildLocatorOptions, buildLocatorPatterns, getFileNameExtension, getModuleExport, hasOwnProperty, hasStringProperty, isESModule, isFilePath, isJestRuntimeEnvironment, isLocatorInfo, isObject, isSafeObjectKey, isTsNodeRuntimeEnvironment, isTypeScriptError, isVitestRuntimeEnvironment, load, loadPackageField, loadPackageFieldSync, loadSync, locate, locateMany, locateManySync, locateSync, locateUp, locateUpSync, pathToLocatorInfo, registerLoader, removeFileNameExtension, setModuleLoader, toArray, toModuleRecord, wrapLoaderError };
|
|
244
|
+
//# sourceMappingURL=index.d.mts.map
|