summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/escape-exec-path.js
blob: 42b64934867dd1cdee34b05dfd6efb96952b521c (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 isWindows = require('./is-windows.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 (isWindows) {
    return path.normalize(str).split(/\\/).map(windowsQuotes).join('\\')
  } else if (/[^-_.~/\w]/.test(str)) {
    return "'" + str.replace(/'/g, "'\"'\"'") + "'"
  } else {
    return str
  }
}