1
- 'use strict'
2
- var path = require ( 'path' )
3
- var isWindows = require ( './is-windows.js' )
1
+ const { normalize } = require ( 'path' )
2
+ const isWindows = require ( './is-windows.js' )
4
3
5
4
/*
6
5
Escape the name of an executable suitable for passing to the system shell.
@@ -9,22 +8,14 @@ Windows is easy, wrap in double quotes and you're done, as there's no
9
8
facility to create files with quotes in their names.
10
9
11
10
Unix-likes are a little more complicated, wrap in single quotes and escape
12
- any single quotes in the filename.
11
+ any single quotes in the filename. The '"'"' construction ends the quoted
12
+ block, creates a new " quoted string with ' in it. So, `foo'bar` becomes
13
+ `'foo'"'"'bar'`, which is the bash way of saying `'foo' + "'" + 'bar'`.
13
14
*/
14
15
15
- module . exports = escapify
16
+ const winQuote = str => ! / / . test ( str ) ? str : '"' + str + '"'
17
+ const winEsc = str => normalize ( str ) . split ( / \\ / ) . map ( winQuote ) . join ( '\\' )
16
18
17
- function windowsQuotes ( str ) {
18
- if ( ! / / . test ( str ) ) return str
19
- return '"' + str + '"'
20
- }
21
-
22
- function escapify ( str ) {
23
- if ( isWindows ) {
24
- return path . normalize ( str ) . split ( / \\ / ) . map ( windowsQuotes ) . join ( '\\' )
25
- } else if ( / [ ^ - _ . ~ / \w ] / . test ( str ) ) {
26
- return "'" + str . replace ( / ' / g, "'\"'\"'" ) + "'"
27
- } else {
28
- return str
29
- }
30
- }
19
+ module . exports = str => isWindows ? winEsc ( str )
20
+ : / [ ^ - _ . ~ / \w ] / . test ( str ) ? "'" + str . replace ( / ' / g, `'"'"'` ) + "'"
21
+ : str
0 commit comments