@theholocron/datapad 5.0.0-alpha.15 → 5.0.0-alpha.151
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 +72 -10
- package/dist/index.d.mts +154 -26
- package/dist/index.mjs +225 -108
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -43,15 +43,61 @@ export const defineConfig = createDefineConfig<TasksConfig>();
|
|
|
43
43
|
|
|
44
44
|
## API
|
|
45
45
|
|
|
46
|
-
### `loadConfigFile<T>({ cwd, name, extensions? })`
|
|
47
|
-
|
|
48
|
-
Loads the first `<name>.config.<ext>` found
|
|
49
|
-
**TS-first** — `.ts` → `.js` → `.mjs` → `.cjs` → `.json` — overridable
|
|
50
|
-
`extensions`. Returns `{ config, filepath } | null`. `null` means "no
|
|
51
|
-
file"; a file that exists but cannot be parsed /
|
|
52
|
-
export throws `ConfigFileError`.
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
### `loadConfigFile<T>({ cwd, name, extensions?, walkUp? })`
|
|
47
|
+
|
|
48
|
+
Loads the first `<name>.config.<ext>` found starting at `cwd`. Probe order
|
|
49
|
+
is **TS-first** — `.ts` → `.js` → `.mjs` → `.cjs` → `.json` — overridable
|
|
50
|
+
via `extensions`. Returns `{ config, filepath } | null`. `null` means "no
|
|
51
|
+
such file anywhere searched"; a file that exists but cannot be parsed /
|
|
52
|
+
loaded / has no default export throws `ConfigFileError`.
|
|
53
|
+
|
|
54
|
+
`walkUp: true` (off by default) adds the "nearest config" search
|
|
55
|
+
`cosmiconfig`/`postcss`/`stylelint`/ESLint's flat config all do: when `cwd`
|
|
56
|
+
has no match, check its parent, then its parent's parent, and so on —
|
|
57
|
+
stopping at the first directory containing a `.git` entry (that directory
|
|
58
|
+
is still searched before stopping) or the filesystem root. Never wanders
|
|
59
|
+
past a repo boundary into an unrelated enclosing directory. `loadLayered`
|
|
60
|
+
passes its own `walkUp` through to both the dedicated-file and
|
|
61
|
+
fallback-file lookups. Built on `@theholocron/fs-utils`'s generic
|
|
62
|
+
`findUpward`/`hasGitEntry` — not reimplemented here.
|
|
63
|
+
|
|
64
|
+
### `loadConfigFromContent<T>({ dir, content, name, extension })`
|
|
65
|
+
|
|
66
|
+
Loads config from content that didn't come from a file already on disk —
|
|
67
|
+
writes it to `<dir>/<name>.config.<extension>` first, then loads it through
|
|
68
|
+
the exact same internals `loadConfigFile` uses for a file it discovered
|
|
69
|
+
itself. `dir` matters: it's what upward `node_modules` resolution sees, so
|
|
70
|
+
content that does `import { defineConfig } from "@theholocron/cli"` only
|
|
71
|
+
resolves if `dir` sits under a tree where that package is a real
|
|
72
|
+
dependency — [`withConfigSandbox`](#withconfigsandbox-nodemodules-root-prefix--run) prepares
|
|
73
|
+
one. Callers own creating and cleaning up `dir`. Returns
|
|
74
|
+
`{ config, filepath }`; throws `ConfigFileError` the same way
|
|
75
|
+
`loadConfigFile` does on a parse/load failure.
|
|
76
|
+
|
|
77
|
+
Built for `@theholocron/sentinel`'s `validateConfig()` — it fetches a
|
|
78
|
+
repo's `holocron.config.*` from GitHub's API, not a local checkout, but
|
|
79
|
+
still needs the fetched content to execute as a real module.
|
|
80
|
+
|
|
81
|
+
### `readConfigSource({ name, fetch, extensions? })`
|
|
82
|
+
|
|
83
|
+
`loadConfigFile`'s extension probe for a config that lives somewhere other
|
|
84
|
+
than the local disk. Asks `fetch(path)` for `<name>.config.<ext>` in
|
|
85
|
+
extension-priority order (`fetch` returns the content, or `undefined` when
|
|
86
|
+
the file isn't there, and throws for any other failure) and returns the first
|
|
87
|
+
hit as `{ path, extension, content }`, or `undefined`. Hand the result to
|
|
88
|
+
`loadConfigFromContent`.
|
|
89
|
+
|
|
90
|
+
### `withConfigSandbox({ nodeModules, root?, prefix? }, run)`
|
|
91
|
+
|
|
92
|
+
Runs `run(dir)` with a throwaway directory ready to be
|
|
93
|
+
`loadConfigFromContent`'s `dir`: `node_modules` symlinked to
|
|
94
|
+
`options.nodeModules` (so the config's package imports resolve) and a
|
|
95
|
+
`{"type":"module"}` `package.json` (so `tsx` doesn't default the files to
|
|
96
|
+
CommonJS and `require()` a pure-ESM dependency). Created under the OS temp dir
|
|
97
|
+
by default — read-only deployments can't write inside a package — and removed
|
|
98
|
+
afterwards, also when `run` throws.
|
|
99
|
+
|
|
100
|
+
### `loadLayered<T>({ cwd, name, fallback?, extensions?, walkUp?, coerce? })`
|
|
55
101
|
|
|
56
102
|
`<name>.config.*` layered over the `[fallback.key]` of
|
|
57
103
|
`<fallback.file>.config.*`. Either, both, or neither may be present; the
|
|
@@ -59,11 +105,27 @@ dedicated file wins on conflict (`vitest`-over-`vite`). Returns
|
|
|
59
105
|
`{ config, filepath, sources } | null` — `sources` lists the absolute
|
|
60
106
|
paths actually merged, lowest priority first.
|
|
61
107
|
|
|
108
|
+
- `fallback.preloaded` — the `<fallback.file>.config.*` the caller has
|
|
109
|
+
already loaded (`null`: looked for, not there), used as is instead of loading
|
|
110
|
+
it again.
|
|
111
|
+
- `coerce(raw)` — turns each layer's raw value into the shape being merged
|
|
112
|
+
before the merge (a bare array standing for `{ tasks: [...] }`, say);
|
|
113
|
+
returning `undefined` treats the layer as absent.
|
|
114
|
+
|
|
115
|
+
### `mergeLayers<T>(layers)`
|
|
116
|
+
|
|
117
|
+
The merge `loadLayered` uses, for layers loaded some other way: deep-merges
|
|
118
|
+
`layers` lowest priority first, skipping `undefined`, and returns a copy — or
|
|
119
|
+
`undefined` when there is nothing to merge.
|
|
120
|
+
|
|
62
121
|
### `mergeConfig<T>(base, override)`
|
|
63
122
|
|
|
64
123
|
Deep-merge, `vite`-style: plain objects merge recursively, arrays
|
|
65
124
|
concatenate (`base` first), `undefined` in `override` is skipped, every
|
|
66
|
-
other `override` value replaces `base`. Neither input is mutated.
|
|
125
|
+
other `override` value replaces `base`. Neither input is mutated. A
|
|
126
|
+
re-export of `@theholocron/object-utils`'s `deepMerge` under this
|
|
127
|
+
package's established name — the merge logic itself has no awareness of
|
|
128
|
+
configs at all, so it lives there instead of being duplicated here.
|
|
67
129
|
|
|
68
130
|
### `createDefineConfig<T>()`
|
|
69
131
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { deepMerge as mergeConfig } from "@theholocron/object-utils";
|
|
2
|
+
//#region src/default-extensions.d.ts
|
|
3
|
+
/** Default extension probe order, highest priority first. */
|
|
4
|
+
declare const DEFAULT_EXTENSIONS: readonly ["ts", "js", "mjs", "cjs", "json"];
|
|
5
|
+
//#endregion
|
|
1
6
|
//#region src/define.d.ts
|
|
2
7
|
/**
|
|
3
8
|
* Build a typed identity `defineConfig` for a specific config shape.
|
|
@@ -29,37 +34,73 @@ declare class ConfigFileError extends Error {
|
|
|
29
34
|
filepath?: string | undefined);
|
|
30
35
|
}
|
|
31
36
|
//#endregion
|
|
32
|
-
//#region src/load.d.ts
|
|
33
|
-
/**
|
|
34
|
-
* Discover and load `<name>.config.*` files. Holocron-agnostic — no
|
|
35
|
-
* schema, no validation, no defaults. Consumers layer those on top.
|
|
36
|
-
*
|
|
37
|
-
* Probe order is **TS-first**: `.ts` → `.js` → `.mjs` → `.cjs` → `.json`.
|
|
38
|
-
* TS is loaded through `tsx`'s `tsImport` (a runtime dependency) so a
|
|
39
|
-
* typed `defineConfig` file works with no build step.
|
|
40
|
-
*/
|
|
41
|
-
/** Default extension probe order, highest priority first. */
|
|
42
|
-
declare const DEFAULT_EXTENSIONS: readonly ["ts", "js", "mjs", "cjs", "json"];
|
|
37
|
+
//#region src/load-config-file.type.d.ts
|
|
43
38
|
interface LoadConfigFileOptions {
|
|
44
|
-
/** Directory to
|
|
39
|
+
/** Directory to start looking in. */
|
|
45
40
|
cwd: string;
|
|
46
41
|
/** Base name — `"holocron"` resolves `holocron.config.{ts,js,mjs,cjs,json}`. */
|
|
47
42
|
name: string;
|
|
48
43
|
/** Override the extension probe order. */
|
|
49
44
|
extensions?: readonly string[];
|
|
45
|
+
/**
|
|
46
|
+
* Walk upward through ancestor directories when `cwd` itself has no
|
|
47
|
+
* match, stopping at the first directory containing a `.git` entry (that
|
|
48
|
+
* directory is still searched before stopping) or the filesystem root.
|
|
49
|
+
* Off by default — exact-`cwd`-only, unchanged behavior for any existing
|
|
50
|
+
* caller that doesn't pass this.
|
|
51
|
+
*/
|
|
52
|
+
walkUp?: boolean;
|
|
50
53
|
}
|
|
51
54
|
interface Loaded<T> {
|
|
52
55
|
config: T;
|
|
53
56
|
/** Absolute path of the file the value came from. */
|
|
54
57
|
filepath: string;
|
|
55
58
|
}
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/load-config-file.d.ts
|
|
56
61
|
/**
|
|
57
|
-
* Load the first `<name>.config.<ext>`
|
|
58
|
-
* `
|
|
59
|
-
*
|
|
62
|
+
* Load the first `<name>.config.<ext>` found starting at `cwd` — just
|
|
63
|
+
* `cwd` itself by default, or walking up through ancestor directories when
|
|
64
|
+
* `walkUp` is set (see the module doc comment for the stop conditions).
|
|
65
|
+
* Returns `null` when none is present anywhere searched; throws
|
|
66
|
+
* {@link ConfigFileError} when a file exists but cannot be loaded.
|
|
60
67
|
*/
|
|
61
68
|
declare function loadConfigFile<T>(opts: LoadConfigFileOptions): Promise<Loaded<T> | null>;
|
|
62
|
-
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/load-config-from-content.type.d.ts
|
|
71
|
+
interface LoadConfigFromContentOptions {
|
|
72
|
+
/**
|
|
73
|
+
* Directory to write `<name>.config.<extension>` into before loading —
|
|
74
|
+
* this is what upward `node_modules` resolution sees, so a config that
|
|
75
|
+
* does `import { defineConfig } from "@theholocron/cli"` only resolves
|
|
76
|
+
* if `dir` sits under a tree where that package is a real dependency.
|
|
77
|
+
* Callers own creating and cleaning up `dir` — this function only
|
|
78
|
+
* writes the one file into it.
|
|
79
|
+
*/
|
|
80
|
+
dir: string;
|
|
81
|
+
/** Raw file content — not necessarily sourced from a local file at all (a network fetch, a git blob, …). */
|
|
82
|
+
content: string;
|
|
83
|
+
/** Base name — `"holocron"` writes `holocron.config.<extension>`. */
|
|
84
|
+
name: string;
|
|
85
|
+
/** Which of `DEFAULT_EXTENSIONS` `content` is — determines how it's interpreted (ts/js/mjs/cjs parsed as a module, json as `JSON.parse`). */
|
|
86
|
+
extension: string;
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/load-config-from-content.d.ts
|
|
90
|
+
/**
|
|
91
|
+
* Load config from content that didn't come from a file already on disk —
|
|
92
|
+
* writes it to `<dir>/<name>.config.<extension>` first, then loads it
|
|
93
|
+
* through the exact same path {@link loadConfigFile} uses for a file it
|
|
94
|
+
* discovered itself. Exists because sourcing config content from somewhere
|
|
95
|
+
* other than the local filesystem (a GitHub API fetch, for one — see
|
|
96
|
+
* `@theholocron/sentinel`) doesn't change what loading it correctly means:
|
|
97
|
+
* a `.ts` config still needs real module resolution, `defineConfig` import
|
|
98
|
+
* included, not a re-implemented parser.
|
|
99
|
+
*/
|
|
100
|
+
declare function loadConfigFromContent<T>(opts: LoadConfigFromContentOptions): Promise<Loaded<T>>;
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/load-layered.type.d.ts
|
|
103
|
+
interface LoadLayeredOptions<T = unknown> extends LoadConfigFileOptions {
|
|
63
104
|
/**
|
|
64
105
|
* When no dedicated `<name>.config.*` exists — or in addition to it —
|
|
65
106
|
* read `<file>.config.*` and take its `[key]`. The dedicated file (if
|
|
@@ -68,7 +109,20 @@ interface LoadLayeredOptions extends LoadConfigFileOptions {
|
|
|
68
109
|
fallback?: {
|
|
69
110
|
file: string;
|
|
70
111
|
key: string;
|
|
112
|
+
/**
|
|
113
|
+
* The `<file>.config.*` already loaded by the caller (`null`: it was
|
|
114
|
+
* looked for and isn't there). Used as is instead of loading it again;
|
|
115
|
+
* leave it `undefined` to have `loadLayered` find and load it.
|
|
116
|
+
*/
|
|
117
|
+
preloaded?: Loaded<object> | null;
|
|
71
118
|
};
|
|
119
|
+
/**
|
|
120
|
+
* Turns each layer's raw value — the dedicated file's default export, and
|
|
121
|
+
* the fallback's `[key]` — into the shape being merged, before the merge.
|
|
122
|
+
* Return `undefined` to treat the layer as absent. For configs that accept
|
|
123
|
+
* shorthand (a bare array standing for `{ tasks: [...] }`).
|
|
124
|
+
*/
|
|
125
|
+
coerce?: (raw: unknown) => T | undefined;
|
|
72
126
|
}
|
|
73
127
|
interface LayeredResult<T> {
|
|
74
128
|
config: T;
|
|
@@ -77,25 +131,99 @@ interface LayeredResult<T> {
|
|
|
77
131
|
/** Absolute paths actually merged, lowest priority first. */
|
|
78
132
|
sources: string[];
|
|
79
133
|
}
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/load-layered.d.ts
|
|
80
136
|
/**
|
|
81
137
|
* `<name>.config.*` layered over the `[fallback.key]` of
|
|
82
138
|
* `<fallback.file>.config.*`. Either, both, or neither may be present;
|
|
83
139
|
* the dedicated file wins on conflict (vitest-over-vite). Returns `null`
|
|
84
140
|
* when neither source resolves.
|
|
85
141
|
*/
|
|
86
|
-
declare function loadLayered<T>(opts: LoadLayeredOptions): Promise<LayeredResult<T> | null>;
|
|
142
|
+
declare function loadLayered<T>(opts: LoadLayeredOptions<T>): Promise<LayeredResult<T> | null>;
|
|
87
143
|
//#endregion
|
|
88
|
-
//#region src/merge.d.ts
|
|
144
|
+
//#region src/merge-layers.d.ts
|
|
89
145
|
/**
|
|
90
|
-
* Deep-
|
|
146
|
+
* Deep-merges config layers, lowest priority first (a later layer wins on
|
|
147
|
+
* scalars; arrays concatenate; nested objects merge). `undefined` layers are
|
|
148
|
+
* skipped. Returns `undefined` when there is no layer to merge. The result is
|
|
149
|
+
* a copy for object layers — it never aliases a layer.
|
|
150
|
+
*
|
|
151
|
+
* The one merge `loadLayered` and every caller layering configs it loaded
|
|
152
|
+
* some other way (content from an API, say) use, so layering means the same
|
|
153
|
+
* thing everywhere.
|
|
154
|
+
*/
|
|
155
|
+
declare function mergeLayers<T>(layers: readonly (T | undefined)[]): T | undefined;
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/read-config-source.type.d.ts
|
|
158
|
+
/** A config file's content fetched from somewhere other than the local disk. */
|
|
159
|
+
interface ConfigSource {
|
|
160
|
+
/** The file name probed, e.g. `holocron.config.ts` — relative, as `fetch` was asked for it. */
|
|
161
|
+
path: string;
|
|
162
|
+
/** Which extension matched — what {@link loadConfigFromContent} needs as `extension`. */
|
|
163
|
+
extension: string;
|
|
164
|
+
/** The raw file content. */
|
|
165
|
+
content: string;
|
|
166
|
+
}
|
|
167
|
+
interface ReadConfigSourceOptions {
|
|
168
|
+
/** Base name — `"holocron"` probes `holocron.config.{ts,js,mjs,cjs,json}`. */
|
|
169
|
+
name: string;
|
|
170
|
+
/**
|
|
171
|
+
* Fetches one file by relative path (`holocron.config.ts`): its content, or
|
|
172
|
+
* `undefined` when it doesn't exist. Throw for anything else (a network
|
|
173
|
+
* failure, a permission error) — that propagates rather than reading as "absent".
|
|
174
|
+
*/
|
|
175
|
+
fetch: (path: string) => Promise<string | undefined>;
|
|
176
|
+
/** Override the extension probe order. */
|
|
177
|
+
extensions?: readonly string[];
|
|
178
|
+
}
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/read-config-source.d.ts
|
|
181
|
+
/**
|
|
182
|
+
* {@link probeExtensions} for a config that lives somewhere other than the
|
|
183
|
+
* local disk (a GitHub API, a git blob): probes `<name>.config.<ext>` in
|
|
184
|
+
* extension-priority order through `options.fetch`, returning the first file
|
|
185
|
+
* that exists, or `undefined` when none does. Pass the result to
|
|
186
|
+
* `loadConfigFromContent` to execute it exactly as a local file would be.
|
|
187
|
+
*/
|
|
188
|
+
declare function readConfigSource(options: ReadConfigSourceOptions): Promise<ConfigSource | undefined>;
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/with-config-sandbox.type.d.ts
|
|
191
|
+
interface ConfigSandboxOptions {
|
|
192
|
+
/**
|
|
193
|
+
* A real `node_modules` directory to make visible from the sandbox. A config
|
|
194
|
+
* that does `import { defineConfig } from "@theholocron/cli"` only resolves
|
|
195
|
+
* if the directory it sits in has that package reachable by Node's upward
|
|
196
|
+
* `node_modules` search, so the sandbox gets a symlink to this one (not a
|
|
197
|
+
* copy — nothing is duplicated).
|
|
198
|
+
*/
|
|
199
|
+
nodeModules: string;
|
|
200
|
+
/** Where to create the sandbox. Defaults to the OS temp dir. */
|
|
201
|
+
root?: string;
|
|
202
|
+
/** Prefix for the sandbox directory's name. */
|
|
203
|
+
prefix?: string;
|
|
204
|
+
}
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/with-config-sandbox.d.ts
|
|
207
|
+
/**
|
|
208
|
+
* Runs `run` with a throwaway directory prepared for
|
|
209
|
+
* {@link loadConfigFromContent}'s `dir`, and removes it afterwards (also when
|
|
210
|
+
* `run` throws).
|
|
211
|
+
*
|
|
212
|
+
* The directory is made for a TS/JS config that imports packages:
|
|
91
213
|
*
|
|
92
|
-
* -
|
|
93
|
-
*
|
|
94
|
-
* -
|
|
95
|
-
*
|
|
214
|
+
* - `node_modules` is a symlink to `options.nodeModules`, so those imports
|
|
215
|
+
* resolve as if the config sat next to that directory.
|
|
216
|
+
* - A `package.json` of `{"type":"module"}` is written into it. Node decides
|
|
217
|
+
* whether a `.ts`/`.js` file is ESM or CommonJS from the nearest ancestor
|
|
218
|
+
* `package.json`'s `type`, defaulting to CommonJS — and `tsx`'s loader,
|
|
219
|
+
* correctly honouring that, would then `require()` a pure-ESM dependency
|
|
220
|
+
* such as `@theholocron/cli`'s `dist/index.mjs`, which Node rejects outright
|
|
221
|
+
* ("require() of ES Module … not supported").
|
|
96
222
|
*
|
|
97
|
-
*
|
|
223
|
+
* The directory lives under the OS temp dir by default rather than inside any
|
|
224
|
+
* package: read-only deployments (Vercel's Node.js Functions ship the bundle
|
|
225
|
+
* read-only under `/var/task`) can't create a directory there.
|
|
98
226
|
*/
|
|
99
|
-
declare function
|
|
227
|
+
declare function withConfigSandbox<R>(options: ConfigSandboxOptions, run: (dir: string) => Promise<R>): Promise<R>;
|
|
100
228
|
//#endregion
|
|
101
|
-
export { ConfigFileError, DEFAULT_EXTENSIONS, type LayeredResult, type LoadConfigFileOptions, type LoadLayeredOptions, type Loaded, createDefineConfig, loadConfigFile, loadLayered, mergeConfig };
|
|
229
|
+
export { ConfigFileError, type ConfigSandboxOptions, type ConfigSource, DEFAULT_EXTENSIONS, type LayeredResult, type LoadConfigFileOptions, type LoadConfigFromContentOptions, type LoadLayeredOptions, type Loaded, type ReadConfigSourceOptions, createDefineConfig, loadConfigFile, loadConfigFromContent, loadLayered, mergeConfig, mergeLayers, readConfigSource, withConfigSandbox };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { findUpward, hasGitEntry } from "@theholocron/fs-utils";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { mkdir, mkdtemp, readFile, rm, stat, symlink, writeFile } from "node:fs/promises";
|
|
3
4
|
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { errorMessage } from "@theholocron/misc-utils";
|
|
6
|
+
import { deepMerge, deepMerge as mergeConfig } from "@theholocron/object-utils";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
//#region src/default-extensions.ts
|
|
9
|
+
/** Default extension probe order, highest priority first. */
|
|
10
|
+
const DEFAULT_EXTENSIONS = [
|
|
11
|
+
"ts",
|
|
12
|
+
"js",
|
|
13
|
+
"mjs",
|
|
14
|
+
"cjs",
|
|
15
|
+
"json"
|
|
16
|
+
];
|
|
17
|
+
//#endregion
|
|
4
18
|
//#region src/define.ts
|
|
5
19
|
/**
|
|
6
20
|
* Build a typed identity `defineConfig` for a specific config shape.
|
|
@@ -34,69 +48,166 @@ var ConfigFileError = class extends Error {
|
|
|
34
48
|
}
|
|
35
49
|
};
|
|
36
50
|
//#endregion
|
|
37
|
-
//#region src/
|
|
51
|
+
//#region src/is-file.ts
|
|
52
|
+
async function isFile(path) {
|
|
53
|
+
try {
|
|
54
|
+
return (await stat(path)).isFile();
|
|
55
|
+
} catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/load-file.ts
|
|
38
61
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* -
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* Neither input is mutated.
|
|
62
|
+
* The actual file-loading mechanics `load.ts`'s exported functions share:
|
|
63
|
+
* JSON parsing, dynamic `import()` (TS via `tsx`'s persistent ESM loader,
|
|
64
|
+
* JS/MJS/CJS natively), and unwrapping the CJS/ESM default-export
|
|
65
|
+
* double-wrap `tsx`'s transform produces. Kept separate from `load.ts`'s
|
|
66
|
+
* own orchestration (which directory, which extension order, upward
|
|
67
|
+
* search) — this file only knows "given one exact filepath and
|
|
68
|
+
* extension, load it," always throwing {@link ConfigFileError} on failure.
|
|
47
69
|
*/
|
|
48
|
-
function
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
70
|
+
async function loadFile(filepath, ext) {
|
|
71
|
+
if (ext === "json") return loadJson(filepath);
|
|
72
|
+
return extractDefault(filepath, await importModule(filepath, ext));
|
|
73
|
+
}
|
|
74
|
+
async function loadJson(filepath) {
|
|
75
|
+
const text = await readFile(filepath, "utf8");
|
|
76
|
+
try {
|
|
77
|
+
return JSON.parse(text);
|
|
78
|
+
} catch (err) {
|
|
79
|
+
throw new ConfigFileError(`${filepath} is not valid JSON: ${errorMessage(err)}`, filepath);
|
|
57
80
|
}
|
|
58
|
-
return out;
|
|
59
81
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
82
|
+
let tsRegistered;
|
|
83
|
+
async function registerTsx() {
|
|
84
|
+
const { register } = await import("tsx/esm/api");
|
|
85
|
+
register();
|
|
64
86
|
}
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region src/load.ts
|
|
67
87
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* typed `defineConfig` file works with no build step.
|
|
88
|
+
* Import `filepath`. For `.ts`, `tsx`'s ESM loader is registered first, once
|
|
89
|
+
* per process (lazily — only when a `.ts` file is actually loaded).
|
|
90
|
+
* `register()` is used over `tsImport()` because a single run may load
|
|
91
|
+
* several TS files (`loadLayered` reads a dedicated file *and* a parent
|
|
92
|
+
* file) and `tsImport`'s one-off register/unregister cycle is not reentrant.
|
|
74
93
|
*/
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
94
|
+
async function importModule(filepath, ext) {
|
|
95
|
+
if (ext === "ts") {
|
|
96
|
+
tsRegistered ??= registerTsx();
|
|
97
|
+
await tsRegistered;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
return await import(pathToFileURL(filepath).href);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
throw new ConfigFileError(`could not load ${filepath}: ${errorMessage(err)}`, filepath);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Unwrap `export default`. `tsx` CJS-transforms `export default x` into
|
|
107
|
+
* `exports.default = x`, which dynamic import wraps as
|
|
108
|
+
* `{ default: { __esModule: true, default: x } }` — strip the extra layer
|
|
109
|
+
* when present so ESM and CJS outputs both resolve.
|
|
110
|
+
*/
|
|
111
|
+
function extractDefault(filepath, mod) {
|
|
112
|
+
const outer = mod.default;
|
|
113
|
+
const raw = outer?.__esModule === true ? outer.default : outer;
|
|
114
|
+
if (raw === void 0 || raw === null) throw new ConfigFileError(`${filepath} must have a default export (use \`export default defineConfig({…})\`)`, filepath);
|
|
115
|
+
return raw;
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/probe-extensions.ts
|
|
83
119
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
120
|
+
* Probe `dir` for `<name>.config.<ext>` in extension-priority order,
|
|
121
|
+
* loading and returning the first match. The one piece of per-directory
|
|
122
|
+
* orchestration `loadConfigFile` needs on both its non-walking and
|
|
123
|
+
* walking paths — pulled out so it can be unit-tested directly instead of
|
|
124
|
+
* only through `loadConfigFile`'s full probe/walk behavior.
|
|
87
125
|
*/
|
|
88
|
-
async function
|
|
89
|
-
const extensions = opts.extensions ?? DEFAULT_EXTENSIONS;
|
|
126
|
+
async function probeExtensions(dir, name, extensions) {
|
|
90
127
|
for (const ext of extensions) {
|
|
91
|
-
const filepath = join(
|
|
128
|
+
const filepath = join(dir, `${name}.config.${ext}`);
|
|
92
129
|
if (!await isFile(filepath)) continue;
|
|
93
130
|
return {
|
|
94
131
|
config: await loadFile(filepath, ext),
|
|
95
132
|
filepath
|
|
96
133
|
};
|
|
97
134
|
}
|
|
98
|
-
return null;
|
|
99
135
|
}
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/load-config-file.ts
|
|
138
|
+
/**
|
|
139
|
+
* Discover and load `<name>.config.*` files, or load one from content that
|
|
140
|
+
* didn't come from a file at all ({@link loadConfigFromContent}).
|
|
141
|
+
* Holocron-agnostic — no schema, no validation, no defaults. Consumers
|
|
142
|
+
* layer those on top.
|
|
143
|
+
*
|
|
144
|
+
* Probe order is **TS-first**: `.ts` → `.js` → `.mjs` → `.cjs` → `.json`.
|
|
145
|
+
* TS is loaded through `tsx`'s `tsImport` (a runtime dependency) so a
|
|
146
|
+
* typed `defineConfig` file works with no build step.
|
|
147
|
+
*
|
|
148
|
+
* `walkUp` (off by default) adds the "nearest config" search
|
|
149
|
+
* cosmiconfig/postcss/stylelint/ESLint's flat config all do: when `cwd`
|
|
150
|
+
* itself has no match, check its parent, then its parent's parent, and so
|
|
151
|
+
* on — stopping at the first directory containing a `.git` entry (checked
|
|
152
|
+
* after that directory's own config probe, so the project root itself is
|
|
153
|
+
* still searched) or the filesystem root. Never wanders past a repo
|
|
154
|
+
* boundary into an unrelated enclosing directory. Built on
|
|
155
|
+
* `@theholocron/fs-utils`'s `findUpward`/`hasGitEntry` — the same
|
|
156
|
+
* upward-walk shape `@theholocron/sentinel`'s own `findPackageRoot` needs,
|
|
157
|
+
* only with a different predicate.
|
|
158
|
+
*/
|
|
159
|
+
/**
|
|
160
|
+
* Load the first `<name>.config.<ext>` found starting at `cwd` — just
|
|
161
|
+
* `cwd` itself by default, or walking up through ancestor directories when
|
|
162
|
+
* `walkUp` is set (see the module doc comment for the stop conditions).
|
|
163
|
+
* Returns `null` when none is present anywhere searched; throws
|
|
164
|
+
* {@link ConfigFileError} when a file exists but cannot be loaded.
|
|
165
|
+
*/
|
|
166
|
+
async function loadConfigFile(opts) {
|
|
167
|
+
const extensions = opts.extensions ?? DEFAULT_EXTENSIONS;
|
|
168
|
+
const checkDir = (dir) => probeExtensions(dir, opts.name, extensions);
|
|
169
|
+
if (!opts.walkUp) return await checkDir(opts.cwd) ?? null;
|
|
170
|
+
return await findUpward(opts.cwd, checkDir, hasGitEntry) ?? null;
|
|
171
|
+
}
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/load-config-from-content.ts
|
|
174
|
+
/**
|
|
175
|
+
* Load config from content that didn't come from a file already on disk —
|
|
176
|
+
* writes it to `<dir>/<name>.config.<extension>` first, then loads it
|
|
177
|
+
* through the exact same path {@link loadConfigFile} uses for a file it
|
|
178
|
+
* discovered itself. Exists because sourcing config content from somewhere
|
|
179
|
+
* other than the local filesystem (a GitHub API fetch, for one — see
|
|
180
|
+
* `@theholocron/sentinel`) doesn't change what loading it correctly means:
|
|
181
|
+
* a `.ts` config still needs real module resolution, `defineConfig` import
|
|
182
|
+
* included, not a re-implemented parser.
|
|
183
|
+
*/
|
|
184
|
+
async function loadConfigFromContent(opts) {
|
|
185
|
+
const filepath = join(opts.dir, `${opts.name}.config.${opts.extension}`);
|
|
186
|
+
await writeFile(filepath, opts.content, "utf8");
|
|
187
|
+
return {
|
|
188
|
+
config: await loadFile(filepath, opts.extension),
|
|
189
|
+
filepath
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/merge-layers.ts
|
|
194
|
+
/**
|
|
195
|
+
* Deep-merges config layers, lowest priority first (a later layer wins on
|
|
196
|
+
* scalars; arrays concatenate; nested objects merge). `undefined` layers are
|
|
197
|
+
* skipped. Returns `undefined` when there is no layer to merge. The result is
|
|
198
|
+
* a copy for object layers — it never aliases a layer.
|
|
199
|
+
*
|
|
200
|
+
* The one merge `loadLayered` and every caller layering configs it loaded
|
|
201
|
+
* some other way (content from an API, say) use, so layering means the same
|
|
202
|
+
* thing everywhere.
|
|
203
|
+
*/
|
|
204
|
+
function mergeLayers(layers) {
|
|
205
|
+
const present = layers.filter((layer) => layer !== void 0);
|
|
206
|
+
if (present.length === 0) return void 0;
|
|
207
|
+
return present.reduce((acc, layer) => deepMerge(acc, layer), {});
|
|
208
|
+
}
|
|
209
|
+
//#endregion
|
|
210
|
+
//#region src/load-layered.ts
|
|
100
211
|
/**
|
|
101
212
|
* `<name>.config.*` layered over the `[fallback.key]` of
|
|
102
213
|
* `<fallback.file>.config.*`. Either, both, or neither may be present;
|
|
@@ -104,88 +215,94 @@ async function loadConfigFile(opts) {
|
|
|
104
215
|
* when neither source resolves.
|
|
105
216
|
*/
|
|
106
217
|
async function loadLayered(opts) {
|
|
218
|
+
const coerce = opts.coerce ?? ((raw) => raw);
|
|
107
219
|
const dedicated = await loadConfigFile(opts);
|
|
220
|
+
let top;
|
|
221
|
+
let topFile = null;
|
|
222
|
+
if (dedicated) {
|
|
223
|
+
top = coerce(dedicated.config);
|
|
224
|
+
if (top !== void 0) topFile = dedicated.filepath;
|
|
225
|
+
}
|
|
108
226
|
let base;
|
|
109
227
|
let baseFile = null;
|
|
110
228
|
if (opts.fallback) {
|
|
111
|
-
const parent = await loadConfigFile({
|
|
229
|
+
const parent = opts.fallback.preloaded !== void 0 ? opts.fallback.preloaded : await loadConfigFile({
|
|
112
230
|
cwd: opts.cwd,
|
|
113
231
|
name: opts.fallback.file,
|
|
114
|
-
extensions: opts.extensions
|
|
232
|
+
extensions: opts.extensions,
|
|
233
|
+
walkUp: opts.walkUp
|
|
115
234
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
235
|
+
const raw = (parent?.config)?.[opts.fallback.key];
|
|
236
|
+
if (parent && raw !== void 0) {
|
|
237
|
+
base = coerce(raw);
|
|
238
|
+
if (base !== void 0) baseFile = parent.filepath;
|
|
119
239
|
}
|
|
120
240
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const sources = [baseFile,
|
|
241
|
+
const config = mergeLayers([base, top]);
|
|
242
|
+
if (config === void 0) return null;
|
|
243
|
+
const sources = [baseFile, topFile].filter((s) => s !== null);
|
|
124
244
|
return {
|
|
125
245
|
config,
|
|
126
|
-
filepath:
|
|
246
|
+
filepath: topFile ?? baseFile,
|
|
127
247
|
sources
|
|
128
248
|
};
|
|
129
249
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return extractDefault(filepath, ext === "ts" ? await importTs(filepath) : await importModule(filepath));
|
|
133
|
-
}
|
|
134
|
-
async function loadJson(filepath) {
|
|
135
|
-
const text = await readFile(filepath, "utf8");
|
|
136
|
-
try {
|
|
137
|
-
return JSON.parse(text);
|
|
138
|
-
} catch (err) {
|
|
139
|
-
throw new ConfigFileError(`${filepath} is not valid JSON: ${message(err)}`, filepath);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
async function importModule(filepath) {
|
|
143
|
-
try {
|
|
144
|
-
return await import(pathToFileURL(filepath).href);
|
|
145
|
-
} catch (err) {
|
|
146
|
-
throw new ConfigFileError(`could not load ${filepath}: ${message(err)}`, filepath);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
let tsRegistered;
|
|
150
|
-
async function registerTsx() {
|
|
151
|
-
const { register } = await import("tsx/esm/api");
|
|
152
|
-
register();
|
|
153
|
-
}
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/read-config-source.ts
|
|
154
252
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* `
|
|
253
|
+
* {@link probeExtensions} for a config that lives somewhere other than the
|
|
254
|
+
* local disk (a GitHub API, a git blob): probes `<name>.config.<ext>` in
|
|
255
|
+
* extension-priority order through `options.fetch`, returning the first file
|
|
256
|
+
* that exists, or `undefined` when none does. Pass the result to
|
|
257
|
+
* `loadConfigFromContent` to execute it exactly as a local file would be.
|
|
160
258
|
*/
|
|
161
|
-
async function
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
259
|
+
async function readConfigSource(options) {
|
|
260
|
+
for (const extension of options.extensions ?? DEFAULT_EXTENSIONS) {
|
|
261
|
+
const path = `${options.name}.config.${extension}`;
|
|
262
|
+
const content = await options.fetch(path);
|
|
263
|
+
if (content !== void 0) return {
|
|
264
|
+
path,
|
|
265
|
+
extension,
|
|
266
|
+
content
|
|
267
|
+
};
|
|
168
268
|
}
|
|
169
269
|
}
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/with-config-sandbox.ts
|
|
170
272
|
/**
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* `
|
|
174
|
-
*
|
|
273
|
+
* Runs `run` with a throwaway directory prepared for
|
|
274
|
+
* {@link loadConfigFromContent}'s `dir`, and removes it afterwards (also when
|
|
275
|
+
* `run` throws).
|
|
276
|
+
*
|
|
277
|
+
* The directory is made for a TS/JS config that imports packages:
|
|
278
|
+
*
|
|
279
|
+
* - `node_modules` is a symlink to `options.nodeModules`, so those imports
|
|
280
|
+
* resolve as if the config sat next to that directory.
|
|
281
|
+
* - A `package.json` of `{"type":"module"}` is written into it. Node decides
|
|
282
|
+
* whether a `.ts`/`.js` file is ESM or CommonJS from the nearest ancestor
|
|
283
|
+
* `package.json`'s `type`, defaulting to CommonJS — and `tsx`'s loader,
|
|
284
|
+
* correctly honouring that, would then `require()` a pure-ESM dependency
|
|
285
|
+
* such as `@theholocron/cli`'s `dist/index.mjs`, which Node rejects outright
|
|
286
|
+
* ("require() of ES Module … not supported").
|
|
287
|
+
*
|
|
288
|
+
* The directory lives under the OS temp dir by default rather than inside any
|
|
289
|
+
* package: read-only deployments (Vercel's Node.js Functions ship the bundle
|
|
290
|
+
* read-only under `/var/task`) can't create a directory there.
|
|
175
291
|
*/
|
|
176
|
-
function
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
return raw;
|
|
181
|
-
}
|
|
182
|
-
async function isFile(path) {
|
|
292
|
+
async function withConfigSandbox(options, run) {
|
|
293
|
+
const root = options.root ?? tmpdir();
|
|
294
|
+
await mkdir(root, { recursive: true });
|
|
295
|
+
const dir = await mkdtemp(join(root, options.prefix ?? "datapad-"));
|
|
183
296
|
try {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return
|
|
297
|
+
await symlink(options.nodeModules, join(dir, "node_modules"), "dir");
|
|
298
|
+
await writeFile(join(dir, "package.json"), JSON.stringify({ type: "module" }), "utf8");
|
|
299
|
+
return await run(dir);
|
|
300
|
+
} finally {
|
|
301
|
+
await rm(dir, {
|
|
302
|
+
recursive: true,
|
|
303
|
+
force: true
|
|
304
|
+
});
|
|
187
305
|
}
|
|
188
306
|
}
|
|
189
|
-
const message = (err) => err instanceof Error ? err.message : String(err);
|
|
190
307
|
//#endregion
|
|
191
|
-
export { ConfigFileError, DEFAULT_EXTENSIONS, createDefineConfig, loadConfigFile, loadLayered, mergeConfig };
|
|
308
|
+
export { ConfigFileError, DEFAULT_EXTENSIONS, createDefineConfig, loadConfigFile, loadConfigFromContent, loadLayered, mergeConfig, mergeLayers, readConfigSource, withConfigSandbox };
|
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.151",
|
|
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",
|
|
@@ -32,13 +32,16 @@
|
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
|
+
"@theholocron/fs-utils": "^1.13.0",
|
|
36
|
+
"@theholocron/misc-utils": "^1.13.0",
|
|
37
|
+
"@theholocron/object-utils": "^1.13.0",
|
|
35
38
|
"tsx": "4.23.12"
|
|
36
39
|
},
|
|
37
40
|
"devDependencies": {
|
|
38
|
-
"@theholocron/eslint-config": "^8.
|
|
39
|
-
"@theholocron/tsconfig": "^8.
|
|
40
|
-
"@theholocron/tsdown-config": "^8.
|
|
41
|
-
"@theholocron/vitest-config": "^8.
|
|
41
|
+
"@theholocron/eslint-config": "^8.7.1",
|
|
42
|
+
"@theholocron/tsconfig": "^8.7.1",
|
|
43
|
+
"@theholocron/tsdown-config": "^8.7.1",
|
|
44
|
+
"@theholocron/vitest-config": "^8.7.0",
|
|
42
45
|
"@types/node": "^26",
|
|
43
46
|
"@vitest/coverage-v8": "^4.1.11",
|
|
44
47
|
"@vitest/eslint-plugin": "^1.6.27",
|