escape-arg.js 657 B

123456789101112131415161718192021222324252627
  1. 'use strict'
  2. var path = require('path')
  3. var isWindows = require('./is-windows.js')
  4. /*
  5. Escape the name of an executable suitable for passing to the system shell.
  6. Windows is easy, wrap in double quotes and you're done, as there's no
  7. facility to create files with quotes in their names.
  8. Unix-likes are a little more complicated, wrap in single quotes and escape
  9. any single quotes in the filename.
  10. */
  11. module.exports = escapify
  12. function escapify (str) {
  13. if (isWindows) {
  14. return '"' + path.normalize(str) + '"'
  15. } else {
  16. if (/[^-_.~/\w]/.test(str)) {
  17. return "'" + str.replace(/'/g, "'\"'\"'") + "'"
  18. } else {
  19. return str
  20. }
  21. }
  22. }