From 1a88d5833574bebdfd4bfe63b76ed4fe7939d222 Mon Sep 17 00:00:00 2001 From: milahu Date: Thu, 24 Nov 2022 09:37:03 +0100 Subject: [PATCH 1/2] fix shebangExpr: ignore -S argument for /usr/bin/env --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index ec8dcaf..f7436b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -104,7 +104,7 @@ import {promisify} from 'util' import path = require('path') import isWindows = require('is-windows') import CMD_EXTENSION = require('cmd-extension') -const shebangExpr = /^#!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+)(.*)$/ +const shebangExpr = /^#!\s*(?:\/usr\/bin\/env(?:\s+-S\s*)?)?\s*([^ \t]+)(.*)$/ const DEFAULT_OPTIONS = { // Create PowerShell file by default if the option hasn't been specified createPwshFile: true, From b9f4c9a694be2f4ad12c47c45e97bf99f29900ce Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Tue, 13 Dec 2022 03:07:53 +0200 Subject: [PATCH 2/2] add tests --- test/__snapshots__/test.js.snap | 59 +++++++++++++++++++++++++++++++++ test/setup.js | 3 +- test/test.js | 12 +++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap index 4ae2170..f69b8e0 100644 --- a/test/__snapshots__/test.js.snap +++ b/test/__snapshots__/test.js.snap @@ -735,3 +735,62 @@ if ($MyInvocation.ExpectingInput) { exit $LASTEXITCODE " `; + +exports[`shebang with -S from.env.S.shim 1`] = ` +"#!/bin/sh +basedir=$(dirname \\"$(echo \\"$0\\" | sed -e 's,\\\\\\\\,/,g')\\") + +case \`uname\` in + *CYGWIN*) basedir=\`cygpath -w \\"$basedir\\"\`;; +esac + +if [ -x \\"$basedir/node\\" ]; then + exec \\"$basedir/node\\" --expose_gc \\"$basedir/from.env.S\\" \\"$@\\" +else + exec node --expose_gc \\"$basedir/from.env.S\\" \\"$@\\" +fi +" +`; + +exports[`shebang with -S from.env.S.shim.cmd 1`] = ` +"@SETLOCAL +@IF EXIST \\"%~dp0\\\\node.exe\\" ( + \\"%~dp0\\\\node.exe\\" --expose_gc \\"%~dp0\\\\from.env.S\\" %* +) ELSE ( + @SET PATHEXT=%PATHEXT:;.JS;=;% + node --expose_gc \\"%~dp0\\\\from.env.S\\" %* +) +" +`; + +exports[`shebang with -S from.env.S.shim.ps1 1`] = ` +"#!/usr/bin/env pwsh +$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent + +$exe=\\"\\" +if ($PSVersionTable.PSVersion -lt \\"6.0\\" -or $IsWindows) { + # Fix case when both the Windows and Linux builds of Node + # are installed in the same directory + $exe=\\".exe\\" +} +$ret=0 +if (Test-Path \\"$basedir/node$exe\\") { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & \\"$basedir/node$exe\\" --expose_gc \\"$basedir/from.env.S\\" $args + } else { + & \\"$basedir/node$exe\\" --expose_gc \\"$basedir/from.env.S\\" $args + } + $ret=$LASTEXITCODE +} else { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & \\"node$exe\\" --expose_gc \\"$basedir/from.env.S\\" $args + } else { + & \\"node$exe\\" --expose_gc \\"$basedir/from.env.S\\" $args + } + $ret=$LASTEXITCODE +} +exit $ret +" +`; diff --git a/test/setup.js b/test/setup.js index 0956545..00f460f 100644 --- a/test/setup.js +++ b/test/setup.js @@ -23,7 +23,8 @@ const fixtureFiles = { 'src.env': '#!/usr/bin/env node\nconsole.log(/hi/)\n', 'src.env.args': '#!/usr/bin/env node --expose_gc\ngc()\n', 'src.sh': '#!/usr/bin/sh\necho hi\n', - 'src.sh.args': '#!/usr/bin/sh -x\necho hi\n' + 'src.sh.args': '#!/usr/bin/sh -x\necho hi\n', + 'from.env.S': '#!/usr/bin/env -S node --expose_gc\ngc()\n' }, [fixtures2]: { 'src.sh.args': '#!/usr/bin/sh -x\necho hi\n' diff --git a/test/test.js b/test/test.js index 539b1ac..d441d9d 100755 --- a/test/test.js +++ b/test/test.js @@ -178,3 +178,15 @@ testOnWindows('explicit shebang with args, linking to another drive on Windows', testFile(`${to}.cmd`, '\r\n') testFile(`${to}.ps1`) }) + +describe('shebang with -S', () => { + const src = path.resolve(fixtures, 'from.env.S') + const to = path.resolve(fixtures, 'from.env.S.shim') + beforeAll(() => { + return cmdShim(src, to, { createCmdFile: true, fs }) + }) + + testFile(to) + testFile(`${to}.cmd`, '\r\n') + testFile(`${to}.ps1`) +})