Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | 'use strict'; |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 2 | const shebangRegex = require('shebang-regex'); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 3 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 4 | module.exports = (string = '') => { |
| 5 | const match = string.match(shebangRegex); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 6 | |
| 7 | if (!match) { |
| 8 | return null; |
| 9 | } |
| 10 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 11 | const [path, argument] = match[0].replace(/#! ?/, '').split(' '); |
| 12 | const binary = path.split('/').pop(); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 13 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 14 | if (binary === 'env') { |
| 15 | return argument; |
| 16 | } |
| 17 | |
| 18 | return argument ? `${binary} ${argument}` : binary; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 19 | }; |