@wtf/project-config 1.1.1 → 3.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/dist/load.d.ts CHANGED
@@ -1,45 +1,57 @@
1
1
  /**
2
2
  * A project's configuration, as a tool reads it: {@link loadConfig}, the object of the first file
3
3
  * of it found at the root of the project, of code or of JSON, the file it came from, and the
4
- * others found, which are ignored; {@link loadConfigSync}, the same of a JSON file or package.json
5
- * alone, for a tool that reads its config synchronously; {@link ignoredWarning}, what to say of
6
- * the files ignored; and {@link importFileModule}, which imports a configuration of code, as
7
- * `wtf.config.ts`, by its file URL, as Node imports a module.
4
+ * others found, which are ignored; {@link ignoredWarning}, what to say of the files ignored; and
5
+ * {@link importFileModule}, which imports a configuration of code, as `wtf.config.ts`, by its file
6
+ * URL, as Node imports a module.
7
+ *
8
+ * Each reads by the platform it is given, and nothing of the process: its files, its paths, the
9
+ * folder it works in, which the project is looked for from, and its environment, whose variables
10
+ * are over the configuration. The entry point builds it, as `nodePlatform()` of
11
+ * `@wtf/platform-node/platform` makes Node's:
8
12
  *
9
13
  * ```ts
14
+ * import { nodeStorage } from '@wtf/platform-fs-node/storage'
15
+ * import { nodePlatform } from '@wtf/platform-node/platform'
16
+ *
10
17
  * import { loadConfig } from '@wtf/project-config/load'
11
- * import { nodeConfigPlatform } from '@wtf/project-config/node'
12
18
  *
13
- * const resolved = await loadConfig(process.cwd(), {
14
- * platform: nodeConfigPlatform(),
15
- * environment: process.env
16
- * })
19
+ * const platform = nodePlatform({ fetch, storage: nodeStorage() })
20
+ * const resolved = await loadConfig({ platform })
17
21
  * ```
18
22
  *
19
23
  * @packageDocumentation
20
24
  */
21
- import { type Environment } from './env.ts';
25
+ import type { Platform } from '@wtf/platform';
22
26
  import type { ConfigDefaults, ConfigPlatform, ImportModule, ResolvedConfig } from './index.ts';
23
- import { type LocateNames, type LocateOptions, type LocateSyncOptions } from './locate.ts';
24
- /** How a configuration is read, apart from the platform: its names, the defaults of the tool's
25
- * package, and the environment over it. */
27
+ import { type LocateNames } from './locate.ts';
28
+ /** How a configuration is read, apart from the platform: the folder of the project, its names, and
29
+ * the defaults of the tool's package. */
26
30
  export type LoadLayers = LocateNames & {
31
+ /**
32
+ * The folder of the project, which its root is looked for from: the working folder of the
33
+ * platform, `dirs.cwd`, by default.
34
+ */
35
+ readonly folder?: string;
27
36
  /**
28
37
  * The configuration under the project's, as the tool's package gives it: a section the project
29
38
  * gives none of is the one of the defaults, and one it gives is merged over it (`sectionOf` of
30
39
  * ./section).
31
40
  */
32
41
  readonly defaults?: ConfigDefaults;
33
- /**
34
- * The variables of an environment, as a process's: those of the prefix of the names, as `WTF_`,
35
- * are kept, and a section is read with them over it, and its placeholders `${env:NAME}` taken
36
- * from the whole (`sectionOf` of ./section). None by default: nothing here reads the process's
37
- * own, which `loadConfig` of ./node gives.
38
- */
39
- readonly environment?: Environment;
40
42
  };
41
- /** How a configuration is read asynchronously. */
42
- export type LoadOptions = LocateOptions & LoadLayers & {
43
+ /**
44
+ * What a configuration is read by: the platform's files, its paths and its working
45
+ * folder ({@link ConfigPlatform}), and its environment, `env`, of which the variables of the prefix
46
+ * of the names, as `WTF_`, are kept, and a section is read with them over it, and its placeholders
47
+ * `${env:NAME}` taken from the whole (`sectionOf` of ./section). A `Platform` of `@wtf/platform` is
48
+ * one.
49
+ */
50
+ export type LoadPlatform = ConfigPlatform & Pick<Platform, 'env'>;
51
+ /** How a configuration is read. */
52
+ export type LoadOptions = LoadLayers & {
53
+ /** The platform it is read by ({@link LoadPlatform}). */
54
+ readonly platform: LoadPlatform;
43
55
  /**
44
56
  * What imports a file of code: by default `import()` of its file URL, as the platform's paths
45
57
  * give it ({@link importFileModule}). A caller of a file system of its own, not the one its
@@ -47,8 +59,6 @@ export type LoadOptions = LocateOptions & LoadLayers & {
47
59
  */
48
60
  readonly importModule?: ImportModule;
49
61
  };
50
- /** How a configuration is read synchronously. */
51
- export type LoadSyncOptions = LocateSyncOptions & LoadLayers;
52
62
  /**
53
63
  * What imports the module of the file at an absolute path, by its file URL, as the paths `path`
54
64
  * give it: what imports a configuration of code by default, `wtf.config.ts` among them, whose types
@@ -57,24 +67,17 @@ export type LoadSyncOptions = LocateSyncOptions & LoadLayers;
57
67
  */
58
68
  export declare const importFileModule: (path: Pick<ConfigPlatform["path"], "toFileUrl">) => ImportModule;
59
69
  /**
60
- * The configuration of the project in `folder`: the object of the first file of it at the root of
61
- * the project, in the order of the files of its names — by default `wtf.config.mjs`,
62
- * `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`, then the `wtf` key of package.json — and
63
- * the file it came from; `{}`, of no file, when none is there. Each other file found is in
64
- * `ignored`. The `defaults` a tool gives are kept beside it, for `sectionOf` of ./section to merge
65
- * the project's sections over, and so are the variables of the `environment` it gives of the
66
- * prefix of the names, `WTF_` by default, with the whole environment, which placeholders are taken
67
- * from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that
70
+ * The configuration of the project in `folder`, the working folder of the platform by default: the
71
+ * object of the first file of it at the root of the project, in the order of the files of its
72
+ * names — by default `wtf.config.mjs`, `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`,
73
+ * then the `wtf` key of package.json — and the file it came from; `{}`, of no file, when none is
74
+ * there. Each other file found is in `ignored`. The `defaults` a tool gives are kept beside it, for
75
+ * `sectionOf` of ./section to merge the project's sections over, and so are the variables of the
76
+ * environment of the platform, its `env`, of the prefix of the names, `WTF_` by default, with the
77
+ * whole environment, which placeholders are taken from. No placeholder is expanded here. A file of code is imported by `importModule`. A file that
68
78
  * holds no object, or a module that fails to load, is an {@link InvalidConfigFileError}.
69
79
  */
70
- export declare function loadConfig(folder: string, { importModule, platform, ...options }: LoadOptions): Promise<ResolvedConfig>;
71
- /**
72
- * The configuration of the project in `folder`, as {@link loadConfig} reads it, read synchronously:
73
- * of a JSON file of the names, `wtf.config.json` by default, or the key of package.json, `wtf` by
74
- * default, for a tool that loads its own config synchronously. A configuration of code in use, as
75
- * `wtf.config.ts`, is a {@link SyncConfigInCodeError}, as importing it is asynchronous.
76
- */
77
- export declare function loadConfigSync(folder: string, { platform, ...options }: LoadSyncOptions): ResolvedConfig;
80
+ export declare function loadConfig({ importModule, platform, ...options }: LoadOptions): Promise<ResolvedConfig>;
78
81
  /**
79
82
  * What to say of the files of the configuration that are ignored, as a tool warns: `undefined`
80
83
  * when none is.
package/dist/load.js CHANGED
@@ -6,30 +6,10 @@ 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
- import { InvalidConfigFileError, SyncConfigInCodeError } from "./errors.js";
31
- import { filesOf, isObject, kindOf, manifestOf, readAsync, readSync, textOf } from "./files.js";
32
- import { configNamesOf, jsonFilesOf, locateConfig, locateConfigSync } from "./locate.js";
10
+ import { InvalidConfigFileError } from "./errors.js";
11
+ import { filesOf, isObject, kindOf, manifestOf, readAsync, textOf } from "./files.js";
12
+ import { configNamesOf, locateConfig } from "./locate.js";
33
13
  /**
34
14
  * What imports the module of the file at an absolute path, by its file URL, as the paths `path`
35
15
  * give it: what imports a configuration of code by default, `wtf.config.ts` among them, whose types
@@ -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, environment, ...names }) {
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
- ...(environment === undefined
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, environment: _e, ...names }) => names;
64
+ const locateNamesOf = ({ defaults: _d, folder: _f, ...names }) => names;
89
65
  /**
90
- * The configuration of the project in `folder`: the object of the first file of it at the root of
91
- * the project, in the order of the files of its names — by default `wtf.config.mjs`,
92
- * `wtf.config.js`, `wtf.config.ts` and `wtf.config.json`, then the `wtf` key of package.json — and
93
- * the file it came from; `{}`, of no file, when none is there. Each other file found is in
94
- * `ignored`. The `defaults` a tool gives are kept beside it, for `sectionOf` of ./section to merge
95
- * the project's sections over, and so are the variables of the `environment` it gives of the
96
- * prefix of the names, `WTF_` by default, with the whole environment, which placeholders are taken
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(folder, { importModule, platform, ...options }) {
76
+ export async function loadConfig({ importModule, platform, ...options }) {
101
77
  const { namespace } = configNamesOf(options);
102
- const { root, key, sources } = await locateConfig(folder, { platform, ...locateNamesOf(options) });
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'
@@ -109,24 +88,6 @@ export async function loadConfig(folder, { importModule, platform, ...options })
109
88
  : await readAsync(configOfJson(source, key), filesOf(platform));
110
89
  return { namespace, root, key, config, source, ignored, ...layers };
111
90
  }
112
- /**
113
- * The configuration of the project in `folder`, as {@link loadConfig} reads it, read synchronously:
114
- * of a JSON file of the names, `wtf.config.json` by default, or the key of package.json, `wtf` by
115
- * default, for a tool that loads its own config synchronously. A configuration of code in use, as
116
- * `wtf.config.ts`, is a {@link SyncConfigInCodeError}, as importing it is asynchronous.
117
- */
118
- export function loadConfigSync(folder, { platform, ...options }) {
119
- const { namespace } = configNamesOf(options);
120
- const { root, key, sources } = locateConfigSync(folder, { platform, ...locateNamesOf(options) });
121
- const [source, ...ignored] = sources;
122
- const layers = layersOf(options);
123
- if (source === undefined)
124
- return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers };
125
- if (kindOf(source.file) === 'code')
126
- throw new SyncConfigInCodeError(source.path, { files: jsonFilesOf(options), key });
127
- const config = readSync(configOfJson(source, key), platform.files);
128
- return { namespace, root, key, config, source, ignored, ...layers };
129
- }
130
91
  /**
131
92
  * What to say of the files of the configuration that are ignored, as a tool warns: `undefined`
132
93
  * when none is.
package/dist/load.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"load.js","sourceRoot":"","sources":["../src/load.ts"],"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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":";;;;;;;;AA0BA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAoB,MAAM,UAAU,CAAA;AACrE,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAc,MAAM,EAAE,MAAM,YAAY,CAAA;AASjG,OAAO,EAAE,aAAa,EAAE,YAAY,EAAoB,MAAM,aAAa,CAAA;AAuC3E;;;;;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,QAAQ,GAAY,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAA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AAE,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,EAAE,GAAG,MAAM,EAAE,CAAA;IACxF,MAAM,MAAM,GACV,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,MAAM;QAC5B,CAAC,CAAC,MAAM,cAAc,CAAC,MAAM,EAAE,YAAY,IAAI,gBAAgB,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC/E,CAAC,CAAC,MAAM,SAAS,CAAC,YAAY,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;IACnE,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 ignoredWarning}, what to say of the files ignored; and\n * {@link importFileModule}, which imports a configuration of code, as `wtf.config.ts`, by its file\n * 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'\n\nimport { envPrefixOf, variablesOf, type Environment } from './env.ts'\nimport { InvalidConfigFileError } from './errors.ts'\nimport { filesOf, isObject, kindOf, manifestOf, readAsync, type Reads, textOf } from './files.ts'\nimport type {\n ConfigDefaults,\n ConfigPlatform,\n ConfigSource,\n ImportModule,\n NamespaceConfig,\n ResolvedConfig\n} from './index.ts'\nimport { configNamesOf, locateConfig, type LocateNames } 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: 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/** How a configuration is read. */\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/**\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 * 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
@@ -1,4 +1,4 @@
1
- import type { ConfigNames, ConfigPlatform, ConfigSyncPlatform, LocatedConfig } from './index.ts';
1
+ import type { ConfigNames, ConfigPlatform, LocatedConfig } from './index.ts';
2
2
  /** The namespace a configuration is of when none is given: `wtf`, as `wtf.config.json`. */
3
3
  export declare const DEFAULT_NAMESPACE = "wtf";
4
4
  /**
@@ -22,8 +22,8 @@ export type NamespaceNames = ConfigNames & {
22
22
  export declare function configNamesOf({ namespace, files, key, env }?: NamespaceNames): Required<ConfigNames> & {
23
23
  readonly namespace: string;
24
24
  };
25
- /** The JSON files of the names of a namespace, which a write makes or changes, and a synchronous
26
- * load reads: those of its files that are no code, in their order. */
25
+ /** The JSON files of the names of a namespace, which a write makes or changes: those of its files
26
+ * that are no code, in their order. */
27
27
  export declare const jsonFilesOf: (names?: NamespaceNames) => readonly string[];
28
28
  /** Where the configuration is looked for, apart from the platform: the namespace and its names,
29
29
  * each its default when not given, and whether the root is looked for from the folder up. */
@@ -34,19 +34,15 @@ export type LocateNames = NamespaceNames & {
34
34
  */
35
35
  readonly up?: boolean;
36
36
  };
37
- /** Where the configuration is looked for, asynchronously, by the platform's files. */
37
+ /** Where the configuration is looked for, by the platform's files. */
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 `nodeConfigPlatform()` of ./node gives it, and its paths.
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
- /** Where the configuration is looked for, synchronously, by the platform's files. */
46
- export type LocateSyncOptions = LocateNames & {
47
- /** The platform it is read by synchronously, as `nodeSyncPlatform()` of ./node gives it. */
48
- readonly platform: ConfigSyncPlatform;
49
- };
50
46
  /**
51
47
  * The folder the configuration of the project in `folder` is looked for in: the root of the
52
48
  * workspace it is in, the nearest from `folder` up with a pnpm-workspace.yaml or a package.json of
@@ -54,9 +50,6 @@ export type LocateSyncOptions = LocateNames & {
54
50
  * else `folder`. One configuration then serves each package of a workspace.
55
51
  */
56
52
  export declare const projectRootOf: (folder: string, { platform }: Pick<LocateOptions, "platform">) => Promise<string>;
57
- /** The folder the configuration of the project in `folder` is looked for in, as
58
- * {@link projectRootOf} finds it, read synchronously. */
59
- export declare const projectRootOfSync: (folder: string, { platform }: Pick<LocateSyncOptions, "platform">) => string;
60
53
  /**
61
54
  * Each file of the configuration of the project in `folder`, at its root, in the order of the
62
55
  * files of its names (`wtf.config.mjs`, `.js`, `.ts` and `.json` by default), and package.json
@@ -66,6 +59,3 @@ export declare const projectRootOfSync: (folder: string, { platform }: Pick<Loca
66
59
  * a `TypeError`, as {@link configNamesOf} says.
67
60
  */
68
61
  export declare const locateConfig: (folder: string, { platform, ...names }: LocateOptions) => Promise<LocatedConfig>;
69
- /** Each file of the configuration of the project in `folder`, as {@link locateConfig} finds them,
70
- * read synchronously. */
71
- export declare const locateConfigSync: (folder: string, { platform, ...names }: LocateSyncOptions) => LocatedConfig;
package/dist/locate.js CHANGED
@@ -5,20 +5,22 @@
5
5
  * {@link configNamesOf}, those names: the files ({@link configFilesOf} of the namespace by
6
6
  * default), the key of package.json (the namespace) and the prefix of the environment's variables
7
7
  * (the key). The namespace is `wtf` by default ({@link DEFAULT_NAMESPACE}). Each reads by the
8
- * platform it is given, asynchronously; {@link projectRootOfSync} and {@link locateConfigSync} by
9
- * the files of a synchronous one.
8
+ * platform it is given.
10
9
  *
11
10
  * ```ts
11
+ * import { nodeStorage } from '@wtf/platform-fs-node/storage'
12
+ * import { nodePlatform } from '@wtf/platform-node/platform'
13
+ *
12
14
  * import { locateConfig } from '@wtf/project-config/locate'
13
- * import { nodeConfigPlatform } from '@wtf/project-config/node'
14
15
  *
15
- * const { root, sources } = await locateConfig(process.cwd(), { platform: nodeConfigPlatform() })
16
+ * const platform = nodePlatform({ fetch, storage: nodeStorage() })
17
+ * const { root, sources } = await locateConfig(platform.dirs.cwd, { platform })
16
18
  * ```
17
19
  *
18
20
  * @packageDocumentation
19
21
  */
20
22
  import { envNameOf } from "./env.js";
21
- import { filesOf, isObject, kindOf, manifestAt, namesOf, PACKAGE_JSON, readAsync, readSync, textOf } from "./files.js";
23
+ import { filesOf, isObject, kindOf, manifestAt, namesOf, PACKAGE_JSON, readAsync, textOf } from "./files.js";
22
24
  /** The namespace a configuration is of when none is given: `wtf`, as `wtf.config.json`. */
23
25
  export const DEFAULT_NAMESPACE = 'wtf';
24
26
  /**
@@ -55,8 +57,8 @@ export function configNamesOf({ namespace = DEFAULT_NAMESPACE, files, key, env }
55
57
  throw new TypeError(`The name of the variables of the environment is empty: name one, as ${names.key}.`);
56
58
  return names;
57
59
  }
58
- /** The JSON files of the names of a namespace, which a write makes or changes, and a synchronous
59
- * load reads: those of its files that are no code, in their order. */
60
+ /** The JSON files of the names of a namespace, which a write makes or changes: those of its files
61
+ * that are no code, in their order. */
60
62
  export const jsonFilesOf = (names = {}) => configNamesOf(names).files.filter((file) => kindOf(file) === 'json');
61
63
  /** Whether the folder is the root of a workspace: a pnpm-workspace.yaml, or npm's `workspaces`. */
62
64
  function* isWorkspaceRoot(folder, { join }) {
@@ -103,9 +105,6 @@ function* locate(folder, { up = true, ...names }, path) {
103
105
  * else `folder`. One configuration then serves each package of a workspace.
104
106
  */
105
107
  export const projectRootOf = (folder, { platform }) => readAsync(rootOf(folder, platform.path), filesOf(platform));
106
- /** The folder the configuration of the project in `folder` is looked for in, as
107
- * {@link projectRootOf} finds it, read synchronously. */
108
- export const projectRootOfSync = (folder, { platform }) => readSync(rootOf(folder, platform.path), platform.files);
109
108
  /**
110
109
  * Each file of the configuration of the project in `folder`, at its root, in the order of the
111
110
  * files of its names (`wtf.config.mjs`, `.js`, `.ts` and `.json` by default), and package.json
@@ -115,7 +114,4 @@ export const projectRootOfSync = (folder, { platform }) => readSync(rootOf(folde
115
114
  * a `TypeError`, as {@link configNamesOf} says.
116
115
  */
117
116
  export const locateConfig = (folder, { platform, ...names }) => readAsync(locate(folder, names, platform.path), filesOf(platform));
118
- /** Each file of the configuration of the project in `folder`, as {@link locateConfig} finds them,
119
- * read synchronously. */
120
- export const locateConfigSync = (folder, { platform, ...names }) => readSync(locate(folder, names, platform.path), platform.files);
121
117
  //# sourceMappingURL=locate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"locate.js","sourceRoot":"","sources":["../src/locate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;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,KAA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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;;;;;;;;;;;;;;;;;;;;GAoBG;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,MAAM,EACP,MAAM,YAAY,CAAA;AAGnB,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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AAK,GAAG,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAClC,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC,CAAA;IACjE,MAAM,OAAO,GAAmB;QAC9B,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC1C,GAAG,CAAC,QAAQ,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;KAClF,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,CAAA;IACxD,OAAO,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;;;;;;;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","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.\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 textOf\n} from './files.ts'\nimport type { ConfigNames, ConfigPlatform, ConfigSource, LocatedConfig } 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: those of its files\n * 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, 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/** 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/**\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"]}
package/dist/named.d.ts CHANGED
@@ -18,8 +18,8 @@
18
18
  * Each entry is a factory of the value, `(options) => value`: a function, which takes the options
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
- * 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,
21
+ * over an earlier one by name. A module is loaded by what the caller gives, Node's
22
+ * `nodeLoadModule` of `@wtf/platform-node/module`, so this module runs on any platform. What fails is a `NamedError` of ./errors,
23
23
  * which names where: the path, from where the caller says the sets or the reference are, the index
24
24
  * of the set and the name of the entry, and the module and the export.
25
25
  *
@@ -84,6 +84,8 @@ export type NamedSets<T> = NamedSet<T> | readonly NamedSet<T>[];
84
84
  /**
85
85
  * What loads a module of a {@link ModuleReference}: the module of the text it names, as an object
86
86
  * of its exports, or a failure, which a `NamedError` of `UNLOADABLE_MODULE` holds as its cause.
87
+ * Node's is `nodeLoadModule(root, beside?)` of `@wtf/platform-node/module`, whose `LoadModule` of
88
+ * `@wtf/platform/module` is this one.
87
89
  */
88
90
  export type LoadModule = (module: string) => Promise<unknown>;
89
91
  /**