or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdcore-api.mdindex.mdloaders.md
tile.json

core-api.mddocs/

Core API

The core API provides the main entry points for configuration discovery and loading through factory functions and explorer instances.

Capabilities

Factory Functions

cosmiconfig()

Creates an asynchronous cosmiconfig explorer instance.

/**
 * Creates an asynchronous cosmiconfig explorer instance
 * @param moduleName - Module name used to generate search patterns
 * @param options - Configuration options for customizing behavior
 * @returns PublicExplorer instance for async operations
 */
function cosmiconfig(moduleName: string, options?: Options): PublicExplorer;

Usage Examples:

import { cosmiconfig } from "cosmiconfig";

// Basic usage with module name
const explorer = cosmiconfig("babel");

// With custom options
const customExplorer = cosmiconfig("myapp", {
  searchPlaces: ["package.json", ".myapprc", ".myapp.config.js"],
  cache: false
});

cosmiconfigSync()

Creates a synchronous cosmiconfig explorer instance.

/**
 * Creates a synchronous cosmiconfig explorer instance
 * @param moduleName - Module name used to generate search patterns
 * @param options - Configuration options for sync operations
 * @returns PublicExplorerSync instance for sync operations
 */
function cosmiconfigSync(moduleName: string, options?: OptionsSync): PublicExplorerSync;

Usage Examples:

import { cosmiconfigSync } from "cosmiconfig";

const explorerSync = cosmiconfigSync("prettier");
const result = explorerSync.search();

Explorer Methods

search()

Searches for configuration starting from a specified directory (or current working directory).

/**
 * Search for configuration starting from a directory
 * @param searchFrom - Directory to start searching from (defaults to process.cwd())
 * @returns Promise resolving to configuration result or null if not found
 */
search(searchFrom?: string): Promise<CosmiconfigResult>;

// Synchronous version
search(searchFrom?: string): CosmiconfigResult;

Usage Examples:

// Search from current directory
const result = await explorer.search();

// Search from specific directory
const result = await explorer.search("/path/to/project");

// Handle result
if (result) {
  console.log("Config found:", result.config);
  console.log("File path:", result.filepath);
  console.log("Is empty:", result.isEmpty);
} else {
  console.log("No configuration found");
}

load()

Loads configuration from a specific file path.

/**
 * Load configuration from a specific file path
 * @param filepath - Path to configuration file
 * @returns Promise resolving to configuration result or null if file doesn't exist
 */
load(filepath: string): Promise<CosmiconfigResult>;

// Synchronous version
load(filepath: string): CosmiconfigResult;

Usage Examples:

// Load specific configuration file
const result = await explorer.load("./config/myapp.config.js");

// Load with error handling
try {
  const result = await explorer.load("./nonexistent.config.js");
  if (result) {
    console.log("Config:", result.config);
  }
} catch (error) {
  console.error("Error loading config:", error);
}

Cache Management

Methods for managing internal caches used by explorers.

clearLoadCache()

Clears the cache used by the load() method.

/**
 * Clear the cache used by the load() method
 */
clearLoadCache(): void;

clearSearchCache()

Clears the cache used by the search() method.

/**
 * Clear the cache used by the search() method
 */
clearSearchCache(): void;

clearCaches()

Clears all caches (both load and search caches).

/**
 * Clear all caches (both load and search caches)
 */
clearCaches(): void;

Usage Examples:

const explorer = cosmiconfig("myapp");

// Use explorer methods
await explorer.search();
await explorer.load("./config.js");

// Clear specific cache
explorer.clearLoadCache();

// Or clear all caches
explorer.clearCaches();

Explorer Interfaces

Complete interface definitions for explorer instances:

interface PublicExplorerBase {
  clearLoadCache(): void;
  clearSearchCache(): void;
  clearCaches(): void;
}

interface PublicExplorer extends PublicExplorerBase {
  search(searchFrom?: string): Promise<CosmiconfigResult>;
  load(filepath: string): Promise<CosmiconfigResult>;
}

interface PublicExplorerSync extends PublicExplorerBase {
  search(searchFrom?: string): CosmiconfigResult;
  load(filepath: string): CosmiconfigResult;
}

Result Type

The result returned by search and load operations:

type CosmiconfigResult = {
  /** The parsed configuration object */
  config: any;
  /** Absolute path to the configuration file */
  filepath: string;
  /** True if the configuration file was empty or contained only whitespace */
  isEmpty?: boolean;
} | null;

Understanding Results:

  • null indicates no configuration was found
  • config contains the parsed configuration data
  • filepath is the absolute path where configuration was found
  • isEmpty is true when a file exists but contains no meaningful configuration