blob: f35db30851c777b62df472eae9fb8e1e658b2095 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001'use strict';
Tim van der Lippe16aca392020-11-13 11:37:13 +00002const shebangRegex = require('shebang-regex');
Yang Guo4fd355c2019-09-19 10:59:03 +02003
Tim van der Lippe16aca392020-11-13 11:37:13 +00004module.exports = (string = '') => {
5 const match = string.match(shebangRegex);
Yang Guo4fd355c2019-09-19 10:59:03 +02006
7 if (!match) {
8 return null;
9 }
10
Tim van der Lippe16aca392020-11-13 11:37:13 +000011 const [path, argument] = match[0].replace(/#! ?/, '').split(' ');
12 const binary = path.split('/').pop();
Yang Guo4fd355c2019-09-19 10:59:03 +020013
Tim van der Lippe16aca392020-11-13 11:37:13 +000014 if (binary === 'env') {
15 return argument;
16 }
17
18 return argument ? `${binary} ${argument}` : binary;
Yang Guo4fd355c2019-09-19 10:59:03 +020019};