@wtf/project-config 1.0.1 → 1.1.1
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 +63 -14
- package/dist/env.js +51 -9
- package/dist/env.js.map +1 -1
- package/dist/errors.d.ts +62 -2
- package/dist/errors.js +41 -0
- package/dist/errors.js.map +1 -1
- package/dist/named.d.ts +197 -0
- package/dist/named.js +313 -0
- package/dist/named.js.map +1 -0
- package/dist/node.d.ts +15 -0
- package/dist/node.js +53 -4
- package/dist/node.js.map +1 -1
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -6,20 +6,21 @@ One file for the settings of a project's tools: a `wtf` object with a section fo
|
|
|
6
6
|
environment's variables over it, checks it by the tool's schema, and writes values back. A repo
|
|
7
7
|
loads its config itself and hands each section to the tool it is for, as a preset's options.
|
|
8
8
|
|
|
9
|
-
| Subpath | What you get
|
|
10
|
-
| --------------- |
|
|
11
|
-
| `.` | The types: `NamespaceConfig`, `SectionsOf`, `ResolvedConfig`, `SectionSchema`, `ConfigPlatform`, `ConfigSyncPlatform`, …
|
|
12
|
-
| `./node` | `loadConfig(folder?)` and `loadConfigSync(folder?)`, of Node's disk and `process.env`; `nodeConfigPlatform()`, `nodeSyncPlatform()`
|
|
13
|
-
| `./load` | `loadConfig(folder, { platform })`, of code or JSON; `loadConfigSync(folder, { platform })`, of JSON alone; `ignoredWarning`
|
|
14
|
-
| `./section` | `sectionOf(resolved, name, schema?)` and `sectionOfSync`: a tool's section, the environment over it, checked; `issuesOf`, `mergeSection`
|
|
15
|
-
| `./locate` | `locateConfig`, `projectRootOf` and their sync twins; `configFilesOf`, `configNamesOf`, `jsonFilesOf`, `DEFAULT_NAMESPACE`
|
|
16
|
-
| `./env` | `withEnvironment`, `variablesOf`, `envNameOf`, `envPrefixOf`: variables such as `WTF_VITE_COMPRESS` over a value
|
|
17
|
-
| `./placeholder` | `expandPlaceholders` and `expandString`: each `${env:NAME}` replaced by its variable
|
|
18
|
-
| `./path` | `configPathOf('vite.compress')`, `textOfConfigPath`, `valueAt`
|
|
19
|
-
| `./write` | `setConfigValue` and `unsetConfigValue`, in the JSON file or in package.json's key
|
|
20
|
-
| `./schema` | `namespaceSchema('wtf', sections)`: one JSON Schema for the whole file, of each tool's schema
|
|
21
|
-
| `./define` | `defineConfig<S>(config)`: a code config, typed by the sections of the tools
|
|
22
|
-
| `./
|
|
9
|
+
| Subpath | What you get |
|
|
10
|
+
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
11
|
+
| `.` | The types: `NamespaceConfig`, `SectionsOf`, `ResolvedConfig`, `SectionSchema`, `ConfigPlatform`, `ConfigSyncPlatform`, … |
|
|
12
|
+
| `./node` | `loadConfig(folder?)` and `loadConfigSync(folder?)`, of Node's disk and `process.env`; `nodeConfigPlatform()`, `nodeSyncPlatform()`; `nodeLoadModule(root?, beside?)` |
|
|
13
|
+
| `./load` | `loadConfig(folder, { platform })`, of code or JSON; `loadConfigSync(folder, { platform })`, of JSON alone; `ignoredWarning` |
|
|
14
|
+
| `./section` | `sectionOf(resolved, name, schema?)` and `sectionOfSync`: a tool's section, the environment over it, checked; `issuesOf`, `mergeSection` |
|
|
15
|
+
| `./locate` | `locateConfig`, `projectRootOf` and their sync twins; `configFilesOf`, `configNamesOf`, `jsonFilesOf`, `DEFAULT_NAMESPACE` |
|
|
16
|
+
| `./env` | `withEnvironment`, `variablesOf`, `envNameOf`, `envPrefixOf`: variables such as `WTF_VITE_COMPRESS` over a value |
|
|
17
|
+
| `./placeholder` | `expandPlaceholders` and `expandString`: each `${env:NAME}` replaced by its variable |
|
|
18
|
+
| `./path` | `configPathOf('vite.compress')`, `textOfConfigPath`, `valueAt` |
|
|
19
|
+
| `./write` | `setConfigValue` and `unsetConfigValue`, in the JSON file or in package.json's key |
|
|
20
|
+
| `./schema` | `namespaceSchema('wtf', sections)`: one JSON Schema for the whole file, of each tool's schema |
|
|
21
|
+
| `./define` | `defineConfig<S>(config)`: a code config, typed by the sections of the tools |
|
|
22
|
+
| `./named` | `resolveNamed`, `resolveNamedSet`, `factoryOf`, `namedSchema`, `namedSetSchema`: a value or a function named in JSON |
|
|
23
|
+
| `./errors` | `ConfigError` and every other error, each with a stable `code` |
|
|
23
24
|
|
|
24
25
|
```ts
|
|
25
26
|
// vite.config.ts
|
|
@@ -69,6 +70,54 @@ whole file, for `$schema` in `wtf.config.json`. A key that starts with `//` is a
|
|
|
69
70
|
JSON Schema of the whole takes one beside the sections, and each preset's schema in its section,
|
|
70
71
|
which it leaves out of the options it gives.
|
|
71
72
|
|
|
73
|
+
## Values and functions by name
|
|
74
|
+
|
|
75
|
+
JSON cannot hold a function, nor share a value a package gives. `./named` lets a config name one
|
|
76
|
+
instead. Your tool keeps a set of entries by name, a project adds its own, and an option names one
|
|
77
|
+
of them in one of three forms:
|
|
78
|
+
|
|
79
|
+
- `"short"`: the entry of that name.
|
|
80
|
+
- `{ "format": "short", "width": 60 }`: the entry, with options. Your tool chooses the key.
|
|
81
|
+
- `{ "module": "./formatters.mjs", "export": "wide" }`: the export of a module, `default` by
|
|
82
|
+
default.
|
|
83
|
+
|
|
84
|
+
An entry is a factory, `(options) => value`. A plain value is one too: the options are merged
|
|
85
|
+
over it. Sets merge in order, a later one over an earlier one by name, and a set can be a map, the
|
|
86
|
+
export of a module that is one, or an array of those:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { nodeLoadModule } from '@wtf/project-config/node'
|
|
90
|
+
import {
|
|
91
|
+
type NamedReference,
|
|
92
|
+
type NamedSet,
|
|
93
|
+
resolveNamed,
|
|
94
|
+
resolveNamedSet
|
|
95
|
+
} from '@wtf/project-config/named'
|
|
96
|
+
|
|
97
|
+
type Format = { readonly width: number; readonly ellipsis: string }
|
|
98
|
+
declare const configured: { formatters?: NamedSet<Format>[]; format: NamedReference }
|
|
99
|
+
|
|
100
|
+
const builtin = { short: { width: 40, ellipsis: '…' } }
|
|
101
|
+
const formatters = await resolveNamedSet<Format>([builtin, ...(configured.formatters ?? [])], {
|
|
102
|
+
load: nodeLoadModule(process.cwd(), import.meta.url)
|
|
103
|
+
})
|
|
104
|
+
const format = await resolveNamed<Format>(configured.format, {
|
|
105
|
+
key: 'format',
|
|
106
|
+
names: formatters,
|
|
107
|
+
kind: 'formatter'
|
|
108
|
+
})
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
A name of no entry throws a `NamedError` of `UNKNOWN_NAME`, which lists the names there are; a
|
|
112
|
+
module that fails to load, `UNLOADABLE_MODULE`; an export it has not, `MISSING_EXPORT`; and a value
|
|
113
|
+
of the wrong shape, or one the `check` you pass refuses, `INVALID_NAMED_VALUE`. Each names where
|
|
114
|
+
it failed: its `at` path, from the `at` you pass, the index of the `set` and the name of the
|
|
115
|
+
`entry`, and the `module` and `export` a value came from. With `checkResults: true`, `check` also
|
|
116
|
+
checks what each function entry gives. `nodeLoadModule`
|
|
117
|
+
loads a path from the root, and a package the root installs, else one installed beside your tool.
|
|
118
|
+
`namedSchema('format', { names, options })` and `namedSetSchema({ entry })` give the schema of a
|
|
119
|
+
reference and of the sets, so an editor completes the names.
|
|
120
|
+
|
|
72
121
|
## Any platform
|
|
73
122
|
|
|
74
123
|
`./load`, `./locate` and `./write` read through the platform you pass: a `Platform` of
|
package/dist/env.js
CHANGED
|
@@ -48,16 +48,52 @@ function schemasOf(node, root, seen = new Set()) {
|
|
|
48
48
|
const propertyOf = (node, root, key) => schemasOf(node, root).flatMap((schema) => isNode(schema.properties) && Object.hasOwn(schema.properties, key)
|
|
49
49
|
? [schema.properties[key]]
|
|
50
50
|
: []);
|
|
51
|
-
/** Whether
|
|
52
|
-
* or an `enum` of strings,
|
|
53
|
-
function
|
|
51
|
+
/** Whether the schemas `nodes`, of the document `root`, take a string: one of them, or a branch of
|
|
52
|
+
* one, of a `type` `string`, a `const` or an `enum` of strings, or a union of such branches. */
|
|
53
|
+
function takesString(nodes, root) {
|
|
54
54
|
const schemas = nodes.flatMap((node) => schemasOf(node, root));
|
|
55
55
|
return schemas.some((schema) => schema.type === 'string' ||
|
|
56
56
|
typeof schema.const === 'string' ||
|
|
57
57
|
(Array.isArray(schema.enum) && schema.enum.every((value) => typeof value === 'string')) ||
|
|
58
58
|
['anyOf', 'oneOf'].some((key) => Array.isArray(schema[key]) &&
|
|
59
|
-
schema[key].every((branch) =>
|
|
59
|
+
schema[key].every((branch) => takesString([branch], root))));
|
|
60
60
|
}
|
|
61
|
+
/** The types of JSON `value` is of: `integer` beside `number` of a whole number. */
|
|
62
|
+
const typesOf = (value) => value === null
|
|
63
|
+
? ['null']
|
|
64
|
+
: Array.isArray(value)
|
|
65
|
+
? ['array']
|
|
66
|
+
: typeof value === 'number'
|
|
67
|
+
? Number.isInteger(value)
|
|
68
|
+
? ['number', 'integer']
|
|
69
|
+
: ['number']
|
|
70
|
+
: [typeof value];
|
|
71
|
+
/**
|
|
72
|
+
* Whether the schema `node`, of the document `root`, takes a value of the type of `value`: of its
|
|
73
|
+
* `type`, `const` and `enum`, each of its `allOf`, and some branch of its `anyOf` and `oneOf`, each
|
|
74
|
+
* by its `$ref`; a schema that says none of those takes any value.
|
|
75
|
+
*/
|
|
76
|
+
function takes(node, value, root, seen = new Set()) {
|
|
77
|
+
const schema = derefOf(node, root);
|
|
78
|
+
if (typeof schema === 'boolean')
|
|
79
|
+
return schema;
|
|
80
|
+
if (!isNode(schema) || seen.has(schema))
|
|
81
|
+
return true;
|
|
82
|
+
const within = new Set(seen).add(schema);
|
|
83
|
+
const { type } = schema;
|
|
84
|
+
const types = typeof type === 'string' ? [type] : Array.isArray(type) ? type : undefined;
|
|
85
|
+
const text = JSON.stringify(value);
|
|
86
|
+
const branches = (key) => Array.isArray(schema[key]) ? schema[key] : [];
|
|
87
|
+
return ((types === undefined || typesOf(value).some((each) => types.includes(each))) &&
|
|
88
|
+
(!('const' in schema) || JSON.stringify(schema.const) === text) &&
|
|
89
|
+
(!Array.isArray(schema.enum) || schema.enum.some((each) => JSON.stringify(each) === text)) &&
|
|
90
|
+
branches('allOf').every((branch) => takes(branch, value, root, within)) &&
|
|
91
|
+
['anyOf', 'oneOf'].every((key) => branches(key).length === 0 ||
|
|
92
|
+
branches(key).some((branch) => takes(branch, value, root, within))));
|
|
93
|
+
}
|
|
94
|
+
/** Whether each of the schemas `nodes`, of the document `root`, takes a value of the type of
|
|
95
|
+
* `value` ({@link takes}). */
|
|
96
|
+
const takenBy = (nodes, value, root) => nodes.every((node) => takes(node, value, root));
|
|
61
97
|
/** The keys a value can have, as the value `value` and the schemas `nodes` name them, the
|
|
62
98
|
* longest of them as a variable names it first, so `API_KEY` is `apiKey` before it is `api.key`. */
|
|
63
99
|
function keysOf(value, nodes, root) {
|
|
@@ -86,17 +122,23 @@ function pathOf(rest, value, nodes, root) {
|
|
|
86
122
|
}
|
|
87
123
|
return undefined;
|
|
88
124
|
}
|
|
89
|
-
/**
|
|
90
|
-
*
|
|
125
|
+
/**
|
|
126
|
+
* The value of a variable's text, of the schemas `nodes`: its JSON, of a value of a type they take
|
|
127
|
+
* other than a string, so `2` is a number and `[2, "newest"]` a list where a value may be a name,
|
|
128
|
+
* a number or a list; else the text where they take a string, so a name stays a name, and JSON
|
|
129
|
+
* that is no value they take is the text; else its JSON, else the text.
|
|
130
|
+
*/
|
|
91
131
|
function parsedOf(text, nodes, root) {
|
|
92
|
-
|
|
93
|
-
return text;
|
|
132
|
+
let parsed;
|
|
94
133
|
try {
|
|
95
|
-
|
|
134
|
+
parsed = JSON.parse(text);
|
|
96
135
|
}
|
|
97
136
|
catch {
|
|
98
137
|
return text;
|
|
99
138
|
}
|
|
139
|
+
if (typeof parsed !== 'string' && takenBy(nodes, parsed, root))
|
|
140
|
+
return parsed;
|
|
141
|
+
return takesString(nodes, root) ? text : parsed;
|
|
100
142
|
}
|
|
101
143
|
/** `value` with `over` at `path`, merged over what is there ({@link mergeSection}). */
|
|
102
144
|
function withValueAt(value, path, over) {
|
package/dist/env.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1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AAO,EAAE,KAAK,SAAS;YACrB,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,CAAA;IAC5E,CAAC,CAAC,CAAA;IACF,IAAI,MAAM,GAAG,KAAK,CAAA;IAClB,MAAM,GAAG,GAAuB,EAAE,CAAA;IAClC,KAAK,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,KAAK,CAAC,QAAQ,CAC1D,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,CAChF,EAAE,CAAC;QACF,MAAM,GAAG,WAAW,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;QACxC,GAAG,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAC9B,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAA;AAC/B,CAAC","sourcesContent":["/**\n * The configuration of a project, as the environment overrides it: a variable of the prefix of its\n * names, of the key of package.json by default, as `WTF_` of the key `wtf` ({@link envPrefixOf}),\n * followed by the path of a value, each key as a variable names it ({@link envNameOf}):\n * `WTF_VITE_COMPRESS` sets `wtf.vite.compress`. A key is one a value under it already has, or one\n * the schema of its section names, so `apiKey` is `API_KEY` and no `_` is ever taken for a key it\n * is not. A value is JSON, else the text as it is, and the text always of a value the schema takes\n * only as a string. {@link variablesOf} gives the variables of a prefix, and\n * {@link withEnvironment} a value with them over it, as `sectionOf` of ./section reads a section.\n * The variables are what a caller gives, as `loadConfig` of ./load takes them: nothing here reads\n * the environment of a process.\n *\n * ```ts\n * withEnvironment({ compress: { threshold: 1024 } }, { WTF_VITE_COMPRESS_THRESHOLD: '512' }, 'WTF_VITE_')\n * // { value: { compress: { threshold: 512 } }, set: [{ variable: 'WTF_VITE_COMPRESS_THRESHOLD', path: ['compress', 'threshold'] }] }\n * ```\n *\n * @packageDocumentation\n */\nimport type { StandardJSONSchemaV1 } from '@standard-schema/spec'\n\nimport type { ConfigPath, JsonSchema } from './index.ts'\nimport { mergeSection } from './merge.ts'\n\n/** The variables of an environment, by their names, as a process's environment holds them. */\nexport type Environment = Readonly<Record<string, string | undefined>>\n\n/** A variable of the environment that set a value of the configuration: its name, and the path\n * of the value. */\nexport type EnvironmentValue = {\n readonly variable: string\n readonly path: ConfigPath\n}\n\n/** A value with the environment over it: the value, and each variable that set some of it. */\nexport type WithEnvironment = {\n readonly value: unknown\n readonly set: readonly EnvironmentValue[]\n}\n\n/**\n * A name as a variable of the environment writes it: its words in capitals, joined by `_`, a word\n * of camelCase its own, as `apiKey` is `API_KEY`, and any other character a `_`.\n */\nexport const envNameOf = (name: string): string =>\n name\n .replaceAll(/([a-z\\d])([A-Z])/g, '$1_$2')\n .replaceAll(/[^A-Za-z\\d]+/g, '_')\n .replaceAll(/^_|_$/g, '')\n .toUpperCase()\n\n/** The prefix of the variables of the environment of `name`, the key of package.json by default:\n * {@link envNameOf} it, and `_`, as `wtf` is `WTF_` and `wtf-vite` `WTF_VITE_`. */\nexport const envPrefixOf = (name: string): string => `${envNameOf(name)}_`\n\n/** The variables of `environment` of `prefix`, as {@link envPrefixOf} gives it, each set and not\n * empty, by their names. */\nexport function variablesOf(environment: Environment, prefix: string): Record<string, string> {\n const variables: Record<string, string> = {}\n for (const [name, value] of Object.entries(environment))\n if (\n name.startsWith(prefix) &&\n name.length > prefix.length &&\n value !== undefined &&\n value !== ''\n )\n variables[name] = value\n return variables\n}\n\ntype Node = Readonly<Record<string, unknown>>\n\nconst isNode = (value: unknown): value is Node =>\n typeof value === 'object' && value !== null && !Array.isArray(value)\n\n/** The node a `$ref` of the document `root` names, as `#/$defs/api`; the node itself of none. */\nfunction derefOf(node: unknown, root: unknown): unknown {\n if (!isNode(node) || typeof node.$ref !== 'string' || !node.$ref.startsWith('#')) return node\n let at: unknown = root\n for (const part of node.$ref.slice(1).split('/').filter(Boolean))\n at = isNode(at) ? at[part.replaceAll('~1', '/').replaceAll('~0', '~')] : undefined\n return at\n}\n\n/** The schemas `node` is, of the document `root`: itself, and each of its `allOf`, `anyOf` and\n * `oneOf`, each by its `$ref`. */\nfunction schemasOf(node: unknown, root: unknown, seen = new Set<unknown>()): Node[] {\n const schema = derefOf(node, root)\n if (!isNode(schema) || seen.has(schema)) return []\n seen.add(schema)\n const branches = ['allOf', 'anyOf', 'oneOf'].flatMap((key) =>\n Array.isArray(schema[key]) ? (schema[key] as unknown[]) : []\n )\n return [schema, ...branches.flatMap((branch) => schemasOf(branch, root, seen))]\n}\n\n/** The schema of the key `key` of a value of the schema `node`: of each of its `properties` that\n * names it. */\nconst propertyOf = (node: unknown, root: unknown, key: string): unknown[] =>\n schemasOf(node, root).flatMap((schema) =>\n isNode(schema.properties) && Object.hasOwn(schema.properties, key)\n ? [schema.properties[key]]\n : []\n )\n\n/** Whether a value of the schemas `nodes` is only ever a string: of a `type` `string`, a `const`\n * or an `enum` of strings, each branch of it so. */\nfunction onlyString(nodes: readonly unknown[], root: unknown): boolean {\n const schemas = nodes.flatMap((node) => schemasOf(node, root))\n return schemas.some(\n (schema) =>\n schema.type === 'string' ||\n typeof schema.const === 'string' ||\n (Array.isArray(schema.enum) && schema.enum.every((value) => typeof value === 'string')) ||\n (['anyOf', 'oneOf'] as const).some(\n (key) =>\n Array.isArray(schema[key]) &&\n (schema[key] as unknown[]).every((branch) => onlyString([branch], root))\n )\n )\n}\n\n/** The keys a value can have, as the value `value` and the schemas `nodes` name them, the\n * longest of them as a variable names it first, so `API_KEY` is `apiKey` before it is `api.key`. */\nfunction keysOf(value: unknown, nodes: readonly unknown[], root: unknown): string[] {\n const keys = new Set(isNode(value) ? Object.keys(value) : [])\n for (const schema of nodes.flatMap((node) => schemasOf(node, root)))\n if (isNode(schema.properties)) for (const key of Object.keys(schema.properties)) keys.add(key)\n return [...keys].toSorted((a, b) => envNameOf(b).length - envNameOf(a).length)\n}\n\n/** The path the rest of a variable's name, `rest`, names in `value`, of the schemas `nodes`, and\n * the schemas of the value there; `undefined` when it names no key there is. */\nfunction pathOf(\n rest: string,\n value: unknown,\n nodes: readonly unknown[],\n root: unknown\n): { readonly path: string[]; readonly nodes: readonly unknown[] } | undefined {\n for (const key of keysOf(value, nodes, root)) {\n const name = envNameOf(key)\n if (name === '') continue\n const under = nodes.flatMap((node) => propertyOf(node, root, key))\n if (rest === name) return { path: [key], nodes: under }\n if (rest.startsWith(`${name}_`)) {\n const found = pathOf(\n rest.slice(name.length + 1),\n isNode(value) ? value[key] : undefined,\n under,\n root\n )\n if (found !== undefined) return { path: [key, ...found.path], nodes: found.nodes }\n }\n }\n return undefined\n}\n\n/** The value of a variable's text: the text of a value that is only ever a string, else its JSON,\n * else the text. */\nfunction parsedOf(text: string, nodes: readonly unknown[], root: unknown): unknown {\n if (onlyString(nodes, root)) return text\n try {\n return JSON.parse(text) as unknown\n } catch {\n return text\n }\n}\n\n/** `value` with `over` at `path`, merged over what is there ({@link mergeSection}). */\nfunction withValueAt(value: unknown, path: ConfigPath, over: unknown): unknown {\n const [key, ...rest] = path\n if (key === undefined) return mergeSection(value, over)\n const object = isNode(value) ? value : {}\n return { ...object, [key]: withValueAt(object[key], rest, over) }\n}\n\n/**\n * The JSON Schema of `schema`, as a tool checks its section by it: the schema itself, of a JSON\n * Schema; of a Standard Schema that gives one, as zod's does, the JSON Schema of its input; else\n * none, of a function, or a schema that gives none.\n */\nexport function jsonSchemaOf(schema: unknown): JsonSchema | undefined {\n if (typeof schema === 'boolean') return schema\n if (!isNode(schema)) return undefined\n if (!('~standard' in schema)) return schema\n const converter = (schema as Partial<StandardJSONSchemaV1>)['~standard']?.jsonSchema\n if (converter === undefined) return undefined\n try {\n return converter.input({ target: 'draft-2020-12' })\n } catch {\n return undefined\n }\n}\n\n/**\n * `value` with the variables `variables` of `prefix` over it: each variable that names a path of\n * a key there is, in `value` or in `schema`, a JSON Schema of it, sets the value at that path,\n * merged over what is there ({@link mergeSection}), and the variable of the prefix itself, without\n * its last `_`, the whole value, a variable of a shorter path first, so `WTF_VITE_COMPRESS_THRESHOLD`\n * is over what `WTF_VITE` sets, of the prefix `WTF_VITE_`. A variable that names no key there\n * is is passed over.\n */\nexport function withEnvironment(\n value: unknown,\n variables: Readonly<Record<string, string>>,\n prefix: string,\n schema?: JsonSchema\n): WithEnvironment {\n const nodes = schema === undefined ? [] : [schema]\n const whole = prefix.slice(0, -1)\n const found = Object.entries(variables).flatMap(([variable, text]) => {\n const at =\n variable === whole\n ? { path: [], nodes }\n : variable.startsWith(prefix)\n ? pathOf(variable.slice(prefix.length), value, nodes, schema)\n : undefined\n return at === undefined\n ? []\n : [{ variable, path: at.path, value: parsedOf(text, at.nodes, schema) }]\n })\n let result = value\n const set: EnvironmentValue[] = []\n for (const { variable, path, value: over } of found.toSorted(\n (a, b) => a.path.length - b.path.length || a.variable.localeCompare(b.variable)\n )) {\n result = withValueAt(result, path, over)\n set.push({ variable, path })\n }\n return { value: result, set }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAkBzC;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAY,EAAU,EAAE,CAChD,IAAI;KACD,UAAU,CAAC,mBAAmB,EAAE,OAAO,CAAC;KACxC,UAAU,CAAC,eAAe,EAAE,GAAG,CAAC;KAChC,UAAU,CAAC,QAAQ,EAAE,EAAE,CAAC;KACxB,WAAW,EAAE,CAAA;AAElB;mFACmF;AACnF,MAAM,CA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,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,CAAA;IAC5E,CAAC,CAAC,CAAA;IACF,IAAI,MAAM,GAAG,KAAK,CAAA;IAClB,MAAM,GAAG,GAAuB,EAAE,CAAA;IAClC,KAAK,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,KAAK,CAAC,QAAQ,CAC1D,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,CAChF,EAAE,CAAC;QACF,MAAM,GAAG,WAAW,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;QACxC,GAAG,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAC9B,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAA;AAC/B,CAAC","sourcesContent":["/**\n * The configuration of a project, as the environment overrides it: a variable of the prefix of its\n * names, of the key of package.json by default, as `WTF_` of the key `wtf` ({@link envPrefixOf}),\n * followed by the path of a value, each key as a variable names it ({@link envNameOf}):\n * `WTF_VITE_COMPRESS` sets `wtf.vite.compress`. A key is one a value under it already has, or one\n * the schema of its section names, so `apiKey` is `API_KEY` and no `_` is ever taken for a key it\n * is not. A value is JSON of a type the schema takes other than a string, else the text as it is\n * where the schema takes a string, so `2` is a number and a name stays a name where a value is\n * either; of no schema, JSON, else the text. {@link variablesOf} gives the variables of a prefix, and\n * {@link withEnvironment} a value with them over it, as `sectionOf` of ./section reads a section.\n * The variables are what a caller gives, as `loadConfig` of ./load takes them: nothing here reads\n * the environment of a process.\n *\n * ```ts\n * withEnvironment({ compress: { threshold: 1024 } }, { WTF_VITE_COMPRESS_THRESHOLD: '512' }, 'WTF_VITE_')\n * // { value: { compress: { threshold: 512 } }, set: [{ variable: 'WTF_VITE_COMPRESS_THRESHOLD', path: ['compress', 'threshold'] }] }\n * ```\n *\n * @packageDocumentation\n */\nimport type { StandardJSONSchemaV1 } from '@standard-schema/spec'\n\nimport type { ConfigPath, JsonSchema } from './index.ts'\nimport { mergeSection } from './merge.ts'\n\n/** The variables of an environment, by their names, as a process's environment holds them. */\nexport type Environment = Readonly<Record<string, string | undefined>>\n\n/** A variable of the environment that set a value of the configuration: its name, and the path\n * of the value. */\nexport type EnvironmentValue = {\n readonly variable: string\n readonly path: ConfigPath\n}\n\n/** A value with the environment over it: the value, and each variable that set some of it. */\nexport type WithEnvironment = {\n readonly value: unknown\n readonly set: readonly EnvironmentValue[]\n}\n\n/**\n * A name as a variable of the environment writes it: its words in capitals, joined by `_`, a word\n * of camelCase its own, as `apiKey` is `API_KEY`, and any other character a `_`.\n */\nexport const envNameOf = (name: string): string =>\n name\n .replaceAll(/([a-z\\d])([A-Z])/g, '$1_$2')\n .replaceAll(/[^A-Za-z\\d]+/g, '_')\n .replaceAll(/^_|_$/g, '')\n .toUpperCase()\n\n/** The prefix of the variables of the environment of `name`, the key of package.json by default:\n * {@link envNameOf} it, and `_`, as `wtf` is `WTF_` and `wtf-vite` `WTF_VITE_`. */\nexport const envPrefixOf = (name: string): string => `${envNameOf(name)}_`\n\n/** The variables of `environment` of `prefix`, as {@link envPrefixOf} gives it, each set and not\n * empty, by their names. */\nexport function variablesOf(environment: Environment, prefix: string): Record<string, string> {\n const variables: Record<string, string> = {}\n for (const [name, value] of Object.entries(environment))\n if (\n name.startsWith(prefix) &&\n name.length > prefix.length &&\n value !== undefined &&\n value !== ''\n )\n variables[name] = value\n return variables\n}\n\ntype Node = Readonly<Record<string, unknown>>\n\nconst isNode = (value: unknown): value is Node =>\n typeof value === 'object' && value !== null && !Array.isArray(value)\n\n/** The node a `$ref` of the document `root` names, as `#/$defs/api`; the node itself of none. */\nfunction derefOf(node: unknown, root: unknown): unknown {\n if (!isNode(node) || typeof node.$ref !== 'string' || !node.$ref.startsWith('#')) return node\n let at: unknown = root\n for (const part of node.$ref.slice(1).split('/').filter(Boolean))\n at = isNode(at) ? at[part.replaceAll('~1', '/').replaceAll('~0', '~')] : undefined\n return at\n}\n\n/** The schemas `node` is, of the document `root`: itself, and each of its `allOf`, `anyOf` and\n * `oneOf`, each by its `$ref`. */\nfunction schemasOf(node: unknown, root: unknown, seen = new Set<unknown>()): Node[] {\n const schema = derefOf(node, root)\n if (!isNode(schema) || seen.has(schema)) return []\n seen.add(schema)\n const branches = ['allOf', 'anyOf', 'oneOf'].flatMap((key) =>\n Array.isArray(schema[key]) ? (schema[key] as unknown[]) : []\n )\n return [schema, ...branches.flatMap((branch) => schemasOf(branch, root, seen))]\n}\n\n/** The schema of the key `key` of a value of the schema `node`: of each of its `properties` that\n * names it. */\nconst propertyOf = (node: unknown, root: unknown, key: string): unknown[] =>\n schemasOf(node, root).flatMap((schema) =>\n isNode(schema.properties) && Object.hasOwn(schema.properties, key)\n ? [schema.properties[key]]\n : []\n )\n\n/** Whether the schemas `nodes`, of the document `root`, take a string: one of them, or a branch of\n * one, of a `type` `string`, a `const` or an `enum` of strings, or a union of such branches. */\nfunction takesString(nodes: readonly unknown[], root: unknown): boolean {\n const schemas = nodes.flatMap((node) => schemasOf(node, root))\n return schemas.some(\n (schema) =>\n schema.type === 'string' ||\n typeof schema.const === 'string' ||\n (Array.isArray(schema.enum) && schema.enum.every((value) => typeof value === 'string')) ||\n (['anyOf', 'oneOf'] as const).some(\n (key) =>\n Array.isArray(schema[key]) &&\n (schema[key] as unknown[]).every((branch) => takesString([branch], root))\n )\n )\n}\n\n/** The types of JSON `value` is of: `integer` beside `number` of a whole number. */\nconst typesOf = (value: unknown): readonly string[] =>\n value === null\n ? ['null']\n : Array.isArray(value)\n ? ['array']\n : typeof value === 'number'\n ? Number.isInteger(value)\n ? ['number', 'integer']\n : ['number']\n : [typeof value]\n\n/**\n * Whether the schema `node`, of the document `root`, takes a value of the type of `value`: of its\n * `type`, `const` and `enum`, each of its `allOf`, and some branch of its `anyOf` and `oneOf`, each\n * by its `$ref`; a schema that says none of those takes any value.\n */\nfunction takes(node: unknown, value: unknown, root: unknown, seen = new Set<unknown>()): boolean {\n const schema = derefOf(node, root)\n if (typeof schema === 'boolean') return schema\n if (!isNode(schema) || seen.has(schema)) return true\n const within = new Set(seen).add(schema)\n const { type } = schema\n const types = typeof type === 'string' ? [type] : Array.isArray(type) ? type : undefined\n const text = JSON.stringify(value)\n const branches = (key: string): unknown[] =>\n Array.isArray(schema[key]) ? (schema[key] as unknown[]) : []\n return (\n (types === undefined || typesOf(value).some((each) => types.includes(each))) &&\n (!('const' in schema) || JSON.stringify(schema.const) === text) &&\n (!Array.isArray(schema.enum) || schema.enum.some((each) => JSON.stringify(each) === text)) &&\n branches('allOf').every((branch) => takes(branch, value, root, within)) &&\n (['anyOf', 'oneOf'] as const).every(\n (key) =>\n branches(key).length === 0 ||\n branches(key).some((branch) => takes(branch, value, root, within))\n )\n )\n}\n\n/** Whether each of the schemas `nodes`, of the document `root`, takes a value of the type of\n * `value` ({@link takes}). */\nconst takenBy = (nodes: readonly unknown[], value: unknown, root: unknown): boolean =>\n nodes.every((node) => takes(node, value, root))\n\n/** The keys a value can have, as the value `value` and the schemas `nodes` name them, the\n * longest of them as a variable names it first, so `API_KEY` is `apiKey` before it is `api.key`. */\nfunction keysOf(value: unknown, nodes: readonly unknown[], root: unknown): string[] {\n const keys = new Set(isNode(value) ? Object.keys(value) : [])\n for (const schema of nodes.flatMap((node) => schemasOf(node, root)))\n if (isNode(schema.properties)) for (const key of Object.keys(schema.properties)) keys.add(key)\n return [...keys].toSorted((a, b) => envNameOf(b).length - envNameOf(a).length)\n}\n\n/** The path the rest of a variable's name, `rest`, names in `value`, of the schemas `nodes`, and\n * the schemas of the value there; `undefined` when it names no key there is. */\nfunction pathOf(\n rest: string,\n value: unknown,\n nodes: readonly unknown[],\n root: unknown\n): { readonly path: string[]; readonly nodes: readonly unknown[] } | undefined {\n for (const key of keysOf(value, nodes, root)) {\n const name = envNameOf(key)\n if (name === '') continue\n const under = nodes.flatMap((node) => propertyOf(node, root, key))\n if (rest === name) return { path: [key], nodes: under }\n if (rest.startsWith(`${name}_`)) {\n const found = pathOf(\n rest.slice(name.length + 1),\n isNode(value) ? value[key] : undefined,\n under,\n root\n )\n if (found !== undefined) return { path: [key, ...found.path], nodes: found.nodes }\n }\n }\n return undefined\n}\n\n/**\n * The value of a variable's text, of the schemas `nodes`: its JSON, of a value of a type they take\n * other than a string, so `2` is a number and `[2, \"newest\"]` a list where a value may be a name,\n * a number or a list; else the text where they take a string, so a name stays a name, and JSON\n * that is no value they take is the text; else its JSON, else the text.\n */\nfunction parsedOf(text: string, nodes: readonly unknown[], root: unknown): unknown {\n let parsed: unknown\n try {\n parsed = JSON.parse(text) as unknown\n } catch {\n return text\n }\n if (typeof parsed !== 'string' && takenBy(nodes, parsed, root)) return parsed\n return takesString(nodes, root) ? text : parsed\n}\n\n/** `value` with `over` at `path`, merged over what is there ({@link mergeSection}). */\nfunction withValueAt(value: unknown, path: ConfigPath, over: unknown): unknown {\n const [key, ...rest] = path\n if (key === undefined) return mergeSection(value, over)\n const object = isNode(value) ? value : {}\n return { ...object, [key]: withValueAt(object[key], rest, over) }\n}\n\n/**\n * The JSON Schema of `schema`, as a tool checks its section by it: the schema itself, of a JSON\n * Schema; of a Standard Schema that gives one, as zod's does, the JSON Schema of its input; else\n * none, of a function, or a schema that gives none.\n */\nexport function jsonSchemaOf(schema: unknown): JsonSchema | undefined {\n if (typeof schema === 'boolean') return schema\n if (!isNode(schema)) return undefined\n if (!('~standard' in schema)) return schema\n const converter = (schema as Partial<StandardJSONSchemaV1>)['~standard']?.jsonSchema\n if (converter === undefined) return undefined\n try {\n return converter.input({ target: 'draft-2020-12' })\n } catch {\n return undefined\n }\n}\n\n/**\n * `value` with the variables `variables` of `prefix` over it: each variable that names a path of\n * a key there is, in `value` or in `schema`, a JSON Schema of it, sets the value at that path,\n * merged over what is there ({@link mergeSection}), and the variable of the prefix itself, without\n * its last `_`, the whole value, a variable of a shorter path first, so `WTF_VITE_COMPRESS_THRESHOLD`\n * is over what `WTF_VITE` sets, of the prefix `WTF_VITE_`. A variable that names no key there\n * is is passed over.\n */\nexport function withEnvironment(\n value: unknown,\n variables: Readonly<Record<string, string>>,\n prefix: string,\n schema?: JsonSchema\n): WithEnvironment {\n const nodes = schema === undefined ? [] : [schema]\n const whole = prefix.slice(0, -1)\n const found = Object.entries(variables).flatMap(([variable, text]) => {\n const at =\n variable === whole\n ? { path: [], nodes }\n : variable.startsWith(prefix)\n ? pathOf(variable.slice(prefix.length), value, nodes, schema)\n : undefined\n return at === undefined\n ? []\n : [{ variable, path: at.path, value: parsedOf(text, at.nodes, schema) }]\n })\n let result = value\n const set: EnvironmentValue[] = []\n for (const { variable, path, value: over } of found.toSorted(\n (a, b) => a.path.length - b.path.length || a.variable.localeCompare(b.variable)\n )) {\n result = withValueAt(result, path, over)\n set.push({ variable, path })\n }\n return { value: result, set }\n}\n"]}
|
package/dist/errors.d.ts
CHANGED
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
* {@link NoConfigValueError}, a value asked for that is not set; and
|
|
10
10
|
* {@link EnvPlaceholderUnsetError}, a value that points at a variable of the environment that is
|
|
11
11
|
* not set; {@link SyncConfigInCodeError}, a configuration of code loaded synchronously; and
|
|
12
|
-
* {@link SyncSchemaError}, a section checked synchronously by a schema that checks asynchronously
|
|
13
|
-
*
|
|
12
|
+
* {@link SyncSchemaError}, a section checked synchronously by a schema that checks asynchronously;
|
|
13
|
+
* and {@link NamedError}, a reference of ./named that names no entry, or a module, an export or a
|
|
14
|
+
* value that is not what it says. One that the arguments of a person cause exits 2.
|
|
14
15
|
*
|
|
15
16
|
* @packageDocumentation
|
|
16
17
|
*/
|
|
@@ -149,3 +150,62 @@ export declare class SyncSchemaError extends ConfigError {
|
|
|
149
150
|
readonly section: string;
|
|
150
151
|
constructor(section: string);
|
|
151
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* The codes of a {@link NamedError}: `UNKNOWN_NAME`, a reference of a name of no entry;
|
|
155
|
+
* `UNLOADABLE_MODULE`, a module that fails to load, or of no loader given; `MISSING_EXPORT`, an
|
|
156
|
+
* export the module has not; and `INVALID_NAMED_VALUE`, a reference, a set or a value that is not
|
|
157
|
+
* of its shape, or that its check refuses.
|
|
158
|
+
*/
|
|
159
|
+
export type NamedErrorCode = 'UNKNOWN_NAME' | 'UNLOADABLE_MODULE' | 'MISSING_EXPORT' | 'INVALID_NAMED_VALUE';
|
|
160
|
+
/** What a {@link NamedError} names, of its code: where it failed, the name and those there are,
|
|
161
|
+
* the module and its export, or the issues of a check. */
|
|
162
|
+
export type NamedErrorDetails = {
|
|
163
|
+
/** Where the value that failed is, by its path from what the caller gave, of any code. */
|
|
164
|
+
readonly at?: ConfigPath;
|
|
165
|
+
/** The index of the set that failed in the array of sets, of any code. */
|
|
166
|
+
readonly set?: number;
|
|
167
|
+
/** The name of the entry that failed, of a set or of a reference, of any code. */
|
|
168
|
+
readonly entry?: string;
|
|
169
|
+
/** The name of no entry, of `UNKNOWN_NAME`. */
|
|
170
|
+
readonly name?: string;
|
|
171
|
+
/** The names of the entries there are, of `UNKNOWN_NAME`. */
|
|
172
|
+
readonly names?: readonly string[];
|
|
173
|
+
/** The module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a value it exports. */
|
|
174
|
+
readonly module?: string;
|
|
175
|
+
/** The name of its export, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a value it exports. */
|
|
176
|
+
readonly export?: string;
|
|
177
|
+
/** What the check found wrong, of `INVALID_NAMED_VALUE`. */
|
|
178
|
+
readonly issues?: readonly ConfigIssue[];
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* A reference of ./named that cannot be resolved, of a stable code of {@link NamedErrorCode}: a
|
|
182
|
+
* name of no entry, whose message lists the names there are; a module that fails to load; an
|
|
183
|
+
* export it has not; or a reference, a set or a value of no shape it takes. Of any code, it names
|
|
184
|
+
* where it failed: its path, `at`, the index of its `set` and the name of its `entry`, and the
|
|
185
|
+
* `module` and `export` a value came from. A tool maps each code to its own, as it chooses.
|
|
186
|
+
*/
|
|
187
|
+
export declare class NamedError extends ConfigError {
|
|
188
|
+
readonly code: NamedErrorCode;
|
|
189
|
+
/**
|
|
190
|
+
* Where the value that failed is, by its path from what the caller gave: the path the caller
|
|
191
|
+
* gives as `at`, the index of the set in an array of sets, and the name of the entry, as
|
|
192
|
+
* `['formatters', '1', 'wide']`; empty when nothing narrows it.
|
|
193
|
+
*/
|
|
194
|
+
readonly at: ConfigPath;
|
|
195
|
+
/** The index of the set that failed in the array of sets: none of one set alone, or of a reference. */
|
|
196
|
+
readonly set: number | undefined;
|
|
197
|
+
/** The name of the entry that failed, of a set or of the one a reference names: none of others. */
|
|
198
|
+
readonly entry: string | undefined;
|
|
199
|
+
/** The name of no entry, of `UNKNOWN_NAME`. */
|
|
200
|
+
readonly unknown: string | undefined;
|
|
201
|
+
/** The names of the entries there are, of `UNKNOWN_NAME`; none of any other code. */
|
|
202
|
+
readonly names: readonly string[];
|
|
203
|
+
/** The module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a value it exports. */
|
|
204
|
+
readonly module: string | undefined;
|
|
205
|
+
/** The name of the export of the module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a
|
|
206
|
+
* value it exports. */
|
|
207
|
+
readonly export: string | undefined;
|
|
208
|
+
/** What the check found wrong, of `INVALID_NAMED_VALUE` of a value it refuses; none of others. */
|
|
209
|
+
readonly issues: readonly ConfigIssue[];
|
|
210
|
+
constructor(code: NamedErrorCode, message: string, details?: NamedErrorDetails, options?: ErrorOptions);
|
|
211
|
+
}
|
package/dist/errors.js
CHANGED
|
@@ -172,4 +172,45 @@ export class SyncSchemaError extends ConfigError {
|
|
|
172
172
|
this.section = section;
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* A reference of ./named that cannot be resolved, of a stable code of {@link NamedErrorCode}: a
|
|
177
|
+
* name of no entry, whose message lists the names there are; a module that fails to load; an
|
|
178
|
+
* export it has not; or a reference, a set or a value of no shape it takes. Of any code, it names
|
|
179
|
+
* where it failed: its path, `at`, the index of its `set` and the name of its `entry`, and the
|
|
180
|
+
* `module` and `export` a value came from. A tool maps each code to its own, as it chooses.
|
|
181
|
+
*/
|
|
182
|
+
export class NamedError extends ConfigError {
|
|
183
|
+
/**
|
|
184
|
+
* Where the value that failed is, by its path from what the caller gave: the path the caller
|
|
185
|
+
* gives as `at`, the index of the set in an array of sets, and the name of the entry, as
|
|
186
|
+
* `['formatters', '1', 'wide']`; empty when nothing narrows it.
|
|
187
|
+
*/
|
|
188
|
+
at;
|
|
189
|
+
/** The index of the set that failed in the array of sets: none of one set alone, or of a reference. */
|
|
190
|
+
set;
|
|
191
|
+
/** The name of the entry that failed, of a set or of the one a reference names: none of others. */
|
|
192
|
+
entry;
|
|
193
|
+
/** The name of no entry, of `UNKNOWN_NAME`. */
|
|
194
|
+
unknown;
|
|
195
|
+
/** The names of the entries there are, of `UNKNOWN_NAME`; none of any other code. */
|
|
196
|
+
names;
|
|
197
|
+
/** The module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a value it exports. */
|
|
198
|
+
module;
|
|
199
|
+
/** The name of the export of the module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a
|
|
200
|
+
* value it exports. */
|
|
201
|
+
export;
|
|
202
|
+
/** What the check found wrong, of `INVALID_NAMED_VALUE` of a value it refuses; none of others. */
|
|
203
|
+
issues;
|
|
204
|
+
constructor(code, message, details = {}, options) {
|
|
205
|
+
super(code, message, options);
|
|
206
|
+
this.at = details.at ?? [];
|
|
207
|
+
this.set = details.set;
|
|
208
|
+
this.entry = details.entry;
|
|
209
|
+
this.unknown = details.name;
|
|
210
|
+
this.names = details.names ?? [];
|
|
211
|
+
this.module = details.module;
|
|
212
|
+
this.export = details.export;
|
|
213
|
+
this.issues = details.issues ?? [];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
175
216
|
//# sourceMappingURL=errors.js.map
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC;wDACwD;AACxD,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC3B,IAAI,CAAQ;IACrB,kDAAkD;IACzC,QAAQ,GAAW,CAAC,CAAA;IAE7B,YAAY,IAAY,EAAE,OAAe,EAAE,OAAsB;QAC/D,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAA;IAC7B,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,sBAAuB,SAAQ,WAAW;IACrD,sCAAsC;IAC7B,IAAI,CAAQ;IAErB,YAAY,IAAY,EAAE,MAAc,EAAE,OAAsB;QAC9D,KAAK,CAAC,qBAAqB,EAAE,GAAG,IAAI,KAAK,MAAM,EAAE,EAAE,OAAO,CAAC,CAAA;QAC3D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,WAAW;IAClD,2DAA2D;IAClD,IAAI,CAAQ;IACrB,gCAAgC;IACvB,OAAO,CAAQ;IACxB,sDAAsD;IAC7C,MAAM,CAAwB;IAEvC,YACE,IAAY,EACZ,SAAiB,EACjB,OAAe,EACf,MAA8B,EAC9B,MAAc,SAAS;QAEvB,KAAK,CACH,wBAAwB,EACxB;YACE,GAAG,IAAI,SAAS,OAAO,mBAAmB,SAAS,8BAA8B;YACjF,GAAG,MA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CAAA;IACxB,CAAC;CACF","sourcesContent":["/**\n * What `@wtf/project-config` throws, each a {@link ConfigError} of a stable `code`:\n * {@link InvalidConfigFileError}, a file of the configuration that holds no object;\n * {@link InvalidSectionError}, a section its tool's schema refuses; {@link ConfigPathError}, a path\n * that names no value; {@link ConfigInCodeError}, a write to a configuration of code;\n * {@link NoConfigFileError}, a write with no package.json to hold it;\n * {@link ConfigFileMismatchError}, a write to a file that is not the one in use;\n * {@link UnwritableConfigFileError}, a write to a file that is never written;\n * {@link NoConfigValueError}, a value asked for that is not set; and\n * {@link EnvPlaceholderUnsetError}, a value that points at a variable of the environment that is\n * not set; {@link SyncConfigInCodeError}, a configuration of code loaded synchronously; and\n * {@link SyncSchemaError}, a section checked synchronously by a schema that checks asynchronously.\n * One that the arguments of a person cause exits 2.\n *\n * @packageDocumentation\n */\nimport type { ConfigIssue, ConfigPath } from './index.ts'\nimport { textOfPath } from './text.ts'\n\n/** What this package throws: an Error with a stable SCREAMING_SNAKE `code`, and the `exitCode` a\n * command that fails by it exits with, 1 by default. */\nexport class ConfigError extends Error {\n readonly code: string\n /** What a command that fails by it exits with. */\n readonly exitCode: number = 1\n\n constructor(code: string, message: string, options?: ErrorOptions) {\n super(message, options)\n this.code = code\n this.name = new.target.name\n }\n}\n\n/**\n * A file of the configuration that holds no configuration: no JSON, no object, or a module that\n * fails to load, or exports no object.\n */\nexport class InvalidConfigFileError extends ConfigError {\n /** The file, by its absolute path. */\n readonly file: string\n\n constructor(file: string, reason: string, options?: ErrorOptions) {\n super('INVALID_CONFIG_FILE', `${file}: ${reason}`, options)\n this.file = file\n }\n}\n\n/**\n * A section of the configuration that the schema of its tool refuses: each issue, by the file and\n * the path of the value, from the key that names the configuration, the namespace by default.\n */\nexport class InvalidSectionError extends ConfigError {\n /** The file of the configuration, by its absolute path. */\n readonly file: string\n /** The section, by its name. */\n readonly section: string\n /** What is wrong, each by its path in the section. */\n readonly issues: readonly ConfigIssue[]\n\n constructor(\n file: string,\n namespace: string,\n section: string,\n issues: readonly ConfigIssue[],\n key: string = namespace\n ) {\n super(\n 'INVALID_CONFIG_SECTION',\n [\n `${file}: the ${section} section of the ${namespace} configuration is not valid:`,\n ...issues.map(({ path, message }) => ` ${textOfPath([key, section, ...path])}: ${message}`)\n ].join('\\n')\n )\n this.file = file\n this.section = section\n this.issues = issues\n }\n}\n\n/**\n * A path of the configuration that names no value to read or write: an empty one, or one that\n * goes into a value that is no object or list. A usage failure: it exits 2.\n */\nexport class ConfigPathError extends ConfigError {\n /** The path, as it was given. */\n readonly path: string\n override readonly exitCode: number = 2\n\n constructor(path: string, reason: string) {\n super('INVALID_CONFIG_PATH', `${path === '' ? 'The path' : path} ${reason}`)\n this.path = path\n }\n}\n\n/**\n * A write to a configuration of code, as `wtf.config.mjs`, `wtf.config.js` or `wtf.config.ts`: code\n * is changed by a person, and not by a tool. Its message names what is written: the JSON files, and\n * the key of package.json.\n */\nexport class ConfigInCodeError extends ConfigError {\n /** The file of code, by its absolute path. */\n readonly file: string\n\n constructor(file: string, written: { readonly files: readonly string[]; readonly key: string }) {\n const names = [...written.files, `the \"${written.key}\" key of package.json`]\n super(\n 'CONFIG_IN_CODE',\n `The configuration is code, in ${file}: change it there. Only ${names.join(' and ')} ${names.length === 1 ? 'is' : 'are'} written.`\n )\n this.file = file\n }\n}\n\n/**\n * A write to the configuration of a project that has none, in the key of package.json, the\n * default, where the root has no package.json. Its message names the key, and the JSON file to make\n * it in instead, if there is one. A usage failure: it exits 2.\n */\nexport class NoConfigFileError extends ConfigError {\n /** The root of the project. */\n readonly root: string\n override readonly exitCode: number = 2\n\n constructor(\n root: string,\n {\n namespace,\n key = namespace,\n json\n }: { readonly namespace: string; readonly key?: string; readonly json?: string | undefined }\n ) {\n super(\n 'NO_CONFIG_FILE',\n `No package.json is in ${root} to hold the ${namespace} configuration in its \"${key}\" key: make one there${json === undefined ? '' : `, or name ${json} to make the configuration in`}.`\n )\n this.root = root\n }\n}\n\n/**\n * A write to a file of the configuration that is not the one in use, as its JSON file named where\n * package.json holds it. A usage failure: it exits 2.\n */\nexport class ConfigFileMismatchError extends ConfigError {\n /** The file that holds the configuration, by its absolute path. */\n readonly file: string\n /** The file named. */\n readonly named: string\n override readonly exitCode: number = 2\n\n constructor(file: string, named: string) {\n super(\n 'CONFIG_FILE_MISMATCH',\n `The configuration is in ${file}, and not in ${named}: write it there, or move it.`\n )\n this.file = file\n this.named = named\n }\n}\n\n/**\n * A write to a file that is no JSON file of the names of the configuration, nor package.json: one\n * of code, or one its names do not hold. Its message names those that are written. A usage\n * failure: it exits 2.\n */\nexport class UnwritableConfigFileError extends ConfigError {\n /** The file named. */\n readonly named: string\n override readonly exitCode: number = 2\n\n constructor(named: string, written: readonly string[]) {\n super(\n 'UNWRITABLE_CONFIG_FILE',\n `${named} is no file the configuration is written to: name ${[...written, 'package.json'].join(' or ')}.`\n )\n this.named = named\n }\n}\n\n/** A value of the configuration that is not set, which was asked for. */\nexport class NoConfigValueError extends ConfigError {\n /** The path, as it was given. */\n readonly path: string\n /** The file of the configuration, by its absolute path, or `undefined` when there is none. */\n readonly file: string | undefined\n\n constructor(path: string, file: string | undefined) {\n super(\n 'NO_CONFIG_VALUE',\n file === undefined\n ? `${path} is not set: the project has no configuration.`\n : `${path} is not set in ${file}.`\n )\n this.path = path\n this.file = file\n }\n}\n\n/**\n * A value of the configuration that points at a variable of the environment, by a placeholder\n * `${env:NAME}`, which is not set, or is empty. Its message names the path of the value, from the\n * key that names the configuration, and the variable, and holds no value.\n */\nexport class EnvPlaceholderUnsetError extends ConfigError {\n /** The path of the value that holds the placeholder. */\n readonly path: ConfigPath\n /** The name of the variable of the environment. */\n readonly variable: string\n\n constructor(path: ConfigPath, variable: string) {\n super(\n 'CONFIG_ENV_PLACEHOLDER_UNSET',\n `${textOfPath(path)}: points at the variable ${variable} of the environment by \\${env:${variable}}, which is not set, or is empty.`\n )\n this.path = path\n this.variable = variable\n }\n}\n\n/**\n * A configuration of code, as `wtf.config.ts`, loaded synchronously, as `loadConfigSync` of ./load\n * loads it for a tool that reads its config synchronously: code is imported, which is\n * asynchronous. Its message names the files a synchronous load reads, JSON and package.json's key.\n */\nexport class SyncConfigInCodeError extends ConfigError {\n /** The file of code, by its absolute path. */\n readonly file: string\n\n constructor(file: string, read: { readonly files: readonly string[]; readonly key: string }) {\n const names = [...read.files, `the \"${read.key}\" key of package.json`]\n super(\n 'CONFIG_IN_CODE_SYNC',\n `The configuration is code, in ${file}, which a synchronous load cannot import: write it in ${names.join(' or ')}, or load it with loadConfig.`\n )\n this.file = file\n }\n}\n\n/**\n * A section checked synchronously, as `sectionOfSync` of ./section checks it, by a schema that\n * checks asynchronously: a Standard Schema whose `validate` gives a promise, or a function that\n * does.\n */\nexport class SyncSchemaError extends ConfigError {\n /** The section, by its name. */\n readonly section: string\n\n constructor(section: string) {\n super(\n 'SCHEMA_NOT_SYNC',\n `The schema of the ${section} section checks it asynchronously: read it with sectionOf.`\n )\n this.section = section\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC;wDACwD;AACxD,MAAM,OAAO,WAAY,SAAQ,KAAK;IAC3B,IAAI,CAAQ;IACrB,kDAAkD;IACzC,QAAQ,GAAW,CAAC,CAAA;IAE7B,YAAY,IAAY,EAAE,OAAe,EAAE,OAAsB;QAC/D,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAA;IAC7B,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,sBAAuB,SAAQ,WAAW;IACrD,sCAAsC;IAC7B,IAAI,CAAQ;IAErB,YAAY,IAAY,EAAE,MAAc,EAAE,OAAsB;QAC9D,KAAK,CAAC,qBAAqB,EAAE,GA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AAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,EAAE,CAAA;IACpC,CAAC;CACF","sourcesContent":["/**\n * What `@wtf/project-config` throws, each a {@link ConfigError} of a stable `code`:\n * {@link InvalidConfigFileError}, a file of the configuration that holds no object;\n * {@link InvalidSectionError}, a section its tool's schema refuses; {@link ConfigPathError}, a path\n * that names no value; {@link ConfigInCodeError}, a write to a configuration of code;\n * {@link NoConfigFileError}, a write with no package.json to hold it;\n * {@link ConfigFileMismatchError}, a write to a file that is not the one in use;\n * {@link UnwritableConfigFileError}, a write to a file that is never written;\n * {@link NoConfigValueError}, a value asked for that is not set; and\n * {@link EnvPlaceholderUnsetError}, a value that points at a variable of the environment that is\n * not set; {@link SyncConfigInCodeError}, a configuration of code loaded synchronously; and\n * {@link SyncSchemaError}, a section checked synchronously by a schema that checks asynchronously;\n * and {@link NamedError}, a reference of ./named that names no entry, or a module, an export or a\n * value that is not what it says. One that the arguments of a person cause exits 2.\n *\n * @packageDocumentation\n */\nimport type { ConfigIssue, ConfigPath } from './index.ts'\nimport { textOfPath } from './text.ts'\n\n/** What this package throws: an Error with a stable SCREAMING_SNAKE `code`, and the `exitCode` a\n * command that fails by it exits with, 1 by default. */\nexport class ConfigError extends Error {\n readonly code: string\n /** What a command that fails by it exits with. */\n readonly exitCode: number = 1\n\n constructor(code: string, message: string, options?: ErrorOptions) {\n super(message, options)\n this.code = code\n this.name = new.target.name\n }\n}\n\n/**\n * A file of the configuration that holds no configuration: no JSON, no object, or a module that\n * fails to load, or exports no object.\n */\nexport class InvalidConfigFileError extends ConfigError {\n /** The file, by its absolute path. */\n readonly file: string\n\n constructor(file: string, reason: string, options?: ErrorOptions) {\n super('INVALID_CONFIG_FILE', `${file}: ${reason}`, options)\n this.file = file\n }\n}\n\n/**\n * A section of the configuration that the schema of its tool refuses: each issue, by the file and\n * the path of the value, from the key that names the configuration, the namespace by default.\n */\nexport class InvalidSectionError extends ConfigError {\n /** The file of the configuration, by its absolute path. */\n readonly file: string\n /** The section, by its name. */\n readonly section: string\n /** What is wrong, each by its path in the section. */\n readonly issues: readonly ConfigIssue[]\n\n constructor(\n file: string,\n namespace: string,\n section: string,\n issues: readonly ConfigIssue[],\n key: string = namespace\n ) {\n super(\n 'INVALID_CONFIG_SECTION',\n [\n `${file}: the ${section} section of the ${namespace} configuration is not valid:`,\n ...issues.map(({ path, message }) => ` ${textOfPath([key, section, ...path])}: ${message}`)\n ].join('\\n')\n )\n this.file = file\n this.section = section\n this.issues = issues\n }\n}\n\n/**\n * A path of the configuration that names no value to read or write: an empty one, or one that\n * goes into a value that is no object or list. A usage failure: it exits 2.\n */\nexport class ConfigPathError extends ConfigError {\n /** The path, as it was given. */\n readonly path: string\n override readonly exitCode: number = 2\n\n constructor(path: string, reason: string) {\n super('INVALID_CONFIG_PATH', `${path === '' ? 'The path' : path} ${reason}`)\n this.path = path\n }\n}\n\n/**\n * A write to a configuration of code, as `wtf.config.mjs`, `wtf.config.js` or `wtf.config.ts`: code\n * is changed by a person, and not by a tool. Its message names what is written: the JSON files, and\n * the key of package.json.\n */\nexport class ConfigInCodeError extends ConfigError {\n /** The file of code, by its absolute path. */\n readonly file: string\n\n constructor(file: string, written: { readonly files: readonly string[]; readonly key: string }) {\n const names = [...written.files, `the \"${written.key}\" key of package.json`]\n super(\n 'CONFIG_IN_CODE',\n `The configuration is code, in ${file}: change it there. Only ${names.join(' and ')} ${names.length === 1 ? 'is' : 'are'} written.`\n )\n this.file = file\n }\n}\n\n/**\n * A write to the configuration of a project that has none, in the key of package.json, the\n * default, where the root has no package.json. Its message names the key, and the JSON file to make\n * it in instead, if there is one. A usage failure: it exits 2.\n */\nexport class NoConfigFileError extends ConfigError {\n /** The root of the project. */\n readonly root: string\n override readonly exitCode: number = 2\n\n constructor(\n root: string,\n {\n namespace,\n key = namespace,\n json\n }: { readonly namespace: string; readonly key?: string; readonly json?: string | undefined }\n ) {\n super(\n 'NO_CONFIG_FILE',\n `No package.json is in ${root} to hold the ${namespace} configuration in its \"${key}\" key: make one there${json === undefined ? '' : `, or name ${json} to make the configuration in`}.`\n )\n this.root = root\n }\n}\n\n/**\n * A write to a file of the configuration that is not the one in use, as its JSON file named where\n * package.json holds it. A usage failure: it exits 2.\n */\nexport class ConfigFileMismatchError extends ConfigError {\n /** The file that holds the configuration, by its absolute path. */\n readonly file: string\n /** The file named. */\n readonly named: string\n override readonly exitCode: number = 2\n\n constructor(file: string, named: string) {\n super(\n 'CONFIG_FILE_MISMATCH',\n `The configuration is in ${file}, and not in ${named}: write it there, or move it.`\n )\n this.file = file\n this.named = named\n }\n}\n\n/**\n * A write to a file that is no JSON file of the names of the configuration, nor package.json: one\n * of code, or one its names do not hold. Its message names those that are written. A usage\n * failure: it exits 2.\n */\nexport class UnwritableConfigFileError extends ConfigError {\n /** The file named. */\n readonly named: string\n override readonly exitCode: number = 2\n\n constructor(named: string, written: readonly string[]) {\n super(\n 'UNWRITABLE_CONFIG_FILE',\n `${named} is no file the configuration is written to: name ${[...written, 'package.json'].join(' or ')}.`\n )\n this.named = named\n }\n}\n\n/** A value of the configuration that is not set, which was asked for. */\nexport class NoConfigValueError extends ConfigError {\n /** The path, as it was given. */\n readonly path: string\n /** The file of the configuration, by its absolute path, or `undefined` when there is none. */\n readonly file: string | undefined\n\n constructor(path: string, file: string | undefined) {\n super(\n 'NO_CONFIG_VALUE',\n file === undefined\n ? `${path} is not set: the project has no configuration.`\n : `${path} is not set in ${file}.`\n )\n this.path = path\n this.file = file\n }\n}\n\n/**\n * A value of the configuration that points at a variable of the environment, by a placeholder\n * `${env:NAME}`, which is not set, or is empty. Its message names the path of the value, from the\n * key that names the configuration, and the variable, and holds no value.\n */\nexport class EnvPlaceholderUnsetError extends ConfigError {\n /** The path of the value that holds the placeholder. */\n readonly path: ConfigPath\n /** The name of the variable of the environment. */\n readonly variable: string\n\n constructor(path: ConfigPath, variable: string) {\n super(\n 'CONFIG_ENV_PLACEHOLDER_UNSET',\n `${textOfPath(path)}: points at the variable ${variable} of the environment by \\${env:${variable}}, which is not set, or is empty.`\n )\n this.path = path\n this.variable = variable\n }\n}\n\n/**\n * A configuration of code, as `wtf.config.ts`, loaded synchronously, as `loadConfigSync` of ./load\n * loads it for a tool that reads its config synchronously: code is imported, which is\n * asynchronous. Its message names the files a synchronous load reads, JSON and package.json's key.\n */\nexport class SyncConfigInCodeError extends ConfigError {\n /** The file of code, by its absolute path. */\n readonly file: string\n\n constructor(file: string, read: { readonly files: readonly string[]; readonly key: string }) {\n const names = [...read.files, `the \"${read.key}\" key of package.json`]\n super(\n 'CONFIG_IN_CODE_SYNC',\n `The configuration is code, in ${file}, which a synchronous load cannot import: write it in ${names.join(' or ')}, or load it with loadConfig.`\n )\n this.file = file\n }\n}\n\n/**\n * A section checked synchronously, as `sectionOfSync` of ./section checks it, by a schema that\n * checks asynchronously: a Standard Schema whose `validate` gives a promise, or a function that\n * does.\n */\nexport class SyncSchemaError extends ConfigError {\n /** The section, by its name. */\n readonly section: string\n\n constructor(section: string) {\n super(\n 'SCHEMA_NOT_SYNC',\n `The schema of the ${section} section checks it asynchronously: read it with sectionOf.`\n )\n this.section = section\n }\n}\n\n/**\n * The codes of a {@link NamedError}: `UNKNOWN_NAME`, a reference of a name of no entry;\n * `UNLOADABLE_MODULE`, a module that fails to load, or of no loader given; `MISSING_EXPORT`, an\n * export the module has not; and `INVALID_NAMED_VALUE`, a reference, a set or a value that is not\n * of its shape, or that its check refuses.\n */\nexport type NamedErrorCode =\n | 'UNKNOWN_NAME'\n | 'UNLOADABLE_MODULE'\n | 'MISSING_EXPORT'\n | 'INVALID_NAMED_VALUE'\n\n/** What a {@link NamedError} names, of its code: where it failed, the name and those there are,\n * the module and its export, or the issues of a check. */\nexport type NamedErrorDetails = {\n /** Where the value that failed is, by its path from what the caller gave, of any code. */\n readonly at?: ConfigPath\n /** The index of the set that failed in the array of sets, of any code. */\n readonly set?: number\n /** The name of the entry that failed, of a set or of a reference, of any code. */\n readonly entry?: string\n /** The name of no entry, of `UNKNOWN_NAME`. */\n readonly name?: string\n /** The names of the entries there are, of `UNKNOWN_NAME`. */\n readonly names?: readonly string[]\n /** The module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a value it exports. */\n readonly module?: string\n /** The name of its export, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a value it exports. */\n readonly export?: string\n /** What the check found wrong, of `INVALID_NAMED_VALUE`. */\n readonly issues?: readonly ConfigIssue[]\n}\n\n/**\n * A reference of ./named that cannot be resolved, of a stable code of {@link NamedErrorCode}: a\n * name of no entry, whose message lists the names there are; a module that fails to load; an\n * export it has not; or a reference, a set or a value of no shape it takes. Of any code, it names\n * where it failed: its path, `at`, the index of its `set` and the name of its `entry`, and the\n * `module` and `export` a value came from. A tool maps each code to its own, as it chooses.\n */\nexport class NamedError extends ConfigError {\n declare readonly code: NamedErrorCode\n /**\n * Where the value that failed is, by its path from what the caller gave: the path the caller\n * gives as `at`, the index of the set in an array of sets, and the name of the entry, as\n * `['formatters', '1', 'wide']`; empty when nothing narrows it.\n */\n readonly at: ConfigPath\n /** The index of the set that failed in the array of sets: none of one set alone, or of a reference. */\n readonly set: number | undefined\n /** The name of the entry that failed, of a set or of the one a reference names: none of others. */\n readonly entry: string | undefined\n /** The name of no entry, of `UNKNOWN_NAME`. */\n readonly unknown: string | undefined\n /** The names of the entries there are, of `UNKNOWN_NAME`; none of any other code. */\n readonly names: readonly string[]\n /** The module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a value it exports. */\n readonly module: string | undefined\n /** The name of the export of the module, of `UNLOADABLE_MODULE` and `MISSING_EXPORT`, and of a\n * value it exports. */\n readonly export: string | undefined\n /** What the check found wrong, of `INVALID_NAMED_VALUE` of a value it refuses; none of others. */\n readonly issues: readonly ConfigIssue[]\n\n constructor(\n code: NamedErrorCode,\n message: string,\n details: NamedErrorDetails = {},\n options?: ErrorOptions\n ) {\n super(code, message, options)\n this.at = details.at ?? []\n this.set = details.set\n this.entry = details.entry\n this.unknown = details.name\n this.names = details.names ?? []\n this.module = details.module\n this.export = details.export\n this.issues = details.issues ?? []\n }\n}\n"]}
|
package/dist/named.d.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value or a function a configuration of JSON names, where JSON cannot hold it, and the code
|
|
3
|
+
* that finds it: a set of named entries, as a tool gives some and a project adds its own, and a
|
|
4
|
+
* reference to one of them, or to the export of a module. {@link resolveNamed} gives the value a
|
|
5
|
+
* reference is; {@link resolveNamedSet} the entries of one set or of sets merged in order;
|
|
6
|
+
* {@link factoryOf} the entry of a plain value, the options of a reference merged over it; and
|
|
7
|
+
* {@link namedSchema} and {@link namedSetSchema} the schema of a reference and of a set, a
|
|
8
|
+
* Standard Schema that checks it and gives its JSON Schema, so an editor completes the names.
|
|
9
|
+
*
|
|
10
|
+
* A reference is one of three forms:
|
|
11
|
+
*
|
|
12
|
+
* - `"short"`: the entry of that name;
|
|
13
|
+
* - `{ "format": "short", "width": 60 }`: the entry of that name, by a key the caller chooses,
|
|
14
|
+
* with the options beside it, which the entry is given;
|
|
15
|
+
* - `{ "module": "./formatters.mjs", "export": "wide" }`: the export of a module, `default` by
|
|
16
|
+
* default, as it is.
|
|
17
|
+
*
|
|
18
|
+
* Each entry is a factory of the value, `(options) => value`: a function, which takes the options
|
|
19
|
+
* as it chooses, or a plain value, whose options are merged over it ({@link factoryOf}). A set is
|
|
20
|
+
* a map of entries by name, the export of a module that is one, or an array of those, a later set
|
|
21
|
+
* over an earlier one by name. A module is loaded by what the caller gives, `nodeLoadModule` of
|
|
22
|
+
* ./node of Node's, so this module runs on any platform. What fails is a `NamedError` of ./errors,
|
|
23
|
+
* which names where: the path, from where the caller says the sets or the reference are, the index
|
|
24
|
+
* of the set and the name of the entry, and the module and the export.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { factoryOf, resolveNamed, resolveNamedSet } from '@wtf/project-config/named'
|
|
28
|
+
*
|
|
29
|
+
* type Format = { readonly width: number; readonly ellipsis: string }
|
|
30
|
+
*
|
|
31
|
+
* const formatters = await resolveNamedSet<Format>([
|
|
32
|
+
* { short: factoryOf({ width: 40, ellipsis: '…' }) },
|
|
33
|
+
* { wide: { width: 120, ellipsis: '' } }
|
|
34
|
+
* ])
|
|
35
|
+
* const format = await resolveNamed<Format>(
|
|
36
|
+
* { format: 'short', width: 60 },
|
|
37
|
+
* { key: 'format', names: formatters, kind: 'formatter' }
|
|
38
|
+
* )
|
|
39
|
+
* // { width: 60, ellipsis: '…' }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @packageDocumentation
|
|
43
|
+
*/
|
|
44
|
+
import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec';
|
|
45
|
+
import type { ConfigPath, SectionSchema } from './index.ts';
|
|
46
|
+
/**
|
|
47
|
+
* The export of a module, as a configuration of JSON names it: the module, by what the loader
|
|
48
|
+
* resolves, as a path from the root of the project or the name of a package, and the name of its
|
|
49
|
+
* export, `default` by default.
|
|
50
|
+
*/
|
|
51
|
+
export type ModuleReference = {
|
|
52
|
+
/** The module, as the loader resolves it: `./formatters.mjs`, or a package. */
|
|
53
|
+
readonly module: string;
|
|
54
|
+
/** The name of its export: `default` by default. */
|
|
55
|
+
readonly export?: string | undefined;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* A reference to an entry by its name under the key `K`, with the options beside it, which the
|
|
59
|
+
* entry is given: `{ "format": "short", "width": 60 }` of the key `format`. Of a key not known, as
|
|
60
|
+
* `string`, any object of options.
|
|
61
|
+
*/
|
|
62
|
+
export type NamedUse<K extends string = string> = string extends K ? NamedOptions : {
|
|
63
|
+
readonly [P in K]: string;
|
|
64
|
+
} & NamedOptions;
|
|
65
|
+
/**
|
|
66
|
+
* A reference, as a configuration of JSON gives it: the name of an entry; the name under the key
|
|
67
|
+
* `K`, with options; or the export of a module.
|
|
68
|
+
*/
|
|
69
|
+
export type NamedReference<K extends string = string> = string | NamedUse<K> | ModuleReference;
|
|
70
|
+
/** The options a reference gives an entry: each beside its name, but the key and any `undefined`. */
|
|
71
|
+
export type NamedOptions = Readonly<Record<string, unknown>>;
|
|
72
|
+
/** An entry of a set: what makes the value of the options a reference gives, none of a name alone. */
|
|
73
|
+
export type NamedFactory<T> = (options: NamedOptions) => T | Promise<T>;
|
|
74
|
+
/** The entries of a set, each a factory, by their names, as {@link resolveNamedSet} gives them. */
|
|
75
|
+
export type NamedEntries<T> = Readonly<Record<string, NamedFactory<T>>>;
|
|
76
|
+
/**
|
|
77
|
+
* A set of entries, as a configuration gives it: a map of each by its name, a factory, a plain
|
|
78
|
+
* value or the export of a module that is one of those; or the export of a module that is such a
|
|
79
|
+
* map.
|
|
80
|
+
*/
|
|
81
|
+
export type NamedSet<T> = ModuleReference | Readonly<Record<string, NamedFactory<T> | ModuleReference | T>>;
|
|
82
|
+
/** The sets of a configuration: one, or an array of them, merged in order, each over those before. */
|
|
83
|
+
export type NamedSets<T> = NamedSet<T> | readonly NamedSet<T>[];
|
|
84
|
+
/**
|
|
85
|
+
* What loads a module of a {@link ModuleReference}: the module of the text it names, as an object
|
|
86
|
+
* of its exports, or a failure, which a `NamedError` of `UNLOADABLE_MODULE` holds as its cause.
|
|
87
|
+
*/
|
|
88
|
+
export type LoadModule = (module: string) => Promise<unknown>;
|
|
89
|
+
/**
|
|
90
|
+
* What a set and a reference are resolved by alike: the loader of their modules, the check of
|
|
91
|
+
* their values, their kind, and where they are in the configuration.
|
|
92
|
+
*/
|
|
93
|
+
export type NamedCheckOptions<T> = {
|
|
94
|
+
/** What loads each module named: none by default, of which a module is unloadable. */
|
|
95
|
+
readonly load?: LoadModule | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* What checks a value: a Standard Schema, a JSON Schema or a function, as `issuesOf` of
|
|
98
|
+
* ./section takes it. Of a set, each value an entry is, of a map or of a module, but a function,
|
|
99
|
+
* whose results it checks too of `checkResults`; of a reference, the value it resolves to.
|
|
100
|
+
*/
|
|
101
|
+
readonly check?: SectionSchema<T> | undefined;
|
|
102
|
+
/** What the entries are, which an error names: `entry` by default, as `formatter`. */
|
|
103
|
+
readonly kind?: string | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* Where the sets, or the reference, are in the configuration, which the `at` of an error starts
|
|
106
|
+
* with, and its message names, as `['formatters']`: none by default.
|
|
107
|
+
*/
|
|
108
|
+
readonly at?: ConfigPath | undefined;
|
|
109
|
+
};
|
|
110
|
+
/** How a set is resolved: as {@link NamedCheckOptions} say, and whether `check` checks what a
|
|
111
|
+
* function entry gives. */
|
|
112
|
+
export type NamedSetOptions<T> = NamedCheckOptions<T> & {
|
|
113
|
+
/**
|
|
114
|
+
* Whether `check` checks too what each function entry gives, each time it is called, as it
|
|
115
|
+
* checks a value entry once: no by default, of which a function entry is used as it is.
|
|
116
|
+
*/
|
|
117
|
+
readonly checkResults?: boolean | undefined;
|
|
118
|
+
};
|
|
119
|
+
/** How a reference is resolved: the key of its names, its entries, and what
|
|
120
|
+
* {@link NamedCheckOptions} say. */
|
|
121
|
+
export type NamedResolveOptions<T> = NamedCheckOptions<T> & {
|
|
122
|
+
/** The key a reference of options names its entry by, as `format`: none takes a name alone. */
|
|
123
|
+
readonly key?: string | undefined;
|
|
124
|
+
/** The entries a reference names, by their names: none by default. */
|
|
125
|
+
readonly names?: NamedEntries<T> | undefined;
|
|
126
|
+
};
|
|
127
|
+
/** The schema of a reference or a set: a Standard Schema that checks it, and gives its JSON Schema. */
|
|
128
|
+
export type NamedSchema<T> = StandardSchemaV1<unknown, T> & StandardJSONSchemaV1<unknown, T>;
|
|
129
|
+
/**
|
|
130
|
+
* The entry of `value`: of a plain object, `value` with the options a reference gives beside the
|
|
131
|
+
* name merged over it, key by key, and those of `undefined` left out, so
|
|
132
|
+
* `{ format: 'short', width: 60 }` gives `{ width: 60, ellipsis: '…' }` of
|
|
133
|
+
* `{ width: 40, ellipsis: '…' }`; of any other value, the value, which takes no options, so options
|
|
134
|
+
* given are a `NamedError` of `INVALID_NAMED_VALUE`. {@link resolveNamedSet} makes each value of a
|
|
135
|
+
* set that is no function an entry so.
|
|
136
|
+
*/
|
|
137
|
+
export declare function factoryOf<T>(value: T): NamedFactory<T>;
|
|
138
|
+
/**
|
|
139
|
+
* The entries of `sets`, each a factory, by their names: of each set in order, a map of entries by
|
|
140
|
+
* name or the export of a module that is one, a later set over an earlier one by name, so a
|
|
141
|
+
* project's own are over those of a package; none of `undefined`. Each entry is a function, a
|
|
142
|
+
* factory as it is, or a plain value, checked by `check`, of {@link factoryOf}; or the export of
|
|
143
|
+
* a module that is one of those, loaded by `load`. Of `checkResults`, `check` checks what each
|
|
144
|
+
* function entry gives too, each time it is called. Each module is loaded at once, so a set that
|
|
145
|
+
* fails does before any entry is used. Fails with a `NamedError` of `UNLOADABLE_MODULE` or
|
|
146
|
+
* `MISSING_EXPORT` of a module, and of `INVALID_NAMED_VALUE` of a set that is no map, or a value
|
|
147
|
+
* `check` refuses. The error names where it failed: its `at` is the path `at` gives, the index of
|
|
148
|
+
* the set in an array of sets, its `set`, and the name of the entry, its `entry`, as
|
|
149
|
+
* `['formatters', '1', 'wide']`, and its message starts with the path of the set; and its `module`
|
|
150
|
+
* and `export` are those of a set or an entry a module exports.
|
|
151
|
+
*/
|
|
152
|
+
export declare function resolveNamedSet<T>(sets: NamedSets<T> | undefined, options?: NamedSetOptions<T>): Promise<NamedEntries<T>>;
|
|
153
|
+
/**
|
|
154
|
+
* The value `reference` is: of a name, the entry of that name of `names`, given no options; of an
|
|
155
|
+
* object of the `key`, the entry of the name under it, given the options beside it but those of
|
|
156
|
+
* `undefined`; of the export of a module, `{ module, export }`, the export as it is, loaded by
|
|
157
|
+
* `load`. With a `check`, the value is checked by it. Fails with a `NamedError`: `UNKNOWN_NAME` of
|
|
158
|
+
* a name of no entry, whose message lists the names there are; `UNLOADABLE_MODULE` and
|
|
159
|
+
* `MISSING_EXPORT` of a module; and `INVALID_NAMED_VALUE` of a reference of none of the forms, or
|
|
160
|
+
* a value `check` refuses. The error names where it failed: its `at` is the path `at` gives, which
|
|
161
|
+
* its message starts with; its `entry` the name of the entry whose value fails, or which fails as
|
|
162
|
+
* it is given its options; and its `module` and `export` those of a reference to a module.
|
|
163
|
+
*/
|
|
164
|
+
export declare function resolveNamed<T>(reference: NamedReference, { key, names, load, check, kind, at }?: NamedResolveOptions<T>): Promise<T>;
|
|
165
|
+
/** What the schema of a reference says of it. */
|
|
166
|
+
export type NamedSchemaOptions = {
|
|
167
|
+
/** The names an editor completes: those of the entries there are. */
|
|
168
|
+
readonly names?: readonly string[] | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* The schema of the options beside a name, as each entry takes them: a JSON Schema, or a
|
|
171
|
+
* Standard Schema that gives one, of an object, whose `properties` are each an option. Any by
|
|
172
|
+
* default.
|
|
173
|
+
*/
|
|
174
|
+
readonly options?: SectionSchema | undefined;
|
|
175
|
+
/** What the entries are, which the descriptions name: `entry` by default. */
|
|
176
|
+
readonly kind?: string | undefined;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* The schema of a reference of the key `key`: a name, of `names`, which an editor completes;
|
|
180
|
+
* `{ [key]: name, … }`, with the options of `options` beside it, each optional, as they are over
|
|
181
|
+
* the entry's own; or `{ module, export }`. Its JSON Schema, which `input()` of its
|
|
182
|
+
* `~standard.jsonSchema` gives, holds no `$schema`, so it goes in a tool's own as it is.
|
|
183
|
+
*/
|
|
184
|
+
export declare function namedSchema<K extends string>(key: K, { names, options, kind }?: NamedSchemaOptions): NamedSchema<NamedReference<K>>;
|
|
185
|
+
/** What the schema of a set says of it. */
|
|
186
|
+
export type NamedSetSchemaOptions = {
|
|
187
|
+
/** The schema of the value of an entry: a JSON Schema, or a Standard Schema that gives one. Any by default. */
|
|
188
|
+
readonly entry?: SectionSchema | undefined;
|
|
189
|
+
/** What the entries are, which the descriptions name: `entry` by default. */
|
|
190
|
+
readonly kind?: string | undefined;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* The schema of the sets of entries: a set, or an array of sets, merged in order; each set the
|
|
194
|
+
* export of a module, or a map of each entry by its name, of `entry`, or the export of a module.
|
|
195
|
+
* Its JSON Schema holds no `$schema`, as {@link namedSchema}'s.
|
|
196
|
+
*/
|
|
197
|
+
export declare function namedSetSchema({ entry, kind }?: NamedSetSchemaOptions): NamedSchema<NamedSets<unknown>>;
|
package/dist/named.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { jsonSchemaOf } from "./env.js";
|
|
2
|
+
import { NamedError } from "./errors.js";
|
|
3
|
+
import { issuesOf, issuesOfSync } from "./section.js";
|
|
4
|
+
import { textOfPath } from "./text.js";
|
|
5
|
+
/** Whether `value` is an object of keys, as JSON's: no list, and no instance of a class. */
|
|
6
|
+
const isPlain = (value) => {
|
|
7
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
8
|
+
return false;
|
|
9
|
+
const prototype = Object.getPrototypeOf(value);
|
|
10
|
+
return prototype === Object.prototype || prototype === null;
|
|
11
|
+
};
|
|
12
|
+
/** Whether `value` is the export of a module: an object whose `module` is a text. */
|
|
13
|
+
const isModule = (value) => isPlain(value) && typeof value['module'] === 'string';
|
|
14
|
+
/** `word` after its article, `a` or `an`, as `an entry`. */
|
|
15
|
+
const article = (word) => `${/^[aeiou]/i.test(word) ? 'an' : 'a'} ${word}`;
|
|
16
|
+
/** `text` of a capital first. */
|
|
17
|
+
const capital = (text) => `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
|
|
18
|
+
/** `value` as an error names it. */
|
|
19
|
+
const textOf = (value) => typeof value === 'function' ? 'a function' : (JSON.stringify(value) ?? String(value));
|
|
20
|
+
/** The options of `options` that are given: each but those of `undefined`. */
|
|
21
|
+
const givenOf = (options) => Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined));
|
|
22
|
+
/**
|
|
23
|
+
* The entry of `value`: of a plain object, `value` with the options a reference gives beside the
|
|
24
|
+
* name merged over it, key by key, and those of `undefined` left out, so
|
|
25
|
+
* `{ format: 'short', width: 60 }` gives `{ width: 60, ellipsis: '…' }` of
|
|
26
|
+
* `{ width: 40, ellipsis: '…' }`; of any other value, the value, which takes no options, so options
|
|
27
|
+
* given are a `NamedError` of `INVALID_NAMED_VALUE`. {@link resolveNamedSet} makes each value of a
|
|
28
|
+
* set that is no function an entry so.
|
|
29
|
+
*/
|
|
30
|
+
export function factoryOf(value) {
|
|
31
|
+
return (options) => {
|
|
32
|
+
const given = givenOf(options);
|
|
33
|
+
if (isPlain(value))
|
|
34
|
+
return { ...value, ...given };
|
|
35
|
+
if (Object.keys(given).length === 0)
|
|
36
|
+
return value;
|
|
37
|
+
throw new NamedError('INVALID_NAMED_VALUE', `${textOf(value)} takes no options, and is given ${Object.keys(given).join(', ')}.`);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** The text of the issues a check found. */
|
|
41
|
+
const issuesText = (issues) => issues
|
|
42
|
+
.map(({ path, message }) => (path.length === 0 ? message : `${path.join('.')}: ${message}`))
|
|
43
|
+
.join('; ');
|
|
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 = {}) {
|
|
66
|
+
if (check === undefined)
|
|
67
|
+
return value;
|
|
68
|
+
const { issues, value: read } = await issuesOf(value, check);
|
|
69
|
+
if (issues.length === 0)
|
|
70
|
+
return read;
|
|
71
|
+
throw namedError('INVALID_NAMED_VALUE', `${failing}: ${issuesText(issues)}.`, place, {
|
|
72
|
+
...details,
|
|
73
|
+
issues
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The export `reference.export`, `default` by default, of the module `reference.module`, loaded by
|
|
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.
|
|
80
|
+
*/
|
|
81
|
+
async function exportOf(reference, load, place) {
|
|
82
|
+
const { module } = reference;
|
|
83
|
+
const name = reference.export ?? 'default';
|
|
84
|
+
const details = moduleDetails(reference);
|
|
85
|
+
const as = place.as === undefined ? '' : ` as ${place.as}`;
|
|
86
|
+
if (load === undefined)
|
|
87
|
+
throw namedError('UNLOADABLE_MODULE', `The module ${module} cannot be loaded, for its export ${name}${as}: no loader of modules is given.`, place, details);
|
|
88
|
+
let loaded;
|
|
89
|
+
try {
|
|
90
|
+
loaded = await load(module);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
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 });
|
|
94
|
+
}
|
|
95
|
+
const exports = loaded;
|
|
96
|
+
const found = typeof exports === 'object' && exports !== null ? exports[name] : undefined;
|
|
97
|
+
if (found === undefined)
|
|
98
|
+
throw namedError('MISSING_EXPORT', `The module ${module} has no export ${name}${as}.`, place, details);
|
|
99
|
+
return found;
|
|
100
|
+
}
|
|
101
|
+
/** What a module's export is, as an error names it. */
|
|
102
|
+
const exportText = ({ module, export: name = 'default' }) => `The export ${name} of ${module}`;
|
|
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));
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The entries of `sets`, each a factory, by their names: of each set in order, a map of entries by
|
|
126
|
+
* name or the export of a module that is one, a later set over an earlier one by name, so a
|
|
127
|
+
* project's own are over those of a package; none of `undefined`. Each entry is a function, a
|
|
128
|
+
* factory as it is, or a plain value, checked by `check`, of {@link factoryOf}; or the export of
|
|
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
|
|
131
|
+
* fails does before any entry is used. Fails with a `NamedError` of `UNLOADABLE_MODULE` or
|
|
132
|
+
* `MISSING_EXPORT` of a module, and of `INVALID_NAMED_VALUE` of a set that is no map, or a value
|
|
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.
|
|
137
|
+
*/
|
|
138
|
+
export async function resolveNamedSet(sets, options = {}) {
|
|
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` });
|
|
149
|
+
if (!isPlain(map))
|
|
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));
|
|
151
|
+
const entries = await Promise.all(Object.entries(map).map(async ([name, value]) => [
|
|
152
|
+
name,
|
|
153
|
+
await entryOf(value, name, { ...place, at: [...place.at, name], entry: name }, options)
|
|
154
|
+
]));
|
|
155
|
+
return Object.fromEntries(entries);
|
|
156
|
+
}));
|
|
157
|
+
return Object.assign({}, ...resolved);
|
|
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
|
+
});
|
|
167
|
+
/**
|
|
168
|
+
* The value `reference` is: of a name, the entry of that name of `names`, given no options; of an
|
|
169
|
+
* object of the `key`, the entry of the name under it, given the options beside it but those of
|
|
170
|
+
* `undefined`; of the export of a module, `{ module, export }`, the export as it is, loaded by
|
|
171
|
+
* `load`. With a `check`, the value is checked by it. Fails with a `NamedError`: `UNKNOWN_NAME` of
|
|
172
|
+
* a name of no entry, whose message lists the names there are; `UNLOADABLE_MODULE` and
|
|
173
|
+
* `MISSING_EXPORT` of a module; and `INVALID_NAMED_VALUE` of a reference of none of the forms, or
|
|
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.
|
|
177
|
+
*/
|
|
178
|
+
export async function resolveNamed(reference, { key, names = {}, load, check, kind = 'entry', at = [] } = {}) {
|
|
179
|
+
const place = { at, where: at };
|
|
180
|
+
const use = reference;
|
|
181
|
+
if (isModule(use) && (key === undefined || !(key in use)))
|
|
182
|
+
return checked(await exportOf(use, load, place), check, `${exportText(use)} is not valid`, place, moduleDetails(use));
|
|
183
|
+
const named = typeof use === 'string'
|
|
184
|
+
? { name: use, options: {} }
|
|
185
|
+
: isPlain(use) && key !== undefined && typeof use[key] === 'string'
|
|
186
|
+
? (() => {
|
|
187
|
+
const { [key]: name, ...options } = use;
|
|
188
|
+
return { name: name, options: givenOf(options) };
|
|
189
|
+
})()
|
|
190
|
+
: undefined;
|
|
191
|
+
if (named === undefined)
|
|
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);
|
|
193
|
+
const { name, options } = named;
|
|
194
|
+
if (!Object.hasOwn(names, name)) {
|
|
195
|
+
const known = Object.keys(names);
|
|
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 });
|
|
197
|
+
}
|
|
198
|
+
const entry = names[name];
|
|
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);
|
|
210
|
+
}
|
|
211
|
+
/** The JSON Schema of the export of a module. */
|
|
212
|
+
const MODULE_SCHEMA = {
|
|
213
|
+
description: 'The export of a module: `default` by default.',
|
|
214
|
+
type: 'object',
|
|
215
|
+
properties: {
|
|
216
|
+
module: {
|
|
217
|
+
description: 'The module: a path from the root, or a package.',
|
|
218
|
+
type: 'string',
|
|
219
|
+
minLength: 1
|
|
220
|
+
},
|
|
221
|
+
export: {
|
|
222
|
+
description: 'The name of its export: `default` by default.',
|
|
223
|
+
type: 'string',
|
|
224
|
+
minLength: 1
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
required: ['module'],
|
|
228
|
+
additionalProperties: false
|
|
229
|
+
};
|
|
230
|
+
/** The JSON Schema of a name: a text, of the `names` an editor completes. */
|
|
231
|
+
const nameSchema = (kind, names) => ({
|
|
232
|
+
description: `The name of ${article(kind)}.`,
|
|
233
|
+
type: 'string',
|
|
234
|
+
minLength: 1,
|
|
235
|
+
...(names === undefined || names.length === 0 ? {} : { examples: [...names] })
|
|
236
|
+
});
|
|
237
|
+
/** The JSON Schema of `schema`, as an object of its keywords: `{}`, any value, of none. */
|
|
238
|
+
const objectSchemaOf = (schema) => {
|
|
239
|
+
const json = schema === undefined ? undefined : jsonSchemaOf(schema);
|
|
240
|
+
if (json === undefined || json === true)
|
|
241
|
+
return {};
|
|
242
|
+
if (json === false)
|
|
243
|
+
return { not: {} };
|
|
244
|
+
const { $schema: _draft, $id: _id, ...rest } = json;
|
|
245
|
+
return rest;
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* The schema of `json`, of the type `T`: it checks a value by ajv, as `issuesOfSync` of ./section
|
|
249
|
+
* checks a JSON Schema, and gives `json`, of draft 2020-12, as the JSON Schema of its input and
|
|
250
|
+
* output alike.
|
|
251
|
+
*/
|
|
252
|
+
function schemaOf(json) {
|
|
253
|
+
const jsonSchema = () => ({ ...json });
|
|
254
|
+
return {
|
|
255
|
+
'~standard': {
|
|
256
|
+
version: 1,
|
|
257
|
+
vendor: '@wtf/project-config',
|
|
258
|
+
validate: (value) => {
|
|
259
|
+
const { issues } = issuesOfSync(value, json);
|
|
260
|
+
return issues.length === 0
|
|
261
|
+
? { value: value }
|
|
262
|
+
: { issues: issues.map(({ path, message }) => ({ path, message })) };
|
|
263
|
+
},
|
|
264
|
+
jsonSchema: { input: jsonSchema, output: jsonSchema }
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* The schema of a reference of the key `key`: a name, of `names`, which an editor completes;
|
|
270
|
+
* `{ [key]: name, … }`, with the options of `options` beside it, each optional, as they are over
|
|
271
|
+
* the entry's own; or `{ module, export }`. Its JSON Schema, which `input()` of its
|
|
272
|
+
* `~standard.jsonSchema` gives, holds no `$schema`, so it goes in a tool's own as it is.
|
|
273
|
+
*/
|
|
274
|
+
export function namedSchema(key, { names, options, kind = 'entry' } = {}) {
|
|
275
|
+
const name = nameSchema(kind, names);
|
|
276
|
+
const { properties, required: _required, type: _type, ...rest } = objectSchemaOf(options);
|
|
277
|
+
return schemaOf({
|
|
278
|
+
description: `${capital(article(kind))}: its name, { "${key}": name, … } with options over its own, or the export of a module.`,
|
|
279
|
+
anyOf: [
|
|
280
|
+
name,
|
|
281
|
+
{
|
|
282
|
+
...rest,
|
|
283
|
+
description: `${capital(article(kind))} by its name, with options over its own.`,
|
|
284
|
+
type: 'object',
|
|
285
|
+
properties: { ...(isPlain(properties) ? properties : {}), [key]: name },
|
|
286
|
+
required: [key]
|
|
287
|
+
},
|
|
288
|
+
MODULE_SCHEMA
|
|
289
|
+
]
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* The schema of the sets of entries: a set, or an array of sets, merged in order; each set the
|
|
294
|
+
* export of a module, or a map of each entry by its name, of `entry`, or the export of a module.
|
|
295
|
+
* Its JSON Schema holds no `$schema`, as {@link namedSchema}'s.
|
|
296
|
+
*/
|
|
297
|
+
export function namedSetSchema({ entry, kind = 'entry' } = {}) {
|
|
298
|
+
const set = {
|
|
299
|
+
description: `A set of ${kind}s: a map of each by its name, or the export of a module that is one.`,
|
|
300
|
+
anyOf: [
|
|
301
|
+
MODULE_SCHEMA,
|
|
302
|
+
{
|
|
303
|
+
type: 'object',
|
|
304
|
+
additionalProperties: { anyOf: [MODULE_SCHEMA, objectSchemaOf(entry)] }
|
|
305
|
+
}
|
|
306
|
+
]
|
|
307
|
+
};
|
|
308
|
+
return schemaOf({
|
|
309
|
+
description: `The sets of ${kind}s: a set, or an array of sets merged in order, each over those before.`,
|
|
310
|
+
anyOf: [set, { type: 'array', items: set }]
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
//# sourceMappingURL=named.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"named.js","sourceRoot":"","sources":["../src/named.ts"],"names":[],"mappings":"AA6CA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,EAAE,UAAU,EAA+C,MAAM,aAAa,CAAA;AAErF,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAoGtC,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AG,cAAc,CAAC,OAAO,CAAC,CAAA;IACzF,OAAO,QAAQ,CAAC;QACd,WAAW,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,kBAAkB,GAAG,oEAAoE;QAC/H,KAAK,EAAE;YACL,IAAI;YACJ;gBACE,GAAG,IAAI;gBACP,WAAW,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,0CAA0C;gBAChF,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE,EAAE,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE;gBACvE,QAAQ,EAAE,CAAC,GAAG,CAAC;aAChB;YACD,aAAa;SACd;KACF,CAAC,CAAA;AACJ,CAAC;AAUD;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,KAA4B,EAAE;IAGlF,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 * 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,5 +1,6 @@
|
|
|
1
1
|
import type { ConfigPlatform, ConfigSyncPlatform, ResolvedConfig } from './index.ts';
|
|
2
2
|
import { type LoadOptions, type LoadSyncOptions } from './load.ts';
|
|
3
|
+
import type { LoadModule } from './named.ts';
|
|
3
4
|
/** Node's platform the configuration is read and written by: the disk from its root, its paths,
|
|
4
5
|
* and the process's working folder. */
|
|
5
6
|
export declare const nodeConfigPlatform: () => ConfigPlatform;
|
|
@@ -18,3 +19,17 @@ export declare const loadConfig: (folder?: string, options?: Partial<LoadOptions
|
|
|
18
19
|
* and the process's environment, unless `options` gives others.
|
|
19
20
|
*/
|
|
20
21
|
export declare const loadConfigSync: (folder?: string, options?: Partial<LoadSyncOptions>) => ResolvedConfig;
|
|
22
|
+
/**
|
|
23
|
+
* 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 process's working folder by default; a package, the one
|
|
25
|
+
* `root` installs, else the one installed beside `beside`, as a tool gives its own
|
|
26
|
+
* `import.meta.url`, so a package a global install puts beside the tool is found too. What fails
|
|
27
|
+
* to resolve or load fails of the error of `root`'s resolve, or of the import.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { nodeLoadModule } from '@wtf/project-config/node'
|
|
31
|
+
*
|
|
32
|
+
* const load = nodeLoadModule(process.cwd(), import.meta.url)
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const nodeLoadModule: (root?: string, beside?: string | URL) => LoadModule;
|
package/dist/node.js
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
1
9
|
/**
|
|
2
10
|
* Node's platform and loads of the configuration: {@link nodeConfigPlatform}, the disk as handles
|
|
3
11
|
* of the File System Standard, by `nodeStorage()` of `@wtf/platform-fs-node/storage`, and the paths
|
|
4
12
|
* of `nodePath()` of `@wtf/platform-node/path`; {@link nodeSyncPlatform}, the disk read
|
|
5
|
-
* synchronously, by `node:fs`;
|
|
6
|
-
* of the process's working folder, its environment and Node's platform by default
|
|
7
|
-
*
|
|
13
|
+
* synchronously, by `node:fs`; {@link loadConfig} and {@link loadConfigSync}, those of ./load,
|
|
14
|
+
* of the process's working folder, its environment and Node's platform by default; and
|
|
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:
|
|
8
17
|
*
|
|
9
18
|
* ```ts
|
|
10
19
|
* import { loadConfig } from '@wtf/project-config/node'
|
|
@@ -19,7 +28,9 @@
|
|
|
19
28
|
* @packageDocumentation
|
|
20
29
|
*/
|
|
21
30
|
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
22
|
-
import {
|
|
31
|
+
import { createRequire } from 'node:module';
|
|
32
|
+
import { isAbsolute, join } from 'node:path';
|
|
33
|
+
import { pathToFileURL } from 'node:url';
|
|
23
34
|
import { nodeStorage } from '@wtf/platform-fs-node/storage';
|
|
24
35
|
import { nodePath } from '@wtf/platform-node/path';
|
|
25
36
|
import { loadConfig as loadWith, loadConfigSync as loadSyncWith } from "./load.js";
|
|
@@ -87,4 +98,42 @@ export const loadConfigSync = (folder = process.cwd(), options = {}) => loadSync
|
|
|
87
98
|
...options,
|
|
88
99
|
platform: options.platform ?? nodeSyncPlatform()
|
|
89
100
|
});
|
|
101
|
+
/**
|
|
102
|
+
* The file of the module `module`: of an absolute path, itself; of a path from the root, `./` or
|
|
103
|
+
* `../`, of `root`; of a package, the one `root` installs, as a devDependency of the project, else
|
|
104
|
+
* the one installed beside `beside`, as of a global install of a tool and its plugins.
|
|
105
|
+
*/
|
|
106
|
+
function fileOf(module, root, beside) {
|
|
107
|
+
if (isAbsolute(module))
|
|
108
|
+
return module;
|
|
109
|
+
if (/^\.{1,2}\//.test(module))
|
|
110
|
+
return join(root, module);
|
|
111
|
+
try {
|
|
112
|
+
return createRequire(join(root, 'package.json')).resolve(module);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (beside === undefined)
|
|
116
|
+
throw error;
|
|
117
|
+
try {
|
|
118
|
+
return createRequire(beside).resolve(module);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 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 process's working folder by default; a package, the one
|
|
128
|
+
* `root` installs, else the one installed beside `beside`, as a tool gives its own
|
|
129
|
+
* `import.meta.url`, so a package a global install puts beside the tool is found too. What fails
|
|
130
|
+
* to resolve or load fails of the error of `root`'s resolve, or of the import.
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { nodeLoadModule } from '@wtf/project-config/node'
|
|
134
|
+
*
|
|
135
|
+
* const load = nodeLoadModule(process.cwd(), import.meta.url)
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export const nodeLoadModule = (root = process.cwd(), beside) => async (module) => import(__rewriteRelativeImportExtension(pathToFileURL(fileOf(module, root, beside)).href));
|
|
90
139
|
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAA;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAGlD,OAAO,EACL,UAAU,IAAI,QAAQ,EACtB,cAAc,IAAI,YAAY,EAG/B,MAAM,WAAW,CAAA;AA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CAAC,CAAA;QAC9C,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,KAAK,CAAA;QACb,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,cAAc,GACzB,CAAC,OAAe,OAAO,CAAC,GAAG,EAAE,EAAE,MAAqB,EAAc,EAAE,CACpE,KAAK,EAAE,MAAM,EAAE,EAAE,CACf,MAAM,kCAAC,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,IAAI,EAAC,CAAA","sourcesContent":["/**\n * Node's platform and loads of the configuration: {@link nodeConfigPlatform}, the disk as handles\n * of the File System Standard, by `nodeStorage()` of `@wtf/platform-fs-node/storage`, and the paths\n * of `nodePath()` of `@wtf/platform-node/path`; {@link nodeSyncPlatform}, the disk read\n * synchronously, by `node:fs`; {@link loadConfig} and {@link loadConfigSync}, those of ./load,\n * of the process's working folder, its environment and Node's platform by default; and\n * {@link nodeLoadModule}, what loads a module a reference of ./named names. A repo loads its config\n * by them, and spreads a section into a preset's options:\n *\n * ```ts\n * import { loadConfig } from '@wtf/project-config/node'\n * import { sectionOf } from '@wtf/project-config/section'\n * import { rootConfig } from '@wtf/project-vitest/config'\n * import { vitestSchema } from '@wtf/project-vitest/schema'\n *\n * const config = await loadConfig()\n * export default rootConfig({ ...(await sectionOf(config, 'vitest', vitestSchema)) })\n * ```\n *\n * @packageDocumentation\n */\nimport { readdirSync, readFileSync, statSync } from 'node:fs'\nimport { createRequire } from 'node:module'\nimport { isAbsolute, join } from 'node:path'\nimport { pathToFileURL } from 'node:url'\n\nimport { nodeStorage } from '@wtf/platform-fs-node/storage'\nimport { nodePath } from '@wtf/platform-node/path'\n\nimport type { ConfigPlatform, ConfigSyncPlatform, ResolvedConfig } from './index.ts'\nimport {\n loadConfig as loadWith,\n loadConfigSync as loadSyncWith,\n type LoadOptions,\n type LoadSyncOptions\n} from './load.ts'\nimport type { LoadModule } from './named.ts'\n\n/** Node's platform the configuration is read and written by: the disk from its root, its paths,\n * and the process's working folder. */\nexport const nodeConfigPlatform = (): ConfigPlatform => ({\n navigator: { storage: nodeStorage() },\n path: nodePath(),\n dirs: { cwd: process.cwd() }\n})\n\n/** Whether `error` is Node's of a file or a folder not there, or of the other kind. */\nconst isMissing = (error: unknown): boolean =>\n ['ENOENT', 'ENOTDIR', 'EISDIR'].includes((error as NodeJS.ErrnoException | undefined)?.code ?? '')\n\n/** Node's platform the configuration is read by synchronously: the disk by `node:fs`, and its\n * paths. */\nexport const nodeSyncPlatform = (): ConfigSyncPlatform => ({\n path: nodePath(),\n files: {\n text: (path) => {\n try {\n return readFileSync(path, 'utf8')\n } catch (error) {\n if (isMissing(error)) return undefined\n throw error\n }\n },\n files: (folder) => {\n try {\n // A link is what it links to, as a handle of the disk reads it.\n return new Set(\n readdirSync(folder).filter((name) => {\n try {\n return statSync(join(folder, name)).isFile()\n } catch {\n return false\n }\n })\n )\n } catch (error) {\n if (isMissing(error)) return new Set<string>()\n throw error\n }\n }\n }\n})\n\n/**\n * The configuration of the project in `folder`, the process's working folder by default, as\n * `loadConfig` of ./load reads it: the namespace `wtf` by default, of Node's platform and the\n * process's environment, unless `options` gives others.\n */\nexport const loadConfig = (\n folder: string = process.cwd(),\n options: Partial<LoadOptions> = {}\n): Promise<ResolvedConfig> =>\n loadWith(folder, {\n environment: process.env,\n ...options,\n platform: options.platform ?? nodeConfigPlatform()\n })\n\n/**\n * The configuration of the project in `folder`, the process's working folder by default, as\n * `loadConfigSync` of ./load reads it, of a JSON file or package.json alone: of Node's platform\n * and the process's environment, unless `options` gives others.\n */\nexport const loadConfigSync = (\n folder: string = process.cwd(),\n options: Partial<LoadSyncOptions> = {}\n): ResolvedConfig =>\n loadSyncWith(folder, {\n environment: process.env,\n ...options,\n platform: options.platform ?? nodeSyncPlatform()\n })\n\n/**\n * The file of the module `module`: of an absolute path, itself; of a path from the root, `./` or\n * `../`, of `root`; of a package, the one `root` installs, as a devDependency of the project, else\n * the one installed beside `beside`, as of a global install of a tool and its plugins.\n */\nfunction fileOf(module: string, root: string, beside: string | URL | undefined): string {\n if (isAbsolute(module)) return module\n if (/^\\.{1,2}\\//.test(module)) return join(root, module)\n try {\n return createRequire(join(root, 'package.json')).resolve(module)\n } catch (error) {\n if (beside === undefined) throw error\n try {\n return createRequire(beside).resolve(module)\n } catch {\n throw error\n }\n }\n}\n\n/**\n * Node's loader of the modules a reference of ./named names, by `import()` of its file: a path, of\n * `root`, the root of the project, the process's working folder by default; a package, the one\n * `root` installs, else the one installed beside `beside`, as a tool gives its own\n * `import.meta.url`, so a package a global install puts beside the tool is found too. What fails\n * to resolve or load fails of the error of `root`'s resolve, or of the import.\n *\n * ```ts\n * import { nodeLoadModule } from '@wtf/project-config/node'\n *\n * const load = nodeLoadModule(process.cwd(), import.meta.url)\n * ```\n */\nexport const nodeLoadModule =\n (root: string = process.cwd(), beside?: string | URL): LoadModule =>\n async (module) =>\n import(pathToFileURL(fileOf(module, root, beside)).href)\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wtf/project-config",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Read and write a project's configuration of its tools: one object under a namespace, wtf by default, with a section for each tool, in wtf.config.{mjs,js,ts,json} or the wtf key of package.json, the first found. Find it (./locate); load it, asynchronously with code, or synchronously of JSON and package.json alone (./load); read a tool's section, checked by its Standard Schema, a JSON Schema or a function, with WTF_<SECTION>_<KEY> variables over it (./section, ./env); replace ${env:NAME} placeholders (./placeholder); get a value by its path (./path); write a value (./write); the JSON Schema of the whole (./schema); a code config, typed (./define); Node's platform and
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Read and write a project's configuration of its tools: one object under a namespace, wtf by default, with a section for each tool, in wtf.config.{mjs,js,ts,json} or the wtf key of package.json, the first found. Find it (./locate); load it, asynchronously with code, or synchronously of JSON and package.json alone (./load); read a tool's section, checked by its Standard Schema, a JSON Schema or a function, with WTF_<SECTION>_<KEY> variables over it (./section, ./env); replace ${env:NAME} placeholders (./placeholder); get a value by its path (./path); write a value (./write); the JSON Schema of the whole (./schema); a code config, typed (./define); a value or a function named in JSON, by a name, a name with options or the export of a module, of sets merged in order (./named); Node's platform, loads and loader of modules (./node); and the errors (./errors).",
|
|
5
5
|
"homepage": "https://gitlab.com/wetryfun/project/project/-/tree/main/packages/project-config",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://gitlab.com/wetryfun/project/project/-/issues"
|
|
@@ -58,6 +58,10 @@
|
|
|
58
58
|
"types": "./dist/define.d.ts",
|
|
59
59
|
"default": "./dist/define.js"
|
|
60
60
|
},
|
|
61
|
+
"./named": {
|
|
62
|
+
"types": "./dist/named.d.ts",
|
|
63
|
+
"default": "./dist/named.js"
|
|
64
|
+
},
|
|
61
65
|
"./node": {
|
|
62
66
|
"types": "./dist/node.d.ts",
|
|
63
67
|
"default": "./dist/node.js"
|