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

Script Interpreter Analyzer

Build a utility that analyzes script files to extract and categorize their interpreter information from shebang lines.

Requirements

Your task is to implement a script analyzer with the following functionality:

  1. Parse Script Content: Accept a script file's content as a string and extract interpreter information from its shebang line.

  2. Extract Interpreter Details: From the shebang line, extract:

    • The full shebang line (including the #! prefix)
    • The interpreter path (without the #! prefix)
    • The base interpreter name (the final component of the path before any arguments)
  3. Categorize Scripts: Determine the script type based on the interpreter. Support these categories:

    • node - for Node.js scripts
    • python - for Python scripts (python, python2, python3)
    • shell - for shell scripts (bash, sh, zsh)
    • other - for any other interpreter
    • none - for files without a valid shebang
  4. Handle Edge Cases: Properly handle:

    • Scripts without shebang lines
    • Shebangs with interpreter arguments (e.g., #!/usr/bin/env node --experimental-modules)
    • Different path formats (direct paths like /bin/bash vs env-based like /usr/bin/env bash)

Implementation

Create a module that exports a function analyzeScript(content) which takes a script's content as a string and returns an object with the following structure:

{
  fullShebang: string | null,      // Complete shebang line or null
  interpreterPath: string | null,  // Path without #! or null
  interpreterName: string | null,  // Base interpreter name or null
  scriptType: string               // Category: 'node', 'python', 'shell', 'other', 'none'
}

Test Cases { @test }

Test File: analyzer.test.js

import { analyzeScript } from './analyzer.js';

// Test 1: Node.js script with env
const nodeScript = '#!/usr/bin/env node\nconsole.log("hello");';
const nodeResult = analyzeScript(nodeScript);
console.assert(nodeResult.fullShebang === '#!/usr/bin/env node');
console.assert(nodeResult.interpreterPath === '/usr/bin/env node');
console.assert(nodeResult.interpreterName === 'node');
console.assert(nodeResult.scriptType === 'node');

// Test 2: Python script with arguments
const pythonScript = '#!/usr/bin/env python3 -u\nprint("hello")';
const pythonResult = analyzeScript(pythonScript);
console.assert(pythonResult.interpreterName === 'python3');
console.assert(pythonResult.scriptType === 'python');

// Test 3: Shell script with direct path
const shellScript = '#!/bin/bash\necho "hello"';
const shellResult = analyzeScript(shellScript);
console.assert(shellResult.fullShebang === '#!/bin/bash');
console.assert(shellResult.interpreterPath === '/bin/bash');
console.assert(shellResult.interpreterName === 'bash');
console.assert(shellResult.scriptType === 'shell');

// Test 4: No shebang
const noShebang = 'console.log("hello");';
const noShebangResult = analyzeScript(noShebang);
console.assert(noShebangResult.fullShebang === null);
console.assert(noShebangResult.interpreterPath === null);
console.assert(noShebangResult.interpreterName === null);
console.assert(noShebangResult.scriptType === 'none');

console.log('All tests passed!');

Dependencies { .dependencies }

shebang-regex { .dependency }

Provides regular expression matching for Unix shebang lines.

Install with Tessl CLI

npx tessl i tessl/npm-shebang-regex

tile.json