jsesc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env node
  2. (function() {
  3. var fs = require('fs');
  4. var stringEscape = require('../jsesc.js');
  5. var strings = process.argv.splice(2);
  6. var stdin = process.stdin;
  7. var data;
  8. var timeout;
  9. var isObject = false;
  10. var options = {};
  11. var log = console.log;
  12. var main = function() {
  13. var option = strings[0];
  14. if (/^(?:-h|--help|undefined)$/.test(option)) {
  15. log(
  16. 'jsesc v%s - https://mths.be/jsesc',
  17. stringEscape.version
  18. );
  19. log([
  20. '\nUsage:\n',
  21. '\tjsesc [string]',
  22. '\tjsesc [-s | --single-quotes] [string]',
  23. '\tjsesc [-d | --double-quotes] [string]',
  24. '\tjsesc [-w | --wrap] [string]',
  25. '\tjsesc [-e | --escape-everything] [string]',
  26. '\tjsesc [-t | --escape-etago] [string]',
  27. '\tjsesc [-6 | --es6] [string]',
  28. '\tjsesc [-l | --lowercase-hex] [string]',
  29. '\tjsesc [-j | --json] [string]',
  30. '\tjsesc [-o | --object] [stringified_object]', // `JSON.parse()` the argument
  31. '\tjsesc [-p | --pretty] [string]', // `compact: false`
  32. '\tjsesc [-v | --version]',
  33. '\tjsesc [-h | --help]',
  34. '\nExamples:\n',
  35. '\tjsesc \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  36. '\tjsesc --json \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  37. '\tjsesc --json --escape-everything \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  38. '\tjsesc --double-quotes --wrap \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  39. '\techo \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\' | jsesc'
  40. ].join('\n'));
  41. return process.exit(1);
  42. }
  43. if (/^(?:-v|--version)$/.test(option)) {
  44. log('v%s', stringEscape.version);
  45. return process.exit(1);
  46. }
  47. strings.forEach(function(string) {
  48. // Process options
  49. if (/^(?:-s|--single-quotes)$/.test(string)) {
  50. options.quotes = 'single';
  51. return;
  52. }
  53. if (/^(?:-d|--double-quotes)$/.test(string)) {
  54. options.quotes = 'double';
  55. return;
  56. }
  57. if (/^(?:-w|--wrap)$/.test(string)) {
  58. options.wrap = true;
  59. return;
  60. }
  61. if (/^(?:-e|--escape-everything)$/.test(string)) {
  62. options.escapeEverything = true;
  63. return;
  64. }
  65. if (/^(?:-t|--escape-etago)$/.test(string)) {
  66. options.escapeEtago = true;
  67. return;
  68. }
  69. if (/^(?:-6|--es6)$/.test(string)) {
  70. options.es6 = true;
  71. return;
  72. }
  73. if (/^(?:-l|--lowercase-hex)$/.test(string)) {
  74. options.lowercaseHex = true;
  75. return;
  76. }
  77. if (/^(?:-j|--json)$/.test(string)) {
  78. options.json = true;
  79. return;
  80. }
  81. if (/^(?:-o|--object)$/.test(string)) {
  82. isObject = true;
  83. return;
  84. }
  85. if (/^(?:-p|--pretty)$/.test(string)) {
  86. isObject = true;
  87. options.compact = false;
  88. return;
  89. }
  90. // Process string(s)
  91. var result;
  92. try {
  93. if (isObject) {
  94. string = JSON.parse(string);
  95. }
  96. result = stringEscape(string, options);
  97. log(result);
  98. } catch(error) {
  99. log(error.message + '\n');
  100. log('Error: failed to escape.');
  101. log('If you think this is a bug in jsesc, please report it:');
  102. log('https://github.com/mathiasbynens/jsesc/issues/new');
  103. log(
  104. '\nStack trace using jsesc@%s:\n',
  105. stringEscape.version
  106. );
  107. log(error.stack);
  108. return process.exit(1);
  109. }
  110. });
  111. // Return with exit status 0 outside of the `forEach` loop, in case
  112. // multiple strings were passed in.
  113. return process.exit(0);
  114. };
  115. if (stdin.isTTY) {
  116. // handle shell arguments
  117. main();
  118. } else {
  119. // Either the script is called from within a non-TTY context,
  120. // or `stdin` content is being piped in.
  121. if (!process.stdout.isTTY) { // called from a non-TTY context
  122. timeout = setTimeout(function() {
  123. // if no piped data arrived after a while, handle shell arguments
  124. main();
  125. }, 250);
  126. }
  127. data = '';
  128. stdin.on('data', function(chunk) {
  129. clearTimeout(timeout);
  130. data += chunk;
  131. });
  132. stdin.on('end', function() {
  133. strings.push(data.trim());
  134. main();
  135. });
  136. stdin.resume();
  137. }
  138. }());