CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lilconfig

A zero-dependency alternative to cosmiconfig for loading configuration files

89

1.21x
Overview
Eval results
Files

task.mdevals/scenario-7/

Configuration Extractor

Build a simple configuration extraction tool that searches for and extracts project configurations stored in package.json files.

Background

Many Node.js tools store their configuration directly within the project's package.json file under specific properties. Your tool should be able to find package.json files in the directory tree and extract configurations from them.

Requirements

Create a module that exports two functions:

1. extractConfig(configName, propertyPath)

Searches upward from the current directory to find a package.json file that contains a configuration at the specified property path.

Parameters:

  • configName (string): The name to use when searching for configuration files
  • propertyPath (string or array, optional): The property path within package.json to extract from. If not provided, defaults to using configName as the top-level property. Can be a simple string like "prettier", a nested path like "config.myTool" (string with dots), or an array like ["config", "myTool"]

Returns:

  • An object with config and filepath properties if found
  • null if no matching configuration is found

Behavior:

  • Search upward through the directory tree starting from the current directory
  • When a package.json is found, check if it contains the specified configuration property
  • If the property doesn't exist in that package.json, continue searching upward
  • Return the first matching configuration found

2. loadConfigFromFile(filepath, propertyPath)

Loads a specific package.json file and extracts a configuration from it.

Parameters:

  • filepath (string): Path to the package.json file to load
  • propertyPath (string or array): The property path to extract (e.g., "prettier", "config.eslint", or ["config", "eslint"])

Returns:

  • An object with config and filepath properties
  • The filepath should be the absolute path to the file

Example Usage

// Example 1: Simple top-level property
const result = await extractConfig('prettier');
// Returns: { config: {...}, filepath: '/path/to/package.json' }

// Example 2: Nested property using dot notation
const result = await extractConfig('eslint', 'config.eslint');

// Example 3: Nested property using array notation
const result = await extractConfig('myTool', ['config', 'tools', 'myTool']);

// Example 4: Load from specific file
const result = await loadConfigFromFile('./package.json', 'prettier');

Dependencies { .dependencies }

lilconfig { .dependency }

Provides configuration file search and loading capabilities.

Implementation Files

  • config-extractor.js - Main implementation
  • config-extractor.test.js - Test file

Test Cases { .tests }

Test 1: Extract top-level property { .test }

Setup: Create a package.json with:

{
  "name": "test-project",
  "prettier": {
    "semi": false,
    "singleQuote": true
  }
}

Test:

const result = await extractConfig('prettier');
assert(result !== null);
assert.deepEqual(result.config, { semi: false, singleQuote: true });
assert(result.filepath.endsWith('package.json'));

Test 2: Extract nested property with dot notation { .test }

Setup: Create a package.json with:

{
  "name": "test-project",
  "config": {
    "myapp": {
      "port": 3000,
      "host": "localhost"
    }
  }
}

Test:

const result = await extractConfig('myapp', 'config.myapp');
assert(result !== null);
assert.deepEqual(result.config, { port: 3000, host: 'localhost' });

Test 3: Return null when property not found { .test }

Setup: Use a package.json without the target property.

Test:

const result = await extractConfig('nonexistent');
assert.strictEqual(result, null);

Test 4: Load from specific file { .test }

Setup: Create a package.json at a known path with a "prettier" property.

Test:

const result = await loadConfigFromFile('./package.json', 'prettier');
assert(result !== null);
assert(result.config !== undefined);
assert(result.filepath.endsWith('package.json'));

Install with Tessl CLI

npx tessl i tessl/npm-lilconfig

tile.json