Get the FREE Ultimate OpenClaw Setup Guide →

cosmiconfig-setup

Scanned
npx machina-cli add skill a5c-ai/babysitter/cosmiconfig-setup --openclaw
Files (1)
SKILL.md
1.4 KB

Cosmiconfig Setup

Set up cosmiconfig for hierarchical config loading.

Capabilities

  • Configure cosmiconfig search paths
  • Set up format loaders (JSON, YAML, TOML)
  • Create TypeScript config support
  • Implement config caching
  • Handle config validation

Generated Patterns

import { cosmiconfig, cosmiconfigSync } from 'cosmiconfig';
import { TypeScriptLoader } from 'cosmiconfig-typescript-loader';

const moduleName = 'myapp';

const explorer = cosmiconfig(moduleName, {
  searchPlaces: [
    'package.json',
    `.${moduleName}rc`,
    `.${moduleName}rc.json`,
    `.${moduleName}rc.yaml`,
    `.${moduleName}rc.yml`,
    `.${moduleName}rc.js`,
    `.${moduleName}rc.ts`,
    `.${moduleName}rc.cjs`,
    `${moduleName}.config.js`,
    `${moduleName}.config.ts`,
    `${moduleName}.config.cjs`,
  ],
  loaders: {
    '.ts': TypeScriptLoader(),
  },
});

export async function loadConfig(searchFrom?: string) {
  const result = await explorer.search(searchFrom);
  if (!result || result.isEmpty) {
    return { config: getDefaultConfig(), filepath: null };
  }
  return { config: { ...getDefaultConfig(), ...result.config }, filepath: result.filepath };
}

Target Processes

  • configuration-management-system
  • cli-application-bootstrap
  • mcp-server-bootstrap

Source

git clone https://github.com/a5c-ai/babysitter/blob/main/plugins/babysitter/skills/babysit/process/specializations/cli-mcp-development/skills/cosmiconfig-setup/SKILL.mdView on GitHub

Overview

Cosmiconfig setup enables hierarchical configuration loading from multiple sources and formats. It defines search paths, loaders (JSON, YAML, TOML, and TypeScript), and includes caching and validation to ensure reliable runtime config.

How This Skill Works

The skill configures a cosmiconfig explorer with a moduleName and a detailed searchPlaces list, plus a TypeScript loader. It then implements loadConfig to search from a given path, fall back to defaults when no config is found, and merge the loaded config with getDefaultConfig for a consistent result.

When to Use It

  • Building a CLI or service that reads configuration from package.json, dotfiles, and moduleName.config.* across multiple formats
  • You need TypeScript-based config files and want to load .ts configurations via a dedicated TypeScript loader
  • You want config results cached to avoid repeated disk IO in long-running processes
  • You require a default config that merges with user-provided config for sane fallback values
  • You are implementing a configuration-management or bootstrap flow (e.g., mcp-server-bootstrap) that relies on hierarchical sources

Quick Start

  1. Step 1: Install cosmiconfig and the TypeScript loader then import them in your setup
  2. Step 2: Create an explorer with moduleName and a searchPlaces array; add loaders for .ts
  3. Step 3: Implement loadConfig(searchFrom?) to search, merge with defaults, and return { config, filepath }

Best Practices

  • Define a clear and comprehensive searchPlaces order to cover all supported formats
  • Provide a centralized getDefaultConfig and consistently merge it with loaded config
  • Include a TypeScript loader (e.g., cosmiconfig-typescript-loader) for TS-based configs
  • Validate or sanitize loaded config before use to prevent runtime errors
  • Implement caching with a strategy for invalidation when source files change

Example Use Cases

  • CLI bootstrap that loads configuration from package.json, .myapprc, and myapp.config.{js,ts} in multiple formats
  • Application that supports JSON, YAML, and TOML config files with a TypeScript-based config file
  • Configuration-management-system using cosmiconfig to load from various sources and formats
  • mcp-server-bootstrap process that reads hierarchical config for server initialization
  • Project that relies on a configurable search path including package.json and module-specific config files

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers