配置文件用什么格式? JSON 不适合手写,YAML 缩进容易出错。TOML 看似完美,但大多数工具生成的 TOML 嵌套层级太深:
# 默认生成的
[server]
[server.database]
[server.database.connection]
host = "localhost"
port = 5432
人类更喜欢扁平的:
# 实际想要的
[server]
database.connection = {
host = "localhost",
port = 5432,
}
json-pretty-toml - 将 JSON 转换为扁平化 TOML ,遵循三个原则:
[section])section.key = value 或 section."key.name" = {...}){ ... }),使用TOML 1.1的内连风格$ echo '{"gateway":{"port":18789,"auth":{"mode":"token"}}}' | json-pretty-toml
[gateway]
port = 18789
auth.mode = "token"
支持多行内联表、自动引号转义、数组格式化等。
npm install -g json-pretty-toml
或直接用 npx:
npx json-pretty-toml < config.json > config.toml
json-pretty-toml适合需要阅读/偶尔编辑配置文件的场合
1
Dispatcher 6 小时 13 分钟前
让我想起了 ini 这种配置文件。尤其是 win95 的 system.ini 和 win.ini
历史就是个大轮回啊。 |