### Install Lilconfig Source: https://github.com/antonk52/lilconfig/blob/master/readme.md Install the package via npm. ```sh npm install lilconfig ``` -------------------------------- ### Basic Usage of Lilconfig Source: https://github.com/antonk52/lilconfig/blob/master/readme.md Demonstrates importing and using both asynchronous and synchronous configuration loading methods. ```js import {lilconfig, lilconfigSync} from 'lilconfig'; // all keys are optional const options = { stopDir: '/Users/you/some/dir', searchPlaces: ['package.json', 'myapp.conf.js'], ignoreEmptySearchPlaces: false } lilconfig( 'myapp', options // optional ).search() // Promise lilconfigSync( 'myapp', options // optional ).load(pathToConfig) // LilconfigResult /** * LilconfigResult * { * config: any; // your config * filepath: string; * } */ ``` -------------------------------- ### Configure Search Places Source: https://context7.com/antonk52/lilconfig/llms.txt Demonstrates the default search order for configuration files and how to override them using the searchPlaces option. ```javascript import {lilconfig, lilconfigSync} from 'lilconfig'; // Default search places for async (includes .mjs): // - package.json // - .myapprc.json // - .myapprc.js // - .myapprc.cjs // - .myapprc.mjs // - .config/myapprc // - .config/myapprc.json // - .config/myapprc.js // - .config/myapprc.cjs // - .config/myapprc.mjs // - myapp.config.js // - myapp.config.cjs // - myapp.config.mjs // Default search places for sync (excludes .mjs): // - package.json // - .myapprc.json // - .myapprc.js // - .myapprc.cjs // - .config/myapprc // - .config/myapprc.json // - .config/myapprc.js // - .config/myapprc.cjs // - myapp.config.js // - myapp.config.cjs // Custom search places override defaults entirely const searcher = lilconfig('myapp', { searchPlaces: [ 'myapp.config.js', '.myapprc.json', 'package.json' ] }); ``` -------------------------------- ### Search and load configuration asynchronously Source: https://context7.com/antonk52/lilconfig/llms.txt Creates an asynchronous searcher to find and load configuration files, supporting custom options like stop directories and result transformations. ```javascript import {lilconfig} from 'lilconfig'; // Basic usage - search for config starting from current directory const searcher = lilconfig('myapp'); // Search returns a promise with config result const result = await searcher.search(); // result: { config: { setting: 'value' }, filepath: '/path/to/.myapprc.json' } // result: null (if no config found) // Search from a specific directory const result = await searcher.search('/path/to/project/src/components'); // Load a specific config file directly const result = await searcher.load('./myapp.config.js'); // result: { config: { setting: 'value' }, filepath: '/absolute/path/myapp.config.js' } // With full options const searcher = lilconfig('myapp', { stopDir: '/Users/you/projects', searchPlaces: [ 'package.json', '.myapprc', '.myapprc.json', '.myapprc.js', 'myapp.config.js' ], ignoreEmptySearchPlaces: true, cache: true, packageProp: 'myapp', // or nested: 'config.myapp' or ['config', 'myapp'] transform: (result) => { if (!result) return null; return { ...result, config: { ...result.config, transformed: true } }; } }); ``` -------------------------------- ### lilconfig - Async Configuration Searcher Source: https://context7.com/antonk52/lilconfig/llms.txt Creates an asynchronous configuration searcher that traverses directories upward to find and load configuration files. ```APIDOC ## lilconfig(name, options) ### Description Creates an asynchronous configuration searcher that searches for and loads configuration files. The searcher traverses from the specified directory upward until it finds a configuration file or reaches the stop directory. ### Parameters #### Path Parameters - **name** (string) - Required - The name of the application, used to construct search file names. #### Request Body - **options** (object) - Optional - Configuration options including stopDir, searchPlaces, ignoreEmptySearchPlaces, cache, packageProp, and transform function. ### Request Example const searcher = lilconfig('myapp', { stopDir: '/home/user', cache: true }); ### Response #### Success Response (200) - **result** (object) - Contains the loaded config object and the filepath of the found configuration file, or null if not found. ``` -------------------------------- ### lilconfigSync - Sync Configuration Searcher Source: https://context7.com/antonk52/lilconfig/llms.txt Creates a synchronous configuration searcher that returns results directly. Note that .mjs files are not supported in sync mode. ```APIDOC ## lilconfigSync(name, options) ### Description Creates a synchronous configuration searcher with the same API as the async version but returns results directly instead of promises. ### Parameters #### Path Parameters - **name** (string) - Required - The name of the application. #### Request Body - **options** (object) - Optional - Configuration options including stopDir, searchPlaces, and loaders. ### Response #### Success Response (200) - **result** (object) - Returns the configuration result object directly or null if not found. ``` -------------------------------- ### Search and load configuration synchronously Source: https://context7.com/antonk52/lilconfig/llms.txt Creates a synchronous searcher that returns results directly. Note that .mjs files are not supported in sync mode. ```javascript import {lilconfigSync} from 'lilconfig'; // Basic synchronous search const searcher = lilconfigSync('myapp'); const result = searcher.search(); if (result) { console.log('Config found:', result.config); console.log('At path:', result.filepath); } else { console.log('No configuration found'); } // Load specific file synchronously const result = lilconfigSync('myapp').load('./config.json'); // With options - search from specific location with stop directory const result = lilconfigSync('myapp', { stopDir: __dirname, searchPlaces: ['myapp.config.js', 'package.json'] }).search('/project/src/deep/nested/dir'); ``` -------------------------------- ### Clear Lilconfig Caches Source: https://context7.com/antonk52/lilconfig/llms.txt Demonstrates how to clear the load cache, search cache, or both. Caching is enabled by default and can be disabled. ```javascript import {lilconfig, lilconfigSync} from 'lilconfig'; const searcher = lilconfig('myapp', { cache: true }); // First search populates cache const result1 = await searcher.search('/project/src'); // Subsequent searches from same or child paths use cache const result2 = await searcher.search('/project/src/components'); // result1 === result2 (same reference from cache) // Clear only the load cache (for .load() results) searcher.clearLoadCache(); // Clear only the search cache (for .search() results) searcher.clearSearchCache(); // Clear both caches searcher.clearCaches(); // After clearing, next search performs fresh file system lookups const result3 = await searcher.search('/project/src'); // result1 !== result3 (different reference) // Disable caching entirely const uncachedSearcher = lilconfig('myapp', { cache: false }); ``` -------------------------------- ### Implement custom configuration loaders Source: https://context7.com/antonk52/lilconfig/llms.txt Define custom loader functions to parse non-standard configuration formats like YAML or TypeScript. ```javascript import {lilconfig, lilconfigSync} from 'lilconfig'; import yaml from 'yaml'; import {transpileModule} from 'typescript'; // YAML loader function loadYaml(filepath, content) { return yaml.parse(content); } const yamlSearcher = lilconfig('myapp', { searchPlaces: ['myapp.config.yaml', 'myapp.config.yml', '.myapprc'], loaders: { '.yaml': loadYaml, '.yml': loadYaml, 'noExt': loadYaml // for files without extension } }); const result = await yamlSearcher.search(); // TypeScript loader (sync) const tsLoader = (filepath, content) => { const result = transpileModule(content, {}).outputText; return eval(result); }; const tsSearcher = lilconfigSync('myapp', { searchPlaces: ['myapp.config.ts'], loaders: { '.ts': tsLoader } }); // Async loader example const asyncLoader = async (filepath, content) => { const data = JSON.parse(content); // Perform async operations like fetching remote config return { ...data, loaded: true }; }; const asyncSearcher = lilconfig('myapp', { loaders: { '.json': asyncLoader } }); ``` -------------------------------- ### Implement YAML Loader Source: https://github.com/antonk52/lilconfig/blob/master/readme.md Custom loader configuration to add support for YAML files, which are not supported by default. ```js import {lilconfig} from 'lilconfig'; import yaml from 'yaml'; function loadYaml(filepath, content) { return yaml.parse(content); } const options = { loaders: { '.yaml': loadYaml, '.yml': loadYaml, // loader for files with no extension noExt: loadYaml } }; lilconfig('myapp', options) .search() .then(result => { result // {config, filepath} }); ``` -------------------------------- ### Package.json Configuration with Lilconfig Source: https://context7.com/antonk52/lilconfig/llms.txt Configure lilconfig to read settings from a specific property within package.json. Supports nested property paths for flexibility. ```javascript import {lilconfig, lilconfigSync} from 'lilconfig'; // Reads from "myapp" key in package.json // package.json: { "myapp": { "setting": true } } const result = lilconfigSync('myapp').load('./package.json'); // result.config: { setting: true } // Custom package property // package.json: { "tools": { "myapp": { "enabled": true } } } const searcher = lilconfigSync('myapp', { packageProp: 'tools.myapp' // or ['tools', 'myapp'] }); const result = searcher.load('./package.json'); // result.config: { enabled: true } // Reading nested config via search const searcher = lilconfig('prettier', { packageProp: 'prettier', searchPlaces: ['package.json', '.prettierrc'] }); const result = await searcher.search(); ``` -------------------------------- ### Custom Loaders Source: https://context7.com/antonk52/lilconfig/llms.txt Custom loaders allow parsing of non-standard configuration file formats by providing a function that processes file content. ```APIDOC ## Custom Loaders ### Description Loaders are functions that parse configuration file content. Custom loaders enable support for additional formats like YAML or TypeScript. ### Parameters #### Request Body - **loaders** (object) - Required - A map where keys are file extensions (or 'noExt') and values are functions with signature (filepath, content) => parsedData. ### Request Example const searcher = lilconfig('myapp', { loaders: { '.yaml': (filepath, content) => yaml.parse(content) } }); ``` -------------------------------- ### Lilconfig Default Loaders Source: https://context7.com/antonk52/lilconfig/llms.txt Access and extend default loaders provided by lilconfig for various file types. Custom loaders can be added for formats like YAML or TOML, or existing ones can be overridden. ```javascript import {lilconfig, defaultLoaders, defaultLoadersSync} from 'lilconfig'; // View available default loaders console.log(Object.keys(defaultLoaders)); // ['.js', '.mjs', '.cjs', '.json', 'noExt'] console.log(Object.keys(defaultLoadersSync)); // ['.js', '.cjs', '.json', 'noExt'] // Extend default loaders with custom ones const searcher = lilconfig('myapp', { loaders: { ...defaultLoaders, '.yaml': (filepath, content) => require('yaml').parse(content), '.toml': (filepath, content) => require('toml').parse(content) } }); // Override a specific default loader const strictJsonSearcher = lilconfig('myapp', { loaders: { '.json': (filepath, content) => { const config = JSON.parse(content); if (!config.version) { throw new Error('Config must have a version field'); } return config; } } }); ``` -------------------------------- ### Lilconfig Transform Function Source: https://context7.com/antonk52/lilconfig/llms.txt Use the transform option to modify configuration results before they are returned. This can be used for validation, normalization, or adding default values. Supports both synchronous and asynchronous transformations. ```javascript import {lilconfig, lilconfigSync} from 'lilconfig'; // Add default values and validation const searcher = lilconfig('myapp', { transform: (result) => { if (result === null) { // Return default config when none found return { config: { debug: false, port: 3000 }, filepath: 'default' }; } // Merge with defaults return { ...result, config: { debug: false, port: 3000, ...result.config } }; } }); const result = await searcher.search(); // Always returns a config object, never null // Async transform const asyncSearcher = lilconfig('myapp', { transform: async (result) => { if (!result) return null; // Perform async validation or enrichment const validated = await validateConfig(result.config); return { ...result, config: validated }; } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.