@ fnet/ 配置
这个 @fnet/config 模块是一种用于简化应用程序配置文件的管理和访问程序的功能。 它通过注重易用性和灵活性,帮助用户从位于指定目录的YAML文件中检索配置设置. 它既满足了对配置文件的单一和分批处理,也允许用户无缝地与自定义的环境变量合作.
如何运作
模块扫描指定目录以定位匹配所提供名称的YAML配置文件. 用户可以指定默认目录,相对路径,甚至使用当前工作目录. 一旦找到一个配置,模块会解析它,并根据文件的内容可选择地更新系统的环境变量.
关键特性
- 目录搜索: 在指定目录和配置文件相对路径的序列中自动搜索.
- YAML 解析: 处理YAML文件并提取其数据以便于访问.
- 环境可变一体化:可选地将配置设置注入系统的环境变量.
- 批处理: 支持通过接受数组名称同时处理多个配置文件.
- 标记过滤:允许根据指定的标记对YAML内容进行选择性处理.
- 可选文件存在:用户可以指定缺少配置文件是否触发出错.
结论
这个 @fnet/config 模块提供了管理配置文件的直截了当的方法,使用户更容易组织和访问应用程序设置. 由于其不复杂的设置和有用的功能,它为管理应用程序配置提供了实用的解决方案,而不需要复杂.
@ fnet/ 配置开发者指南
概览
这个 @fnet/config 库简化了应用程序中配置文件的加载和管理程序。 对于配置需要动态和灵活的环境特别有用。 库允许开发者在指定的目录中无所顾忌地定位配置文件,并可选择将配置定义的环境变量传输到 process.env。 。 。 .
安装
用于 @fnet/config 在您的工程中,您可以通过 npm 或 纱线安装它,其命令如下:
npm install @fnet/config
yarn add @fnet/config
使用量
库提供了一个默认的同步函数,旨在定位和处理配置文件,处理各种扩展,例如 .fnet, (中文(简体) ), .fnet.yaml,以及 .fnet.yml。下面是关于如何使用图书馆的分步骤指南.
import loadConfig from '@fnet/config';
async function loadMyConfig() {
try {
const config = await loadConfig({
name: 'myConfig', // Name of the config file without extension
dir: ['path/to/config', 'another/path'], // Directories to search
rel: ['./.fnet/'], // Relative paths within directories
transferEnv: true, // Transfer env variables to process.env
optional: false, // Throws error if file not found
tags: ['production'] // Filter by specific tags in YAML
});
console.log('Configuration file:', config.file);
console.log('Parsed data:', config.data);
console.log('Raw content:', config.raw);
} catch (error) {
console.error('Error loading configuration:', error);
}
}
loadMyConfig();
实例
以下是一些实例,说明典型的使用情景: @fnet/config。 。 。 .
例1: 装入单个配置文件
import loadConfig from '@fnet/config';
async function loadSingleConfig() {
const config = await loadConfig({ name: 'app-config' });
console.log(config.file); // Absolute path to the found file
console.log(config.data); // Parsed configuration data
}
示例2: 装入多个配置文件
import loadConfig from '@fnet/config';
async function loadMultipleConfigs() {
const configs = await loadConfig({
name: ['db-config', 'api-config'],
dir: process.cwd()
});
configs.forEach(config => {
console.log('File:', config.file);
console.log('Data:', config.data);
});
}
例3:处理可选配置
import loadConfig from '@fnet/config';
async function loadOptionalConfig() {
try {
const config = await loadConfig({
name: 'optional-config',
optional: true // Returns null if file not found
});
if (config) {
console.log('Configuration loaded:', config.data);
} else {
console.log('Configuration file not found');
}
} catch (e) {
console.log('Unexpected error:', e.message);
}
}
配置文件搜索
库搜索配置文件的顺序如下:
- 在指定的目录(或
process.cwd()如果未指明) - 通过指定的相对路径(默认为
./.fnet/, (中文(简体) ),../.fnet/, (中文(简体) ),../../.fnet/, (中文(简体) ),../../../.fnet/(中文(简体) ) - 有支持的扩展 :
.fnet, (中文(简体) ),.fnet.yaml, (中文(简体) ),.fnet.yml
返回值结构
函数返回具有以下属性的对象(或对象数组):
file: 找到配置文件的绝对路径data: 解析配置数据config: 数据别名(过期)raw: Raw YAML 内容
致谢
这个 @fnet/config 库用途 @fnet/yaml 用于解析 YAML 文件,确保高效和可靠地处理配置文件.
输入外观
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
name:
oneOf:
- type: string
- type: array
items:
type: string
description: The name of the configuration or an array of names.
dir:
oneOf:
- type: string
- type: array
items:
type: string
description: The directories to search within. If not specified, uses process.cwd()
rel:
type: array
items:
type: string
default:
- ./.fnet/
- ../.fnet/
- ../../.fnet/
- ../../../.fnet/
description: Relative paths to consider within directories, will be normalized.
transferEnv:
type: boolean
default: true
description: Whether to transfer 'env' values from the configuration to process.env.
optional:
type: boolean
default: false
description: Whether the presence of the file is optional. If false, throws an
error when file isn't found.
tags:
type: array
items:
type: string
default: []
description: An array of tags used for filtering YAML content.
required:
- name
additionalProperties: false
description: Schema for the configuration file loader input options.
输出Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
file:
type: string
description: Absolute path to the found configuration file
data:
type: object
description: Parsed configuration data
config:
type: object
description: Alias for data (deprecated)
deprecated: true
raw:
type: string
description: Raw YAML content
required:
- file
- data
- config
- raw
additionalProperties: false
description: Schema for the configuration file loader output format