yaml-json-toml-loader
npx machina-cli add skill a5c-ai/babysitter/yaml-json-toml-loader --openclawFiles (1)
SKILL.md
1.5 KB
YAML/JSON/TOML Loader
Generate multi-format config file loaders.
Generated Patterns
import fs from 'fs';
import path from 'path';
import yaml from 'yaml';
import toml from '@iarna/toml';
type ConfigFormat = 'json' | 'yaml' | 'toml' | 'auto';
export function loadConfigFile(filepath: string, format: ConfigFormat = 'auto'): unknown {
const content = fs.readFileSync(filepath, 'utf-8');
const ext = format === 'auto' ? path.extname(filepath).toLowerCase() : `.${format}`;
switch (ext) {
case '.json': return JSON.parse(content);
case '.yaml': case '.yml': return yaml.parse(content);
case '.toml': return toml.parse(content);
default: throw new Error(`Unknown config format: ${ext}`);
}
}
export function saveConfigFile(filepath: string, data: unknown, format: ConfigFormat = 'auto'): void {
const ext = format === 'auto' ? path.extname(filepath).toLowerCase() : `.${format}`;
let content: string;
switch (ext) {
case '.json': content = JSON.stringify(data, null, 2); break;
case '.yaml': case '.yml': content = yaml.stringify(data); break;
case '.toml': content = toml.stringify(data as any); break;
default: throw new Error(`Unknown format: ${ext}`);
}
fs.writeFileSync(filepath, content);
}
Target Processes
- configuration-management-system
- cli-application-bootstrap
Source
git clone https://github.com/a5c-ai/babysitter/blob/main/plugins/babysitter/skills/babysit/process/specializations/cli-mcp-development/skills/yaml-json-toml-loader/SKILL.mdView on GitHub Overview
yaml-json-toml-loader provides loadConfigFile and saveConfigFile to read and write configuration files in YAML, JSON, or TOML. It supports auto-detection by file extension and exposes a simple API for managing multi-format configs in Node.js projects.
How This Skill Works
The module imports fs and path, along with yaml and @iarna/toml to parse and stringify data. loadConfigFile reads a file and selects the parser based on the extension (or a forced format), while saveConfigFile stringifies data to the chosen format and writes it back. It uses a ConfigFormat type of 'json' | 'yaml' | 'toml' | 'auto' and falls back to errors for unknown formats.
When to Use It
- Loading configuration files in a CLI or server app that may be in JSON, YAML, or TOML.
- Converting or migrating config data between formats without manual reformatting.
- Automating environment setup where multiple format support is required.
- Building a configuration-management workflow that reads from or writes to various config files.
- Developing tooling that needs consistent read/write semantics across formats.
Quick Start
- Step 1: Ensure dependencies are available (yaml and @iarna/toml) and import loadConfigFile / saveConfigFile.
- Step 2: Call loadConfigFile('config.yaml') or loadConfigFile('config.json', 'auto') to read data.
- Step 3: Modify the data and persist with saveConfigFile('config.toml', data) or saveConfigFile('config.yaml', data, 'yaml').
Best Practices
- Validate loaded data before use to prevent runtime errors.
- Prefer explicit format when possible to avoid auto-detection ambiguity.
- Handle errors gracefully with try/catch and meaningful messages.
- Keep dependencies up to date and pin versions for stability.
- Consider schema validation for complex configs to ensure consistency across formats.
Example Use Cases
- A CLI tool that reads user settings from config.yaml, config.json, or config.toml depending on user preference.
- An app bootstrapper that saves generated config in the chosen format and reloads it on startup.
- A DevOps script that loads deployment parameters from a YAML file, updates values, and writes back to TOML.
- A multi-environment service that loads environment-specific configs in different formats for testing and production.
- A configuration-management utility that mirrors config changes across YAML, JSON, and TOML representations.
Frequently Asked Questions
Add this skill to your agents