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-8/

Script Interpreter Analyzer

Build a utility that analyzes script files to extract and parse their interpreter directives (shebang lines). The tool should handle shebangs with complex interpreter specifications that include arguments and flags.

Requirements

Your solution should provide a function analyzeScriptInterpreter that:

  1. Takes a string containing script content as input
  2. Returns an object with the following properties:
    • hasShebang (boolean): whether the script has a valid shebang line at the beginning
    • fullShebang (string | null): the complete shebang line including #!, or null if no shebang
    • interpreterPath (string | null): the full interpreter directive (without #!), or null if no shebang
    • interpreterCommand (string | null): just the interpreter executable path (first part before any spaces), or null if no shebang
    • interpreterArgs (string[]): array of arguments passed to the interpreter, or empty array if none or no shebang

The function should correctly handle:

  • Simple interpreter paths like #!/bin/bash
  • Environment-based interpreters like #!/usr/bin/env node
  • Complex directives with multiple arguments like #!/usr/bin/env python3 -u -W ignore
  • Scripts without shebangs
  • Shebangs that appear anywhere other than the start of the file (these should be treated as no shebang)

Test Cases

  • Given script #!/usr/bin/env node\nconsole.log("hello");, the function returns an object with hasShebang: true, fullShebang: "#!/usr/bin/env node", interpreterPath: "/usr/bin/env node", interpreterCommand: "/usr/bin/env", and interpreterArgs: ["node"] @test

  • Given script #!/usr/bin/python3 -u -W ignore\nprint("test"), the function returns an object with hasShebang: true, interpreterPath: "/usr/bin/python3 -u -W ignore", interpreterCommand: "/usr/bin/python3", and interpreterArgs: ["-u", "-W", "ignore"] @test

  • Given script console.log("no shebang"); (no shebang), the function returns an object with hasShebang: false and all other properties as null or empty array @test

  • Given script \n#!/bin/bash\necho "test" (shebang not at start), the function returns an object with hasShebang: false since the shebang must be at the very beginning @test

Implementation

@generates

API

/**
 * Analyzes a script to extract interpreter information from its shebang line.
 *
 * @param {string} scriptContent - The content of the script file
 * @returns {Object} An object containing:
 *   - hasShebang: boolean indicating if a valid shebang exists at the start
 *   - fullShebang: the complete shebang line including #!, or null
 *   - interpreterPath: the interpreter directive without #!, or null
 *   - interpreterCommand: just the executable path (before first space), or null
 *   - interpreterArgs: array of arguments passed to the interpreter
 */
function analyzeScriptInterpreter(scriptContent) {
  // IMPLEMENTATION HERE
}

module.exports = { analyzeScriptInterpreter };

Dependencies { .dependencies }

shebang-regex { .dependency }

Provides regular expression for matching and extracting shebang lines from scripts.

Install with Tessl CLI

npx tessl i tessl/npm-shebang-regex

tile.json