@wtf/project-config 1.1.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -26
- package/dist/env.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js.map +1 -1
- package/dist/load.d.ts +56 -30
- package/dist/load.js +25 -42
- package/dist/load.js.map +1 -1
- package/dist/locate.d.ts +3 -2
- package/dist/locate.js +5 -2
- package/dist/locate.js.map +1 -1
- package/dist/node.d.ts +11 -24
- package/dist/node.js +56 -79
- package/dist/node.js.map +1 -1
- package/dist/section.d.ts +5 -5
- package/dist/section.js +5 -5
- package/dist/section.js.map +1 -1
- package/dist/write.js +5 -4
- package/dist/write.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -6,35 +6,44 @@ 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
|
-
| `./
|
|
13
|
-
| `./
|
|
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`
|
|
9
|
+
| Subpath | What you get |
|
|
10
|
+
| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
|
11
|
+
| `.` | The types: `NamespaceConfig`, `SectionsOf`, `ResolvedConfig`, `SectionSchema`, `ConfigPlatform`, `ConfigSyncPlatform`, … |
|
|
12
|
+
| `./load` | `loadConfig({ platform })`, of code or JSON; `loadConfigSync({ platform })`, of JSON alone; `ignoredWarning` |
|
|
13
|
+
| `./node` | `nodeSyncPlatform(platform)`, a platform's files read synchronously by `node:fs`; `nodeLoadModule(root, beside?)` |
|
|
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` |
|
|
24
24
|
|
|
25
25
|
```ts
|
|
26
26
|
// vite.config.ts
|
|
27
|
-
import {
|
|
27
|
+
import { nodeStorage } from '@wtf/platform-fs-node/storage'
|
|
28
|
+
import { nodePlatform } from '@wtf/platform-node/platform'
|
|
29
|
+
import { loadConfig } from '@wtf/project-config/load'
|
|
28
30
|
import { sectionOf } from '@wtf/project-config/section'
|
|
29
|
-
import { baseConfig
|
|
31
|
+
import { baseConfig } from '@wtf/project-vite/config'
|
|
32
|
+
import { viteSchema } from '@wtf/project-vite/schema'
|
|
30
33
|
|
|
31
|
-
const
|
|
32
|
-
|
|
34
|
+
const platform = nodePlatform({ fetch, storage: nodeStorage() })
|
|
35
|
+
const config = await loadConfig({ platform })
|
|
36
|
+
export default baseConfig({ platform, ...(await sectionOf(config, 'vite', viteSchema)) })
|
|
33
37
|
```
|
|
34
38
|
|
|
39
|
+
The config file is the entry point: it builds the platform, and the loads read through it alone,
|
|
40
|
+
its files, its working folder, where the project is looked for from, and its environment. Nothing
|
|
41
|
+
here reads the process.
|
|
42
|
+
|
|
35
43
|
## Where it looks
|
|
36
44
|
|
|
37
|
-
It looks at the root of the workspace the folder is in
|
|
45
|
+
It looks at the root of the workspace the folder is in, the working folder of the platform by
|
|
46
|
+
default (the nearest folder up with a
|
|
38
47
|
pnpm-workspace.yaml, or a package.json of `workspaces`), else the nearest package.json's folder.
|
|
39
48
|
There it uses the first of `wtf.config.mjs`, `wtf.config.js`, `wtf.config.ts`, `wtf.config.json`
|
|
40
49
|
and the `wtf` key of package.json, and ignores the others: `ignoredWarning` says which. A tool can
|
|
@@ -47,13 +56,14 @@ imports it, `wtf.config.ts` too, as it strips the types itself.
|
|
|
47
56
|
## Asynchronous and synchronous
|
|
48
57
|
|
|
49
58
|
`loadConfig` reads every kind of file, as importing code is asynchronous. `loadConfigSync` is for a
|
|
50
|
-
tool that loads its own config synchronously: it reads `wtf.config.json` and package.json's key,
|
|
59
|
+
tool that loads its own config synchronously: it reads `wtf.config.json` and package.json's key, of
|
|
60
|
+
a platform whose files are read synchronously, as `nodeSyncPlatform(platform)` of `./node` gives,
|
|
51
61
|
and throws a `SyncConfigInCodeError` when the config in use is code. `sectionOfSync` checks a
|
|
52
62
|
section synchronously, and throws a `SyncSchemaError` of a schema that answers with a promise.
|
|
53
63
|
|
|
54
64
|
## The environment over it
|
|
55
65
|
|
|
56
|
-
The loads of
|
|
66
|
+
The loads keep the environment of the platform, its `env`. A variable `WTF_<SECTION>_<KEY>` sets a value of a
|
|
57
67
|
section as it is read, a JSON value or else the text: `WTF_VITE_COMPRESS=false` sets
|
|
58
68
|
`wtf.vite.compress`. A key of camelCase is its words in capitals, so `testTimeout` is
|
|
59
69
|
`TEST_TIMEOUT`. A variable sets a key the section has, or its schema names, and no other. A string in a section can point at a variable with
|
|
@@ -97,9 +107,11 @@ import {
|
|
|
97
107
|
type Format = { readonly width: number; readonly ellipsis: string }
|
|
98
108
|
declare const configured: { formatters?: NamedSet<Format>[]; format: NamedReference }
|
|
99
109
|
|
|
110
|
+
declare const root: string
|
|
111
|
+
|
|
100
112
|
const builtin = { short: { width: 40, ellipsis: '…' } }
|
|
101
113
|
const formatters = await resolveNamedSet<Format>([builtin, ...(configured.formatters ?? [])], {
|
|
102
|
-
load: nodeLoadModule(
|
|
114
|
+
load: nodeLoadModule(root, import.meta.url)
|
|
103
115
|
})
|
|
104
116
|
const format = await resolveNamed<Format>(configured.format, {
|
|
105
117
|
key: 'format',
|
|
@@ -114,7 +126,8 @@ of the wrong shape, or one the `check` you pass refuses, `INVALID_NAMED_VALUE`.
|
|
|
114
126
|
it failed: its `at` path, from the `at` you pass, the index of the `set` and the name of the
|
|
115
127
|
`entry`, and the `module` and `export` a value came from. With `checkResults: true`, `check` also
|
|
116
128
|
checks what each function entry gives. `nodeLoadModule`
|
|
117
|
-
loads a path from the root, and a package the root installs, else one installed beside
|
|
129
|
+
loads a path from the root you give it, and a package the root installs, else one installed beside
|
|
130
|
+
your tool.
|
|
118
131
|
`namedSchema('format', { names, options })` and `namedSetSchema({ entry })` give the schema of a
|
|
119
132
|
reference and of the sets, so an editor completes the names.
|
|
120
133
|
|
|
@@ -122,7 +135,8 @@ reference and of the sets, so an editor completes the names.
|
|
|
122
135
|
|
|
123
136
|
`./load`, `./locate` and `./write` read through the platform you pass: a `Platform` of
|
|
124
137
|
[`@wtf/platform`](https://www.npmjs.com/package/@wtf/platform), whose `navigator.storage` gives
|
|
125
|
-
the root of a WHATWG File System, as a browser's or `@wtf/platform-fs-memory`'s
|
|
126
|
-
|
|
138
|
+
the root of a WHATWG File System, as a browser's or `@wtf/platform-fs-memory`'s, or
|
|
139
|
+
`nodeStorage()` of `@wtf/platform-fs-node` for Node's disk. The sync load takes a platform whose
|
|
140
|
+
files are read synchronously, which `nodeSyncPlatform` of `./node` makes of Node's.
|
|
127
141
|
|
|
128
142
|
See the [configuration guide](../project-docs/guides/config.md).
|
package/dist/env.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,CAAC,MAAM,WAAW,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,GAAG,SAAS,CAAC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AC5E,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"]}
|
|
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,CAAC,MAAM,WAAW,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAA;AAE1E;4BAC4B;AAC5B,MAAM,UAAU,WAAW,CAAC,WAAwB,EAAE,MAAc;IAClE,MAAM,SAAS,GAA2B,EAAE,CAAA;IAC5C,KAAK,MAAM,CAAC,IAAI,EAAE,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AE,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 of the environment of\n * the platform it is given: nothing here reads 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/index.d.ts
CHANGED
|
@@ -174,8 +174,8 @@ export type ImportModule = (path: string) => Promise<unknown>;
|
|
|
174
174
|
/**
|
|
175
175
|
* What the configuration is read and written by, asynchronously: the platform's storage, whose
|
|
176
176
|
* `getDirectory()` is the root of its file system, its paths, and its working folder, as
|
|
177
|
-
* `@wtf/platform-fs/files` reads a platform's files. A `Platform` of `@wtf/platform` is one,
|
|
178
|
-
* `
|
|
177
|
+
* `@wtf/platform-fs/files` reads a platform's files. A `Platform` of `@wtf/platform` is one, as
|
|
178
|
+
* `nodePlatform()` of `@wtf/platform-node/platform` makes Node's.
|
|
179
179
|
*/
|
|
180
180
|
export type ConfigPlatform = FilesPlatform;
|
|
181
181
|
/** What reads a project's files synchronously, by their absolute paths. */
|
|
@@ -187,8 +187,8 @@ export type SyncFiles = {
|
|
|
187
187
|
};
|
|
188
188
|
/**
|
|
189
189
|
* What the configuration is read by synchronously: the platform's paths, and its files, read
|
|
190
|
-
* synchronously, as no handle of the File System Standard is. `nodeSyncPlatform()` of ./node
|
|
191
|
-
* Node's.
|
|
190
|
+
* synchronously, as no handle of the File System Standard is. `nodeSyncPlatform()` of ./node adds
|
|
191
|
+
* Node's files to a platform.
|
|
192
192
|
*/
|
|
193
193
|
export type ConfigSyncPlatform = {
|
|
194
194
|
readonly path: Pick<Path, 'resolve' | 'join' | 'dirname'>;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * The types of a project's configuration under a namespace, `wtf` by default: the object\n * ({@link NamespaceConfig}), made of a section for each tool ({@link ConfigSection}), or of the\n * sections the schemas of the tools type ({@link SectionsOf}); the names it is looked for by\n * ({@link ConfigNames}); the file it is in ({@link ConfigSource}); where it is\n * ({@link LocatedConfig}); what a tool reads ({@link ResolvedConfig}); the defaults a tool's\n * package gives ({@link ConfigDefaults}); a path in it ({@link ConfigPath}); what checks a section\n * ({@link SectionSchema}) and what it finds wrong ({@link ConfigIssue}); what a write did\n * ({@link ConfigWrite}); and what it is read by ({@link ConfigPlatform}, {@link ConfigSyncPlatform}).\n * Types only, with no code.\n *\n * @packageDocumentation\n * @module index\n */\nimport type { StandardJSONSchemaV1, StandardSchemaV1, StandardTypedV1 } from '@standard-schema/spec'\nimport type { FilesPlatform } from '@wtf/platform-fs/files'\nimport type { Path } from '@wtf/platform/path'\n\n/**\n * The section of one tool, by its key `K` and its value `V`, as `ConfigSection<'vite', ViteOptions>`.\n * A section is optional: a project configures the tools it chooses.\n */\nexport type ConfigSection<K extends string, V> = { readonly [P in K]?: V }\n\n/**\n * The sections of the schemas `S`, each by its name: the input type of each schema, as\n * `SectionsOf<{ vite: typeof viteSchema }>` is `{ vite?: ViteOptions }`. A code config is typed by\n * it with `defineConfig` of ./define.\n */\nexport type SectionsOf<S extends Readonly<Record<string, StandardTypedV1>>> = {\n readonly [K in keyof S]?: StandardTypedV1.InferInput<S[K]>\n}\n\n/**\n * The configuration of a project under a namespace: the sections `S`, each checked by its type;\n * the `$schema` a JSON file names its schema by; and any other section, of a tool not named, as it\n * is. A tool reads its own section, and no other.\n *\n * ```ts\n * const config: NamespaceConfig<{ vite?: { compress?: false } }> = { vite: { compress: false } }\n * ```\n */\nexport type NamespaceConfig<S extends object = Record<never, never>> = S & {\n /** The JSON Schema of a JSON file of the configuration, as an editor reads it. */\n readonly $schema?: string\n readonly [section: string]: unknown\n}\n\n/**\n * The names the configuration of a namespace is looked for by, each a tool's to give: the files at\n * the root of the project, the key of its package.json, and the prefix of the environment's\n * variables over it. Of the namespace `wtf`, by default, `wtf.config.mjs`, `wtf.config.js`,\n * `wtf.config.ts` and `wtf.config.json`, the `wtf` key, and the variables `WTF_*`.\n */\nexport type ConfigNames = {\n /**\n * The files it can be in, in the order the first found is used, each of code (`.mjs`, `.js`,\n * `.ts`, `.cjs`, `.mts` or `.cts`) or of JSON (`.json`). The key of package.json comes last.\n */\n readonly files?: readonly string[]\n /** The key of package.json that holds it when no file does: the namespace by default. */\n readonly key?: string\n /**\n * The name the environment's variables over it start with: the key by default, so `wtf` is\n * `WTF_`, and `WTF_VITE_COMPRESS` sets `wtf.vite.compress`.\n */\n readonly env?: string\n}\n\n/** A file of the configuration found at the root of a project: one of its files, or package.json. */\nexport type ConfigSource = {\n /** Its name, as the names give it, or `package.json`. */\n readonly file: string\n /** Its absolute path. */\n readonly path: string\n}\n\n/**\n * Where a project's configuration is: the root of the project, the key of package.json, and each\n * file of it found there, in the order of its files, then package.json. The first is used; the\n * others are ignored.\n */\nexport type LocatedConfig = {\n readonly root: string\n /** The key of package.json, which a path of the configuration starts from: `wtf.vite.compress`. */\n readonly key: string\n readonly sources: readonly ConfigSource[]\n}\n\n/**\n * The configuration a tool's package gives under the project's, as its own defaults: each section\n * the project gives is merged over it, value by value, as `sectionOf` of ./section reads it.\n */\nexport type ConfigDefaults = {\n /** The object, of the sections of the namespace. */\n readonly config: NamespaceConfig\n /** The file it is in, by its absolute path, which an error of a section of it names. */\n readonly path: string\n}\n\n/**\n * The environment a tool reads the configuration with: the prefix of the variables over it, as\n * `WTF_`; each of those that is set and not empty, by its name; and the whole environment, in\n * `values`, which the placeholders `${env:NAME}` of a section are taken from. Only `sectionOf` of\n * ./section reads `values`: nothing shows or writes it.\n */\nexport type ConfigEnvironment = {\n readonly prefix: string\n readonly variables: Readonly<Record<string, string>>\n readonly values: Readonly<Record<string, string | undefined>>\n}\n\n/** A project's configuration, as a tool reads it. */\nexport type ResolvedConfig = {\n /** Its namespace. */\n readonly namespace: string\n /** The root of the project, where it was looked for. */\n readonly root: string\n /** The key of package.json it was looked for in, which a path of it starts from. */\n readonly key: string\n /** The object, `{}` when no file holds one. */\n readonly config: NamespaceConfig\n /** The file it came from, or `undefined` when no file holds one. */\n readonly source: ConfigSource | undefined\n /** The other files found, which are ignored, as only the first is used. */\n readonly ignored: readonly ConfigSource[]\n /** The configuration of the tool's package under the project's, when the tool gives one. */\n readonly defaults?: ConfigDefaults | undefined\n /**\n * The environment the tool gave, which a section is read with: its variables over the project's\n * configuration, and the values its placeholders are taken from. None when no environment was\n * given: then a placeholder names a variable that is not set.\n */\n readonly environment?: ConfigEnvironment | undefined\n}\n\n/** A path in the configuration, by its keys from the top: `['vite', 'compress']`. A key of digits\n * is an index of a list. */\nexport type ConfigPath = readonly string[]\n\n/** What is wrong with a value of a section: where it is, by its path in the section, and why. */\nexport type ConfigIssue = {\n readonly path: ConfigPath\n readonly message: string\n}\n\n/** A JSON Schema, as the object of its keywords, or `true` or `false`. */\nexport type JsonSchema = { readonly [keyword: string]: unknown } | boolean\n\n/** A function that checks a section: what is wrong with it, none when it is valid. */\nexport type ValidateSection = (\n value: unknown\n) => readonly ConfigIssue[] | Promise<readonly ConfigIssue[]>\n\n/**\n * What a tool checks its section by: a Standard Schema, as a preset's `./schema` exports one, or\n * zod's, whose output is what the section reads as; a JSON Schema of draft 2020-12, or a Standard\n * JSON Schema, which ajv checks; or a function of its own.\n */\nexport type SectionSchema<T = unknown> =\n | StandardSchemaV1<unknown, T>\n | StandardJSONSchemaV1<unknown, T>\n | JsonSchema\n | ValidateSection\n\n/**\n * What a write did: where, at which path, the value there before and after it, and whether it\n * changed the file, or made it.\n */\nexport type ConfigWrite = {\n readonly root: string\n /** The key of package.json, which a path of the configuration starts from. */\n readonly key: string\n /** The file written, or `undefined` when nothing was there to unset. */\n readonly source: ConfigSource | undefined\n /** The other files of the configuration found, which a reader ignores. */\n readonly ignored: readonly ConfigSource[]\n readonly path: ConfigPath\n /** The value at the path before the write, `undefined` when there was none. */\n readonly previous: unknown\n /** The value at the path after the write, `undefined` when it was unset. */\n readonly value: unknown\n /** Whether the file was written: false when the value was already so. */\n readonly changed: boolean\n /** Whether the file, or the key of package.json, was made by the write. */\n readonly created: boolean\n}\n\n/** What imports a file of code, as `wtf.config.ts`, by its absolute path: its module, whose\n * `default` is the configuration. */\nexport type ImportModule = (path: string) => Promise<unknown>\n\n/**\n * What the configuration is read and written by, asynchronously: the platform's storage, whose\n * `getDirectory()` is the root of its file system, its paths, and its working folder, as\n * `@wtf/platform-fs/files` reads a platform's files. A `Platform` of `@wtf/platform` is one,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * The types of a project's configuration under a namespace, `wtf` by default: the object\n * ({@link NamespaceConfig}), made of a section for each tool ({@link ConfigSection}), or of the\n * sections the schemas of the tools type ({@link SectionsOf}); the names it is looked for by\n * ({@link ConfigNames}); the file it is in ({@link ConfigSource}); where it is\n * ({@link LocatedConfig}); what a tool reads ({@link ResolvedConfig}); the defaults a tool's\n * package gives ({@link ConfigDefaults}); a path in it ({@link ConfigPath}); what checks a section\n * ({@link SectionSchema}) and what it finds wrong ({@link ConfigIssue}); what a write did\n * ({@link ConfigWrite}); and what it is read by ({@link ConfigPlatform}, {@link ConfigSyncPlatform}).\n * Types only, with no code.\n *\n * @packageDocumentation\n * @module index\n */\nimport type { StandardJSONSchemaV1, StandardSchemaV1, StandardTypedV1 } from '@standard-schema/spec'\nimport type { FilesPlatform } from '@wtf/platform-fs/files'\nimport type { Path } from '@wtf/platform/path'\n\n/**\n * The section of one tool, by its key `K` and its value `V`, as `ConfigSection<'vite', ViteOptions>`.\n * A section is optional: a project configures the tools it chooses.\n */\nexport type ConfigSection<K extends string, V> = { readonly [P in K]?: V }\n\n/**\n * The sections of the schemas `S`, each by its name: the input type of each schema, as\n * `SectionsOf<{ vite: typeof viteSchema }>` is `{ vite?: ViteOptions }`. A code config is typed by\n * it with `defineConfig` of ./define.\n */\nexport type SectionsOf<S extends Readonly<Record<string, StandardTypedV1>>> = {\n readonly [K in keyof S]?: StandardTypedV1.InferInput<S[K]>\n}\n\n/**\n * The configuration of a project under a namespace: the sections `S`, each checked by its type;\n * the `$schema` a JSON file names its schema by; and any other section, of a tool not named, as it\n * is. A tool reads its own section, and no other.\n *\n * ```ts\n * const config: NamespaceConfig<{ vite?: { compress?: false } }> = { vite: { compress: false } }\n * ```\n */\nexport type NamespaceConfig<S extends object = Record<never, never>> = S & {\n /** The JSON Schema of a JSON file of the configuration, as an editor reads it. */\n readonly $schema?: string\n readonly [section: string]: unknown\n}\n\n/**\n * The names the configuration of a namespace is looked for by, each a tool's to give: the files at\n * the root of the project, the key of its package.json, and the prefix of the environment's\n * variables over it. Of the namespace `wtf`, by default, `wtf.config.mjs`, `wtf.config.js`,\n * `wtf.config.ts` and `wtf.config.json`, the `wtf` key, and the variables `WTF_*`.\n */\nexport type ConfigNames = {\n /**\n * The files it can be in, in the order the first found is used, each of code (`.mjs`, `.js`,\n * `.ts`, `.cjs`, `.mts` or `.cts`) or of JSON (`.json`). The key of package.json comes last.\n */\n readonly files?: readonly string[]\n /** The key of package.json that holds it when no file does: the namespace by default. */\n readonly key?: string\n /**\n * The name the environment's variables over it start with: the key by default, so `wtf` is\n * `WTF_`, and `WTF_VITE_COMPRESS` sets `wtf.vite.compress`.\n */\n readonly env?: string\n}\n\n/** A file of the configuration found at the root of a project: one of its files, or package.json. */\nexport type ConfigSource = {\n /** Its name, as the names give it, or `package.json`. */\n readonly file: string\n /** Its absolute path. */\n readonly path: string\n}\n\n/**\n * Where a project's configuration is: the root of the project, the key of package.json, and each\n * file of it found there, in the order of its files, then package.json. The first is used; the\n * others are ignored.\n */\nexport type LocatedConfig = {\n readonly root: string\n /** The key of package.json, which a path of the configuration starts from: `wtf.vite.compress`. */\n readonly key: string\n readonly sources: readonly ConfigSource[]\n}\n\n/**\n * The configuration a tool's package gives under the project's, as its own defaults: each section\n * the project gives is merged over it, value by value, as `sectionOf` of ./section reads it.\n */\nexport type ConfigDefaults = {\n /** The object, of the sections of the namespace. */\n readonly config: NamespaceConfig\n /** The file it is in, by its absolute path, which an error of a section of it names. */\n readonly path: string\n}\n\n/**\n * The environment a tool reads the configuration with: the prefix of the variables over it, as\n * `WTF_`; each of those that is set and not empty, by its name; and the whole environment, in\n * `values`, which the placeholders `${env:NAME}` of a section are taken from. Only `sectionOf` of\n * ./section reads `values`: nothing shows or writes it.\n */\nexport type ConfigEnvironment = {\n readonly prefix: string\n readonly variables: Readonly<Record<string, string>>\n readonly values: Readonly<Record<string, string | undefined>>\n}\n\n/** A project's configuration, as a tool reads it. */\nexport type ResolvedConfig = {\n /** Its namespace. */\n readonly namespace: string\n /** The root of the project, where it was looked for. */\n readonly root: string\n /** The key of package.json it was looked for in, which a path of it starts from. */\n readonly key: string\n /** The object, `{}` when no file holds one. */\n readonly config: NamespaceConfig\n /** The file it came from, or `undefined` when no file holds one. */\n readonly source: ConfigSource | undefined\n /** The other files found, which are ignored, as only the first is used. */\n readonly ignored: readonly ConfigSource[]\n /** The configuration of the tool's package under the project's, when the tool gives one. */\n readonly defaults?: ConfigDefaults | undefined\n /**\n * The environment the tool gave, which a section is read with: its variables over the project's\n * configuration, and the values its placeholders are taken from. None when no environment was\n * given: then a placeholder names a variable that is not set.\n */\n readonly environment?: ConfigEnvironment | undefined\n}\n\n/** A path in the configuration, by its keys from the top: `['vite', 'compress']`. A key of digits\n * is an index of a list. */\nexport type ConfigPath = readonly string[]\n\n/** What is wrong with a value of a section: where it is, by its path in the section, and why. */\nexport type ConfigIssue = {\n readonly path: ConfigPath\n readonly message: string\n}\n\n/** A JSON Schema, as the object of its keywords, or `true` or `false`. */\nexport type JsonSchema = { readonly [keyword: string]: unknown } | boolean\n\n/** A function that checks a section: what is wrong with it, none when it is valid. */\nexport type ValidateSection = (\n value: unknown\n) => readonly ConfigIssue[] | Promise<readonly ConfigIssue[]>\n\n/**\n * What a tool checks its section by: a Standard Schema, as a preset's `./schema` exports one, or\n * zod's, whose output is what the section reads as; a JSON Schema of draft 2020-12, or a Standard\n * JSON Schema, which ajv checks; or a function of its own.\n */\nexport type SectionSchema<T = unknown> =\n | StandardSchemaV1<unknown, T>\n | StandardJSONSchemaV1<unknown, T>\n | JsonSchema\n | ValidateSection\n\n/**\n * What a write did: where, at which path, the value there before and after it, and whether it\n * changed the file, or made it.\n */\nexport type ConfigWrite = {\n readonly root: string\n /** The key of package.json, which a path of the configuration starts from. */\n readonly key: string\n /** The file written, or `undefined` when nothing was there to unset. */\n readonly source: ConfigSource | undefined\n /** The other files of the configuration found, which a reader ignores. */\n readonly ignored: readonly ConfigSource[]\n readonly path: ConfigPath\n /** The value at the path before the write, `undefined` when there was none. */\n readonly previous: unknown\n /** The value at the path after the write, `undefined` when it was unset. */\n readonly value: unknown\n /** Whether the file was written: false when the value was already so. */\n readonly changed: boolean\n /** Whether the file, or the key of package.json, was made by the write. */\n readonly created: boolean\n}\n\n/** What imports a file of code, as `wtf.config.ts`, by its absolute path: its module, whose\n * `default` is the configuration. */\nexport type ImportModule = (path: string) => Promise<unknown>\n\n/**\n * What the configuration is read and written by, asynchronously: the platform's storage, whose\n * `getDirectory()` is the root of its file system, its paths, and its working folder, as\n * `@wtf/platform-fs/files` reads a platform's files. A `Platform` of `@wtf/platform` is one, as\n * `nodePlatform()` of `@wtf/platform-node/platform` makes Node's.\n */\nexport type ConfigPlatform = FilesPlatform\n\n/** What reads a project's files synchronously, by their absolute paths. */\nexport type SyncFiles = {\n /** The text of the file at `path`, or `undefined` when no file is there. */\n readonly text: (path: string) => string | undefined\n /** The names of the files in `folder`, none when it is no folder. */\n readonly files: (folder: string) => ReadonlySet<string>\n}\n\n/**\n * What the configuration is read by synchronously: the platform's paths, and its files, read\n * synchronously, as no handle of the File System Standard is. `nodeSyncPlatform()` of ./node adds\n * Node's files to a platform.\n */\nexport type ConfigSyncPlatform = {\n readonly path: Pick<Path, 'resolve' | 'join' | 'dirname'>\n readonly files: SyncFiles\n}\n"]}
|
package/dist/load.d.ts
CHANGED
|
@@ -6,40 +6,62 @@
|
|
|
6
6
|
* the files ignored; and {@link importFileModule}, which imports a configuration of code, as
|
|
7
7
|
* `wtf.config.ts`, by its file URL, as Node imports a module.
|
|
8
8
|
*
|
|
9
|
+
* Each reads by the platform it is given, and nothing of the process: its files, its paths, the
|
|
10
|
+
* folder it works in, which the project is looked for from, and its environment, whose variables
|
|
11
|
+
* are over the configuration. The entry point builds it, as `nodePlatform()` of
|
|
12
|
+
* `@wtf/platform-node/platform` makes Node's:
|
|
13
|
+
*
|
|
9
14
|
* ```ts
|
|
15
|
+
* import { nodeStorage } from '@wtf/platform-fs-node/storage'
|
|
16
|
+
* import { nodePlatform } from '@wtf/platform-node/platform'
|
|
17
|
+
*
|
|
10
18
|
* import { loadConfig } from '@wtf/project-config/load'
|
|
11
|
-
* import { nodeConfigPlatform } from '@wtf/project-config/node'
|
|
12
19
|
*
|
|
13
|
-
* const
|
|
14
|
-
*
|
|
15
|
-
* environment: process.env
|
|
16
|
-
* })
|
|
20
|
+
* const platform = nodePlatform({ fetch, storage: nodeStorage() })
|
|
21
|
+
* const resolved = await loadConfig({ platform })
|
|
17
22
|
* ```
|
|
18
23
|
*
|
|
19
24
|
* @packageDocumentation
|
|
20
25
|
*/
|
|
21
|
-
import {
|
|
22
|
-
import type {
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
import type { Platform } from '@wtf/platform';
|
|
27
|
+
import type { Dirs } from '@wtf/platform/dirs';
|
|
28
|
+
import type { ConfigDefaults, ConfigPlatform, ConfigSyncPlatform, ImportModule, ResolvedConfig } from './index.ts';
|
|
29
|
+
import { type LocateNames } from './locate.ts';
|
|
30
|
+
/** How a configuration is read, apart from the platform: the folder of the project, its names, and
|
|
31
|
+
* the defaults of the tool's package. */
|
|
26
32
|
export type LoadLayers = LocateNames & {
|
|
33
|
+
/**
|
|
34
|
+
* The folder of the project, which its root is looked for from: the working folder of the
|
|
35
|
+
* platform, `dirs.cwd`, by default.
|
|
36
|
+
*/
|
|
37
|
+
readonly folder?: string;
|
|
27
38
|
/**
|
|
28
39
|
* The configuration under the project's, as the tool's package gives it: a section the project
|
|
29
40
|
* gives none of is the one of the defaults, and one it gives is merged over it (`sectionOf` of
|
|
30
41
|
* ./section).
|
|
31
42
|
*/
|
|
32
43
|
readonly defaults?: ConfigDefaults;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* What a configuration is read by asynchronously: the platform's files, its paths and its working
|
|
47
|
+
* folder ({@link ConfigPlatform}), and its environment, `env`, of which the variables of the prefix
|
|
48
|
+
* of the names, as `WTF_`, are kept, and a section is read with them over it, and its placeholders
|
|
49
|
+
* `${env:NAME}` taken from the whole (`sectionOf` of ./section). A `Platform` of `@wtf/platform` is
|
|
50
|
+
* one.
|
|
51
|
+
*/
|
|
52
|
+
export type LoadPlatform = ConfigPlatform & Pick<Platform, 'env'>;
|
|
53
|
+
/**
|
|
54
|
+
* What a configuration is read by synchronously: the platform's paths and its files, read
|
|
55
|
+
* synchronously ({@link ConfigSyncPlatform}), its working folder, and its environment, as
|
|
56
|
+
* {@link LoadPlatform} has them. `nodeSyncPlatform()` of ./node adds Node's files to a platform.
|
|
57
|
+
*/
|
|
58
|
+
export type LoadSyncPlatform = ConfigSyncPlatform & Pick<Platform, 'env'> & {
|
|
59
|
+
readonly dirs: Pick<Dirs, 'cwd'>;
|
|
40
60
|
};
|
|
41
61
|
/** How a configuration is read asynchronously. */
|
|
42
|
-
export type LoadOptions =
|
|
62
|
+
export type LoadOptions = LoadLayers & {
|
|
63
|
+
/** The platform it is read by ({@link LoadPlatform}). */
|
|
64
|
+
readonly platform: LoadPlatform;
|
|
43
65
|
/**
|
|
44
66
|
* What imports a file of code: by default `import()` of its file URL, as the platform's paths
|
|
45
67
|
* give it ({@link importFileModule}). A caller of a file system of its own, not the one its
|
|
@@ -48,7 +70,10 @@ export type LoadOptions = LocateOptions & LoadLayers & {
|
|
|
48
70
|
readonly importModule?: ImportModule;
|
|
49
71
|
};
|
|
50
72
|
/** How a configuration is read synchronously. */
|
|
51
|
-
export type LoadSyncOptions =
|
|
73
|
+
export type LoadSyncOptions = LoadLayers & {
|
|
74
|
+
/** The platform it is read by synchronously ({@link LoadSyncPlatform}). */
|
|
75
|
+
readonly platform: LoadSyncPlatform;
|
|
76
|
+
};
|
|
52
77
|
/**
|
|
53
78
|
* What imports the module of the file at an absolute path, by its file URL, as the paths `path`
|
|
54
79
|
* give it: what imports a configuration of code by default, `wtf.config.ts` among them, whose types
|
|
@@ -57,24 +82,25 @@ export type LoadSyncOptions = LocateSyncOptions & LoadLayers;
|
|
|
57
82
|
*/
|
|
58
83
|
export declare const importFileModule: (path: Pick<ConfigPlatform["path"], "toFileUrl">) => ImportModule;
|
|
59
84
|
/**
|
|
60
|
-
* The configuration of the project in `folder
|
|
61
|
-
* the project, in the order of the files of its
|
|
62
|
-
* `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`,
|
|
63
|
-
* the file it came from; `{}`, of no file, when none is
|
|
64
|
-
* `ignored`. The `defaults` a tool gives are kept beside it, for
|
|
65
|
-
* the project's sections over, and so are the variables of the
|
|
66
|
-
* prefix of the names, `WTF_` by default, with the
|
|
67
|
-
* from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that
|
|
85
|
+
* The configuration of the project in `folder`, the working folder of the platform by default: the
|
|
86
|
+
* object of the first file of it at the root of the project, in the order of the files of its
|
|
87
|
+
* names — by default `wtf.config.mjs`, `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`,
|
|
88
|
+
* then the `wtf` key of package.json — and the file it came from; `{}`, of no file, when none is
|
|
89
|
+
* there. Each other file found is in `ignored`. The `defaults` a tool gives are kept beside it, for
|
|
90
|
+
* `sectionOf` of ./section to merge the project's sections over, and so are the variables of the
|
|
91
|
+
* environment of the platform, its `env`, of the prefix of the names, `WTF_` by default, with the
|
|
92
|
+
* whole environment, which placeholders are taken from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that
|
|
68
93
|
* holds no object, or a module that fails to load, is an {@link InvalidConfigFileError}.
|
|
69
94
|
*/
|
|
70
|
-
export declare function loadConfig(
|
|
95
|
+
export declare function loadConfig({ importModule, platform, ...options }: LoadOptions): Promise<ResolvedConfig>;
|
|
71
96
|
/**
|
|
72
|
-
* The configuration of the project in `folder`,
|
|
97
|
+
* The configuration of the project in `folder`, the working folder of the platform by default, with
|
|
98
|
+
* the environment of the platform over it, as {@link loadConfig} reads it, read synchronously:
|
|
73
99
|
* of a JSON file of the names, `wtf.config.json` by default, or the key of package.json, `wtf` by
|
|
74
100
|
* default, for a tool that loads its own config synchronously. A configuration of code in use, as
|
|
75
101
|
* `wtf.config.ts`, is a {@link SyncConfigInCodeError}, as importing it is asynchronous.
|
|
76
102
|
*/
|
|
77
|
-
export declare function loadConfigSync(
|
|
103
|
+
export declare function loadConfigSync({ platform, ...options }: LoadSyncOptions): ResolvedConfig;
|
|
78
104
|
/**
|
|
79
105
|
* What to say of the files of the configuration that are ignored, as a tool warns: `undefined`
|
|
80
106
|
* when none is.
|
package/dist/load.js
CHANGED
|
@@ -6,26 +6,6 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
6
6
|
}
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
|
-
/**
|
|
10
|
-
* A project's configuration, as a tool reads it: {@link loadConfig}, the object of the first file
|
|
11
|
-
* of it found at the root of the project, of code or of JSON, the file it came from, and the
|
|
12
|
-
* others found, which are ignored; {@link loadConfigSync}, the same of a JSON file or package.json
|
|
13
|
-
* alone, for a tool that reads its config synchronously; {@link ignoredWarning}, what to say of
|
|
14
|
-
* the files ignored; and {@link importFileModule}, which imports a configuration of code, as
|
|
15
|
-
* `wtf.config.ts`, by its file URL, as Node imports a module.
|
|
16
|
-
*
|
|
17
|
-
* ```ts
|
|
18
|
-
* import { loadConfig } from '@wtf/project-config/load'
|
|
19
|
-
* import { nodeConfigPlatform } from '@wtf/project-config/node'
|
|
20
|
-
*
|
|
21
|
-
* const resolved = await loadConfig(process.cwd(), {
|
|
22
|
-
* platform: nodeConfigPlatform(),
|
|
23
|
-
* environment: process.env
|
|
24
|
-
* })
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @packageDocumentation
|
|
28
|
-
*/
|
|
29
9
|
import { envPrefixOf, variablesOf } from "./env.js";
|
|
30
10
|
import { InvalidConfigFileError, SyncConfigInCodeError } from "./errors.js";
|
|
31
11
|
import { filesOf, isObject, kindOf, manifestOf, readAsync, readSync, textOf } from "./files.js";
|
|
@@ -73,35 +53,34 @@ function* configOfJson(source, key) {
|
|
|
73
53
|
return value;
|
|
74
54
|
}
|
|
75
55
|
/** What a resolved configuration keeps beside the object: the defaults and the environment. */
|
|
76
|
-
function layersOf({ defaults,
|
|
56
|
+
function layersOf({ defaults, folder: _folder, ...names }, environment) {
|
|
77
57
|
const prefix = envPrefixOf(configNamesOf(names).env);
|
|
78
58
|
return {
|
|
79
59
|
...(defaults === undefined ? {} : { defaults }),
|
|
80
|
-
|
|
81
|
-
? {}
|
|
82
|
-
: {
|
|
83
|
-
environment: { prefix, variables: variablesOf(environment, prefix), values: environment }
|
|
84
|
-
})
|
|
60
|
+
environment: { prefix, variables: variablesOf(environment, prefix), values: environment }
|
|
85
61
|
};
|
|
86
62
|
}
|
|
87
63
|
/** The options of the names alone, without what reads them. */
|
|
88
|
-
const locateNamesOf = ({ defaults: _d,
|
|
64
|
+
const locateNamesOf = ({ defaults: _d, folder: _f, ...names }) => names;
|
|
89
65
|
/**
|
|
90
|
-
* The configuration of the project in `folder
|
|
91
|
-
* the project, in the order of the files of its
|
|
92
|
-
* `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`,
|
|
93
|
-
* the file it came from; `{}`, of no file, when none is
|
|
94
|
-
* `ignored`. The `defaults` a tool gives are kept beside it, for
|
|
95
|
-
* the project's sections over, and so are the variables of the
|
|
96
|
-
* prefix of the names, `WTF_` by default, with the
|
|
97
|
-
* from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that
|
|
66
|
+
* The configuration of the project in `folder`, the working folder of the platform by default: the
|
|
67
|
+
* object of the first file of it at the root of the project, in the order of the files of its
|
|
68
|
+
* names — by default `wtf.config.mjs`, `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`,
|
|
69
|
+
* then the `wtf` key of package.json — and the file it came from; `{}`, of no file, when none is
|
|
70
|
+
* there. Each other file found is in `ignored`. The `defaults` a tool gives are kept beside it, for
|
|
71
|
+
* `sectionOf` of ./section to merge the project's sections over, and so are the variables of the
|
|
72
|
+
* environment of the platform, its `env`, of the prefix of the names, `WTF_` by default, with the
|
|
73
|
+
* whole environment, which placeholders are taken from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that
|
|
98
74
|
* holds no object, or a module that fails to load, is an {@link InvalidConfigFileError}.
|
|
99
75
|
*/
|
|
100
|
-
export async function loadConfig(
|
|
76
|
+
export async function loadConfig({ importModule, platform, ...options }) {
|
|
101
77
|
const { namespace } = configNamesOf(options);
|
|
102
|
-
const { root, key, sources } = await locateConfig(folder
|
|
78
|
+
const { root, key, sources } = await locateConfig(options.folder ?? platform.dirs.cwd, {
|
|
79
|
+
platform,
|
|
80
|
+
...locateNamesOf(options)
|
|
81
|
+
});
|
|
103
82
|
const [source, ...ignored] = sources;
|
|
104
|
-
const layers = layersOf(options);
|
|
83
|
+
const layers = layersOf(options, platform.env);
|
|
105
84
|
if (source === undefined)
|
|
106
85
|
return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers };
|
|
107
86
|
const config = kindOf(source.file) === 'code'
|
|
@@ -110,16 +89,20 @@ export async function loadConfig(folder, { importModule, platform, ...options })
|
|
|
110
89
|
return { namespace, root, key, config, source, ignored, ...layers };
|
|
111
90
|
}
|
|
112
91
|
/**
|
|
113
|
-
* The configuration of the project in `folder`,
|
|
92
|
+
* The configuration of the project in `folder`, the working folder of the platform by default, with
|
|
93
|
+
* the environment of the platform over it, as {@link loadConfig} reads it, read synchronously:
|
|
114
94
|
* of a JSON file of the names, `wtf.config.json` by default, or the key of package.json, `wtf` by
|
|
115
95
|
* default, for a tool that loads its own config synchronously. A configuration of code in use, as
|
|
116
96
|
* `wtf.config.ts`, is a {@link SyncConfigInCodeError}, as importing it is asynchronous.
|
|
117
97
|
*/
|
|
118
|
-
export function loadConfigSync(
|
|
98
|
+
export function loadConfigSync({ platform, ...options }) {
|
|
119
99
|
const { namespace } = configNamesOf(options);
|
|
120
|
-
const { root, key, sources } = locateConfigSync(folder
|
|
100
|
+
const { root, key, sources } = locateConfigSync(options.folder ?? platform.dirs.cwd, {
|
|
101
|
+
platform,
|
|
102
|
+
...locateNamesOf(options)
|
|
103
|
+
});
|
|
121
104
|
const [source, ...ignored] = sources;
|
|
122
|
-
const layers = layersOf(options);
|
|
105
|
+
const layers = layersOf(options, platform.env);
|
|
123
106
|
if (source === undefined)
|
|
124
107
|
return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers };
|
|
125
108
|
if (kindOf(source.file) === 'code')
|