CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-shebang-regex

Regular expression for matching Unix shebang lines at the beginning of files

88

1.54x
Overview
Eval results
Files

task.mdevals/scenario-9/

Script Interpreter Analyzer

Build a utility that analyzes script files to detect and categorize them by their interpreter types.

Overview

You need to create a script analyzer that reads script files and identifies what interpreter each script uses based on its shebang line. The analyzer should support any type of interpreter (Node.js, Python, Bash, Ruby, Perl, etc.) and provide detailed information about the interpreter paths.

Requirements

Core Functionality

The analyzer should:

  1. Read a script file and determine if it has a valid shebang line
  2. Extract the complete interpreter directive from scripts
  3. Categorize scripts by their interpreter type (node, python, bash, ruby, perl, etc.)
  4. Support both direct interpreter paths (e.g., /bin/bash) and env-based paths (e.g., /usr/bin/env node)
  5. Handle scripts without shebang lines gracefully

Input/Output Specifications

Your implementation should export two functions:

  1. hasShebang(scriptContent: string): boolean - Returns true if the script has a valid shebang line at the beginning
  2. analyzeScript(scriptContent: string): ScriptInfo | null - Returns script information or null if no shebang

The ScriptInfo object should have this structure:

{
  hasShebang: boolean,
  fullShebang: string | null,          // Complete line including #!
  interpreterPath: string | null,      // Path without #!
  interpreterType: string | null       // e.g., "node", "python", "bash"
}

Test Cases

  • A script starting with #!/usr/bin/env node\nconsole.log('hello'); should be detected as having a shebang @test
  • A script starting with #!/bin/bash\necho "hello" should be detected as having a shebang @test
  • A script with content console.log('hello'); (no shebang) should return false for hasShebang @test
  • Analyzing #!/usr/bin/env python3\nprint("hello") should return interpreter type "python" @test
  • Analyzing #!/usr/bin/perl\nprint "hello\\n"; should return interpreter type "perl" @test

Interpreter Type Detection

To determine the interpreter type from the interpreter path:

  • Extract the base command name from the interpreter path
  • Handle both direct paths like /bin/bash and env-based paths like /usr/bin/env node
  • Common patterns:
    • If path contains "node" → type is "node"
    • If path contains "python" → type is "python"
    • If path contains "bash" or ends with "sh" → type is "bash"
    • If path contains "ruby" → type is "ruby"
    • If path contains "perl" → type is "perl"

Implementation

@generates

API

/**
 * Checks if a script has a valid shebang line
 * @param {string} scriptContent - The script content to check
 * @returns {boolean} True if script has shebang, false otherwise
 */
function hasShebang(scriptContent) {
  // Implementation here
}

/**
 * Analyzes a script to extract interpreter information
 * @param {string} scriptContent - The script content to analyze
 * @returns {ScriptInfo|null} Script information or null if no shebang
 */
function analyzeScript(scriptContent) {
  // Implementation here
}

module.exports = {
  hasShebang,
  analyzeScript
};

Dependencies { .dependencies }

shebang-regex { .dependency }

Provides regular expression for detecting and parsing shebang lines in scripts.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-shebang-regex

tile.json