or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli-arguments.mdconfiguration-loading.mdconfiguration-management.mdconfiguration-schema.mdconfiguration-types.mddirective-processing.mdfile-resolution.mdindex.mdutilities.md
tile.json

utilities.mddocs/

Utilities

Utility functions for configuration processing including array handling, log level determination, and configuration merging.

Array Utilities

arrayOf

function arrayOf<T>(value: T | T[] | undefined): T[];

Converts a configuration value that can be either an array, a single value, or undefined into an array containing all values.

Parameters:

  • value: T | T[] | undefined - Value to wrap in an array

Returns:

  • T[] - Array containing all the values

Behavior:

  • undefined[] (empty array)
  • Single values → [value] (wrapped in array)
  • Arrays → [...value] (spread to new array)
  • Strings are treated as single values (not iterated over characters)
  • Other iterables are spread into arrays
  • Non-iterable objects are wrapped as single values

Examples:

import { arrayOf } from "@autorest/configuration";

// Single values
console.log(arrayOf("single")); // ["single"]
console.log(arrayOf(42)); // [42]
console.log(arrayOf(true)); // [true]

// Arrays
console.log(arrayOf(["a", "b", "c"])); // ["a", "b", "c"]
console.log(arrayOf([1, 2, 3])); // [1, 2, 3]

// Undefined
console.log(arrayOf(undefined)); // []

// Iterables (but not strings)
console.log(arrayOf(new Set(["x", "y"]))); // ["x", "y"]

isIterable

function isIterable(target: any): target is Iterable<any>;

Type guard function that checks if a value implements the iterable protocol.

Parameters:

  • target: any - Value to check for iterability

Returns:

  • target is Iterable<any> - Type guard indicating if value is iterable

Examples:

import { isIterable } from "@autorest/configuration";

console.log(isIterable([1, 2, 3])); // true
console.log(isIterable("string")); // true  
console.log(isIterable(new Set())); // true
console.log(isIterable(new Map())); // true
console.log(isIterable(42)); // false
console.log(isIterable({})); // false
console.log(isIterable(null)); // false

Logging Utilities

LogOptions

interface LogOptions {
  debug?: boolean;
  verbose?: boolean;
  level?: LogLevel | string;
}

Configuration options for determining log levels.

Properties:

  • debug?: boolean - Enable debug logging
  • verbose?: boolean - Enable verbose logging
  • level?: LogLevel | string - Explicit log level setting

getLogLevel

function getLogLevel(config: LogOptions): LogLevel;

type LogLevel = "debug" | "verbose" | "information" | "warning" | "error";

Determines the appropriate log level from configuration options.

Parameters:

  • config: LogOptions - Configuration with logging options

Returns:

  • LogLevel - Determined log level

Logic:

  1. If debug is true → "debug"
  2. If verbose is true → "verbose"
  3. If level is set and valid → level
  4. Otherwise → "information"

Examples:

import { getLogLevel } from "@autorest/configuration";

console.log(getLogLevel({ debug: true })); // "debug"
console.log(getLogLevel({ verbose: true })); // "verbose"
console.log(getLogLevel({ level: "warning" })); // "warning"
console.log(getLogLevel({})); // "information"

// Debug takes precedence over verbose
console.log(getLogLevel({ debug: true, verbose: true })); // "debug"

isValidLogLevel

function isValidLogLevel(level: string): level is LogLevel;

Type guard that validates if a string is a valid log level.

Parameters:

  • level: string - String to validate as log level

Returns:

  • level is LogLevel - Type guard indicating valid log level

Valid Log Levels:

  • "debug" - Debug level logging
  • "verbose" - Verbose level logging
  • "information" - Information level logging
  • "warning" - Warning level logging
  • "error" - Error level logging

Examples:

import { isValidLogLevel } from "@autorest/configuration";

console.log(isValidLogLevel("debug")); // true
console.log(isValidLogLevel("info")); // false
console.log(isValidLogLevel("ERROR")); // false (case sensitive)
console.log(isValidLogLevel("information")); // true

Configuration Merging Utilities

mergeConfigurations

function mergeConfigurations<T>(configs: Array<T>, options?: MergeOptions): T;

Merges multiple configuration objects together using deep merge with configurable options.

Parameters:

  • configs: Array<T> - Array of configuration objects to merge
  • options?: MergeOptions - Optional merge configuration

Returns:

  • T - Merged configuration object

Merge Behavior:

  • Configurations are merged from left to right
  • Later configurations override earlier ones
  • Objects are deeply merged
  • Arrays can be merged or replaced based on options
  • Primitive values are replaced

Examples:

import { mergeConfigurations } from "@autorest/configuration";

const config1 = {
  "input-file": ["swagger1.yaml"],
  "output-folder": "./generated",
  debug: false
};

const config2 = {
  "input-file": ["swagger2.yaml"],
  verbose: true,
  csharp: true
};

const config3 = {
  debug: true,
  "namespace": "MyApi"
};

const merged = mergeConfigurations([config1, config2, config3]);
console.log(merged);
// Result:
// {
//   "input-file": ["swagger2.yaml"], // Replaced by config2
//   "output-folder": "./generated",  // From config1
//   debug: true,                     // Overridden by config3
//   verbose: true,                   // From config2
//   csharp: true,                    // From config2
//   "namespace": "MyApi"             // From config3
// }

Merge Options

interface MergeOptions {
  arrayMergeStrategy?: "replace" | "merge" | "low-pri-first";
  // Additional merge configuration options
}

Configuration options for controlling merge behavior.

Properties:

  • arrayMergeStrategy?: string - Strategy for merging arrays:
    • "replace" - Replace arrays completely
    • "merge" - Merge arrays together
    • "low-pri-first" - Low priority items first

Usage Examples

Complete Configuration Processing

import { 
  arrayOf, 
  getLogLevel, 
  mergeConfigurations,
  isValidLogLevel 
} from "@autorest/configuration";

// Process CLI arguments
function processCliArgs(args: any) {
  return {
    "input-file": arrayOf(args["input-file"]),
    "exclude-file": arrayOf(args["exclude-file"]),
    debug: args.debug || false,
    verbose: args.verbose || false,
    level: isValidLogLevel(args.level) ? args.level : undefined
  };
}

// Determine logging configuration
function setupLogging(config: any) {
  const logLevel = getLogLevel({
    debug: config.debug,
    verbose: config.verbose,
    level: config.level
  });
  
  console.log(`Log level: ${logLevel}`);
  return logLevel;
}

// Merge multiple configuration sources
function buildFinalConfig(cliConfig: any, fileConfig: any, defaults: any) {
  return mergeConfigurations([
    defaults,      // Lowest priority
    fileConfig,    // Medium priority  
    cliConfig      // Highest priority
  ], {
    arrayMergeStrategy: "low-pri-first"
  });
}

Configuration Normalization

// Normalize configuration arrays
function normalizeConfig(rawConfig: any) {
  return {
    ...rawConfig,
    "input-file": arrayOf(rawConfig["input-file"]),
    "exclude-file": arrayOf(rawConfig["exclude-file"]),
    directive: arrayOf(rawConfig.directive),
    use: arrayOf(rawConfig.use),
    require: arrayOf(rawConfig.require),
    "try-require": arrayOf(rawConfig["try-require"])
  };
}

// Validate logging configuration
function validateLoggingConfig(config: any) {
  if (config.level && !isValidLogLevel(config.level)) {
    throw new Error(`Invalid log level: ${config.level}`);
  }
  
  return {
    ...config,
    logLevel: getLogLevel(config)
  };
}