@wtf/project-config 1.1.0 → 2.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 +44 -27
- package/dist/env.js +51 -9
- package/dist/env.js.map +1 -1
- package/dist/errors.d.ts +26 -8
- package/dist/errors.js +19 -4
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js.map +1 -1
- package/dist/load.d.ts +56 -30
- package/dist/load.js +25 -42
- package/dist/load.js.map +1 -1
- package/dist/locate.d.ts +3 -2
- package/dist/locate.js +5 -2
- package/dist/locate.js.map +1 -1
- package/dist/named.d.ts +40 -13
- package/dist/named.js +98 -32
- package/dist/named.js.map +1 -1
- package/dist/node.d.ts +11 -24
- package/dist/node.js +56 -79
- package/dist/node.js.map +1 -1
- package/dist/section.d.ts +5 -5
- package/dist/section.js +5 -5
- package/dist/section.js.map +1 -1
- package/dist/write.js +5 -4
- package/dist/write.js.map +1 -1
- package/package.json +4 -4
package/dist/named.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsonSchemaOf } from "./env.js";
|
|
2
2
|
import { NamedError } from "./errors.js";
|
|
3
3
|
import { issuesOf, issuesOfSync } from "./section.js";
|
|
4
|
+
import { textOfPath } from "./text.js";
|
|
4
5
|
/** Whether `value` is an object of keys, as JSON's: no list, and no instance of a class. */
|
|
5
6
|
const isPlain = (value) => {
|
|
6
7
|
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
@@ -40,77 +41,129 @@ export function factoryOf(value) {
|
|
|
40
41
|
const issuesText = (issues) => issues
|
|
41
42
|
.map(({ path, message }) => (path.length === 0 ? message : `${path.join('.')}: ${message}`))
|
|
42
43
|
.join('; ');
|
|
43
|
-
/**
|
|
44
|
-
|
|
44
|
+
/** The module and the export of `reference`, as a `NamedError` names them. */
|
|
45
|
+
const moduleDetails = (reference) => ({
|
|
46
|
+
module: reference.module,
|
|
47
|
+
export: reference.export ?? 'default'
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
50
|
+
* A `NamedError` of `code` at `place`: its message after the path of its set or its reference, of
|
|
51
|
+
* one that is not empty, and the path, the set and the entry of the place beside `details`.
|
|
52
|
+
*/
|
|
53
|
+
function namedError(code, message, { at, where, set, entry }, details = {}, options) {
|
|
54
|
+
return new NamedError(code, where.length === 0 ? message : `${textOfPath(where)}: ${message}`, {
|
|
55
|
+
...details,
|
|
56
|
+
at,
|
|
57
|
+
...(set === undefined ? {} : { set }),
|
|
58
|
+
...(entry === undefined ? {} : { entry })
|
|
59
|
+
}, options);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* `value`, checked by `check`, else a `NamedError` of `INVALID_NAMED_VALUE` at `place`, whose
|
|
63
|
+
* message is `failing` and the issues, and `details` beside it.
|
|
64
|
+
*/
|
|
65
|
+
async function checked(value, check, failing, place, details = {}) {
|
|
45
66
|
if (check === undefined)
|
|
46
67
|
return value;
|
|
47
68
|
const { issues, value: read } = await issuesOf(value, check);
|
|
48
69
|
if (issues.length === 0)
|
|
49
70
|
return read;
|
|
50
|
-
throw
|
|
71
|
+
throw namedError('INVALID_NAMED_VALUE', `${failing}: ${issuesText(issues)}.`, place, {
|
|
72
|
+
...details,
|
|
51
73
|
issues
|
|
52
74
|
});
|
|
53
75
|
}
|
|
54
76
|
/**
|
|
55
77
|
* The export `reference.export`, `default` by default, of the module `reference.module`, loaded by
|
|
56
|
-
* `load`: a `NamedError` of `UNLOADABLE_MODULE` of a module that fails to load, or of no
|
|
57
|
-
* and of `MISSING_EXPORT` of an export it has not.
|
|
78
|
+
* `load`: a `NamedError` at `place` of `UNLOADABLE_MODULE` of a module that fails to load, or of no
|
|
79
|
+
* loader, and of `MISSING_EXPORT` of an export it has not, each of the module and the export.
|
|
58
80
|
*/
|
|
59
|
-
async function exportOf(reference, load) {
|
|
81
|
+
async function exportOf(reference, load, place) {
|
|
60
82
|
const { module } = reference;
|
|
61
83
|
const name = reference.export ?? 'default';
|
|
62
|
-
const details =
|
|
84
|
+
const details = moduleDetails(reference);
|
|
85
|
+
const as = place.as === undefined ? '' : ` as ${place.as}`;
|
|
63
86
|
if (load === undefined)
|
|
64
|
-
throw
|
|
87
|
+
throw namedError('UNLOADABLE_MODULE', `The module ${module} cannot be loaded, for its export ${name}${as}: no loader of modules is given.`, place, details);
|
|
65
88
|
let loaded;
|
|
66
89
|
try {
|
|
67
90
|
loaded = await load(module);
|
|
68
91
|
}
|
|
69
92
|
catch (error) {
|
|
70
|
-
throw
|
|
93
|
+
throw namedError('UNLOADABLE_MODULE', `The module ${module} cannot be loaded, for its export ${name}${as}: ${error instanceof Error ? error.message : String(error)}`, place, details, { cause: error });
|
|
71
94
|
}
|
|
72
95
|
const exports = loaded;
|
|
73
96
|
const found = typeof exports === 'object' && exports !== null ? exports[name] : undefined;
|
|
74
97
|
if (found === undefined)
|
|
75
|
-
throw
|
|
98
|
+
throw namedError('MISSING_EXPORT', `The module ${module} has no export ${name}${as}.`, place, details);
|
|
76
99
|
return found;
|
|
77
100
|
}
|
|
78
101
|
/** What a module's export is, as an error names it. */
|
|
79
102
|
const exportText = ({ module, export: name = 'default' }) => `The export ${name} of ${module}`;
|
|
80
|
-
/**
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
103
|
+
/**
|
|
104
|
+
* The entry of `value` of the name `name`, at `place`: a function as it is, or, of `checkResults`,
|
|
105
|
+
* one whose results `check` checks; else the value checked, of {@link factoryOf}.
|
|
106
|
+
*/
|
|
107
|
+
async function entryOf(value, name, place, { load, check, checkResults, kind = 'entry' }) {
|
|
108
|
+
const reference = isModule(value) ? value : undefined;
|
|
109
|
+
const details = reference === undefined ? {} : moduleDetails(reference);
|
|
110
|
+
const found = reference === undefined
|
|
111
|
+
? value
|
|
112
|
+
: await exportOf(reference, load, { ...place, as: `the ${kind} ${name}` });
|
|
113
|
+
const what = reference === undefined
|
|
114
|
+
? `The ${kind} ${name}`
|
|
115
|
+
: `${exportText(reference)}, the ${kind} ${name},`;
|
|
116
|
+
if (typeof found === 'function') {
|
|
117
|
+
const factory = found;
|
|
118
|
+
if (checkResults !== true || check === undefined)
|
|
119
|
+
return factory;
|
|
120
|
+
return async (options) => checked(await factory(options), check, `${what} gives a value that is not valid`, place, details);
|
|
121
|
+
}
|
|
122
|
+
return factoryOf(await checked(found, check, `${what} is not valid`, place, details));
|
|
89
123
|
}
|
|
90
124
|
/**
|
|
91
125
|
* The entries of `sets`, each a factory, by their names: of each set in order, a map of entries by
|
|
92
126
|
* name or the export of a module that is one, a later set over an earlier one by name, so a
|
|
93
127
|
* project's own are over those of a package; none of `undefined`. Each entry is a function, a
|
|
94
128
|
* factory as it is, or a plain value, checked by `check`, of {@link factoryOf}; or the export of
|
|
95
|
-
* a module that is one of those, loaded by `load`.
|
|
129
|
+
* a module that is one of those, loaded by `load`. Of `checkResults`, `check` checks what each
|
|
130
|
+
* function entry gives too, each time it is called. Each module is loaded at once, so a set that
|
|
96
131
|
* fails does before any entry is used. Fails with a `NamedError` of `UNLOADABLE_MODULE` or
|
|
97
132
|
* `MISSING_EXPORT` of a module, and of `INVALID_NAMED_VALUE` of a set that is no map, or a value
|
|
98
|
-
* `check` refuses.
|
|
133
|
+
* `check` refuses. The error names where it failed: its `at` is the path `at` gives, the index of
|
|
134
|
+
* the set in an array of sets, its `set`, and the name of the entry, its `entry`, as
|
|
135
|
+
* `['formatters', '1', 'wide']`, and its message starts with the path of the set; and its `module`
|
|
136
|
+
* and `export` are those of a set or an entry a module exports.
|
|
99
137
|
*/
|
|
100
138
|
export async function resolveNamedSet(sets, options = {}) {
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
|
|
139
|
+
const { at = [], kind = 'entry' } = options;
|
|
140
|
+
const many = Array.isArray(sets);
|
|
141
|
+
const list = sets === undefined ? [] : many ? sets : [sets];
|
|
142
|
+
const resolved = await Promise.all(list.map(async (set, index) => {
|
|
143
|
+
const of = many ? [...at, String(index)] : at;
|
|
144
|
+
const place = { at: of, where: of, ...(many ? { set: index } : {}) };
|
|
145
|
+
const reference = isModule(set) ? set : undefined;
|
|
146
|
+
const map = reference === undefined
|
|
147
|
+
? set
|
|
148
|
+
: await exportOf(reference, options.load, { ...place, as: `a set of ${kind}s` });
|
|
104
149
|
if (!isPlain(map))
|
|
105
|
-
throw
|
|
150
|
+
throw namedError('INVALID_NAMED_VALUE', `${reference === undefined ? textOf(set) : exportText(reference)} is no set: give an object of each ${kind} by its name.`, place, reference === undefined ? {} : moduleDetails(reference));
|
|
106
151
|
const entries = await Promise.all(Object.entries(map).map(async ([name, value]) => [
|
|
107
152
|
name,
|
|
108
|
-
await entryOf(value, name, options)
|
|
153
|
+
await entryOf(value, name, { ...place, at: [...place.at, name], entry: name }, options)
|
|
109
154
|
]));
|
|
110
155
|
return Object.fromEntries(entries);
|
|
111
156
|
}));
|
|
112
157
|
return Object.assign({}, ...resolved);
|
|
113
158
|
}
|
|
159
|
+
/** What `error` names but its place, as a `NamedError` that holds it gives it again. */
|
|
160
|
+
const detailsOf = (error) => ({
|
|
161
|
+
...(error.unknown === undefined ? {} : { name: error.unknown }),
|
|
162
|
+
...(error.module === undefined ? {} : { module: error.module }),
|
|
163
|
+
...(error.export === undefined ? {} : { export: error.export }),
|
|
164
|
+
names: error.names,
|
|
165
|
+
issues: error.issues
|
|
166
|
+
});
|
|
114
167
|
/**
|
|
115
168
|
* The value `reference` is: of a name, the entry of that name of `names`, given no options; of an
|
|
116
169
|
* object of the `key`, the entry of the name under it, given the options beside it but those of
|
|
@@ -118,12 +171,15 @@ export async function resolveNamedSet(sets, options = {}) {
|
|
|
118
171
|
* `load`. With a `check`, the value is checked by it. Fails with a `NamedError`: `UNKNOWN_NAME` of
|
|
119
172
|
* a name of no entry, whose message lists the names there are; `UNLOADABLE_MODULE` and
|
|
120
173
|
* `MISSING_EXPORT` of a module; and `INVALID_NAMED_VALUE` of a reference of none of the forms, or
|
|
121
|
-
* a value `check` refuses.
|
|
174
|
+
* a value `check` refuses. The error names where it failed: its `at` is the path `at` gives, which
|
|
175
|
+
* its message starts with; its `entry` the name of the entry whose value fails, or which fails as
|
|
176
|
+
* it is given its options; and its `module` and `export` those of a reference to a module.
|
|
122
177
|
*/
|
|
123
|
-
export async function resolveNamed(reference, { key, names = {}, load, check, kind = 'entry' } = {}) {
|
|
178
|
+
export async function resolveNamed(reference, { key, names = {}, load, check, kind = 'entry', at = [] } = {}) {
|
|
179
|
+
const place = { at, where: at };
|
|
124
180
|
const use = reference;
|
|
125
181
|
if (isModule(use) && (key === undefined || !(key in use)))
|
|
126
|
-
return checked(await exportOf(use, load), check, exportText(use));
|
|
182
|
+
return checked(await exportOf(use, load, place), check, `${exportText(use)} is not valid`, place, moduleDetails(use));
|
|
127
183
|
const named = typeof use === 'string'
|
|
128
184
|
? { name: use, options: {} }
|
|
129
185
|
: isPlain(use) && key !== undefined && typeof use[key] === 'string'
|
|
@@ -133,14 +189,24 @@ export async function resolveNamed(reference, { key, names = {}, load, check, ki
|
|
|
133
189
|
})()
|
|
134
190
|
: undefined;
|
|
135
191
|
if (named === undefined)
|
|
136
|
-
throw
|
|
192
|
+
throw namedError('INVALID_NAMED_VALUE', `${textOf(use)} is no reference of ${article(kind)}: give its name${key === undefined ? '' : `, { "${key}": name, … }`} or { "module": …, "export": … }.`, place);
|
|
137
193
|
const { name, options } = named;
|
|
138
194
|
if (!Object.hasOwn(names, name)) {
|
|
139
195
|
const known = Object.keys(names);
|
|
140
|
-
throw
|
|
196
|
+
throw namedError('UNKNOWN_NAME', `No ${kind} is named ${JSON.stringify(name)}: the names are ${known.length === 0 ? 'none' : known.join(', ')}.`, place, { name, names: known });
|
|
141
197
|
}
|
|
142
198
|
const entry = names[name];
|
|
143
|
-
|
|
199
|
+
const of = { ...place, entry: name };
|
|
200
|
+
let value;
|
|
201
|
+
try {
|
|
202
|
+
value = await entry(options);
|
|
203
|
+
}
|
|
204
|
+
catch (error) {
|
|
205
|
+
if (!(error instanceof NamedError) || error.entry !== undefined)
|
|
206
|
+
throw error;
|
|
207
|
+
throw namedError(error.code, `The ${kind} ${name} fails: ${error.message}`, of, detailsOf(error), { cause: error });
|
|
208
|
+
}
|
|
209
|
+
return checked(value, check, `The ${kind} ${name} is not valid`, of);
|
|
144
210
|
}
|
|
145
211
|
/** The JSON Schema of the export of a module. */
|
|
146
212
|
const MODULE_SCHEMA = {
|
package/dist/named.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"named.js","sourceRoot":"","sources":["../src/named.ts"],"names":[],"mappings":"AA2CA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAgFrD,4FAA4F;AAC5F,MAAM,OAAO,GAAG,CAAC,KAAc,EAA8C,EAAE;IAC7E,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IACrF,MAAM,SAAS,GAAY,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAA;IACvD,OAAO,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,IAAI,CAAA;AAC7D,CAAC,CAAA;AAED,qFAAqF;AACrF,MAAM,QAAQ,GAAG,CAAC,KAAc,EAA4B,EAAE,CAC5D,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,QAAQ,CAAC,KAAK,QAAQ,CAAA;AAEvD,4DAA4D;AAC5D,MAAM,OAAO,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,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AGlF,MAAM,GAAG,GAAG;QACV,WAAW,EAAE,YAAY,IAAI,sEAAsE;QACnG,KAAK,EAAE;YACL,aAAa;YACb;gBACE,IAAI,EAAE,QAAQ;gBACd,oBAAoB,EAAE,EAAE,KAAK,EAAE,CAAC,aAAa,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,EAAE;aACxE;SACF;KACF,CAAA;IACD,OAAO,QAAQ,CAAC;QACd,WAAW,EAAE,eAAe,IAAI,wEAAwE;QACxG,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;KAC5C,CAAC,CAAA;AACJ,CAAC","sourcesContent":["/**\n * A value or a function a configuration of JSON names, where JSON cannot hold it, and the code\n * that finds it: a set of named entries, as a tool gives some and a project adds its own, and a\n * reference to one of them, or to the export of a module. {@link resolveNamed} gives the value a\n * reference is; {@link resolveNamedSet} the entries of one set or of sets merged in order;\n * {@link factoryOf} the entry of a plain value, the options of a reference merged over it; and\n * {@link namedSchema} and {@link namedSetSchema} the schema of a reference and of a set, a\n * Standard Schema that checks it and gives its JSON Schema, so an editor completes the names.\n *\n * A reference is one of three forms:\n *\n * - `\"short\"`: the entry of that name;\n * - `{ \"format\": \"short\", \"width\": 60 }`: the entry of that name, by a key the caller chooses,\n * with the options beside it, which the entry is given;\n * - `{ \"module\": \"./formatters.mjs\", \"export\": \"wide\" }`: the export of a module, `default` by\n * default, as it is.\n *\n * Each entry is a factory of the value, `(options) => value`: a function, which takes the options\n * as it chooses, or a plain value, whose options are merged over it ({@link factoryOf}). A set is\n * a map of entries by name, the export of a module that is one, or an array of those, a later set\n * over an earlier one by name. A module is loaded by what the caller gives, `nodeLoadModule` of\n * ./node of Node's, so this module runs on any platform. What fails is a `NamedError` of ./errors.\n *\n * ```ts\n * import { factoryOf, resolveNamed, resolveNamedSet } from '@wtf/project-config/named'\n *\n * type Format = { readonly width: number; readonly ellipsis: string }\n *\n * const formatters = await resolveNamedSet<Format>([\n * { short: factoryOf({ width: 40, ellipsis: '…' }) },\n * { wide: { width: 120, ellipsis: '' } }\n * ])\n * const format = await resolveNamed<Format>(\n * { format: 'short', width: 60 },\n * { key: 'format', names: formatters, kind: 'formatter' }\n * )\n * // { width: 60, ellipsis: '…' }\n * ```\n *\n * @packageDocumentation\n */\nimport type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec'\n\nimport { jsonSchemaOf } from './env.ts'\nimport { NamedError } from './errors.ts'\nimport type { ConfigIssue, JsonSchema, SectionSchema } from './index.ts'\nimport { issuesOf, issuesOfSync } from './section.ts'\n\n/**\n * The export of a module, as a configuration of JSON names it: the module, by what the loader\n * resolves, as a path from the root of the project or the name of a package, and the name of its\n * export, `default` by default.\n */\nexport type ModuleReference = {\n /** The module, as the loader resolves it: `./formatters.mjs`, or a package. */\n readonly module: string\n /** The name of its export: `default` by default. */\n readonly export?: string | undefined\n}\n\n/**\n * A reference to an entry by its name under the key `K`, with the options beside it, which the\n * entry is given: `{ \"format\": \"short\", \"width\": 60 }` of the key `format`. Of a key not known, as\n * `string`, any object of options.\n */\nexport type NamedUse<K extends string = string> = string extends K\n ? NamedOptions\n : { readonly [P in K]: string } & NamedOptions\n\n/**\n * A reference, as a configuration of JSON gives it: the name of an entry; the name under the key\n * `K`, with options; or the export of a module.\n */\nexport type NamedReference<K extends string = string> = string | NamedUse<K> | ModuleReference\n\n/** The options a reference gives an entry: each beside its name, but the key and any `undefined`. */\nexport type NamedOptions = Readonly<Record<string, unknown>>\n\n/** An entry of a set: what makes the value of the options a reference gives, none of a name alone. */\nexport type NamedFactory<T> = (options: NamedOptions) => T | Promise<T>\n\n/** The entries of a set, each a factory, by their names, as {@link resolveNamedSet} gives them. */\nexport type NamedEntries<T> = Readonly<Record<string, NamedFactory<T>>>\n\n/**\n * A set of entries, as a configuration gives it: a map of each by its name, a factory, a plain\n * value or the export of a module that is one of those; or the export of a module that is such a\n * map.\n */\nexport type NamedSet<T> =\n | ModuleReference\n | Readonly<Record<string, NamedFactory<T> | ModuleReference | T>>\n\n/** The sets of a configuration: one, or an array of them, merged in order, each over those before. */\nexport type NamedSets<T> = NamedSet<T> | readonly NamedSet<T>[]\n\n/**\n * What loads a module of a {@link ModuleReference}: the module of the text it names, as an object\n * of its exports, or a failure, which a `NamedError` of `UNLOADABLE_MODULE` holds as its cause.\n */\nexport type LoadModule = (module: string) => Promise<unknown>\n\n/** How a set is resolved: the loader of its modules, the check of its values, and their kind. */\nexport type NamedSetOptions<T> = {\n /** What loads each module a set names: none by default, of which a module is unloadable. */\n readonly load?: LoadModule | undefined\n /**\n * What checks each value an entry is, of a map or of a module, but a function: a Standard\n * Schema, a JSON Schema or a function, as `issuesOf` of ./section takes it.\n */\n readonly check?: SectionSchema<T> | undefined\n /** What the entries are, which an error names: `entry` by default, as `formatter`. */\n readonly kind?: string | undefined\n}\n\n/** How a reference is resolved: the key of its names, its entries, and those of a set. */\nexport type NamedResolveOptions<T> = NamedSetOptions<T> & {\n /** The key a reference of options names its entry by, as `format`: none takes a name alone. */\n readonly key?: string | undefined\n /** The entries a reference names, by their names: none by default. */\n readonly names?: NamedEntries<T> | undefined\n}\n\n/** The schema of a reference or a set: a Standard Schema that checks it, and gives its JSON Schema. */\nexport type NamedSchema<T> = StandardSchemaV1<unknown, T> & StandardJSONSchemaV1<unknown, T>\n\n/** Whether `value` is an object of keys, as JSON's: no list, and no instance of a class. */\nconst isPlain = (value: unknown): value is Readonly<Record<string, unknown>> => {\n if (typeof value !== 'object' || value === null || Array.isArray(value)) return false\n const prototype: unknown = Object.getPrototypeOf(value)\n return prototype === Object.prototype || prototype === null\n}\n\n/** Whether `value` is the export of a module: an object whose `module` is a text. */\nconst isModule = (value: unknown): value is ModuleReference =>\n isPlain(value) && typeof value['module'] === 'string'\n\n/** `word` after its article, `a` or `an`, as `an entry`. */\nconst article = (word: string): string => `${/^[aeiou]/i.test(word) ? 'an' : 'a'} ${word}`\n\n/** `text` of a capital first. */\nconst capital = (text: string): string => `${text.charAt(0).toUpperCase()}${text.slice(1)}`\n\n/** `value` as an error names it. */\nconst textOf = (value: unknown): string =>\n typeof value === 'function' ? 'a function' : (JSON.stringify(value) ?? String(value))\n\n/** The options of `options` that are given: each but those of `undefined`. */\nconst givenOf = (options: NamedOptions): NamedOptions =>\n Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined))\n\n/**\n * The entry of `value`: of a plain object, `value` with the options a reference gives beside the\n * name merged over it, key by key, and those of `undefined` left out, so\n * `{ format: 'short', width: 60 }` gives `{ width: 60, ellipsis: '…' }` of\n * `{ width: 40, ellipsis: '…' }`; of any other value, the value, which takes no options, so options\n * given are a `NamedError` of `INVALID_NAMED_VALUE`. {@link resolveNamedSet} makes each value of a\n * set that is no function an entry so.\n */\nexport function factoryOf<T>(value: T): NamedFactory<T> {\n return (options) => {\n const given = givenOf(options)\n if (isPlain(value)) return { ...value, ...given } as T\n if (Object.keys(given).length === 0) return value\n throw new NamedError(\n 'INVALID_NAMED_VALUE',\n `${textOf(value)} takes no options, and is given ${Object.keys(given).join(', ')}.`\n )\n }\n}\n\n/** The text of the issues a check found. */\nconst issuesText = (issues: readonly ConfigIssue[]): string =>\n issues\n .map(({ path, message }) => (path.length === 0 ? message : `${path.join('.')}: ${message}`))\n .join('; ')\n\n/** `value`, checked by `check`, else a `NamedError` of `INVALID_NAMED_VALUE` of what `what` is. */\nasync function checked<T>(value: unknown, check: SectionSchema<T> | undefined, what: string) {\n if (check === undefined) return value as T\n const { issues, value: read } = await issuesOf(value, check)\n if (issues.length === 0) return read\n throw new NamedError('INVALID_NAMED_VALUE', `${what} is not valid: ${issuesText(issues)}.`, {\n issues\n })\n}\n\n/**\n * The export `reference.export`, `default` by default, of the module `reference.module`, loaded by\n * `load`: a `NamedError` of `UNLOADABLE_MODULE` of a module that fails to load, or of no loader,\n * and of `MISSING_EXPORT` of an export it has not.\n */\nasync function exportOf(reference: ModuleReference, load: LoadModule | undefined) {\n const { module } = reference\n const name = reference.export ?? 'default'\n const details = { module, export: name }\n if (load === undefined)\n throw new NamedError(\n 'UNLOADABLE_MODULE',\n `The module ${module} cannot be loaded, for its export ${name}: no loader of modules is given.`,\n details\n )\n let loaded: unknown\n try {\n loaded = await load(module)\n } catch (error) {\n throw new NamedError(\n 'UNLOADABLE_MODULE',\n `The module ${module} cannot be loaded, for its export ${name}: ${error instanceof Error ? error.message : String(error)}`,\n details,\n { cause: error }\n )\n }\n const exports = loaded as Readonly<Record<string, unknown>> | null | undefined\n const found = typeof exports === 'object' && exports !== null ? exports[name] : undefined\n if (found === undefined)\n throw new NamedError('MISSING_EXPORT', `The module ${module} has no export ${name}.`, details)\n return found\n}\n\n/** What a module's export is, as an error names it. */\nconst exportText = ({ module, export: name = 'default' }: ModuleReference): string =>\n `The export ${name} of ${module}`\n\n/** The entry of `value` of the name `name`: a function as it is, else checked, of {@link factoryOf}. */\nasync function entryOf<T>(\n value: unknown,\n name: string,\n { load, check, kind = 'entry' }: NamedSetOptions<T>\n): Promise<NamedFactory<T>> {\n const at = isModule(value) ? await exportOf(value, load) : value\n if (typeof at === 'function') return at as NamedFactory<T>\n const what = isModule(value)\n ? `${exportText(value)}, the ${kind} ${name},`\n : `The ${kind} ${name}`\n return factoryOf(await checked(at, check, what))\n}\n\n/**\n * The entries of `sets`, each a factory, by their names: of each set in order, a map of entries by\n * name or the export of a module that is one, a later set over an earlier one by name, so a\n * project's own are over those of a package; none of `undefined`. Each entry is a function, a\n * factory as it is, or a plain value, checked by `check`, of {@link factoryOf}; or the export of\n * a module that is one of those, loaded by `load`. Each module is loaded at once, so a set that\n * fails does before any entry is used. Fails with a `NamedError` of `UNLOADABLE_MODULE` or\n * `MISSING_EXPORT` of a module, and of `INVALID_NAMED_VALUE` of a set that is no map, or a value\n * `check` refuses.\n */\nexport async function resolveNamedSet<T>(\n sets: NamedSets<T> | undefined,\n options: NamedSetOptions<T> = {}\n): Promise<NamedEntries<T>> {\n const list: readonly unknown[] = sets === undefined ? [] : Array.isArray(sets) ? sets : [sets]\n const resolved = await Promise.all(\n list.map(async (set) => {\n const map = isModule(set) ? await exportOf(set, options.load) : set\n if (!isPlain(map))\n throw new NamedError(\n 'INVALID_NAMED_VALUE',\n `${isModule(set) ? exportText(set) : textOf(set)} is no set: give an object of each ${options.kind ?? 'entry'} by its name.`\n )\n const entries = await Promise.all(\n Object.entries(map).map(async ([name, value]): Promise<[string, NamedFactory<T>]> => [\n name,\n await entryOf(value, name, options)\n ])\n )\n return Object.fromEntries(entries)\n })\n )\n return Object.assign({}, ...resolved) as NamedEntries<T>\n}\n\n/**\n * The value `reference` is: of a name, the entry of that name of `names`, given no options; of an\n * object of the `key`, the entry of the name under it, given the options beside it but those of\n * `undefined`; of the export of a module, `{ module, export }`, the export as it is, loaded by\n * `load`. With a `check`, the value is checked by it. Fails with a `NamedError`: `UNKNOWN_NAME` of\n * a name of no entry, whose message lists the names there are; `UNLOADABLE_MODULE` and\n * `MISSING_EXPORT` of a module; and `INVALID_NAMED_VALUE` of a reference of none of the forms, or\n * a value `check` refuses.\n */\nexport async function resolveNamed<T>(\n reference: NamedReference,\n { key, names = {}, load, check, kind = 'entry' }: NamedResolveOptions<T> = {}\n): Promise<T> {\n const use: unknown = reference\n if (isModule(use) && (key === undefined || !(key in use)))\n return checked(await exportOf(use, load), check, exportText(use))\n const named =\n typeof use === 'string'\n ? { name: use, options: {} }\n : isPlain(use) && key !== undefined && typeof use[key] === 'string'\n ? (() => {\n const { [key]: name, ...options } = use\n return { name: name as string, options: givenOf(options) }\n })()\n : undefined\n if (named === undefined)\n throw new NamedError(\n 'INVALID_NAMED_VALUE',\n `${textOf(use)} is no reference of ${article(kind)}: give its name${key === undefined ? '' : `, { \"${key}\": name, … }`} or { \"module\": …, \"export\": … }.`\n )\n const { name, options } = named\n if (!Object.hasOwn(names, name)) {\n const known = Object.keys(names)\n throw new NamedError(\n 'UNKNOWN_NAME',\n `No ${kind} is named ${JSON.stringify(name)}: the names are ${known.length === 0 ? 'none' : known.join(', ')}.`,\n { name, names: known }\n )\n }\n const entry = names[name] as NamedFactory<T>\n return checked(await entry(options), check, `The ${kind} ${name}`)\n}\n\n/** The JSON Schema of the export of a module. */\nconst MODULE_SCHEMA = {\n description: 'The export of a module: `default` by default.',\n type: 'object',\n properties: {\n module: {\n description: 'The module: a path from the root, or a package.',\n type: 'string',\n minLength: 1\n },\n export: {\n description: 'The name of its export: `default` by default.',\n type: 'string',\n minLength: 1\n }\n },\n required: ['module'],\n additionalProperties: false\n} as const\n\n/** The JSON Schema of a name: a text, of the `names` an editor completes. */\nconst nameSchema = (kind: string, names: readonly string[] | undefined): JsonSchema => ({\n description: `The name of ${article(kind)}.`,\n type: 'string',\n minLength: 1,\n ...(names === undefined || names.length === 0 ? {} : { examples: [...names] })\n})\n\n/** The JSON Schema of `schema`, as an object of its keywords: `{}`, any value, of none. */\nconst objectSchemaOf = (schema: SectionSchema | undefined): Readonly<Record<string, unknown>> => {\n const json = schema === undefined ? undefined : jsonSchemaOf(schema)\n if (json === undefined || json === true) return {}\n if (json === false) return { not: {} }\n const { $schema: _draft, $id: _id, ...rest } = json\n return rest\n}\n\n/**\n * The schema of `json`, of the type `T`: it checks a value by ajv, as `issuesOfSync` of ./section\n * checks a JSON Schema, and gives `json`, of draft 2020-12, as the JSON Schema of its input and\n * output alike.\n */\nfunction schemaOf<T>(json: Readonly<Record<string, unknown>>): NamedSchema<T> {\n const jsonSchema = (): Record<string, unknown> => ({ ...json })\n return {\n '~standard': {\n version: 1,\n vendor: '@wtf/project-config',\n validate: (value) => {\n const { issues } = issuesOfSync(value, json)\n return issues.length === 0\n ? { value: value as T }\n : { issues: issues.map(({ path, message }) => ({ path, message })) }\n },\n jsonSchema: { input: jsonSchema, output: jsonSchema }\n }\n }\n}\n\n/** What the schema of a reference says of it. */\nexport type NamedSchemaOptions = {\n /** The names an editor completes: those of the entries there are. */\n readonly names?: readonly string[] | undefined\n /**\n * The schema of the options beside a name, as each entry takes them: a JSON Schema, or a\n * Standard Schema that gives one, of an object, whose `properties` are each an option. Any by\n * default.\n */\n readonly options?: SectionSchema | undefined\n /** What the entries are, which the descriptions name: `entry` by default. */\n readonly kind?: string | undefined\n}\n\n/**\n * The schema of a reference of the key `key`: a name, of `names`, which an editor completes;\n * `{ [key]: name, … }`, with the options of `options` beside it, each optional, as they are over\n * the entry's own; or `{ module, export }`. Its JSON Schema, which `input()` of its\n * `~standard.jsonSchema` gives, holds no `$schema`, so it goes in a tool's own as it is.\n */\nexport function namedSchema<K extends string>(\n key: K,\n { names, options, kind = 'entry' }: NamedSchemaOptions = {}\n): NamedSchema<NamedReference<K>> {\n const name = nameSchema(kind, names)\n const { properties, required: _required, type: _type, ...rest } = objectSchemaOf(options)\n return schemaOf({\n description: `${capital(article(kind))}: its name, { \"${key}\": name, … } with options over its own, or the export of a module.`,\n anyOf: [\n name,\n {\n ...rest,\n description: `${capital(article(kind))} by its name, with options over its own.`,\n type: 'object',\n properties: { ...(isPlain(properties) ? properties : {}), [key]: name },\n required: [key]\n },\n MODULE_SCHEMA\n ]\n })\n}\n\n/** What the schema of a set says of it. */\nexport type NamedSetSchemaOptions = {\n /** The schema of the value of an entry: a JSON Schema, or a Standard Schema that gives one. Any by default. */\n readonly entry?: SectionSchema | undefined\n /** What the entries are, which the descriptions name: `entry` by default. */\n readonly kind?: string | undefined\n}\n\n/**\n * The schema of the sets of entries: a set, or an array of sets, merged in order; each set the\n * export of a module, or a map of each entry by its name, of `entry`, or the export of a module.\n * Its JSON Schema holds no `$schema`, as {@link namedSchema}'s.\n */\nexport function namedSetSchema({ entry, kind = 'entry' }: NamedSetSchemaOptions = {}): NamedSchema<\n NamedSets<unknown>\n> {\n const set = {\n description: `A set of ${kind}s: a map of each by its name, or the export of a module that is one.`,\n anyOf: [\n MODULE_SCHEMA,\n {\n type: 'object',\n additionalProperties: { anyOf: [MODULE_SCHEMA, objectSchemaOf(entry)] }\n }\n ]\n }\n return schemaOf({\n description: `The sets of ${kind}s: a set, or an array of sets merged in order, each over those before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ACd,WAAW,EAAE,eAAe,IAAI,wEAAwE;QACxG,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;KAC5C,CAAC,CAAA;AACJ,CAAC","sourcesContent":["/**\n * A value or a function a configuration of JSON names, where JSON cannot hold it, and the code\n * that finds it: a set of named entries, as a tool gives some and a project adds its own, and a\n * reference to one of them, or to the export of a module. {@link resolveNamed} gives the value a\n * reference is; {@link resolveNamedSet} the entries of one set or of sets merged in order;\n * {@link factoryOf} the entry of a plain value, the options of a reference merged over it; and\n * {@link namedSchema} and {@link namedSetSchema} the schema of a reference and of a set, a\n * Standard Schema that checks it and gives its JSON Schema, so an editor completes the names.\n *\n * A reference is one of three forms:\n *\n * - `\"short\"`: the entry of that name;\n * - `{ \"format\": \"short\", \"width\": 60 }`: the entry of that name, by a key the caller chooses,\n * with the options beside it, which the entry is given;\n * - `{ \"module\": \"./formatters.mjs\", \"export\": \"wide\" }`: the export of a module, `default` by\n * default, as it is.\n *\n * Each entry is a factory of the value, `(options) => value`: a function, which takes the options\n * as it chooses, or a plain value, whose options are merged over it ({@link factoryOf}). A set is\n * a map of entries by name, the export of a module that is one, or an array of those, a later set\n * over an earlier one by name. A module is loaded by what the caller gives, `nodeLoadModule` of\n * ./node of Node's, so this module runs on any platform. What fails is a `NamedError` of ./errors,\n * which names where: the path, from where the caller says the sets or the reference are, the index\n * of the set and the name of the entry, and the module and the export.\n *\n * ```ts\n * import { factoryOf, resolveNamed, resolveNamedSet } from '@wtf/project-config/named'\n *\n * type Format = { readonly width: number; readonly ellipsis: string }\n *\n * const formatters = await resolveNamedSet<Format>([\n * { short: factoryOf({ width: 40, ellipsis: '…' }) },\n * { wide: { width: 120, ellipsis: '' } }\n * ])\n * const format = await resolveNamed<Format>(\n * { format: 'short', width: 60 },\n * { key: 'format', names: formatters, kind: 'formatter' }\n * )\n * // { width: 60, ellipsis: '…' }\n * ```\n *\n * @packageDocumentation\n */\nimport type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec'\n\nimport { jsonSchemaOf } from './env.ts'\nimport { NamedError, type NamedErrorCode, type NamedErrorDetails } from './errors.ts'\nimport type { ConfigIssue, ConfigPath, JsonSchema, SectionSchema } from './index.ts'\nimport { issuesOf, issuesOfSync } from './section.ts'\nimport { textOfPath } from './text.ts'\n\n/**\n * The export of a module, as a configuration of JSON names it: the module, by what the loader\n * resolves, as a path from the root of the project or the name of a package, and the name of its\n * export, `default` by default.\n */\nexport type ModuleReference = {\n /** The module, as the loader resolves it: `./formatters.mjs`, or a package. */\n readonly module: string\n /** The name of its export: `default` by default. */\n readonly export?: string | undefined\n}\n\n/**\n * A reference to an entry by its name under the key `K`, with the options beside it, which the\n * entry is given: `{ \"format\": \"short\", \"width\": 60 }` of the key `format`. Of a key not known, as\n * `string`, any object of options.\n */\nexport type NamedUse<K extends string = string> = string extends K\n ? NamedOptions\n : { readonly [P in K]: string } & NamedOptions\n\n/**\n * A reference, as a configuration of JSON gives it: the name of an entry; the name under the key\n * `K`, with options; or the export of a module.\n */\nexport type NamedReference<K extends string = string> = string | NamedUse<K> | ModuleReference\n\n/** The options a reference gives an entry: each beside its name, but the key and any `undefined`. */\nexport type NamedOptions = Readonly<Record<string, unknown>>\n\n/** An entry of a set: what makes the value of the options a reference gives, none of a name alone. */\nexport type NamedFactory<T> = (options: NamedOptions) => T | Promise<T>\n\n/** The entries of a set, each a factory, by their names, as {@link resolveNamedSet} gives them. */\nexport type NamedEntries<T> = Readonly<Record<string, NamedFactory<T>>>\n\n/**\n * A set of entries, as a configuration gives it: a map of each by its name, a factory, a plain\n * value or the export of a module that is one of those; or the export of a module that is such a\n * map.\n */\nexport type NamedSet<T> =\n | ModuleReference\n | Readonly<Record<string, NamedFactory<T> | ModuleReference | T>>\n\n/** The sets of a configuration: one, or an array of them, merged in order, each over those before. */\nexport type NamedSets<T> = NamedSet<T> | readonly NamedSet<T>[]\n\n/**\n * What loads a module of a {@link ModuleReference}: the module of the text it names, as an object\n * of its exports, or a failure, which a `NamedError` of `UNLOADABLE_MODULE` holds as its cause.\n */\nexport type LoadModule = (module: string) => Promise<unknown>\n\n/**\n * What a set and a reference are resolved by alike: the loader of their modules, the check of\n * their values, their kind, and where they are in the configuration.\n */\nexport type NamedCheckOptions<T> = {\n /** What loads each module named: none by default, of which a module is unloadable. */\n readonly load?: LoadModule | undefined\n /**\n * What checks a value: a Standard Schema, a JSON Schema or a function, as `issuesOf` of\n * ./section takes it. Of a set, each value an entry is, of a map or of a module, but a function,\n * whose results it checks too of `checkResults`; of a reference, the value it resolves to.\n */\n readonly check?: SectionSchema<T> | undefined\n /** What the entries are, which an error names: `entry` by default, as `formatter`. */\n readonly kind?: string | undefined\n /**\n * Where the sets, or the reference, are in the configuration, which the `at` of an error starts\n * with, and its message names, as `['formatters']`: none by default.\n */\n readonly at?: ConfigPath | undefined\n}\n\n/** How a set is resolved: as {@link NamedCheckOptions} say, and whether `check` checks what a\n * function entry gives. */\nexport type NamedSetOptions<T> = NamedCheckOptions<T> & {\n /**\n * Whether `check` checks too what each function entry gives, each time it is called, as it\n * checks a value entry once: no by default, of which a function entry is used as it is.\n */\n readonly checkResults?: boolean | undefined\n}\n\n/** How a reference is resolved: the key of its names, its entries, and what\n * {@link NamedCheckOptions} say. */\nexport type NamedResolveOptions<T> = NamedCheckOptions<T> & {\n /** The key a reference of options names its entry by, as `format`: none takes a name alone. */\n readonly key?: string | undefined\n /** The entries a reference names, by their names: none by default. */\n readonly names?: NamedEntries<T> | undefined\n}\n\n/** The schema of a reference or a set: a Standard Schema that checks it, and gives its JSON Schema. */\nexport type NamedSchema<T> = StandardSchemaV1<unknown, T> & StandardJSONSchemaV1<unknown, T>\n\n/** Whether `value` is an object of keys, as JSON's: no list, and no instance of a class. */\nconst isPlain = (value: unknown): value is Readonly<Record<string, unknown>> => {\n if (typeof value !== 'object' || value === null || Array.isArray(value)) return false\n const prototype: unknown = Object.getPrototypeOf(value)\n return prototype === Object.prototype || prototype === null\n}\n\n/** Whether `value` is the export of a module: an object whose `module` is a text. */\nconst isModule = (value: unknown): value is ModuleReference =>\n isPlain(value) && typeof value['module'] === 'string'\n\n/** `word` after its article, `a` or `an`, as `an entry`. */\nconst article = (word: string): string => `${/^[aeiou]/i.test(word) ? 'an' : 'a'} ${word}`\n\n/** `text` of a capital first. */\nconst capital = (text: string): string => `${text.charAt(0).toUpperCase()}${text.slice(1)}`\n\n/** `value` as an error names it. */\nconst textOf = (value: unknown): string =>\n typeof value === 'function' ? 'a function' : (JSON.stringify(value) ?? String(value))\n\n/** The options of `options` that are given: each but those of `undefined`. */\nconst givenOf = (options: NamedOptions): NamedOptions =>\n Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined))\n\n/**\n * The entry of `value`: of a plain object, `value` with the options a reference gives beside the\n * name merged over it, key by key, and those of `undefined` left out, so\n * `{ format: 'short', width: 60 }` gives `{ width: 60, ellipsis: '…' }` of\n * `{ width: 40, ellipsis: '…' }`; of any other value, the value, which takes no options, so options\n * given are a `NamedError` of `INVALID_NAMED_VALUE`. {@link resolveNamedSet} makes each value of a\n * set that is no function an entry so.\n */\nexport function factoryOf<T>(value: T): NamedFactory<T> {\n return (options) => {\n const given = givenOf(options)\n if (isPlain(value)) return { ...value, ...given } as T\n if (Object.keys(given).length === 0) return value\n throw new NamedError(\n 'INVALID_NAMED_VALUE',\n `${textOf(value)} takes no options, and is given ${Object.keys(given).join(', ')}.`\n )\n }\n}\n\n/** The text of the issues a check found. */\nconst issuesText = (issues: readonly ConfigIssue[]): string =>\n issues\n .map(({ path, message }) => (path.length === 0 ? message : `${path.join('.')}: ${message}`))\n .join('; ')\n\n/**\n * Where a value of the sets or a reference is, as a `NamedError` names it: its path; the path of\n * its set or its reference, which the message starts with, as the message names the entry itself;\n * the index of its set and the name of its entry; and what it is to be, as `the formatter wide`.\n */\ntype Place = {\n readonly at: ConfigPath\n readonly where: ConfigPath\n readonly set?: number | undefined\n readonly entry?: string | undefined\n readonly as?: string | undefined\n}\n\n/** The module and the export of `reference`, as a `NamedError` names them. */\nconst moduleDetails = (reference: ModuleReference): NamedErrorDetails => ({\n module: reference.module,\n export: reference.export ?? 'default'\n})\n\n/**\n * A `NamedError` of `code` at `place`: its message after the path of its set or its reference, of\n * one that is not empty, and the path, the set and the entry of the place beside `details`.\n */\nfunction namedError(\n code: NamedErrorCode,\n message: string,\n { at, where, set, entry }: Place,\n details: NamedErrorDetails = {},\n options?: ErrorOptions\n): NamedError {\n return new NamedError(\n code,\n where.length === 0 ? message : `${textOfPath(where)}: ${message}`,\n {\n ...details,\n at,\n ...(set === undefined ? {} : { set }),\n ...(entry === undefined ? {} : { entry })\n },\n options\n )\n}\n\n/**\n * `value`, checked by `check`, else a `NamedError` of `INVALID_NAMED_VALUE` at `place`, whose\n * message is `failing` and the issues, and `details` beside it.\n */\nasync function checked<T>(\n value: unknown,\n check: SectionSchema<T> | undefined,\n failing: string,\n place: Place,\n details: NamedErrorDetails = {}\n): Promise<T> {\n if (check === undefined) return value as T\n const { issues, value: read } = await issuesOf(value, check)\n if (issues.length === 0) return read\n throw namedError('INVALID_NAMED_VALUE', `${failing}: ${issuesText(issues)}.`, place, {\n ...details,\n issues\n })\n}\n\n/**\n * The export `reference.export`, `default` by default, of the module `reference.module`, loaded by\n * `load`: a `NamedError` at `place` of `UNLOADABLE_MODULE` of a module that fails to load, or of no\n * loader, and of `MISSING_EXPORT` of an export it has not, each of the module and the export.\n */\nasync function exportOf(\n reference: ModuleReference,\n load: LoadModule | undefined,\n place: Place\n): Promise<unknown> {\n const { module } = reference\n const name = reference.export ?? 'default'\n const details = moduleDetails(reference)\n const as = place.as === undefined ? '' : ` as ${place.as}`\n if (load === undefined)\n throw namedError(\n 'UNLOADABLE_MODULE',\n `The module ${module} cannot be loaded, for its export ${name}${as}: no loader of modules is given.`,\n place,\n details\n )\n let loaded: unknown\n try {\n loaded = await load(module)\n } catch (error) {\n throw namedError(\n 'UNLOADABLE_MODULE',\n `The module ${module} cannot be loaded, for its export ${name}${as}: ${error instanceof Error ? error.message : String(error)}`,\n place,\n details,\n { cause: error }\n )\n }\n const exports = loaded as Readonly<Record<string, unknown>> | null | undefined\n const found = typeof exports === 'object' && exports !== null ? exports[name] : undefined\n if (found === undefined)\n throw namedError(\n 'MISSING_EXPORT',\n `The module ${module} has no export ${name}${as}.`,\n place,\n details\n )\n return found\n}\n\n/** What a module's export is, as an error names it. */\nconst exportText = ({ module, export: name = 'default' }: ModuleReference): string =>\n `The export ${name} of ${module}`\n\n/**\n * The entry of `value` of the name `name`, at `place`: a function as it is, or, of `checkResults`,\n * one whose results `check` checks; else the value checked, of {@link factoryOf}.\n */\nasync function entryOf<T>(\n value: unknown,\n name: string,\n place: Place,\n { load, check, checkResults, kind = 'entry' }: NamedSetOptions<T>\n): Promise<NamedFactory<T>> {\n const reference = isModule(value) ? value : undefined\n const details = reference === undefined ? {} : moduleDetails(reference)\n const found =\n reference === undefined\n ? value\n : await exportOf(reference, load, { ...place, as: `the ${kind} ${name}` })\n const what =\n reference === undefined\n ? `The ${kind} ${name}`\n : `${exportText(reference)}, the ${kind} ${name},`\n if (typeof found === 'function') {\n const factory = found as NamedFactory<T>\n if (checkResults !== true || check === undefined) return factory\n return async (options) =>\n checked(\n await factory(options),\n check,\n `${what} gives a value that is not valid`,\n place,\n details\n )\n }\n return factoryOf(await checked(found, check, `${what} is not valid`, place, details))\n}\n\n/**\n * The entries of `sets`, each a factory, by their names: of each set in order, a map of entries by\n * name or the export of a module that is one, a later set over an earlier one by name, so a\n * project's own are over those of a package; none of `undefined`. Each entry is a function, a\n * factory as it is, or a plain value, checked by `check`, of {@link factoryOf}; or the export of\n * a module that is one of those, loaded by `load`. Of `checkResults`, `check` checks what each\n * function entry gives too, each time it is called. Each module is loaded at once, so a set that\n * fails does before any entry is used. Fails with a `NamedError` of `UNLOADABLE_MODULE` or\n * `MISSING_EXPORT` of a module, and of `INVALID_NAMED_VALUE` of a set that is no map, or a value\n * `check` refuses. The error names where it failed: its `at` is the path `at` gives, the index of\n * the set in an array of sets, its `set`, and the name of the entry, its `entry`, as\n * `['formatters', '1', 'wide']`, and its message starts with the path of the set; and its `module`\n * and `export` are those of a set or an entry a module exports.\n */\nexport async function resolveNamedSet<T>(\n sets: NamedSets<T> | undefined,\n options: NamedSetOptions<T> = {}\n): Promise<NamedEntries<T>> {\n const { at = [], kind = 'entry' } = options\n const many = Array.isArray(sets)\n const list: readonly unknown[] = sets === undefined ? [] : many ? sets : [sets]\n const resolved = await Promise.all(\n list.map(async (set, index) => {\n const of = many ? [...at, String(index)] : at\n const place: Place = { at: of, where: of, ...(many ? { set: index } : {}) }\n const reference = isModule(set) ? set : undefined\n const map =\n reference === undefined\n ? set\n : await exportOf(reference, options.load, { ...place, as: `a set of ${kind}s` })\n if (!isPlain(map))\n throw namedError(\n 'INVALID_NAMED_VALUE',\n `${reference === undefined ? textOf(set) : exportText(reference)} is no set: give an object of each ${kind} by its name.`,\n place,\n reference === undefined ? {} : moduleDetails(reference)\n )\n const entries = await Promise.all(\n Object.entries(map).map(async ([name, value]): Promise<[string, NamedFactory<T>]> => [\n name,\n await entryOf(value, name, { ...place, at: [...place.at, name], entry: name }, options)\n ])\n )\n return Object.fromEntries(entries)\n })\n )\n return Object.assign({}, ...resolved) as NamedEntries<T>\n}\n\n/** What `error` names but its place, as a `NamedError` that holds it gives it again. */\nconst detailsOf = (error: NamedError): NamedErrorDetails => ({\n ...(error.unknown === undefined ? {} : { name: error.unknown }),\n ...(error.module === undefined ? {} : { module: error.module }),\n ...(error.export === undefined ? {} : { export: error.export }),\n names: error.names,\n issues: error.issues\n})\n\n/**\n * The value `reference` is: of a name, the entry of that name of `names`, given no options; of an\n * object of the `key`, the entry of the name under it, given the options beside it but those of\n * `undefined`; of the export of a module, `{ module, export }`, the export as it is, loaded by\n * `load`. With a `check`, the value is checked by it. Fails with a `NamedError`: `UNKNOWN_NAME` of\n * a name of no entry, whose message lists the names there are; `UNLOADABLE_MODULE` and\n * `MISSING_EXPORT` of a module; and `INVALID_NAMED_VALUE` of a reference of none of the forms, or\n * a value `check` refuses. The error names where it failed: its `at` is the path `at` gives, which\n * its message starts with; its `entry` the name of the entry whose value fails, or which fails as\n * it is given its options; and its `module` and `export` those of a reference to a module.\n */\nexport async function resolveNamed<T>(\n reference: NamedReference,\n { key, names = {}, load, check, kind = 'entry', at = [] }: NamedResolveOptions<T> = {}\n): Promise<T> {\n const place: Place = { at, where: at }\n const use: unknown = reference\n if (isModule(use) && (key === undefined || !(key in use)))\n return checked(\n await exportOf(use, load, place),\n check,\n `${exportText(use)} is not valid`,\n place,\n moduleDetails(use)\n )\n const named =\n typeof use === 'string'\n ? { name: use, options: {} }\n : isPlain(use) && key !== undefined && typeof use[key] === 'string'\n ? (() => {\n const { [key]: name, ...options } = use\n return { name: name as string, options: givenOf(options) }\n })()\n : undefined\n if (named === undefined)\n throw namedError(\n 'INVALID_NAMED_VALUE',\n `${textOf(use)} is no reference of ${article(kind)}: give its name${key === undefined ? '' : `, { \"${key}\": name, … }`} or { \"module\": …, \"export\": … }.`,\n place\n )\n const { name, options } = named\n if (!Object.hasOwn(names, name)) {\n const known = Object.keys(names)\n throw namedError(\n 'UNKNOWN_NAME',\n `No ${kind} is named ${JSON.stringify(name)}: the names are ${known.length === 0 ? 'none' : known.join(', ')}.`,\n place,\n { name, names: known }\n )\n }\n const entry = names[name] as NamedFactory<T>\n const of: Place = { ...place, entry: name }\n let value: unknown\n try {\n value = await entry(options)\n } catch (error) {\n if (!(error instanceof NamedError) || error.entry !== undefined) throw error\n throw namedError(\n error.code,\n `The ${kind} ${name} fails: ${error.message}`,\n of,\n detailsOf(error),\n { cause: error }\n )\n }\n return checked(value, check, `The ${kind} ${name} is not valid`, of)\n}\n\n/** The JSON Schema of the export of a module. */\nconst MODULE_SCHEMA = {\n description: 'The export of a module: `default` by default.',\n type: 'object',\n properties: {\n module: {\n description: 'The module: a path from the root, or a package.',\n type: 'string',\n minLength: 1\n },\n export: {\n description: 'The name of its export: `default` by default.',\n type: 'string',\n minLength: 1\n }\n },\n required: ['module'],\n additionalProperties: false\n} as const\n\n/** The JSON Schema of a name: a text, of the `names` an editor completes. */\nconst nameSchema = (kind: string, names: readonly string[] | undefined): JsonSchema => ({\n description: `The name of ${article(kind)}.`,\n type: 'string',\n minLength: 1,\n ...(names === undefined || names.length === 0 ? {} : { examples: [...names] })\n})\n\n/** The JSON Schema of `schema`, as an object of its keywords: `{}`, any value, of none. */\nconst objectSchemaOf = (schema: SectionSchema | undefined): Readonly<Record<string, unknown>> => {\n const json = schema === undefined ? undefined : jsonSchemaOf(schema)\n if (json === undefined || json === true) return {}\n if (json === false) return { not: {} }\n const { $schema: _draft, $id: _id, ...rest } = json\n return rest\n}\n\n/**\n * The schema of `json`, of the type `T`: it checks a value by ajv, as `issuesOfSync` of ./section\n * checks a JSON Schema, and gives `json`, of draft 2020-12, as the JSON Schema of its input and\n * output alike.\n */\nfunction schemaOf<T>(json: Readonly<Record<string, unknown>>): NamedSchema<T> {\n const jsonSchema = (): Record<string, unknown> => ({ ...json })\n return {\n '~standard': {\n version: 1,\n vendor: '@wtf/project-config',\n validate: (value) => {\n const { issues } = issuesOfSync(value, json)\n return issues.length === 0\n ? { value: value as T }\n : { issues: issues.map(({ path, message }) => ({ path, message })) }\n },\n jsonSchema: { input: jsonSchema, output: jsonSchema }\n }\n }\n}\n\n/** What the schema of a reference says of it. */\nexport type NamedSchemaOptions = {\n /** The names an editor completes: those of the entries there are. */\n readonly names?: readonly string[] | undefined\n /**\n * The schema of the options beside a name, as each entry takes them: a JSON Schema, or a\n * Standard Schema that gives one, of an object, whose `properties` are each an option. Any by\n * default.\n */\n readonly options?: SectionSchema | undefined\n /** What the entries are, which the descriptions name: `entry` by default. */\n readonly kind?: string | undefined\n}\n\n/**\n * The schema of a reference of the key `key`: a name, of `names`, which an editor completes;\n * `{ [key]: name, … }`, with the options of `options` beside it, each optional, as they are over\n * the entry's own; or `{ module, export }`. Its JSON Schema, which `input()` of its\n * `~standard.jsonSchema` gives, holds no `$schema`, so it goes in a tool's own as it is.\n */\nexport function namedSchema<K extends string>(\n key: K,\n { names, options, kind = 'entry' }: NamedSchemaOptions = {}\n): NamedSchema<NamedReference<K>> {\n const name = nameSchema(kind, names)\n const { properties, required: _required, type: _type, ...rest } = objectSchemaOf(options)\n return schemaOf({\n description: `${capital(article(kind))}: its name, { \"${key}\": name, … } with options over its own, or the export of a module.`,\n anyOf: [\n name,\n {\n ...rest,\n description: `${capital(article(kind))} by its name, with options over its own.`,\n type: 'object',\n properties: { ...(isPlain(properties) ? properties : {}), [key]: name },\n required: [key]\n },\n MODULE_SCHEMA\n ]\n })\n}\n\n/** What the schema of a set says of it. */\nexport type NamedSetSchemaOptions = {\n /** The schema of the value of an entry: a JSON Schema, or a Standard Schema that gives one. Any by default. */\n readonly entry?: SectionSchema | undefined\n /** What the entries are, which the descriptions name: `entry` by default. */\n readonly kind?: string | undefined\n}\n\n/**\n * The schema of the sets of entries: a set, or an array of sets, merged in order; each set the\n * export of a module, or a map of each entry by its name, of `entry`, or the export of a module.\n * Its JSON Schema holds no `$schema`, as {@link namedSchema}'s.\n */\nexport function namedSetSchema({ entry, kind = 'entry' }: NamedSetSchemaOptions = {}): NamedSchema<\n NamedSets<unknown>\n> {\n const set = {\n description: `A set of ${kind}s: a map of each by its name, or the export of a module that is one.`,\n anyOf: [\n MODULE_SCHEMA,\n {\n type: 'object',\n additionalProperties: { anyOf: [MODULE_SCHEMA, objectSchemaOf(entry)] }\n }\n ]\n }\n return schemaOf({\n description: `The sets of ${kind}s: a set, or an array of sets merged in order, each over those before.`,\n anyOf: [set, { type: 'array', items: set }]\n })\n}\n"]}
|
package/dist/node.d.ts
CHANGED
|
@@ -1,35 +1,22 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { type LoadOptions, type LoadSyncOptions } from './load.ts';
|
|
1
|
+
import type { LoadSyncPlatform } from './load.ts';
|
|
3
2
|
import type { LoadModule } from './named.ts';
|
|
4
|
-
/** Node's platform the configuration is read and written by: the disk from its root, its paths,
|
|
5
|
-
* and the process's working folder. */
|
|
6
|
-
export declare const nodeConfigPlatform: () => ConfigPlatform;
|
|
7
|
-
/** Node's platform the configuration is read by synchronously: the disk by `node:fs`, and its
|
|
8
|
-
* paths. */
|
|
9
|
-
export declare const nodeSyncPlatform: () => ConfigSyncPlatform;
|
|
10
3
|
/**
|
|
11
|
-
* The
|
|
12
|
-
*
|
|
13
|
-
*
|
|
4
|
+
* The platform the configuration is read by synchronously, of `platform`: its paths, its working
|
|
5
|
+
* folder and its environment, as they are, and the disk, read synchronously by `node:fs`, as its
|
|
6
|
+
* files. `loadConfigSync` of ./load reads by it.
|
|
14
7
|
*/
|
|
15
|
-
export declare const
|
|
16
|
-
/**
|
|
17
|
-
* The configuration of the project in `folder`, the process's working folder by default, as
|
|
18
|
-
* `loadConfigSync` of ./load reads it, of a JSON file or package.json alone: of Node's platform
|
|
19
|
-
* and the process's environment, unless `options` gives others.
|
|
20
|
-
*/
|
|
21
|
-
export declare const loadConfigSync: (folder?: string, options?: Partial<LoadSyncOptions>) => ResolvedConfig;
|
|
8
|
+
export declare const nodeSyncPlatform: ({ path, dirs, env }: Omit<LoadSyncPlatform, "files">) => LoadSyncPlatform;
|
|
22
9
|
/**
|
|
23
10
|
* Node's loader of the modules a reference of ./named names, by `import()` of its file: a path, of
|
|
24
|
-
* `root`, the root of the project, the
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
11
|
+
* `root`, the root of the project, as the entry point gives it; a package, the one `root`
|
|
12
|
+
* installs, else the one installed beside `beside`, as a tool gives its own `import.meta.url`, so a
|
|
13
|
+
* package a global install puts beside the tool is found too. What fails to resolve or load fails
|
|
14
|
+
* of the error of `root`'s resolve, or of the import.
|
|
28
15
|
*
|
|
29
16
|
* ```ts
|
|
30
17
|
* import { nodeLoadModule } from '@wtf/project-config/node'
|
|
31
18
|
*
|
|
32
|
-
* const load = nodeLoadModule(
|
|
19
|
+
* const load = nodeLoadModule(resolved.root, import.meta.url)
|
|
33
20
|
* ```
|
|
34
21
|
*/
|
|
35
|
-
export declare const nodeLoadModule: (root
|
|
22
|
+
export declare const nodeLoadModule: (root: string, beside?: string | URL) => LoadModule;
|
package/dist/node.js
CHANGED
|
@@ -7,22 +7,22 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* of the File System Standard
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* {@link nodeLoadModule}, what loads a module a reference of ./named names. A repo loads its config
|
|
16
|
-
* by them, and spreads a section into a preset's options:
|
|
10
|
+
* What of the configuration is Node's by nature: {@link nodeSyncPlatform}, a platform's files read
|
|
11
|
+
* synchronously, by `node:fs`, as no handle of the File System Standard reads them, for
|
|
12
|
+
* `loadConfigSync` of ./load; and {@link nodeLoadModule}, what loads a module a reference of
|
|
13
|
+
* ./named names, by Node's resolution and `import()`. Neither reads anything of the process: the
|
|
14
|
+
* entry point gives the platform, and the root of the project:
|
|
17
15
|
*
|
|
18
16
|
* ```ts
|
|
19
|
-
* import {
|
|
20
|
-
* import {
|
|
21
|
-
* import { rootConfig } from '@wtf/project-vitest/config'
|
|
22
|
-
* import { vitestSchema } from '@wtf/project-vitest/schema'
|
|
17
|
+
* import { nodeStorage } from '@wtf/platform-fs-node/storage'
|
|
18
|
+
* import { nodePlatform } from '@wtf/platform-node/platform'
|
|
23
19
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
20
|
+
* import { loadConfigSync } from '@wtf/project-config/load'
|
|
21
|
+
* import { nodeLoadModule, nodeSyncPlatform } from '@wtf/project-config/node'
|
|
22
|
+
*
|
|
23
|
+
* const platform = nodePlatform({ fetch, storage: nodeStorage() })
|
|
24
|
+
* const resolved = loadConfigSync({ platform: nodeSyncPlatform(platform) })
|
|
25
|
+
* const load = nodeLoadModule(resolved.root, import.meta.url)
|
|
26
26
|
* ```
|
|
27
27
|
*
|
|
28
28
|
* @packageDocumentation
|
|
@@ -31,72 +31,49 @@ import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
|
31
31
|
import { createRequire } from 'node:module';
|
|
32
32
|
import { isAbsolute, join } from 'node:path';
|
|
33
33
|
import { pathToFileURL } from 'node:url';
|
|
34
|
-
import { nodeStorage } from '@wtf/platform-fs-node/storage';
|
|
35
|
-
import { nodePath } from '@wtf/platform-node/path';
|
|
36
|
-
import { loadConfig as loadWith, loadConfigSync as loadSyncWith } from "./load.js";
|
|
37
|
-
/** Node's platform the configuration is read and written by: the disk from its root, its paths,
|
|
38
|
-
* and the process's working folder. */
|
|
39
|
-
export const nodeConfigPlatform = () => ({
|
|
40
|
-
navigator: { storage: nodeStorage() },
|
|
41
|
-
path: nodePath(),
|
|
42
|
-
dirs: { cwd: process.cwd() }
|
|
43
|
-
});
|
|
44
34
|
/** Whether `error` is Node's of a file or a folder not there, or of the other kind. */
|
|
45
35
|
const isMissing = (error) => ['ENOENT', 'ENOTDIR', 'EISDIR'].includes(error?.code ?? '');
|
|
46
|
-
/** Node's
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (isMissing(error))
|
|
75
|
-
return new Set();
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
36
|
+
/** Node's files, read synchronously by `node:fs`, by their absolute paths. */
|
|
37
|
+
const nodeSyncFiles = {
|
|
38
|
+
text: (path) => {
|
|
39
|
+
try {
|
|
40
|
+
return readFileSync(path, 'utf8');
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (isMissing(error))
|
|
44
|
+
return undefined;
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
files: (folder) => {
|
|
49
|
+
try {
|
|
50
|
+
// A link is what it links to, as a handle of the disk reads it.
|
|
51
|
+
return new Set(readdirSync(folder).filter((name) => {
|
|
52
|
+
try {
|
|
53
|
+
return statSync(join(folder, name)).isFile();
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (isMissing(error))
|
|
62
|
+
return new Set();
|
|
63
|
+
throw error;
|
|
78
64
|
}
|
|
79
65
|
}
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* The configuration of the project in `folder`, the process's working folder by default, as
|
|
83
|
-
* `loadConfig` of ./load reads it: the namespace `wtf` by default, of Node's platform and the
|
|
84
|
-
* process's environment, unless `options` gives others.
|
|
85
|
-
*/
|
|
86
|
-
export const loadConfig = (folder = process.cwd(), options = {}) => loadWith(folder, {
|
|
87
|
-
environment: process.env,
|
|
88
|
-
...options,
|
|
89
|
-
platform: options.platform ?? nodeConfigPlatform()
|
|
90
|
-
});
|
|
66
|
+
};
|
|
91
67
|
/**
|
|
92
|
-
* The
|
|
93
|
-
*
|
|
94
|
-
*
|
|
68
|
+
* The platform the configuration is read by synchronously, of `platform`: its paths, its working
|
|
69
|
+
* folder and its environment, as they are, and the disk, read synchronously by `node:fs`, as its
|
|
70
|
+
* files. `loadConfigSync` of ./load reads by it.
|
|
95
71
|
*/
|
|
96
|
-
export const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
72
|
+
export const nodeSyncPlatform = ({ path, dirs, env }) => ({
|
|
73
|
+
path,
|
|
74
|
+
dirs,
|
|
75
|
+
env,
|
|
76
|
+
files: nodeSyncFiles
|
|
100
77
|
});
|
|
101
78
|
/**
|
|
102
79
|
* The file of the module `module`: of an absolute path, itself; of a path from the root, `./` or
|
|
@@ -124,16 +101,16 @@ function fileOf(module, root, beside) {
|
|
|
124
101
|
}
|
|
125
102
|
/**
|
|
126
103
|
* Node's loader of the modules a reference of ./named names, by `import()` of its file: a path, of
|
|
127
|
-
* `root`, the root of the project, the
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
104
|
+
* `root`, the root of the project, as the entry point gives it; a package, the one `root`
|
|
105
|
+
* installs, else the one installed beside `beside`, as a tool gives its own `import.meta.url`, so a
|
|
106
|
+
* package a global install puts beside the tool is found too. What fails to resolve or load fails
|
|
107
|
+
* of the error of `root`'s resolve, or of the import.
|
|
131
108
|
*
|
|
132
109
|
* ```ts
|
|
133
110
|
* import { nodeLoadModule } from '@wtf/project-config/node'
|
|
134
111
|
*
|
|
135
|
-
* const load = nodeLoadModule(
|
|
112
|
+
* const load = nodeLoadModule(resolved.root, import.meta.url)
|
|
136
113
|
* ```
|
|
137
114
|
*/
|
|
138
|
-
export const nodeLoadModule = (root
|
|
115
|
+
export const nodeLoadModule = (root, beside) => async (module) => import(__rewriteRelativeImportExtension(pathToFileURL(fileOf(module, root, beside)).href));
|
|
139
116
|
//# sourceMappingURL=node.js.map
|