summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/escape-exec-path.js
blob: bf94886efa331a195b2415f377f4ecf3e0c03abb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict'
var path = require('path')
var isWindowsShell = require('./is-windows-shell.js')

/*
Escape the name of an executable suitable for passing to the system shell.

Windows is easy, wrap in double quotes and you're done, as there's no
facility to create files with quotes in their names.

Unix-likes are a little more complicated, wrap in single quotes and escape
any single quotes in the filename.
*/

module.exports = escapify

function windowsQuotes (str) {
  if (!/ /.test(str)) return str
  return '"' + str + '"'
}

function escapify (str) {
  if (isWindowsShell) {
    return path.normalize(str).split(/\\/).map(windowsQuotes).join('\\')
  } else if (/[^-_.~/\w]/.test(str)) {
    return "'" + str.replace(/'/g, "'\"'\"'") + "'"
  } else {
    return str
  }
}