File tree Expand file tree Collapse file tree 2 files changed +13
-14
lines changed Expand file tree Collapse file tree 2 files changed +13
-14
lines changed Original file line number Diff line number Diff line change @@ -21,11 +21,14 @@ describe('analyzeShebang', () => {
21
21
22
22
test . each ( [
23
23
[ '#!/bin/sh -' , 'sh' ] ,
24
- [ '#!/usr/bin/env bash' , 'bash' ] ,
25
24
[ '#!/bin/sh' , 'sh' ] ,
25
+ [ '#!/bin/env sh' , 'sh' ] ,
26
+ [ '#!/usr/bin/env bash' , 'bash' ] ,
27
+ [ '#!/bin/env bash' , 'bash' ] ,
26
28
[ '#!/bin/bash' , 'bash' ] ,
27
29
[ '#!/bin/bash -u' , 'bash' ] ,
28
30
[ '#! /bin/bash' , 'bash' ] ,
31
+ [ '#! /bin/dash' , 'dash' ] ,
29
32
[ '#!/usr/bin/bash' , 'bash' ] ,
30
33
] ) ( 'returns a bash dialect for %p' , ( command , expectedDialect ) => {
31
34
expect ( analyzeShebang ( command ) . shellDialect ) . toBe ( expectedDialect )
Original file line number Diff line number Diff line change 1
1
const SHEBANG_REGEXP = / ^ # ! ( .* ) /
2
+ const SHELL_REGEXP = / b i n [ / ] (?: e n v ) ? ( \w + ) /
2
3
3
- // TODO: at some point we could let this return all known shells (like dash, ksh, etc)
4
- // and make the call side decide what to support.
5
- type SupportedBashDialect = 'bash' | 'sh'
4
+ const BASH_DIALECTS = [ 'sh' , 'bash' , 'dash' , 'ksh' ] as const
5
+ type SupportedBashDialect = typeof BASH_DIALECTS [ number ]
6
6
7
7
export function getShebang ( fileContent : string ) : string | null {
8
8
const match = SHEBANG_REGEXP . exec ( fileContent )
@@ -14,16 +14,12 @@ export function getShebang(fileContent: string): string | null {
14
14
}
15
15
16
16
export function getShellDialect ( shebang : string ) : SupportedBashDialect | null {
17
- if ( shebang . startsWith ( '/bin/sh' ) || shebang . startsWith ( '/usr/bin/env sh' ) ) {
18
- return 'sh'
19
- }
20
-
21
- if (
22
- shebang . startsWith ( '/bin/bash' ) ||
23
- shebang . startsWith ( '/usr/bin/bash' ) ||
24
- shebang . startsWith ( '/usr/bin/env bash' )
25
- ) {
26
- return 'bash'
17
+ const match = SHELL_REGEXP . exec ( shebang )
18
+ if ( match && match [ 1 ] ) {
19
+ const bashDialect = match [ 1 ] . trim ( ) as any
20
+ if ( BASH_DIALECTS . includes ( bashDialect ) ) {
21
+ return bashDialect
22
+ }
27
23
}
28
24
29
25
return null
You can’t perform that action at this time.
0 commit comments