Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for env -S = env --split-string, to parse multiple arguments #8

Open
milahu opened this issue Aug 12, 2023 · 0 comments
Open

Comments

@milahu
Copy link

milahu commented Aug 12, 2023

actual

> shebangCommand('#!/usr/bin/env -S node -v');
'-S'

expected

> shebangCommand('#!/usr/bin/env -S node -v');
['node', '-v']

man env

       -S, --split-string=S
              process and split S into separate arguments; used to pass multiple arguments on shebang lines

currently, these shebang parsers fail

related issues

quickfix

function parseShebang(fileText) {
  // based on https://github.com/pnpm/cmd-shim
  // see also https://github.com/npm/cmd-shim
  // examples:
  // "#!/bin/sh" -> ["/bin/sh", ""]
  // "#! /usr/bin/bash a b c" -> ["/usr/bin/bash", " a b c"]
  // "#! /usr/bin/env -S bash a b c" -> ["bash", " a b c"]
  // "#! /usr/bin/env -Sbash a b c" -> ["bash", " a b c"]
  const shebangExpr = /^#!\s*(?:\/usr\/bin\/env\s+(?:-S)?)?\s*(\S+)(.*)$/;
  let firstLineEnd = fileText.indexOf('\n');
  if (firstLineEnd == -1) firstLineEnd = fileText.length;
  const firstLine = fileText.slice(0, firstLineEnd).trimRight();
  const shebang = firstLine.match(shebangExpr);
  if (!shebang) return null;
  const [_, arg0, args] = shebang;
  return [arg0, args];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant