Regular expression for matching Unix shebang lines at the beginning of files
88
Build a utility that analyzes script files to detect and categorize them by their interpreter types.
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.
The analyzer should:
/bin/bash) and env-based paths (e.g., /usr/bin/env node)Your implementation should export two functions:
hasShebang(scriptContent: string): boolean - Returns true if the script has a valid shebang line at the beginninganalyzeScript(scriptContent: string): ScriptInfo | null - Returns script information or null if no shebangThe 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"
}#!/usr/bin/env node\nconsole.log('hello'); should be detected as having a shebang @test#!/bin/bash\necho "hello" should be detected as having a shebang @testconsole.log('hello'); (no shebang) should return false for hasShebang @test#!/usr/bin/env python3\nprint("hello") should return interpreter type "python" @test#!/usr/bin/perl\nprint "hello\\n"; should return interpreter type "perl" @testTo determine the interpreter type from the interpreter path:
/bin/bash and env-based paths like /usr/bin/env node@generates
/**
* 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
};Provides regular expression for detecting and parsing shebang lines in scripts.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-shebang-regexdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9