@theholocron/datapad 5.0.0-alpha.457 → 5.0.0-alpha.459
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 +13 -0
- package/dist/index.d.mts +73 -32
- package/dist/index.mjs +88 -20
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -157,6 +157,19 @@ paths actually merged, lowest priority first.
|
|
|
157
157
|
before the merge (a bare array standing for `{ tasks: [...] }`, say);
|
|
158
158
|
returning `undefined` treats the layer as absent.
|
|
159
159
|
|
|
160
|
+
### `loadLayeredFromContent<T>({ dir, name, fetch, fallback?, extensions?, coerce? })`
|
|
161
|
+
|
|
162
|
+
`loadLayered` for config files that live somewhere other than the local disk. The
|
|
163
|
+
dedicated `<name>.config.*` and the `<fallback.file>.config.*` are probed through
|
|
164
|
+
`fetch` (as `readConfigSource` does), executed in `dir` (a directory prepared by
|
|
165
|
+
`withConfigSandbox`) and merged with `mergeLayers`, the dedicated file winning. Returns
|
|
166
|
+
`{ config, filepath, sources } | null`, with the paths as `fetch` was asked for them.
|
|
167
|
+
|
|
168
|
+
- `fallback.preloaded` — the fallback file as the caller already loaded it, used
|
|
169
|
+
instead of fetching it (then `fallback.file` is not needed).
|
|
170
|
+
- A source that is found but cannot be executed throws a `ConfigFileError` whose
|
|
171
|
+
`filepath` names it; what `fetch` throws propagates unchanged.
|
|
172
|
+
|
|
160
173
|
### `mergeLayers<T>(layers)`
|
|
161
174
|
|
|
162
175
|
The merge `loadLayered` uses, for layers loaded some other way: deep-merges
|
package/dist/index.d.mts
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* parent file, deep-merge, and hand back a plain object. No schema, no
|
|
8
8
|
* validation, no defaults — the consumer owns those. `readConfigSource` +
|
|
9
9
|
* `loadConfigFromContent` cover the same loading for content that didn't come
|
|
10
|
-
* from a file on disk (a network fetch, a git blob, …),
|
|
10
|
+
* from a file on disk (a network fetch, a git blob, …), `loadLayeredFromContent` layers such sources the way
|
|
11
|
+
* `loadLayered` layers files, and
|
|
11
12
|
* `withConfigSandbox` prepares the directory that needs. A config directory of area files (`.holocron/<area>.*`) is
|
|
12
13
|
* read with `loadConfigDir` from disk, or with `readConfigDirSource` / `writeConfigDir` from a remote source; the
|
|
13
14
|
* directory name and the area names are parameters.
|
|
@@ -220,6 +221,76 @@ interface LayeredResult<T> {
|
|
|
220
221
|
* when neither source resolves.
|
|
221
222
|
*/
|
|
222
223
|
export declare function loadLayered<T>(opts: LoadLayeredOptions<T>): Promise<LayeredResult<T> | null>;
|
|
224
|
+
/** A config file's content fetched from somewhere other than the local disk. */
|
|
225
|
+
interface ConfigSource {
|
|
226
|
+
/** The file name probed, e.g. `holocron.config.ts` — relative, as `fetch` was asked for it. */
|
|
227
|
+
path: string;
|
|
228
|
+
/** Which extension matched — what {@link loadConfigFromContent} needs as `extension`. */
|
|
229
|
+
extension: string;
|
|
230
|
+
/** The raw file content. */
|
|
231
|
+
content: string;
|
|
232
|
+
}
|
|
233
|
+
/** Options for fetching a config file's content from a remote source. */
|
|
234
|
+
interface ReadConfigSourceOptions {
|
|
235
|
+
/** Base name — `"holocron"` probes `holocron.config.{ts,js,mjs,cjs,json}`. */
|
|
236
|
+
name: string;
|
|
237
|
+
/**
|
|
238
|
+
* Fetches one file by relative path (`holocron.config.ts`): its content, or
|
|
239
|
+
* `undefined` when it doesn't exist. Throw for anything else (a network
|
|
240
|
+
* failure, a permission error) — that propagates rather than reading as "absent".
|
|
241
|
+
*/
|
|
242
|
+
fetch: (path: string) => Promise<string | undefined>;
|
|
243
|
+
/** Override the extension probe order. */
|
|
244
|
+
extensions?: readonly string[];
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* {@link probeExtensions} for a config that lives somewhere other than the
|
|
248
|
+
* local disk (a GitHub API, a git blob): probes `<name>.config.<ext>` in
|
|
249
|
+
* extension-priority order through `options.fetch`, returning the first file
|
|
250
|
+
* that exists, or `undefined` when none does. Pass the result to
|
|
251
|
+
* `loadConfigFromContent` to execute it exactly as a local file would be.
|
|
252
|
+
*/
|
|
253
|
+
export declare function readConfigSource(options: ReadConfigSourceOptions): Promise<ConfigSource | undefined>;
|
|
254
|
+
/** Options for {@link loadLayeredFromContent}: `loadLayered` for config files read through a `fetch` callback. */
|
|
255
|
+
interface LoadLayeredFromContentOptions<T = unknown> extends ReadConfigSourceOptions {
|
|
256
|
+
/**
|
|
257
|
+
* Directory the fetched files are written into before they are executed (see `loadConfigFromContent`): it has to sit
|
|
258
|
+
* under a tree where the packages a config imports resolve, which is what `withConfigSandbox` prepares. The caller
|
|
259
|
+
* creates and removes it.
|
|
260
|
+
*/
|
|
261
|
+
dir: string;
|
|
262
|
+
/**
|
|
263
|
+
* Read `<file>.config.*` and take its `[key]` as the lower layer; the dedicated file (`name`), if any, is merged on
|
|
264
|
+
* top. Either layer may be absent.
|
|
265
|
+
*/
|
|
266
|
+
fallback?: {
|
|
267
|
+
/** Base name of the fallback file, fetched as `<file>.config.*`. Not needed with `preloaded`. */
|
|
268
|
+
file?: string;
|
|
269
|
+
/** The key in the fallback file whose value is taken. */
|
|
270
|
+
key: string;
|
|
271
|
+
/**
|
|
272
|
+
* The fallback file as the caller already loaded it (`null`: it was looked for and isn't there), used as is instead
|
|
273
|
+
* of fetching it again. Its `filepath` is reported in `sources`.
|
|
274
|
+
*/
|
|
275
|
+
preloaded?: Loaded<object> | null;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Turns each layer's raw value into the shape being merged, before the merge; return `undefined` to treat the layer as
|
|
279
|
+
* absent. Same as `LoadLayeredOptions.coerce`.
|
|
280
|
+
*/
|
|
281
|
+
coerce?: (raw: unknown) => T | undefined;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* {@link loadLayered} for config files that live somewhere other than the local disk (a GitHub API, a git blob): the
|
|
285
|
+
* dedicated `<name>.config.*` layered over the `[fallback.key]` of `<fallback.file>.config.*`, each probed through
|
|
286
|
+
* `options.fetch` and executed in `options.dir`, then merged with {@link mergeLayers} (the dedicated file wins). Returns
|
|
287
|
+
* `null` when neither layer resolves; `filepath` and `sources` are the paths as `fetch` was asked for them
|
|
288
|
+
* (`astromech.config.ts`), lowest priority first.
|
|
289
|
+
*
|
|
290
|
+
* A source that is found but cannot be executed throws a {@link ConfigFileError} whose `filepath` is that path, so a
|
|
291
|
+
* caller can say which file failed. Whatever `fetch` throws (a network failure, a permission error) propagates as it is.
|
|
292
|
+
*/
|
|
293
|
+
export declare function loadLayeredFromContent<T>(options: LoadLayeredFromContentOptions<T>): Promise<LayeredResult<T> | null>;
|
|
223
294
|
/**
|
|
224
295
|
* Deep-merges config layers, lowest priority first (a later layer wins on
|
|
225
296
|
* scalars; arrays concatenate; nested objects merge). `undefined` layers are
|
|
@@ -267,36 +338,6 @@ interface ReadConfigDirSourceOptions {
|
|
|
267
338
|
* imports) passes that tree to {@link writeConfigDir} directly.
|
|
268
339
|
*/
|
|
269
340
|
export declare function readConfigDirSource(options: ReadConfigDirSourceOptions): Promise<ConfigDirSource[]>;
|
|
270
|
-
/** A config file's content fetched from somewhere other than the local disk. */
|
|
271
|
-
interface ConfigSource {
|
|
272
|
-
/** The file name probed, e.g. `holocron.config.ts` — relative, as `fetch` was asked for it. */
|
|
273
|
-
path: string;
|
|
274
|
-
/** Which extension matched — what {@link loadConfigFromContent} needs as `extension`. */
|
|
275
|
-
extension: string;
|
|
276
|
-
/** The raw file content. */
|
|
277
|
-
content: string;
|
|
278
|
-
}
|
|
279
|
-
/** Options for fetching a config file's content from a remote source. */
|
|
280
|
-
interface ReadConfigSourceOptions {
|
|
281
|
-
/** Base name — `"holocron"` probes `holocron.config.{ts,js,mjs,cjs,json}`. */
|
|
282
|
-
name: string;
|
|
283
|
-
/**
|
|
284
|
-
* Fetches one file by relative path (`holocron.config.ts`): its content, or
|
|
285
|
-
* `undefined` when it doesn't exist. Throw for anything else (a network
|
|
286
|
-
* failure, a permission error) — that propagates rather than reading as "absent".
|
|
287
|
-
*/
|
|
288
|
-
fetch: (path: string) => Promise<string | undefined>;
|
|
289
|
-
/** Override the extension probe order. */
|
|
290
|
-
extensions?: readonly string[];
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* {@link probeExtensions} for a config that lives somewhere other than the
|
|
294
|
-
* local disk (a GitHub API, a git blob): probes `<name>.config.<ext>` in
|
|
295
|
-
* extension-priority order through `options.fetch`, returning the first file
|
|
296
|
-
* that exists, or `undefined` when none does. Pass the result to
|
|
297
|
-
* `loadConfigFromContent` to execute it exactly as a local file would be.
|
|
298
|
-
*/
|
|
299
|
-
export declare function readConfigSource(options: ReadConfigSourceOptions): Promise<ConfigSource | undefined>;
|
|
300
341
|
/** Options for the temporary directory a config is loaded in. */
|
|
301
342
|
interface ConfigSandboxOptions {
|
|
302
343
|
/**
|
|
@@ -355,4 +396,4 @@ interface WriteConfigDirOptions {
|
|
|
355
396
|
* checks every path before it writes any.
|
|
356
397
|
*/
|
|
357
398
|
export declare function writeConfigDir(options: WriteConfigDirOptions): Promise<string[]>;
|
|
358
|
-
export { type ConfigDirArea, type ConfigDirFile, type ConfigDirSource, type ConfigSandboxOptions, type ConfigSource, type LayeredResult, type LoadConfigDirOptions, type LoadConfigFileOptions, type LoadConfigFromContentOptions, type LoadLayeredOptions, type Loaded, type ReadConfigDirSourceOptions, type ReadConfigSourceOptions, type WriteConfigDirOptions, mergeConfig };
|
|
399
|
+
export { type ConfigDirArea, type ConfigDirFile, type ConfigDirSource, type ConfigSandboxOptions, type ConfigSource, type LayeredResult, type LoadConfigDirOptions, type LoadConfigFileOptions, type LoadConfigFromContentOptions, type LoadLayeredFromContentOptions, type LoadLayeredOptions, type Loaded, type ReadConfigDirSourceOptions, type ReadConfigSourceOptions, type WriteConfigDirOptions, mergeConfig };
|
package/dist/index.mjs
CHANGED
|
@@ -310,6 +310,93 @@ async function loadLayered(opts) {
|
|
|
310
310
|
sources
|
|
311
311
|
};
|
|
312
312
|
}
|
|
313
|
+
// src/read-config-source/read-config-source.ts
|
|
314
|
+
/**
|
|
315
|
+
* {@link probeExtensions} for a config that lives somewhere other than the
|
|
316
|
+
* local disk (a GitHub API, a git blob): probes `<name>.config.<ext>` in
|
|
317
|
+
* extension-priority order through `options.fetch`, returning the first file
|
|
318
|
+
* that exists, or `undefined` when none does. Pass the result to
|
|
319
|
+
* `loadConfigFromContent` to execute it exactly as a local file would be.
|
|
320
|
+
*/
|
|
321
|
+
async function readConfigSource(options) {
|
|
322
|
+
for (const extension of options.extensions ?? DEFAULT_EXTENSIONS) {
|
|
323
|
+
const path = `${options.name}.config.${extension}`;
|
|
324
|
+
const content = await options.fetch(path);
|
|
325
|
+
if (content !== void 0) return {
|
|
326
|
+
path,
|
|
327
|
+
extension,
|
|
328
|
+
content
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
// src/load-layered-from-content/load-layered-from-content.ts
|
|
333
|
+
/** Executes one fetched source, naming the file (as it was fetched) on a failure. */
|
|
334
|
+
async function execute(source, name, dir) {
|
|
335
|
+
try {
|
|
336
|
+
return (await loadConfigFromContent({
|
|
337
|
+
dir,
|
|
338
|
+
content: source.content,
|
|
339
|
+
name,
|
|
340
|
+
extension: source.extension
|
|
341
|
+
})).config;
|
|
342
|
+
} catch (err) {
|
|
343
|
+
throw new ConfigFileError(err instanceof Error ? err.message : String(err), source.path);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* {@link loadLayered} for config files that live somewhere other than the local disk (a GitHub API, a git blob): the
|
|
348
|
+
* dedicated `<name>.config.*` layered over the `[fallback.key]` of `<fallback.file>.config.*`, each probed through
|
|
349
|
+
* `options.fetch` and executed in `options.dir`, then merged with {@link mergeLayers} (the dedicated file wins). Returns
|
|
350
|
+
* `null` when neither layer resolves; `filepath` and `sources` are the paths as `fetch` was asked for them
|
|
351
|
+
* (`astromech.config.ts`), lowest priority first.
|
|
352
|
+
*
|
|
353
|
+
* A source that is found but cannot be executed throws a {@link ConfigFileError} whose `filepath` is that path, so a
|
|
354
|
+
* caller can say which file failed. Whatever `fetch` throws (a network failure, a permission error) propagates as it is.
|
|
355
|
+
*/
|
|
356
|
+
async function loadLayeredFromContent(options) {
|
|
357
|
+
const { dir, fetch, extensions, fallback } = options;
|
|
358
|
+
const coerce = options.coerce ?? ((raw) => raw);
|
|
359
|
+
const dedicated = await readConfigSource({
|
|
360
|
+
name: options.name,
|
|
361
|
+
fetch,
|
|
362
|
+
extensions
|
|
363
|
+
});
|
|
364
|
+
let top;
|
|
365
|
+
let topFile = null;
|
|
366
|
+
if (dedicated) {
|
|
367
|
+
top = coerce(await execute(dedicated, options.name, dir));
|
|
368
|
+
if (top !== void 0) topFile = dedicated.path;
|
|
369
|
+
}
|
|
370
|
+
let base;
|
|
371
|
+
let baseFile = null;
|
|
372
|
+
if (fallback) {
|
|
373
|
+
let parent = fallback.preloaded;
|
|
374
|
+
if (parent === void 0 && fallback.file !== void 0) {
|
|
375
|
+
const source = await readConfigSource({
|
|
376
|
+
name: fallback.file,
|
|
377
|
+
fetch,
|
|
378
|
+
extensions
|
|
379
|
+
});
|
|
380
|
+
parent = source ? {
|
|
381
|
+
config: await execute(source, fallback.file, dir),
|
|
382
|
+
filepath: source.path
|
|
383
|
+
} : null;
|
|
384
|
+
}
|
|
385
|
+
const raw = (parent?.config)?.[fallback.key];
|
|
386
|
+
if (parent && raw !== void 0) {
|
|
387
|
+
base = coerce(raw);
|
|
388
|
+
if (base !== void 0) baseFile = parent.filepath;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const config = mergeLayers([base, top]);
|
|
392
|
+
if (config === void 0) return null;
|
|
393
|
+
const sources = [baseFile, topFile].filter((s) => s !== null);
|
|
394
|
+
return {
|
|
395
|
+
config,
|
|
396
|
+
filepath: topFile ?? baseFile,
|
|
397
|
+
sources
|
|
398
|
+
};
|
|
399
|
+
}
|
|
313
400
|
// src/read-config-dir-source/read-config-dir-source.ts
|
|
314
401
|
/**
|
|
315
402
|
* {@link readConfigSource} for the area files of a config directory (`.holocron/<area>.<ext>`) that live somewhere
|
|
@@ -336,25 +423,6 @@ async function readConfigDirSource(options) {
|
|
|
336
423
|
}
|
|
337
424
|
return sources;
|
|
338
425
|
}
|
|
339
|
-
// src/read-config-source/read-config-source.ts
|
|
340
|
-
/**
|
|
341
|
-
* {@link probeExtensions} for a config that lives somewhere other than the
|
|
342
|
-
* local disk (a GitHub API, a git blob): probes `<name>.config.<ext>` in
|
|
343
|
-
* extension-priority order through `options.fetch`, returning the first file
|
|
344
|
-
* that exists, or `undefined` when none does. Pass the result to
|
|
345
|
-
* `loadConfigFromContent` to execute it exactly as a local file would be.
|
|
346
|
-
*/
|
|
347
|
-
async function readConfigSource(options) {
|
|
348
|
-
for (const extension of options.extensions ?? DEFAULT_EXTENSIONS) {
|
|
349
|
-
const path = `${options.name}.config.${extension}`;
|
|
350
|
-
const content = await options.fetch(path);
|
|
351
|
-
if (content !== void 0) return {
|
|
352
|
-
path,
|
|
353
|
-
extension,
|
|
354
|
-
content
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
426
|
// src/with-config-sandbox/with-config-sandbox.ts
|
|
359
427
|
/**
|
|
360
428
|
* Runs `run` with a throwaway directory prepared for
|
|
@@ -416,4 +484,4 @@ async function writeConfigDir(options) {
|
|
|
416
484
|
}
|
|
417
485
|
return targets.map(({ target }) => target);
|
|
418
486
|
}
|
|
419
|
-
export { ConfigFileError, DEFAULT_EXTENSIONS, createDefineConfig, loadConfigDir, loadConfigFile, loadConfigFromContent, loadConfigPath, loadLayered, mergeConfig, mergeLayers, readConfigDirSource, readConfigSource, withConfigSandbox, writeConfigDir };
|
|
487
|
+
export { ConfigFileError, DEFAULT_EXTENSIONS, createDefineConfig, loadConfigDir, loadConfigFile, loadConfigFromContent, loadConfigPath, loadLayered, loadLayeredFromContent, mergeConfig, mergeLayers, readConfigDirSource, readConfigSource, withConfigSandbox, writeConfigDir };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/datapad",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.459",
|
|
4
4
|
"description": "Generic config-file loading for Holocron — discover, load (JSON/JS/TS/ESM/CJS), and merge <name>.config.* files with a typed defineConfig.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"config",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"tsx": "4.23.15",
|
|
36
36
|
"@theholocron/fs-utils": "^1.17.0-alpha.4",
|
|
37
|
-
"@theholocron/
|
|
38
|
-
"@theholocron/
|
|
37
|
+
"@theholocron/object-utils": "^1.17.0-alpha.4",
|
|
38
|
+
"@theholocron/misc-utils": "^1.17.0-alpha.4"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^26",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
"tsdown": "^0.23.7",
|
|
48
48
|
"typescript": "^5.9.3",
|
|
49
49
|
"vitest": "^5.1.0",
|
|
50
|
+
"@theholocron/eslint-config": "^8.9.0-alpha.18",
|
|
50
51
|
"@theholocron/tsconfig": "^8.9.0-alpha.18",
|
|
51
52
|
"@theholocron/tsdown-config": "^8.9.0-alpha.18",
|
|
52
|
-
"@theholocron/eslint-config": "^8.9.0-alpha.18",
|
|
53
53
|
"@theholocron/vitest-config": "^8.9.0-alpha.18"
|
|
54
54
|
},
|
|
55
55
|
"engines": {
|