jsesc 3.5 KB

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