@wtf/project-config 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -27
- package/dist/env.js +51 -9
- package/dist/env.js.map +1 -1
- package/dist/errors.d.ts +26 -8
- package/dist/errors.js +19 -4
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js.map +1 -1
- package/dist/load.d.ts +56 -30
- package/dist/load.js +25 -42
- package/dist/load.js.map +1 -1
- package/dist/locate.d.ts +3 -2
- package/dist/locate.js +5 -2
- package/dist/locate.js.map +1 -1
- package/dist/named.d.ts +40 -13
- package/dist/named.js +98 -32
- package/dist/named.js.map +1 -1
- package/dist/node.d.ts +11 -24
- package/dist/node.js +56 -79
- package/dist/node.js.map +1 -1
- package/dist/section.d.ts +5 -5
- package/dist/section.js +5 -5
- package/dist/section.js.map +1 -1
- package/dist/write.js +5 -4
- package/dist/write.js.map +1 -1
- package/package.json +4 -4
package/dist/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')
|
package/dist/load.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load.js","sourceRoot":"","sources":["../src/load.ts"],"names":[],"mappings":";;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,WAAW,EAAE,WAAW,EAAoB,MAAM,UAAU,CAAA;AACrE,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AAC3E,OAAO,EACL,OAAO,EACP,QAAQ,EACR,MAAM,EACN,UAAU,EACV,SAAS,EAET,QAAQ,EACR,MAAM,EACP,MAAM,YAAY,CAAA;AASnB,OAAO,EACL,aAAa,EACb,WAAW,EACX,YAAY,EACZ,gBAAgB,EAIjB,MAAM,aAAa,CAAA;AAkCpB;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAC3B,CAAC,IAA+C,EAAgB,EAAE,CAClE,KAAK,EAAE,IAAI,EAAE,EAAE,CACb,MAAM,kCAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,EAAC,CAAA;AAErC,oGAAoG;AACpG,KAAK,UAAU,cAAc,CAC3B,MAAoB,EACpB,YAA0B;IAE1B,IAAI,MAAe,CAAA;IACnB,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC1C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,sBAAsB,CAC9B,MAAM,CAAC,IAAI,EACX,mBAAmB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAC3E,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAA;IACH,CAAC;IACD,MAAM,QAA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,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,MAAM;QAChC,MAAM,IAAI,qBAAqB,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC,CAAA;IACpF,MAAM,MAAM,GAAG,QAAQ,CAAC,YAAY,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;IAClE,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,EAAE,CAAA;AACrE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,EAC7B,SAAS,EACT,GAAG,EACH,MAAM,EACN,OAAO,EAC0D;IACjE,IAAI,MAAM,KAAK,SAAS,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,SAAS,CAAA;IAClE,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CACrC,MAAM,CAAC,IAAI,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,GAAG,YAAY,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAClE,CAAA;IACD,OAAO,OAAO,SAAS,+BAA+B,MAAM,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,gCAAgC,CAAA;AAChK,CAAC","sourcesContent":["/**\n * A project's configuration, as a tool reads it: {@link loadConfig}, the object of the first file\n * of it found at the root of the project, of code or of JSON, the file it came from, and the\n * others found, which are ignored; {@link loadConfigSync}, the same of a JSON file or package.json\n * alone, for a tool that reads its config synchronously; {@link ignoredWarning}, what to say of\n * the files ignored; and {@link importFileModule}, which imports a configuration of code, as\n * `wtf.config.ts`, by its file URL, as Node imports a module.\n *\n * ```ts\n * import { loadConfig } from '@wtf/project-config/load'\n * import { nodeConfigPlatform } from '@wtf/project-config/node'\n *\n * const resolved = await loadConfig(process.cwd(), {\n * platform: nodeConfigPlatform(),\n * environment: process.env\n * })\n * ```\n *\n * @packageDocumentation\n */\nimport { envPrefixOf, variablesOf, type Environment } from './env.ts'\nimport { InvalidConfigFileError, SyncConfigInCodeError } from './errors.ts'\nimport {\n filesOf,\n isObject,\n kindOf,\n manifestOf,\n readAsync,\n type Reads,\n readSync,\n textOf\n} from './files.ts'\nimport type {\n ConfigDefaults,\n ConfigPlatform,\n ConfigSource,\n ImportModule,\n NamespaceConfig,\n ResolvedConfig\n} from './index.ts'\nimport {\n configNamesOf,\n jsonFilesOf,\n locateConfig,\n locateConfigSync,\n type LocateNames,\n type LocateOptions,\n type LocateSyncOptions\n} from './locate.ts'\n\n/** How a configuration is read, apart from the platform: its names, the defaults of the tool's\n * package, and the environment over it. */\nexport type LoadLayers = LocateNames & {\n /**\n * The configuration under the project's, as the tool's package gives it: a section the project\n * gives none of is the one of the defaults, and one it gives is merged over it (`sectionOf` of\n * ./section).\n */\n readonly defaults?: ConfigDefaults\n /**\n * The variables of an environment, as a process's: those of the prefix of the names, as `WTF_`,\n * are kept, and a section is read with them over it, and its placeholders `${env:NAME}` taken\n * from the whole (`sectionOf` of ./section). None by default: nothing here reads the process's\n * own, which `loadConfig` of ./node gives.\n */\n readonly environment?: Environment\n}\n\n/** How a configuration is read asynchronously. */\nexport type LoadOptions = LocateOptions &\n LoadLayers & {\n /**\n * What imports a file of code: by default `import()` of its file URL, as the platform's paths\n * give it ({@link importFileModule}). A caller of a file system of its own, not the one its\n * runtime imports from, gives one too.\n */\n readonly importModule?: ImportModule\n }\n\n/** How a configuration is read synchronously. */\nexport type LoadSyncOptions = LocateSyncOptions & LoadLayers\n\n/**\n * What imports the module of the file at an absolute path, by its file URL, as the paths `path`\n * give it: what imports a configuration of code by default, `wtf.config.ts` among them, whose types\n * Node strips itself. The runtime imports it, of the file system it imports from, and not the one\n * a caller gives.\n */\nexport const importFileModule =\n (path: Pick<ConfigPlatform['path'], 'toFileUrl'>): ImportModule =>\n async (file) =>\n import(path.toFileUrl(file).href)\n\n/** The configuration of a module: its default export, an object, or a function that returns one. */\nasync function configOfModule(\n source: ConfigSource,\n importModule: ImportModule\n): Promise<NamespaceConfig> {\n let module: unknown\n try {\n module = await importModule(source.path)\n } catch (error) {\n throw new InvalidConfigFileError(\n source.path,\n `failed to load: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error }\n )\n }\n const exported: unknown = isObject(module) ? module['default'] : undefined\n const config: unknown = typeof exported === 'function' ? await exported() : exported\n if (!isObject(config))\n throw new InvalidConfigFileError(\n source.path,\n 'exports no object by default: give `export default { … }`, or a function that returns one.'\n )\n return config\n}\n\n/** The configuration of a JSON file: its own object, or the `key` of package.json. */\nfunction* configOfJson(source: ConfigSource, key: string): Reads<NamespaceConfig> {\n const text = (yield* textOf(source.path)) ?? ''\n if (kindOf(source.file) === 'package') {\n const value = manifestOf(source.path, text)[key]\n if (!isObject(value))\n throw new InvalidConfigFileError(source.path, `its \"${key}\" key is no JSON object.`)\n return value\n }\n let value: unknown\n try {\n value = JSON.parse(text)\n } catch (error) {\n throw new InvalidConfigFileError(source.path, 'is no JSON.', { cause: error })\n }\n if (!isObject(value)) throw new InvalidConfigFileError(source.path, 'is no JSON object.')\n return value\n}\n\n/** What a resolved configuration keeps beside the object: the defaults and the environment. */\nfunction layersOf({ defaults, environment, ...names }: LoadLayers) {\n const prefix = envPrefixOf(configNamesOf(names).env)\n return {\n ...(defaults === undefined ? {} : { defaults }),\n ...(environment === undefined\n ? {}\n : {\n environment: { prefix, variables: variablesOf(environment, prefix), values: environment }\n })\n }\n}\n\n/** The options of the names alone, without what reads them. */\nconst locateNamesOf = ({ defaults: _d, environment: _e, ...names }: LoadLayers): LocateNames =>\n names\n\n/**\n * The configuration of the project in `folder`: the object of the first file of it at the root of\n * the project, in the order of the files of its names — by default `wtf.config.mjs`,\n * `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`, then the `wtf` key of package.json — and\n * the file it came from; `{}`, of no file, when none is there. Each other file found is in\n * `ignored`. The `defaults` a tool gives are kept beside it, for `sectionOf` of ./section to merge\n * the project's sections over, and so are the variables of the `environment` it gives of the\n * prefix of the names, `WTF_` by default, with the whole environment, which placeholders are taken\n * from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that\n * holds no object, or a module that fails to load, is an {@link InvalidConfigFileError}.\n */\nexport async function loadConfig(\n folder: string,\n { importModule, platform, ...options }: LoadOptions\n): Promise<ResolvedConfig> {\n const { namespace } = configNamesOf(options)\n const { root, key, sources } = await locateConfig(folder, { platform, ...locateNamesOf(options) })\n const [source, ...ignored] = sources\n const layers = layersOf(options)\n if (source === undefined)\n return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers }\n const config =\n kindOf(source.file) === 'code'\n ? await configOfModule(source, importModule ?? importFileModule(platform.path))\n : await readAsync(configOfJson(source, key), filesOf(platform))\n return { namespace, root, key, config, source, ignored, ...layers }\n}\n\n/**\n * The configuration of the project in `folder`, as {@link loadConfig} reads it, read synchronously:\n * of a JSON file of the names, `wtf.config.json` by default, or the key of package.json, `wtf` by\n * default, for a tool that loads its own config synchronously. A configuration of code in use, as\n * `wtf.config.ts`, is a {@link SyncConfigInCodeError}, as importing it is asynchronous.\n */\nexport function loadConfigSync(\n folder: string,\n { platform, ...options }: LoadSyncOptions\n): ResolvedConfig {\n const { namespace } = configNamesOf(options)\n const { root, key, sources } = locateConfigSync(folder, { platform, ...locateNamesOf(options) })\n const [source, ...ignored] = sources\n const layers = layersOf(options)\n if (source === undefined)\n return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers }\n if (kindOf(source.file) === 'code')\n throw new SyncConfigInCodeError(source.path, { files: jsonFilesOf(options), key })\n const config = readSync(configOfJson(source, key), platform.files)\n return { namespace, root, key, config, source, ignored, ...layers }\n}\n\n/**\n * What to say of the files of the configuration that are ignored, as a tool warns: `undefined`\n * when none is.\n */\nexport function ignoredWarning({\n namespace,\n key,\n source,\n ignored\n}: Pick<ResolvedConfig, 'namespace' | 'key' | 'source' | 'ignored'>): string | undefined {\n if (source === undefined || ignored.length === 0) return undefined\n const names = ignored.map(({ file }) =>\n kindOf(file) === 'package' ? `the \"${key}\" key of ${file}` : file\n )\n return `The ${namespace} configuration is read from ${source.file}; ${names.join(' and ')} ${names.length === 1 ? 'is' : 'are'} ignored. Keep it in one file.`\n}\n"]}
|
|
1
|
+
{"version":3,"file":"load.js","sourceRoot":"","sources":["../src/load.ts"],"names":[],"mappings":";;;;;;;;AA4BA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAoB,MAAM,UAAU,CAAA;AACrE,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AAC3E,OAAO,EACL,OAAO,EACP,QAAQ,EACR,MAAM,EACN,UAAU,EACV,SAAS,EAET,QAAQ,EACR,MAAM,EACP,MAAM,YAAY,CAAA;AAUnB,OAAO,EACL,aAAa,EACb,WAAW,EACX,YAAY,EACZ,gBAAgB,EAEjB,MAAM,aAAa,CAAA;AAqDpB;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAC3B,CAAC,IAA+C,EAAgB,EAAE,CAClE,KAAK,EAAE,IAAI,EAAE,EAAE,CACb,MAAM,kCAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,EAAC,CAAA;AAErC,oGAAoG;AACpG,KAAK,UAAU,cAAc,CAC3B,MAAoB,EACpB,YAA0B;IAE1B,IAAI,MAAe,CAAA;IACnB,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC1C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,sBAAsB,CAC9B,MAAM,CAAC,IAAI,EACX,mBAAmB,KAAK,YAAY,KAAK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ent":["/**\n * A project's configuration, as a tool reads it: {@link loadConfig}, the object of the first file\n * of it found at the root of the project, of code or of JSON, the file it came from, and the\n * others found, which are ignored; {@link loadConfigSync}, the same of a JSON file or package.json\n * alone, for a tool that reads its config synchronously; {@link ignoredWarning}, what to say of\n * the files ignored; and {@link importFileModule}, which imports a configuration of code, as\n * `wtf.config.ts`, by its file URL, as Node imports a module.\n *\n * Each reads by the platform it is given, and nothing of the process: its files, its paths, the\n * folder it works in, which the project is looked for from, and its environment, whose variables\n * are over the configuration. The entry point builds it, as `nodePlatform()` of\n * `@wtf/platform-node/platform` makes Node's:\n *\n * ```ts\n * import { nodeStorage } from '@wtf/platform-fs-node/storage'\n * import { nodePlatform } from '@wtf/platform-node/platform'\n *\n * import { loadConfig } from '@wtf/project-config/load'\n *\n * const platform = nodePlatform({ fetch, storage: nodeStorage() })\n * const resolved = await loadConfig({ platform })\n * ```\n *\n * @packageDocumentation\n */\nimport type { Platform } from '@wtf/platform'\nimport type { Dirs } from '@wtf/platform/dirs'\n\nimport { envPrefixOf, variablesOf, type Environment } from './env.ts'\nimport { InvalidConfigFileError, SyncConfigInCodeError } from './errors.ts'\nimport {\n filesOf,\n isObject,\n kindOf,\n manifestOf,\n readAsync,\n type Reads,\n readSync,\n textOf\n} from './files.ts'\nimport type {\n ConfigDefaults,\n ConfigPlatform,\n ConfigSource,\n ConfigSyncPlatform,\n ImportModule,\n NamespaceConfig,\n ResolvedConfig\n} from './index.ts'\nimport {\n configNamesOf,\n jsonFilesOf,\n locateConfig,\n locateConfigSync,\n type LocateNames\n} from './locate.ts'\n\n/** How a configuration is read, apart from the platform: the folder of the project, its names, and\n * the defaults of the tool's package. */\nexport type LoadLayers = LocateNames & {\n /**\n * The folder of the project, which its root is looked for from: the working folder of the\n * platform, `dirs.cwd`, by default.\n */\n readonly folder?: string\n /**\n * The configuration under the project's, as the tool's package gives it: a section the project\n * gives none of is the one of the defaults, and one it gives is merged over it (`sectionOf` of\n * ./section).\n */\n readonly defaults?: ConfigDefaults\n}\n\n/**\n * What a configuration is read by asynchronously: the platform's files, its paths and its working\n * folder ({@link ConfigPlatform}), and its environment, `env`, of which the variables of the prefix\n * of the names, as `WTF_`, are kept, and a section is read with them over it, and its placeholders\n * `${env:NAME}` taken from the whole (`sectionOf` of ./section). A `Platform` of `@wtf/platform` is\n * one.\n */\nexport type LoadPlatform = ConfigPlatform & Pick<Platform, 'env'>\n\n/**\n * What a configuration is read by synchronously: the platform's paths and its files, read\n * synchronously ({@link ConfigSyncPlatform}), its working folder, and its environment, as\n * {@link LoadPlatform} has them. `nodeSyncPlatform()` of ./node adds Node's files to a platform.\n */\nexport type LoadSyncPlatform = ConfigSyncPlatform &\n Pick<Platform, 'env'> & { readonly dirs: Pick<Dirs, 'cwd'> }\n\n/** How a configuration is read asynchronously. */\nexport type LoadOptions = LoadLayers & {\n /** The platform it is read by ({@link LoadPlatform}). */\n readonly platform: LoadPlatform\n /**\n * What imports a file of code: by default `import()` of its file URL, as the platform's paths\n * give it ({@link importFileModule}). A caller of a file system of its own, not the one its\n * runtime imports from, gives one too.\n */\n readonly importModule?: ImportModule\n}\n\n/** How a configuration is read synchronously. */\nexport type LoadSyncOptions = LoadLayers & {\n /** The platform it is read by synchronously ({@link LoadSyncPlatform}). */\n readonly platform: LoadSyncPlatform\n}\n\n/**\n * What imports the module of the file at an absolute path, by its file URL, as the paths `path`\n * give it: what imports a configuration of code by default, `wtf.config.ts` among them, whose types\n * Node strips itself. The runtime imports it, of the file system it imports from, and not the one\n * a caller gives.\n */\nexport const importFileModule =\n (path: Pick<ConfigPlatform['path'], 'toFileUrl'>): ImportModule =>\n async (file) =>\n import(path.toFileUrl(file).href)\n\n/** The configuration of a module: its default export, an object, or a function that returns one. */\nasync function configOfModule(\n source: ConfigSource,\n importModule: ImportModule\n): Promise<NamespaceConfig> {\n let module: unknown\n try {\n module = await importModule(source.path)\n } catch (error) {\n throw new InvalidConfigFileError(\n source.path,\n `failed to load: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error }\n )\n }\n const exported: unknown = isObject(module) ? module['default'] : undefined\n const config: unknown = typeof exported === 'function' ? await exported() : exported\n if (!isObject(config))\n throw new InvalidConfigFileError(\n source.path,\n 'exports no object by default: give `export default { … }`, or a function that returns one.'\n )\n return config\n}\n\n/** The configuration of a JSON file: its own object, or the `key` of package.json. */\nfunction* configOfJson(source: ConfigSource, key: string): Reads<NamespaceConfig> {\n const text = (yield* textOf(source.path)) ?? ''\n if (kindOf(source.file) === 'package') {\n const value = manifestOf(source.path, text)[key]\n if (!isObject(value))\n throw new InvalidConfigFileError(source.path, `its \"${key}\" key is no JSON object.`)\n return value\n }\n let value: unknown\n try {\n value = JSON.parse(text)\n } catch (error) {\n throw new InvalidConfigFileError(source.path, 'is no JSON.', { cause: error })\n }\n if (!isObject(value)) throw new InvalidConfigFileError(source.path, 'is no JSON object.')\n return value\n}\n\n/** What a resolved configuration keeps beside the object: the defaults and the environment. */\nfunction layersOf({ defaults, folder: _folder, ...names }: LoadLayers, environment: Environment) {\n const prefix = envPrefixOf(configNamesOf(names).env)\n return {\n ...(defaults === undefined ? {} : { defaults }),\n environment: { prefix, variables: variablesOf(environment, prefix), values: environment }\n }\n}\n\n/** The options of the names alone, without what reads them. */\nconst locateNamesOf = ({ defaults: _d, folder: _f, ...names }: LoadLayers): LocateNames => names\n\n/**\n * The configuration of the project in `folder`, the working folder of the platform by default: the\n * object of the first file of it at the root of the project, in the order of the files of its\n * names — by default `wtf.config.mjs`, `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`,\n * then the `wtf` key of package.json — and the file it came from; `{}`, of no file, when none is\n * there. Each other file found is in `ignored`. The `defaults` a tool gives are kept beside it, for\n * `sectionOf` of ./section to merge the project's sections over, and so are the variables of the\n * environment of the platform, its `env`, of the prefix of the names, `WTF_` by default, with the\n * whole environment, which placeholders are taken from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that\n * holds no object, or a module that fails to load, is an {@link InvalidConfigFileError}.\n */\nexport async function loadConfig({\n importModule,\n platform,\n ...options\n}: LoadOptions): Promise<ResolvedConfig> {\n const { namespace } = configNamesOf(options)\n const { root, key, sources } = await locateConfig(options.folder ?? platform.dirs.cwd, {\n platform,\n ...locateNamesOf(options)\n })\n const [source, ...ignored] = sources\n const layers = layersOf(options, platform.env)\n if (source === undefined)\n return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers }\n const config =\n kindOf(source.file) === 'code'\n ? await configOfModule(source, importModule ?? importFileModule(platform.path))\n : await readAsync(configOfJson(source, key), filesOf(platform))\n return { namespace, root, key, config, source, ignored, ...layers }\n}\n\n/**\n * The configuration of the project in `folder`, the working folder of the platform by default, with\n * the environment of the platform over it, as {@link loadConfig} reads it, read synchronously:\n * of a JSON file of the names, `wtf.config.json` by default, or the key of package.json, `wtf` by\n * default, for a tool that loads its own config synchronously. A configuration of code in use, as\n * `wtf.config.ts`, is a {@link SyncConfigInCodeError}, as importing it is asynchronous.\n */\nexport function loadConfigSync({ platform, ...options }: LoadSyncOptions): ResolvedConfig {\n const { namespace } = configNamesOf(options)\n const { root, key, sources } = locateConfigSync(options.folder ?? platform.dirs.cwd, {\n platform,\n ...locateNamesOf(options)\n })\n const [source, ...ignored] = sources\n const layers = layersOf(options, platform.env)\n if (source === undefined)\n return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers }\n if (kindOf(source.file) === 'code')\n throw new SyncConfigInCodeError(source.path, { files: jsonFilesOf(options), key })\n const config = readSync(configOfJson(source, key), platform.files)\n return { namespace, root, key, config, source, ignored, ...layers }\n}\n\n/**\n * What to say of the files of the configuration that are ignored, as a tool warns: `undefined`\n * when none is.\n */\nexport function ignoredWarning({\n namespace,\n key,\n source,\n ignored\n}: Pick<ResolvedConfig, 'namespace' | 'key' | 'source' | 'ignored'>): string | undefined {\n if (source === undefined || ignored.length === 0) return undefined\n const names = ignored.map(({ file }) =>\n kindOf(file) === 'package' ? `the \"${key}\" key of ${file}` : file\n )\n return `The ${namespace} configuration is read from ${source.file}; ${names.join(' and ')} ${names.length === 1 ? 'is' : 'are'} ignored. Keep it in one file.`\n}\n"]}
|
package/dist/locate.d.ts
CHANGED
|
@@ -38,13 +38,14 @@ export type LocateNames = NamespaceNames & {
|
|
|
38
38
|
export type LocateOptions = LocateNames & {
|
|
39
39
|
/**
|
|
40
40
|
* The platform it is read by: its storage, whose `getDirectory()` is the root of its file
|
|
41
|
-
* system, as a browser's or `
|
|
41
|
+
* system, as a browser's or `nodePlatform()` of `@wtf/platform-node/platform` gives it, its
|
|
42
|
+
* paths, and its working folder, which a relative folder is resolved against.
|
|
42
43
|
*/
|
|
43
44
|
readonly platform: ConfigPlatform;
|
|
44
45
|
};
|
|
45
46
|
/** Where the configuration is looked for, synchronously, by the platform's files. */
|
|
46
47
|
export type LocateSyncOptions = LocateNames & {
|
|
47
|
-
/** The platform it is read by synchronously, as `nodeSyncPlatform()` of ./node
|
|
48
|
+
/** The platform it is read by synchronously, as `nodeSyncPlatform()` of ./node makes it. */
|
|
48
49
|
readonly platform: ConfigSyncPlatform;
|
|
49
50
|
};
|
|
50
51
|
/**
|
package/dist/locate.js
CHANGED
|
@@ -9,10 +9,13 @@
|
|
|
9
9
|
* the files of a synchronous one.
|
|
10
10
|
*
|
|
11
11
|
* ```ts
|
|
12
|
+
* import { nodeStorage } from '@wtf/platform-fs-node/storage'
|
|
13
|
+
* import { nodePlatform } from '@wtf/platform-node/platform'
|
|
14
|
+
*
|
|
12
15
|
* import { locateConfig } from '@wtf/project-config/locate'
|
|
13
|
-
* import { nodeConfigPlatform } from '@wtf/project-config/node'
|
|
14
16
|
*
|
|
15
|
-
* const
|
|
17
|
+
* const platform = nodePlatform({ fetch, storage: nodeStorage() })
|
|
18
|
+
* const { root, sources } = await locateConfig(platform.dirs.cwd, { platform })
|
|
16
19
|
* ```
|
|
17
20
|
*
|
|
18
21
|
* @packageDocumentation
|
package/dist/locate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"locate.js","sourceRoot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,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,CAAA;AAC/B,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,MAAc,EACd,EAAE,QAAQ,EAAmC,EAC5B,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;AAEjF;yDACyD;AACzD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,MAAc,EACd,EAAE,QAAQ,EAAuC,EACzC,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;AAEpE;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,MAAc,EACd,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAiB,EACb,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;AAE/F;yBACyB;AACzB,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,MAAc,EACd,EAAE,QAAQ,EAAE,GAAG,KAAK,EAAqB,EAC1B,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA","sourcesContent":["/**\n * Where a project's configuration is: {@link projectRootOf}, the folder it is looked for in, the\n * root of the workspace the project is in, or the project's own folder; {@link locateConfig}, each\n * file of it there, in the order of the files of its names, of which the first is used; and\n * {@link configNamesOf}, those names: the files ({@link configFilesOf} of the namespace by\n * default), the key of package.json (the namespace) and the prefix of the environment's variables\n * (the key). The namespace is `wtf` by default ({@link DEFAULT_NAMESPACE}). Each reads by the\n * platform it is given, asynchronously; {@link projectRootOfSync} and {@link locateConfigSync} by\n * the files of a synchronous one.\n *\n * ```ts\n * import { locateConfig } from '@wtf/project-config/locate'\n * import { nodeConfigPlatform } from '@wtf/project-config/node'\n *\n * const { root, sources } = await locateConfig(process.cwd(), { platform: nodeConfigPlatform() })\n * ```\n *\n * @packageDocumentation\n */\nimport { envNameOf } from './env.ts'\nimport {\n filesOf,\n isObject,\n kindOf,\n manifestAt,\n namesOf,\n PACKAGE_JSON,\n readAsync,\n type Reads,\n readSync,\n textOf\n} from './files.ts'\nimport type {\n ConfigNames,\n ConfigPlatform,\n ConfigSource,\n ConfigSyncPlatform,\n LocatedConfig\n} from './index.ts'\n\n/** The namespace a configuration is of when none is given: `wtf`, as `wtf.config.json`. */\nexport const DEFAULT_NAMESPACE = 'wtf'\n\n/**\n * The files the configuration of `namespace` can be in by default, in the order the first found is\n * used: code first, as a person who writes code means it to be read, then JSON. After them comes\n * the `namespace` key of package.json, where a write makes it when there is none.\n */\nexport const configFilesOf = (namespace: string = DEFAULT_NAMESPACE): readonly string[] => [\n `${namespace}.config.mjs`,\n `${namespace}.config.js`,\n `${namespace}.config.ts`,\n `${namespace}.config.json`\n]\n\n/** The names of a namespace's configuration: the namespace, `wtf` by default, and the names a\n * tool gives. */\nexport type NamespaceNames = ConfigNames & {\n /** The namespace of the tools: the name of its files and its key by default. `wtf` by default. */\n readonly namespace?: string\n}\n\n/**\n * The names of the configuration of the namespace, each as `names` gives it, else its default: the\n * files, {@link configFilesOf} the namespace; the key of package.json, the namespace; and the\n * prefix of the environment's variables, the key. A file that is neither code nor JSON,\n * package.json among the files, an empty key, or a prefix of no letter or digit, is a `TypeError`.\n */\nexport function configNamesOf({\n namespace = DEFAULT_NAMESPACE,\n files,\n key,\n env\n}: NamespaceNames = {}): Required<ConfigNames> & { readonly namespace: string } {\n const resolvedKey = key ?? namespace\n const names = {\n namespace,\n files: files ?? configFilesOf(namespace),\n key: resolvedKey,\n env: env ?? resolvedKey\n }\n for (const file of names.files)\n if (kindOf(file) === 'package')\n throw new TypeError(\n `${PACKAGE_JSON} is no file of the configuration: it holds it by its key, which \\`key\\` names.`\n )\n if (names.key === '')\n throw new TypeError(`The key of package.json is empty: name one, as ${namespace}.`)\n if (envNameOf(names.env) === '')\n throw new TypeError(\n `The name of the variables of the environment is empty: name one, as ${names.key}.`\n )\n return names\n}\n\n/** The JSON files of the names of a namespace, which a write makes or changes, and a synchronous\n * load reads: those of its files that are no code, in their order. */\nexport const jsonFilesOf = (names: NamespaceNames = {}): readonly string[] =>\n configNamesOf(names).files.filter((file) => kindOf(file) === 'json')\n\n/** Where the configuration is looked for, apart from the platform: the namespace and its names,\n * each its default when not given, and whether the root is looked for from the folder up. */\nexport type LocateNames = NamespaceNames & {\n /**\n * Whether the root is looked for from the folder up, as {@link projectRootOf} finds it: by\n * default. With `false`, the folder given is the root.\n */\n readonly up?: boolean\n}\n\n/** Where the configuration is looked for, asynchronously, by the platform's files. */\nexport type LocateOptions = LocateNames & {\n /**\n * The platform it is read by: its storage, whose `getDirectory()` is the root of its file\n * system, as a browser's or `nodeConfigPlatform()` of ./node gives it, and its paths.\n */\n readonly platform: ConfigPlatform\n}\n\n/** Where the configuration is looked for, synchronously, by the platform's files. */\nexport type LocateSyncOptions = LocateNames & {\n /** The platform it is read by synchronously, as `nodeSyncPlatform()` of ./node gives it. */\n readonly platform: ConfigSyncPlatform\n}\n\n/** The paths the root is looked for by. */\ntype Paths = Pick<ConfigPlatform['path'], 'resolve' | 'join' | 'dirname'>\n\n/** Whether the folder is the root of a workspace: a pnpm-workspace.yaml, or npm's `workspaces`. */\nfunction* isWorkspaceRoot(folder: string, { join }: Paths): Reads<boolean> {\n if ((yield* textOf(join(folder, 'pnpm-workspace.yaml'))) !== undefined) return true\n let manifest: Record<string, unknown> | undefined\n try {\n manifest = yield* manifestAt(join(folder, PACKAGE_JSON))\n } catch {\n return false\n }\n return isObject(manifest) && manifest['workspaces'] !== undefined\n}\n\n/** The root of the project in `folder`, of the reads it asks for. */\nfunction* rootOf(folder: string, path: Paths): Reads<string> {\n const { dirname, join, resolve } = path\n let project: string | undefined\n for (let at = resolve(folder); ; at = dirname(at)) {\n if (yield* isWorkspaceRoot(at, path)) return at\n if (project === undefined && (yield* textOf(join(at, PACKAGE_JSON))) !== undefined) project = at\n if (dirname(at) === at) return project ?? resolve(folder)\n }\n}\n\n/** The configuration of the project in `folder`, located, of the reads it asks for. */\nfunction* locate(\n folder: string,\n { up = true, ...names }: LocateNames,\n path: Paths\n): Reads<LocatedConfig> {\n const { files, key } = configNamesOf(names)\n const root = up ? yield* rootOf(folder, path) : path.resolve(folder)\n const found = yield* namesOf(root)\n const manifest = yield* manifestAt(path.join(root, PACKAGE_JSON))\n const sources: ConfigSource[] = [\n ...files.filter((file) => found.has(file)),\n ...(manifest !== undefined && Object.hasOwn(manifest, key) ? [PACKAGE_JSON] : [])\n ].map((file) => ({ file, path: path.join(root, file) }))\n return { root, key, sources }\n}\n\n/**\n * The folder the configuration of the project in `folder` is looked for in: the root of the\n * workspace it is in, the nearest from `folder` up with a pnpm-workspace.yaml or a package.json of\n * `workspaces`; else the project's own folder, the nearest from `folder` up with a package.json;\n * else `folder`. One configuration then serves each package of a workspace.\n */\nexport const projectRootOf = (\n folder: string,\n { platform }: Pick<LocateOptions, 'platform'>\n): Promise<string> => readAsync(rootOf(folder, platform.path), filesOf(platform))\n\n/** The folder the configuration of the project in `folder` is looked for in, as\n * {@link projectRootOf} finds it, read synchronously. */\nexport const projectRootOfSync = (\n folder: string,\n { platform }: Pick<LocateSyncOptions, 'platform'>\n): string => readSync(rootOf(folder, platform.path), platform.files)\n\n/**\n * Each file of the configuration of the project in `folder`, at its root, in the order of the\n * files of its names (`wtf.config.mjs`, `.js`, `.ts` and `.json` by default), and package.json\n * when it has the key (`wtf` by default). The first is the one in use. The root is\n * {@link projectRootOf} `folder`, or `folder` itself with `up: false`. A package.json that is no\n * JSON object is an `InvalidConfigFileError` of ./errors; names that are not of the configuration,\n * a `TypeError`, as {@link configNamesOf} says.\n */\nexport const locateConfig = (\n folder: string,\n { platform, ...names }: LocateOptions\n): Promise<LocatedConfig> => readAsync(locate(folder, names, platform.path), filesOf(platform))\n\n/** Each file of the configuration of the project in `folder`, as {@link locateConfig} finds them,\n * read synchronously. */\nexport const locateConfigSync = (\n folder: string,\n { platform, ...names }: LocateSyncOptions\n): LocatedConfig => readSync(locate(folder, names, platform.path), platform.files)\n"]}
|
|
1
|
+
{"version":3,"file":"locate.js","sourceRoot":"","sources":["../src/locate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACpC,OAAO,EACL,OAAO,EACP,QAAQ,EACR,MAAM,EACN,UAAU,EACV,OAAO,EACP,YAAY,EACZ,SAAS,EAET,QAAQ,EACR,MAAM,EACP,MAAM,YAAY,CAAA;AASnB,2FAA2F;AAC3F,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAA;AAEtC;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,YAAoB,iBAAiB,EAAqB,EAAE,CAAC;IACzF,GAAG,SAAS,aAAa;IACzB,GAAG,SAAS,YAAY;IACxB,GAAG,SAAS,YAAY;IACxB,GAAG,SAAS,cAAc;CAC3B,CAAA;AASD;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,EAC5B,SAAS,GAAG,iBAAiB,EAC7B,KAAK,EACL,GAAG,EACH,GAAG,KACe,EAAE;IACpB,MAAM,WAAW,GAAG,GAAG,IAAI,SAAS,CAAA;IACpC,MAAM,KAAK,GAAG;QACZ,SAAS;QACT,KAAK,EAAE,KAAK,IAAI,aAAa,CAAC,SAAS,CAAC;QACxC,GAAG,EAAE,WAAW;QAChB,GAAG,EAAE,GAAG,IAAI,WAAW;KACxB,CAAA;IACD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,KAAK;QAC5B,IAAI,MAAM,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Q,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA","sourcesContent":["/**\n * Where a project's configuration is: {@link projectRootOf}, the folder it is looked for in, the\n * root of the workspace the project is in, or the project's own folder; {@link locateConfig}, each\n * file of it there, in the order of the files of its names, of which the first is used; and\n * {@link configNamesOf}, those names: the files ({@link configFilesOf} of the namespace by\n * default), the key of package.json (the namespace) and the prefix of the environment's variables\n * (the key). The namespace is `wtf` by default ({@link DEFAULT_NAMESPACE}). Each reads by the\n * platform it is given, asynchronously; {@link projectRootOfSync} and {@link locateConfigSync} by\n * the files of a synchronous one.\n *\n * ```ts\n * import { nodeStorage } from '@wtf/platform-fs-node/storage'\n * import { nodePlatform } from '@wtf/platform-node/platform'\n *\n * import { locateConfig } from '@wtf/project-config/locate'\n *\n * const platform = nodePlatform({ fetch, storage: nodeStorage() })\n * const { root, sources } = await locateConfig(platform.dirs.cwd, { platform })\n * ```\n *\n * @packageDocumentation\n */\nimport { envNameOf } from './env.ts'\nimport {\n filesOf,\n isObject,\n kindOf,\n manifestAt,\n namesOf,\n PACKAGE_JSON,\n readAsync,\n type Reads,\n readSync,\n textOf\n} from './files.ts'\nimport type {\n ConfigNames,\n ConfigPlatform,\n ConfigSource,\n ConfigSyncPlatform,\n LocatedConfig\n} from './index.ts'\n\n/** The namespace a configuration is of when none is given: `wtf`, as `wtf.config.json`. */\nexport const DEFAULT_NAMESPACE = 'wtf'\n\n/**\n * The files the configuration of `namespace` can be in by default, in the order the first found is\n * used: code first, as a person who writes code means it to be read, then JSON. After them comes\n * the `namespace` key of package.json, where a write makes it when there is none.\n */\nexport const configFilesOf = (namespace: string = DEFAULT_NAMESPACE): readonly string[] => [\n `${namespace}.config.mjs`,\n `${namespace}.config.js`,\n `${namespace}.config.ts`,\n `${namespace}.config.json`\n]\n\n/** The names of a namespace's configuration: the namespace, `wtf` by default, and the names a\n * tool gives. */\nexport type NamespaceNames = ConfigNames & {\n /** The namespace of the tools: the name of its files and its key by default. `wtf` by default. */\n readonly namespace?: string\n}\n\n/**\n * The names of the configuration of the namespace, each as `names` gives it, else its default: the\n * files, {@link configFilesOf} the namespace; the key of package.json, the namespace; and the\n * prefix of the environment's variables, the key. A file that is neither code nor JSON,\n * package.json among the files, an empty key, or a prefix of no letter or digit, is a `TypeError`.\n */\nexport function configNamesOf({\n namespace = DEFAULT_NAMESPACE,\n files,\n key,\n env\n}: NamespaceNames = {}): Required<ConfigNames> & { readonly namespace: string } {\n const resolvedKey = key ?? namespace\n const names = {\n namespace,\n files: files ?? configFilesOf(namespace),\n key: resolvedKey,\n env: env ?? resolvedKey\n }\n for (const file of names.files)\n if (kindOf(file) === 'package')\n throw new TypeError(\n `${PACKAGE_JSON} is no file of the configuration: it holds it by its key, which \\`key\\` names.`\n )\n if (names.key === '')\n throw new TypeError(`The key of package.json is empty: name one, as ${namespace}.`)\n if (envNameOf(names.env) === '')\n throw new TypeError(\n `The name of the variables of the environment is empty: name one, as ${names.key}.`\n )\n return names\n}\n\n/** The JSON files of the names of a namespace, which a write makes or changes, and a synchronous\n * load reads: those of its files that are no code, in their order. */\nexport const jsonFilesOf = (names: NamespaceNames = {}): readonly string[] =>\n configNamesOf(names).files.filter((file) => kindOf(file) === 'json')\n\n/** Where the configuration is looked for, apart from the platform: the namespace and its names,\n * each its default when not given, and whether the root is looked for from the folder up. */\nexport type LocateNames = NamespaceNames & {\n /**\n * Whether the root is looked for from the folder up, as {@link projectRootOf} finds it: by\n * default. With `false`, the folder given is the root.\n */\n readonly up?: boolean\n}\n\n/** Where the configuration is looked for, asynchronously, by the platform's files. */\nexport type LocateOptions = LocateNames & {\n /**\n * The platform it is read by: its storage, whose `getDirectory()` is the root of its file\n * system, as a browser's or `nodePlatform()` of `@wtf/platform-node/platform` gives it, its\n * paths, and its working folder, which a relative folder is resolved against.\n */\n readonly platform: ConfigPlatform\n}\n\n/** Where the configuration is looked for, synchronously, by the platform's files. */\nexport type LocateSyncOptions = LocateNames & {\n /** The platform it is read by synchronously, as `nodeSyncPlatform()` of ./node makes it. */\n readonly platform: ConfigSyncPlatform\n}\n\n/** The paths the root is looked for by. */\ntype Paths = Pick<ConfigPlatform['path'], 'resolve' | 'join' | 'dirname'>\n\n/** Whether the folder is the root of a workspace: a pnpm-workspace.yaml, or npm's `workspaces`. */\nfunction* isWorkspaceRoot(folder: string, { join }: Paths): Reads<boolean> {\n if ((yield* textOf(join(folder, 'pnpm-workspace.yaml'))) !== undefined) return true\n let manifest: Record<string, unknown> | undefined\n try {\n manifest = yield* manifestAt(join(folder, PACKAGE_JSON))\n } catch {\n return false\n }\n return isObject(manifest) && manifest['workspaces'] !== undefined\n}\n\n/** The root of the project in `folder`, of the reads it asks for. */\nfunction* rootOf(folder: string, path: Paths): Reads<string> {\n const { dirname, join, resolve } = path\n let project: string | undefined\n for (let at = resolve(folder); ; at = dirname(at)) {\n if (yield* isWorkspaceRoot(at, path)) return at\n if (project === undefined && (yield* textOf(join(at, PACKAGE_JSON))) !== undefined) project = at\n if (dirname(at) === at) return project ?? resolve(folder)\n }\n}\n\n/** The configuration of the project in `folder`, located, of the reads it asks for. */\nfunction* locate(\n folder: string,\n { up = true, ...names }: LocateNames,\n path: Paths\n): Reads<LocatedConfig> {\n const { files, key } = configNamesOf(names)\n const root = up ? yield* rootOf(folder, path) : path.resolve(folder)\n const found = yield* namesOf(root)\n const manifest = yield* manifestAt(path.join(root, PACKAGE_JSON))\n const sources: ConfigSource[] = [\n ...files.filter((file) => found.has(file)),\n ...(manifest !== undefined && Object.hasOwn(manifest, key) ? [PACKAGE_JSON] : [])\n ].map((file) => ({ file, path: path.join(root, file) }))\n return { root, key, sources }\n}\n\n/**\n * The folder the configuration of the project in `folder` is looked for in: the root of the\n * workspace it is in, the nearest from `folder` up with a pnpm-workspace.yaml or a package.json of\n * `workspaces`; else the project's own folder, the nearest from `folder` up with a package.json;\n * else `folder`. One configuration then serves each package of a workspace.\n */\nexport const projectRootOf = (\n folder: string,\n { platform }: Pick<LocateOptions, 'platform'>\n): Promise<string> => readAsync(rootOf(folder, platform.path), filesOf(platform))\n\n/** The folder the configuration of the project in `folder` is looked for in, as\n * {@link projectRootOf} finds it, read synchronously. */\nexport const projectRootOfSync = (\n folder: string,\n { platform }: Pick<LocateSyncOptions, 'platform'>\n): string => readSync(rootOf(folder, platform.path), platform.files)\n\n/**\n * Each file of the configuration of the project in `folder`, at its root, in the order of the\n * files of its names (`wtf.config.mjs`, `.js`, `.ts` and `.json` by default), and package.json\n * when it has the key (`wtf` by default). The first is the one in use. The root is\n * {@link projectRootOf} `folder`, or `folder` itself with `up: false`. A package.json that is no\n * JSON object is an `InvalidConfigFileError` of ./errors; names that are not of the configuration,\n * a `TypeError`, as {@link configNamesOf} says.\n */\nexport const locateConfig = (\n folder: string,\n { platform, ...names }: LocateOptions\n): Promise<LocatedConfig> => readAsync(locate(folder, names, platform.path), filesOf(platform))\n\n/** Each file of the configuration of the project in `folder`, as {@link locateConfig} finds them,\n * read synchronously. */\nexport const locateConfigSync = (\n folder: string,\n { platform, ...names }: LocateSyncOptions\n): LocatedConfig => readSync(locate(folder, names, platform.path), platform.files)\n"]}
|
package/dist/named.d.ts
CHANGED
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
* as it chooses, or a plain value, whose options are merged over it ({@link factoryOf}). A set is
|
|
20
20
|
* a map of entries by name, the export of a module that is one, or an array of those, a later set
|
|
21
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
|
|
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.
|
|
23
25
|
*
|
|
24
26
|
* ```ts
|
|
25
27
|
* import { factoryOf, resolveNamed, resolveNamedSet } from '@wtf/project-config/named'
|
|
@@ -40,7 +42,7 @@
|
|
|
40
42
|
* @packageDocumentation
|
|
41
43
|
*/
|
|
42
44
|
import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec';
|
|
43
|
-
import type { SectionSchema } from './index.ts';
|
|
45
|
+
import type { ConfigPath, SectionSchema } from './index.ts';
|
|
44
46
|
/**
|
|
45
47
|
* The export of a module, as a configuration of JSON names it: the module, by what the loader
|
|
46
48
|
* resolves, as a path from the root of the project or the name of a package, and the name of its
|
|
@@ -84,20 +86,39 @@ export type NamedSets<T> = NamedSet<T> | readonly NamedSet<T>[];
|
|
|
84
86
|
* of its exports, or a failure, which a `NamedError` of `UNLOADABLE_MODULE` holds as its cause.
|
|
85
87
|
*/
|
|
86
88
|
export type LoadModule = (module: string) => Promise<unknown>;
|
|
87
|
-
/**
|
|
88
|
-
|
|
89
|
-
|
|
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. */
|
|
90
95
|
readonly load?: LoadModule | undefined;
|
|
91
96
|
/**
|
|
92
|
-
* What checks
|
|
93
|
-
*
|
|
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.
|
|
94
100
|
*/
|
|
95
101
|
readonly check?: SectionSchema<T> | undefined;
|
|
96
102
|
/** What the entries are, which an error names: `entry` by default, as `formatter`. */
|
|
97
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;
|
|
98
118
|
};
|
|
99
|
-
/** How a reference is resolved: the key of its names, its entries, and
|
|
100
|
-
|
|
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> & {
|
|
101
122
|
/** The key a reference of options names its entry by, as `format`: none takes a name alone. */
|
|
102
123
|
readonly key?: string | undefined;
|
|
103
124
|
/** The entries a reference names, by their names: none by default. */
|
|
@@ -119,10 +140,14 @@ export declare function factoryOf<T>(value: T): NamedFactory<T>;
|
|
|
119
140
|
* name or the export of a module that is one, a later set over an earlier one by name, so a
|
|
120
141
|
* project's own are over those of a package; none of `undefined`. Each entry is a function, a
|
|
121
142
|
* factory as it is, or a plain value, checked by `check`, of {@link factoryOf}; or the export of
|
|
122
|
-
* a module that is one of those, loaded by `load`.
|
|
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
|
|
123
145
|
* fails does before any entry is used. Fails with a `NamedError` of `UNLOADABLE_MODULE` or
|
|
124
146
|
* `MISSING_EXPORT` of a module, and of `INVALID_NAMED_VALUE` of a set that is no map, or a value
|
|
125
|
-
* `check` refuses.
|
|
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.
|
|
126
151
|
*/
|
|
127
152
|
export declare function resolveNamedSet<T>(sets: NamedSets<T> | undefined, options?: NamedSetOptions<T>): Promise<NamedEntries<T>>;
|
|
128
153
|
/**
|
|
@@ -132,9 +157,11 @@ export declare function resolveNamedSet<T>(sets: NamedSets<T> | undefined, optio
|
|
|
132
157
|
* `load`. With a `check`, the value is checked by it. Fails with a `NamedError`: `UNKNOWN_NAME` of
|
|
133
158
|
* a name of no entry, whose message lists the names there are; `UNLOADABLE_MODULE` and
|
|
134
159
|
* `MISSING_EXPORT` of a module; and `INVALID_NAMED_VALUE` of a reference of none of the forms, or
|
|
135
|
-
* a value `check` refuses.
|
|
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.
|
|
136
163
|
*/
|
|
137
|
-
export declare function resolveNamed<T>(reference: NamedReference, { key, names, load, check, kind }?: NamedResolveOptions<T>): Promise<T>;
|
|
164
|
+
export declare function resolveNamed<T>(reference: NamedReference, { key, names, load, check, kind, at }?: NamedResolveOptions<T>): Promise<T>;
|
|
138
165
|
/** What the schema of a reference says of it. */
|
|
139
166
|
export type NamedSchemaOptions = {
|
|
140
167
|
/** The names an editor completes: those of the entries there are. */
|