or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

command-execution.mdconfiguration-management.mdindex.mdversion-management.md
tile.json

command-execution.mddocs/

Command Execution

Command discovery, validation, and execution system that proxies GitBook commands to the appropriate version while handling argument parsing and error reporting.

Capabilities

GitBook Instance Loading

GitBook instances are loaded through the main manager module using ensureAndLoad() function, which ensures the version exists and loads it for command execution.

Command Execution

Execute specific GitBook commands with argument handling.

/**
 * Execute a specific command from GitBook command list
 * @param commands - Array of available GitBook commands
 * @param command - Command name to execute
 * @param args - Array of positional arguments
 * @param kwargs - Object of named arguments/options
 * @returns Promise resolving to command result
 */
function exec(commands, command, args, kwargs): Promise<any>;

Usage Examples:

const commands = require('gitbook-cli/lib/commands');

// Execute build command
try {
  const result = await commands.exec(gitbook.commands, 'build', ['.'], {
    output: './output',
    format: 'website'
  });
  console.log('Build completed successfully');
} catch (error) {
  console.error('Build failed:', error.message);
}

// Execute serve command with options
await commands.exec(gitbook.commands, 'serve', ['.'], {
  port: 4000,
  lrport: 35729,
  watch: true
});

Help System

Command help and documentation display utilities.

/**
 * Display formatted help for list of commands
 * @param commands - Array of GitBook commands to document
 * @returns void (prints to console)
 */
function help(commands): void;

Usage Examples:

const commands = require('gitbook-cli/lib/commands');

// Display help for all available commands
const gitbook = await manager.ensureAndLoad(process.cwd());
commands.help(gitbook.commands);

// Output format:
//     build                           Build the book
//         --log                       Minimum log level (Default is info)
//         --format                    Format to build (Default is website)
//         --[no-]timing               Print timing debug information
//     
//     serve                           Serve the book as a website  
//         --port                      Port for server (Default is 4000)
//         --lrport                    Port for livereload server

CLI Command Integration

The CLI binary (bin/gitbook.js) integrates these functions to provide the complete command-line interface:

Built-in CLI Commands

The GitBook CLI provides several built-in commands for version management:

# List locally installed GitBook versions
gitbook ls
# Displays all installed versions with current selection marked

# Show currently activated GitBook version for a project
gitbook current
# Resolves version from book.json or uses default

# List available GitBook versions from NPM registry
gitbook ls-remote
# Shows all published versions and release tags

# Download and install a GitBook version
gitbook fetch [version]
# version defaults to '*' (latest) if not specified
# Example: gitbook fetch 2.6.7

# Update to latest version of a release tag
gitbook update [tag]
# tag defaults to 'latest' if not specified
# Example: gitbook update beta

# Remove an installed GitBook version
gitbook uninstall [version]
# Example: gitbook uninstall 2.5.0

# Create an alias pointing to a local GitBook folder
gitbook alias [folder] [version]
# folder defaults to current directory
# version defaults to 'latest'
# Example: gitbook alias /path/to/dev-gitbook dev

# Display help for GitBook commands
gitbook help
# Shows available commands from the loaded GitBook version

Command Proxy System

All other commands are proxied to the appropriate GitBook version:

# Execute any GitBook command through version proxy
gitbook <command> [args...] [options...]

# Common GitBook commands (version-dependent):
gitbook build .                    # Build book to static website
gitbook serve . --port 3000        # Serve book with live reload
gitbook init ./new-book            # Initialize new GitBook project
gitbook pdf . output.pdf           # Export book as PDF
gitbook epub . output.epub         # Export book as EPUB
gitbook mobi . output.mobi         # Export book as MOBI

# Version-specific command execution:
gitbook build . --gitbook=2.6.7    # Use specific GitBook version

Global CLI Options

Available on all commands:

-v, --gitbook [version]    # Specify GitBook version to use
-d, --debug               # Enable verbose error output and stack traces
-V, --version             # Display running versions of CLI and GitBook

Examples:

# Check versions
gitbook -V
# Output: CLI version: 2.3.2, GitBook version: 3.2.3

# Enable debug output
gitbook build . --debug
# Shows detailed error information if build fails

# Use specific version for single command
gitbook serve . --gitbook=2.6.7 --port 4000

Error Handling

Command execution can encounter various error conditions:

  • CommandNotFoundError: When specified command doesn't exist
  • InvalidArgumentError: When command arguments are invalid or missing
  • GitBookLoadError: When GitBook instance fails to load
  • ExecutionError: When command execution fails
  • VersionMismatchError: When command requires different GitBook version
try {
  await commands.exec(gitbookCommands, 'nonexistent', [], {});
} catch (error) {
  if (error.message.includes("doesn't exist")) {
    console.error('Command not found. Available commands:');
    commands.help(gitbookCommands);
  } else {
    console.error('Execution failed:', error.message);
  }
}

Command Object Structure

Commands returned by GitBook instances follow this structure:

interface Command {
  name: string;              // Command name (e.g., 'build', 'serve')
  description: string;       // Human-readable description
  options?: CommandOption[]; // Array of command options
  exec: (args: string[], kwargs: object) => Promise<any>; // Execution function
}

interface CommandOption {
  name: string;           // Option name (without dashes)
  description: string;    // Option description
  defaults?: any;         // Default value if not provided
  values?: string[];      // Allowed values for validation
}

Example Command Object:

{
  name: "build",
  description: "Build the book", 
  options: [
    {
      name: "log",
      description: "Minimum log level",
      defaults: "info",
      values: ["debug", "info", "warn", "error"]
    },
    {
      name: "format", 
      description: "Format to build",
      defaults: "website",
      values: ["website", "json", "ebook"]
    },
    {
      name: "timing",
      description: "Print timing debug information",
      defaults: false
    }
  ],
  exec: async function(args, kwargs) {
    // Command implementation
  }
}