@wtf/project-config 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +78 -2
- package/dist/define.d.ts +26 -0
- package/dist/define.js +7 -0
- package/dist/define.js.map +1 -0
- package/dist/env.d.ts +40 -0
- package/dist/env.js +160 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.d.ts +151 -0
- package/dist/errors.js +175 -0
- package/dist/errors.js.map +1 -0
- package/dist/files.d.ts +51 -0
- package/dist/files.js +120 -0
- package/dist/files.js.map +1 -0
- package/dist/index.d.ts +196 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/load.d.ts +82 -0
- package/dist/load.js +140 -0
- package/dist/load.js.map +1 -0
- package/dist/locate.d.ts +71 -0
- package/dist/locate.js +121 -0
- package/dist/locate.js.map +1 -0
- package/dist/merge.d.ts +10 -0
- package/dist/merge.js +33 -0
- package/dist/merge.js.map +1 -0
- package/dist/node.d.ts +20 -0
- package/dist/node.js +90 -0
- package/dist/node.js.map +1 -0
- package/dist/path.d.ts +14 -0
- package/dist/path.js +40 -0
- package/dist/path.js.map +1 -0
- package/dist/placeholder.d.ts +37 -0
- package/dist/placeholder.js +47 -0
- package/dist/placeholder.js.map +1 -0
- package/dist/schema.d.ts +24 -0
- package/dist/schema.js +58 -0
- package/dist/schema.js.map +1 -0
- package/dist/section.d.ts +39 -0
- package/dist/section.js +169 -0
- package/dist/section.js.map +1 -0
- package/dist/text.d.ts +3 -0
- package/dist/text.js +3 -0
- package/dist/text.js.map +1 -0
- package/dist/write.d.ts +35 -0
- package/dist/write.js +209 -0
- package/dist/write.js.map +1 -0
- package/package.json +89 -4
package/dist/load.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A project's configuration, as a tool reads it: {@link loadConfig}, the object of the first file
|
|
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.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { loadConfig } from '@wtf/project-config/load'
|
|
11
|
+
* import { nodeConfigPlatform } from '@wtf/project-config/node'
|
|
12
|
+
*
|
|
13
|
+
* const resolved = await loadConfig(process.cwd(), {
|
|
14
|
+
* platform: nodeConfigPlatform(),
|
|
15
|
+
* environment: process.env
|
|
16
|
+
* })
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @packageDocumentation
|
|
20
|
+
*/
|
|
21
|
+
import { type Environment } from './env.ts';
|
|
22
|
+
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. */
|
|
26
|
+
export type LoadLayers = LocateNames & {
|
|
27
|
+
/**
|
|
28
|
+
* The configuration under the project's, as the tool's package gives it: a section the project
|
|
29
|
+
* gives none of is the one of the defaults, and one it gives is merged over it (`sectionOf` of
|
|
30
|
+
* ./section).
|
|
31
|
+
*/
|
|
32
|
+
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
|
+
};
|
|
41
|
+
/** How a configuration is read asynchronously. */
|
|
42
|
+
export type LoadOptions = LocateOptions & LoadLayers & {
|
|
43
|
+
/**
|
|
44
|
+
* What imports a file of code: by default `import()` of its file URL, as the platform's paths
|
|
45
|
+
* give it ({@link importFileModule}). A caller of a file system of its own, not the one its
|
|
46
|
+
* runtime imports from, gives one too.
|
|
47
|
+
*/
|
|
48
|
+
readonly importModule?: ImportModule;
|
|
49
|
+
};
|
|
50
|
+
/** How a configuration is read synchronously. */
|
|
51
|
+
export type LoadSyncOptions = LocateSyncOptions & LoadLayers;
|
|
52
|
+
/**
|
|
53
|
+
* What imports the module of the file at an absolute path, by its file URL, as the paths `path`
|
|
54
|
+
* give it: what imports a configuration of code by default, `wtf.config.ts` among them, whose types
|
|
55
|
+
* Node strips itself. The runtime imports it, of the file system it imports from, and not the one
|
|
56
|
+
* a caller gives.
|
|
57
|
+
*/
|
|
58
|
+
export declare const importFileModule: (path: Pick<ConfigPlatform["path"], "toFileUrl">) => ImportModule;
|
|
59
|
+
/**
|
|
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
|
|
68
|
+
* holds no object, or a module that fails to load, is an {@link InvalidConfigFileError}.
|
|
69
|
+
*/
|
|
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;
|
|
78
|
+
/**
|
|
79
|
+
* What to say of the files of the configuration that are ignored, as a tool warns: `undefined`
|
|
80
|
+
* when none is.
|
|
81
|
+
*/
|
|
82
|
+
export declare function ignoredWarning({ namespace, key, source, ignored }: Pick<ResolvedConfig, 'namespace' | 'key' | 'source' | 'ignored'>): string | undefined;
|
package/dist/load.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
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
|
+
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";
|
|
33
|
+
/**
|
|
34
|
+
* What imports the module of the file at an absolute path, by its file URL, as the paths `path`
|
|
35
|
+
* give it: what imports a configuration of code by default, `wtf.config.ts` among them, whose types
|
|
36
|
+
* Node strips itself. The runtime imports it, of the file system it imports from, and not the one
|
|
37
|
+
* a caller gives.
|
|
38
|
+
*/
|
|
39
|
+
export const importFileModule = (path) => async (file) => import(__rewriteRelativeImportExtension(path.toFileUrl(file).href));
|
|
40
|
+
/** The configuration of a module: its default export, an object, or a function that returns one. */
|
|
41
|
+
async function configOfModule(source, importModule) {
|
|
42
|
+
let module;
|
|
43
|
+
try {
|
|
44
|
+
module = await importModule(source.path);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
throw new InvalidConfigFileError(source.path, `failed to load: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
48
|
+
}
|
|
49
|
+
const exported = isObject(module) ? module['default'] : undefined;
|
|
50
|
+
const config = typeof exported === 'function' ? await exported() : exported;
|
|
51
|
+
if (!isObject(config))
|
|
52
|
+
throw new InvalidConfigFileError(source.path, 'exports no object by default: give `export default { … }`, or a function that returns one.');
|
|
53
|
+
return config;
|
|
54
|
+
}
|
|
55
|
+
/** The configuration of a JSON file: its own object, or the `key` of package.json. */
|
|
56
|
+
function* configOfJson(source, key) {
|
|
57
|
+
const text = (yield* textOf(source.path)) ?? '';
|
|
58
|
+
if (kindOf(source.file) === 'package') {
|
|
59
|
+
const value = manifestOf(source.path, text)[key];
|
|
60
|
+
if (!isObject(value))
|
|
61
|
+
throw new InvalidConfigFileError(source.path, `its "${key}" key is no JSON object.`);
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
let value;
|
|
65
|
+
try {
|
|
66
|
+
value = JSON.parse(text);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
throw new InvalidConfigFileError(source.path, 'is no JSON.', { cause: error });
|
|
70
|
+
}
|
|
71
|
+
if (!isObject(value))
|
|
72
|
+
throw new InvalidConfigFileError(source.path, 'is no JSON object.');
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
/** What a resolved configuration keeps beside the object: the defaults and the environment. */
|
|
76
|
+
function layersOf({ defaults, environment, ...names }) {
|
|
77
|
+
const prefix = envPrefixOf(configNamesOf(names).env);
|
|
78
|
+
return {
|
|
79
|
+
...(defaults === undefined ? {} : { defaults }),
|
|
80
|
+
...(environment === undefined
|
|
81
|
+
? {}
|
|
82
|
+
: {
|
|
83
|
+
environment: { prefix, variables: variablesOf(environment, prefix), values: environment }
|
|
84
|
+
})
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/** The options of the names alone, without what reads them. */
|
|
88
|
+
const locateNamesOf = ({ defaults: _d, environment: _e, ...names }) => names;
|
|
89
|
+
/**
|
|
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
|
|
98
|
+
* holds no object, or a module that fails to load, is an {@link InvalidConfigFileError}.
|
|
99
|
+
*/
|
|
100
|
+
export async function loadConfig(folder, { importModule, platform, ...options }) {
|
|
101
|
+
const { namespace } = configNamesOf(options);
|
|
102
|
+
const { root, key, sources } = await locateConfig(folder, { platform, ...locateNamesOf(options) });
|
|
103
|
+
const [source, ...ignored] = sources;
|
|
104
|
+
const layers = layersOf(options);
|
|
105
|
+
if (source === undefined)
|
|
106
|
+
return { namespace, root, key, config: {}, source: undefined, ignored: [], ...layers };
|
|
107
|
+
const config = kindOf(source.file) === 'code'
|
|
108
|
+
? await configOfModule(source, importModule ?? importFileModule(platform.path))
|
|
109
|
+
: await readAsync(configOfJson(source, key), filesOf(platform));
|
|
110
|
+
return { namespace, root, key, config, source, ignored, ...layers };
|
|
111
|
+
}
|
|
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
|
+
/**
|
|
131
|
+
* What to say of the files of the configuration that are ignored, as a tool warns: `undefined`
|
|
132
|
+
* when none is.
|
|
133
|
+
*/
|
|
134
|
+
export function ignoredWarning({ namespace, key, source, ignored }) {
|
|
135
|
+
if (source === undefined || ignored.length === 0)
|
|
136
|
+
return undefined;
|
|
137
|
+
const names = ignored.map(({ file }) => kindOf(file) === 'package' ? `the "${key}" key of ${file}` : file);
|
|
138
|
+
return `The ${namespace} configuration is read from ${source.file}; ${names.join(' and ')} ${names.length === 1 ? 'is' : 'are'} ignored. Keep it in one file.`;
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=load.js.map
|
package/dist/load.js.map
ADDED
|
@@ -0,0 +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,oG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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"]}
|
package/dist/locate.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { ConfigNames, ConfigPlatform, ConfigSyncPlatform, LocatedConfig } from './index.ts';
|
|
2
|
+
/** The namespace a configuration is of when none is given: `wtf`, as `wtf.config.json`. */
|
|
3
|
+
export declare const DEFAULT_NAMESPACE = "wtf";
|
|
4
|
+
/**
|
|
5
|
+
* The files the configuration of `namespace` can be in by default, in the order the first found is
|
|
6
|
+
* used: code first, as a person who writes code means it to be read, then JSON. After them comes
|
|
7
|
+
* the `namespace` key of package.json, where a write makes it when there is none.
|
|
8
|
+
*/
|
|
9
|
+
export declare const configFilesOf: (namespace?: string) => readonly string[];
|
|
10
|
+
/** The names of a namespace's configuration: the namespace, `wtf` by default, and the names a
|
|
11
|
+
* tool gives. */
|
|
12
|
+
export type NamespaceNames = ConfigNames & {
|
|
13
|
+
/** The namespace of the tools: the name of its files and its key by default. `wtf` by default. */
|
|
14
|
+
readonly namespace?: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* The names of the configuration of the namespace, each as `names` gives it, else its default: the
|
|
18
|
+
* files, {@link configFilesOf} the namespace; the key of package.json, the namespace; and the
|
|
19
|
+
* prefix of the environment's variables, the key. A file that is neither code nor JSON,
|
|
20
|
+
* package.json among the files, an empty key, or a prefix of no letter or digit, is a `TypeError`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function configNamesOf({ namespace, files, key, env }?: NamespaceNames): Required<ConfigNames> & {
|
|
23
|
+
readonly namespace: string;
|
|
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. */
|
|
27
|
+
export declare const jsonFilesOf: (names?: NamespaceNames) => readonly string[];
|
|
28
|
+
/** Where the configuration is looked for, apart from the platform: the namespace and its names,
|
|
29
|
+
* each its default when not given, and whether the root is looked for from the folder up. */
|
|
30
|
+
export type LocateNames = NamespaceNames & {
|
|
31
|
+
/**
|
|
32
|
+
* Whether the root is looked for from the folder up, as {@link projectRootOf} finds it: by
|
|
33
|
+
* default. With `false`, the folder given is the root.
|
|
34
|
+
*/
|
|
35
|
+
readonly up?: boolean;
|
|
36
|
+
};
|
|
37
|
+
/** Where the configuration is looked for, asynchronously, by the platform's files. */
|
|
38
|
+
export type LocateOptions = LocateNames & {
|
|
39
|
+
/**
|
|
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.
|
|
42
|
+
*/
|
|
43
|
+
readonly platform: ConfigPlatform;
|
|
44
|
+
};
|
|
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
|
+
/**
|
|
51
|
+
* The folder the configuration of the project in `folder` is looked for in: the root of the
|
|
52
|
+
* workspace it is in, the nearest from `folder` up with a pnpm-workspace.yaml or a package.json of
|
|
53
|
+
* `workspaces`; else the project's own folder, the nearest from `folder` up with a package.json;
|
|
54
|
+
* else `folder`. One configuration then serves each package of a workspace.
|
|
55
|
+
*/
|
|
56
|
+
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
|
+
/**
|
|
61
|
+
* Each file of the configuration of the project in `folder`, at its root, in the order of the
|
|
62
|
+
* files of its names (`wtf.config.mjs`, `.js`, `.ts` and `.json` by default), and package.json
|
|
63
|
+
* when it has the key (`wtf` by default). The first is the one in use. The root is
|
|
64
|
+
* {@link projectRootOf} `folder`, or `folder` itself with `up: false`. A package.json that is no
|
|
65
|
+
* JSON object is an `InvalidConfigFileError` of ./errors; names that are not of the configuration,
|
|
66
|
+
* a `TypeError`, as {@link configNamesOf} says.
|
|
67
|
+
*/
|
|
68
|
+
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
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a project's configuration is: {@link projectRootOf}, the folder it is looked for in, the
|
|
3
|
+
* root of the workspace the project is in, or the project's own folder; {@link locateConfig}, each
|
|
4
|
+
* file of it there, in the order of the files of its names, of which the first is used; and
|
|
5
|
+
* {@link configNamesOf}, those names: the files ({@link configFilesOf} of the namespace by
|
|
6
|
+
* default), the key of package.json (the namespace) and the prefix of the environment's variables
|
|
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.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { locateConfig } from '@wtf/project-config/locate'
|
|
13
|
+
* import { nodeConfigPlatform } from '@wtf/project-config/node'
|
|
14
|
+
*
|
|
15
|
+
* const { root, sources } = await locateConfig(process.cwd(), { platform: nodeConfigPlatform() })
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @packageDocumentation
|
|
19
|
+
*/
|
|
20
|
+
import { envNameOf } from "./env.js";
|
|
21
|
+
import { filesOf, isObject, kindOf, manifestAt, namesOf, PACKAGE_JSON, readAsync, readSync, textOf } from "./files.js";
|
|
22
|
+
/** The namespace a configuration is of when none is given: `wtf`, as `wtf.config.json`. */
|
|
23
|
+
export const DEFAULT_NAMESPACE = 'wtf';
|
|
24
|
+
/**
|
|
25
|
+
* The files the configuration of `namespace` can be in by default, in the order the first found is
|
|
26
|
+
* used: code first, as a person who writes code means it to be read, then JSON. After them comes
|
|
27
|
+
* the `namespace` key of package.json, where a write makes it when there is none.
|
|
28
|
+
*/
|
|
29
|
+
export const configFilesOf = (namespace = DEFAULT_NAMESPACE) => [
|
|
30
|
+
`${namespace}.config.mjs`,
|
|
31
|
+
`${namespace}.config.js`,
|
|
32
|
+
`${namespace}.config.ts`,
|
|
33
|
+
`${namespace}.config.json`
|
|
34
|
+
];
|
|
35
|
+
/**
|
|
36
|
+
* The names of the configuration of the namespace, each as `names` gives it, else its default: the
|
|
37
|
+
* files, {@link configFilesOf} the namespace; the key of package.json, the namespace; and the
|
|
38
|
+
* prefix of the environment's variables, the key. A file that is neither code nor JSON,
|
|
39
|
+
* package.json among the files, an empty key, or a prefix of no letter or digit, is a `TypeError`.
|
|
40
|
+
*/
|
|
41
|
+
export function configNamesOf({ namespace = DEFAULT_NAMESPACE, files, key, env } = {}) {
|
|
42
|
+
const resolvedKey = key ?? namespace;
|
|
43
|
+
const names = {
|
|
44
|
+
namespace,
|
|
45
|
+
files: files ?? configFilesOf(namespace),
|
|
46
|
+
key: resolvedKey,
|
|
47
|
+
env: env ?? resolvedKey
|
|
48
|
+
};
|
|
49
|
+
for (const file of names.files)
|
|
50
|
+
if (kindOf(file) === 'package')
|
|
51
|
+
throw new TypeError(`${PACKAGE_JSON} is no file of the configuration: it holds it by its key, which \`key\` names.`);
|
|
52
|
+
if (names.key === '')
|
|
53
|
+
throw new TypeError(`The key of package.json is empty: name one, as ${namespace}.`);
|
|
54
|
+
if (envNameOf(names.env) === '')
|
|
55
|
+
throw new TypeError(`The name of the variables of the environment is empty: name one, as ${names.key}.`);
|
|
56
|
+
return names;
|
|
57
|
+
}
|
|
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
|
+
export const jsonFilesOf = (names = {}) => configNamesOf(names).files.filter((file) => kindOf(file) === 'json');
|
|
61
|
+
/** Whether the folder is the root of a workspace: a pnpm-workspace.yaml, or npm's `workspaces`. */
|
|
62
|
+
function* isWorkspaceRoot(folder, { join }) {
|
|
63
|
+
if ((yield* textOf(join(folder, 'pnpm-workspace.yaml'))) !== undefined)
|
|
64
|
+
return true;
|
|
65
|
+
let manifest;
|
|
66
|
+
try {
|
|
67
|
+
manifest = yield* manifestAt(join(folder, PACKAGE_JSON));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return isObject(manifest) && manifest['workspaces'] !== undefined;
|
|
73
|
+
}
|
|
74
|
+
/** The root of the project in `folder`, of the reads it asks for. */
|
|
75
|
+
function* rootOf(folder, path) {
|
|
76
|
+
const { dirname, join, resolve } = path;
|
|
77
|
+
let project;
|
|
78
|
+
for (let at = resolve(folder);; at = dirname(at)) {
|
|
79
|
+
if (yield* isWorkspaceRoot(at, path))
|
|
80
|
+
return at;
|
|
81
|
+
if (project === undefined && (yield* textOf(join(at, PACKAGE_JSON))) !== undefined)
|
|
82
|
+
project = at;
|
|
83
|
+
if (dirname(at) === at)
|
|
84
|
+
return project ?? resolve(folder);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The configuration of the project in `folder`, located, of the reads it asks for. */
|
|
88
|
+
function* locate(folder, { up = true, ...names }, path) {
|
|
89
|
+
const { files, key } = configNamesOf(names);
|
|
90
|
+
const root = up ? yield* rootOf(folder, path) : path.resolve(folder);
|
|
91
|
+
const found = yield* namesOf(root);
|
|
92
|
+
const manifest = yield* manifestAt(path.join(root, PACKAGE_JSON));
|
|
93
|
+
const sources = [
|
|
94
|
+
...files.filter((file) => found.has(file)),
|
|
95
|
+
...(manifest !== undefined && Object.hasOwn(manifest, key) ? [PACKAGE_JSON] : [])
|
|
96
|
+
].map((file) => ({ file, path: path.join(root, file) }));
|
|
97
|
+
return { root, key, sources };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The folder the configuration of the project in `folder` is looked for in: the root of the
|
|
101
|
+
* workspace it is in, the nearest from `folder` up with a pnpm-workspace.yaml or a package.json of
|
|
102
|
+
* `workspaces`; else the project's own folder, the nearest from `folder` up with a package.json;
|
|
103
|
+
* else `folder`. One configuration then serves each package of a workspace.
|
|
104
|
+
*/
|
|
105
|
+
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
|
+
/**
|
|
110
|
+
* Each file of the configuration of the project in `folder`, at its root, in the order of the
|
|
111
|
+
* files of its names (`wtf.config.mjs`, `.js`, `.ts` and `.json` by default), and package.json
|
|
112
|
+
* when it has the key (`wtf` by default). The first is the one in use. The root is
|
|
113
|
+
* {@link projectRootOf} `folder`, or `folder` itself with `up: false`. A package.json that is no
|
|
114
|
+
* JSON object is an `InvalidConfigFileError` of ./errors; names that are not of the configuration,
|
|
115
|
+
* a `TypeError`, as {@link configNamesOf} says.
|
|
116
|
+
*/
|
|
117
|
+
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
|
+
//# sourceMappingURL=locate.js.map
|
|
@@ -0,0 +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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,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"]}
|
package/dist/merge.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `over` merged over `under`, as a project's section is over the defaults of its tool: two objects
|
|
3
|
+
* key by key, each key's value merged so; else `over`, a list, a value or `null` as it is, and
|
|
4
|
+
* `under` where `over` is `undefined`.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* mergeSection({ a: { b: 1, c: [1] } }, { a: { c: [2] } }) // { a: { b: 1, c: [2] } }
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare function mergeSection(under: unknown, over: unknown): unknown;
|
package/dist/merge.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One value merged over another, as a layer of the configuration is over the one under it:
|
|
3
|
+
* {@link mergeSection}, which ./section exports.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
/** Whether `value` is an object of keys, as JSON's: no list, and no instance of a class. */
|
|
8
|
+
const isPlain = (value) => {
|
|
9
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
10
|
+
return false;
|
|
11
|
+
const prototype = Object.getPrototypeOf(value);
|
|
12
|
+
return prototype === Object.prototype || prototype === null;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* `over` merged over `under`, as a project's section is over the defaults of its tool: two objects
|
|
16
|
+
* key by key, each key's value merged so; else `over`, a list, a value or `null` as it is, and
|
|
17
|
+
* `under` where `over` is `undefined`.
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* mergeSection({ a: { b: 1, c: [1] } }, { a: { c: [2] } }) // { a: { b: 1, c: [2] } }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function mergeSection(under, over) {
|
|
24
|
+
if (over === undefined)
|
|
25
|
+
return under;
|
|
26
|
+
if (!isPlain(under) || !isPlain(over))
|
|
27
|
+
return over;
|
|
28
|
+
const merged = { ...under };
|
|
29
|
+
for (const [key, value] of Object.entries(over))
|
|
30
|
+
merged[key] = mergeSection(under[key], value);
|
|
31
|
+
return merged;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=merge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merge.js","sourceRoot":"","sources":["../src/merge.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,4FAA4F;AAC5F,MAAM,OAAO,GAAG,CAAC,KAAc,EAA8C,EAAE;IAC7E,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IACrF,MAAM,SAAS,GAAY,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAA;IACvD,OAAO,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,IAAI,CAAA;AAC7D,CAAC,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,IAAa;IACxD,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,KAAK,CAAA;IACpC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAA;IAClD,MAAM,MAAM,GAA4B,EAAE,GAAG,KAAK,EAAE,CAAA;IACpD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,MAAM,CAAC,GAAG,CAAC,GAAG,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAA;IAC9F,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["/**\n * One value merged over another, as a layer of the configuration is over the one under it:\n * {@link mergeSection}, which ./section exports.\n *\n * @packageDocumentation\n */\n/** Whether `value` is an object of keys, as JSON's: no list, and no instance of a class. */\nconst isPlain = (value: unknown): value is Readonly<Record<string, unknown>> => {\n if (typeof value !== 'object' || value === null || Array.isArray(value)) return false\n const prototype: unknown = Object.getPrototypeOf(value)\n return prototype === Object.prototype || prototype === null\n}\n\n/**\n * `over` merged over `under`, as a project's section is over the defaults of its tool: two objects\n * key by key, each key's value merged so; else `over`, a list, a value or `null` as it is, and\n * `under` where `over` is `undefined`.\n *\n * ```ts\n * mergeSection({ a: { b: 1, c: [1] } }, { a: { c: [2] } }) // { a: { b: 1, c: [2] } }\n * ```\n */\nexport function mergeSection(under: unknown, over: unknown): unknown {\n if (over === undefined) return under\n if (!isPlain(under) || !isPlain(over)) return over\n const merged: Record<string, unknown> = { ...under }\n for (const [key, value] of Object.entries(over)) merged[key] = mergeSection(under[key], value)\n return merged\n}\n"]}
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ConfigPlatform, ConfigSyncPlatform, ResolvedConfig } from './index.ts';
|
|
2
|
+
import { type LoadOptions, type LoadSyncOptions } from './load.ts';
|
|
3
|
+
/** Node's platform the configuration is read and written by: the disk from its root, its paths,
|
|
4
|
+
* and the process's working folder. */
|
|
5
|
+
export declare const nodeConfigPlatform: () => ConfigPlatform;
|
|
6
|
+
/** Node's platform the configuration is read by synchronously: the disk by `node:fs`, and its
|
|
7
|
+
* paths. */
|
|
8
|
+
export declare const nodeSyncPlatform: () => ConfigSyncPlatform;
|
|
9
|
+
/**
|
|
10
|
+
* The configuration of the project in `folder`, the process's working folder by default, as
|
|
11
|
+
* `loadConfig` of ./load reads it: the namespace `wtf` by default, of Node's platform and the
|
|
12
|
+
* process's environment, unless `options` gives others.
|
|
13
|
+
*/
|
|
14
|
+
export declare const loadConfig: (folder?: string, options?: Partial<LoadOptions>) => Promise<ResolvedConfig>;
|
|
15
|
+
/**
|
|
16
|
+
* The configuration of the project in `folder`, the process's working folder by default, as
|
|
17
|
+
* `loadConfigSync` of ./load reads it, of a JSON file or package.json alone: of Node's platform
|
|
18
|
+
* and the process's environment, unless `options` gives others.
|
|
19
|
+
*/
|
|
20
|
+
export declare const loadConfigSync: (folder?: string, options?: Partial<LoadSyncOptions>) => ResolvedConfig;
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node's platform and loads of the configuration: {@link nodeConfigPlatform}, the disk as handles
|
|
3
|
+
* of the File System Standard, by `nodeStorage()` of `@wtf/platform-fs-node/storage`, and the paths
|
|
4
|
+
* of `nodePath()` of `@wtf/platform-node/path`; {@link nodeSyncPlatform}, the disk read
|
|
5
|
+
* synchronously, by `node:fs`; and {@link loadConfig} and {@link loadConfigSync}, those of ./load,
|
|
6
|
+
* of the process's working folder, its environment and Node's platform by default. A repo loads its
|
|
7
|
+
* config by them, and spreads a section into a preset's options:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { loadConfig } from '@wtf/project-config/node'
|
|
11
|
+
* import { sectionOf } from '@wtf/project-config/section'
|
|
12
|
+
* import { rootConfig } from '@wtf/project-vitest/config'
|
|
13
|
+
* import { vitestSchema } from '@wtf/project-vitest/schema'
|
|
14
|
+
*
|
|
15
|
+
* const config = await loadConfig()
|
|
16
|
+
* export default rootConfig({ ...(await sectionOf(config, 'vitest', vitestSchema)) })
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @packageDocumentation
|
|
20
|
+
*/
|
|
21
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
22
|
+
import { join } from 'node:path';
|
|
23
|
+
import { nodeStorage } from '@wtf/platform-fs-node/storage';
|
|
24
|
+
import { nodePath } from '@wtf/platform-node/path';
|
|
25
|
+
import { loadConfig as loadWith, loadConfigSync as loadSyncWith } from "./load.js";
|
|
26
|
+
/** Node's platform the configuration is read and written by: the disk from its root, its paths,
|
|
27
|
+
* and the process's working folder. */
|
|
28
|
+
export const nodeConfigPlatform = () => ({
|
|
29
|
+
navigator: { storage: nodeStorage() },
|
|
30
|
+
path: nodePath(),
|
|
31
|
+
dirs: { cwd: process.cwd() }
|
|
32
|
+
});
|
|
33
|
+
/** Whether `error` is Node's of a file or a folder not there, or of the other kind. */
|
|
34
|
+
const isMissing = (error) => ['ENOENT', 'ENOTDIR', 'EISDIR'].includes(error?.code ?? '');
|
|
35
|
+
/** Node's platform the configuration is read by synchronously: the disk by `node:fs`, and its
|
|
36
|
+
* paths. */
|
|
37
|
+
export const nodeSyncPlatform = () => ({
|
|
38
|
+
path: nodePath(),
|
|
39
|
+
files: {
|
|
40
|
+
text: (path) => {
|
|
41
|
+
try {
|
|
42
|
+
return readFileSync(path, 'utf8');
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (isMissing(error))
|
|
46
|
+
return undefined;
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
files: (folder) => {
|
|
51
|
+
try {
|
|
52
|
+
// A link is what it links to, as a handle of the disk reads it.
|
|
53
|
+
return new Set(readdirSync(folder).filter((name) => {
|
|
54
|
+
try {
|
|
55
|
+
return statSync(join(folder, name)).isFile();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (isMissing(error))
|
|
64
|
+
return new Set();
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* The configuration of the project in `folder`, the process's working folder by default, as
|
|
72
|
+
* `loadConfig` of ./load reads it: the namespace `wtf` by default, of Node's platform and the
|
|
73
|
+
* process's environment, unless `options` gives others.
|
|
74
|
+
*/
|
|
75
|
+
export const loadConfig = (folder = process.cwd(), options = {}) => loadWith(folder, {
|
|
76
|
+
environment: process.env,
|
|
77
|
+
...options,
|
|
78
|
+
platform: options.platform ?? nodeConfigPlatform()
|
|
79
|
+
});
|
|
80
|
+
/**
|
|
81
|
+
* The configuration of the project in `folder`, the process's working folder by default, as
|
|
82
|
+
* `loadConfigSync` of ./load reads it, of a JSON file or package.json alone: of Node's platform
|
|
83
|
+
* and the process's environment, unless `options` gives others.
|
|
84
|
+
*/
|
|
85
|
+
export const loadConfigSync = (folder = process.cwd(), options = {}) => loadSyncWith(folder, {
|
|
86
|
+
environment: process.env,
|
|
87
|
+
...options,
|
|
88
|
+
platform: options.platform ?? nodeSyncPlatform()
|
|
89
|
+
});
|
|
90
|
+
//# sourceMappingURL=node.js.map
|