cssesc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const cssesc = require('../cssesc.js');
  4. const strings = process.argv.splice(2);
  5. const stdin = process.stdin;
  6. const options = {};
  7. const log = console.log;
  8. const main = function() {
  9. const option = strings[0];
  10. if (/^(?:-h|--help|undefined)$/.test(option)) {
  11. log(
  12. 'cssesc v%s - https://mths.be/cssesc',
  13. cssesc.version
  14. );
  15. log([
  16. '\nUsage:\n',
  17. '\tcssesc [string]',
  18. '\tcssesc [-i | --identifier] [string]',
  19. '\tcssesc [-s | --single-quotes] [string]',
  20. '\tcssesc [-d | --double-quotes] [string]',
  21. '\tcssesc [-w | --wrap] [string]',
  22. '\tcssesc [-e | --escape-everything] [string]',
  23. '\tcssesc [-v | --version]',
  24. '\tcssesc [-h | --help]',
  25. '\nExamples:\n',
  26. '\tcssesc \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  27. '\tcssesc --identifier \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  28. '\tcssesc --escape-everything \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  29. '\tcssesc --double-quotes --wrap \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\'',
  30. '\techo \'f\xF6o \u2665 b\xE5r \uD834\uDF06 baz\' | cssesc'
  31. ].join('\n'));
  32. return process.exit(1);
  33. }
  34. if (/^(?:-v|--version)$/.test(option)) {
  35. log('v%s', cssesc.version);
  36. return process.exit(1);
  37. }
  38. strings.forEach(function(string) {
  39. // Process options
  40. if (/^(?:-i|--identifier)$/.test(string)) {
  41. options.isIdentifier = true;
  42. return;
  43. }
  44. if (/^(?:-s|--single-quotes)$/.test(string)) {
  45. options.quotes = 'single';
  46. return;
  47. }
  48. if (/^(?:-d|--double-quotes)$/.test(string)) {
  49. options.quotes = 'double';
  50. return;
  51. }
  52. if (/^(?:-w|--wrap)$/.test(string)) {
  53. options.wrap = true;
  54. return;
  55. }
  56. if (/^(?:-e|--escape-everything)$/.test(string)) {
  57. options.escapeEverything = true;
  58. return;
  59. }
  60. // Process string(s)
  61. let result;
  62. try {
  63. result = cssesc(string, options);
  64. log(result);
  65. } catch (exception) {
  66. log(exception.message + '\n');
  67. log('Error: failed to escape.');
  68. log('If you think this is a bug in cssesc, please report it:');
  69. log('https://github.com/mathiasbynens/cssesc/issues/new');
  70. log(
  71. '\nStack trace using cssesc@%s:\n',
  72. cssesc.version
  73. );
  74. log(exception.stack);
  75. return process.exit(1);
  76. }
  77. });
  78. // Return with exit status 0 outside of the `forEach` loop, in case
  79. // multiple strings were passed in.
  80. return process.exit(0);
  81. };
  82. if (stdin.isTTY) {
  83. // handle shell arguments
  84. main();
  85. } else {
  86. let timeout;
  87. // Either the script is called from within a non-TTY context, or `stdin`
  88. // content is being piped in.
  89. if (!process.stdout.isTTY) {
  90. // The script was called from a non-TTY context. This is a rather uncommon
  91. // use case we don’t actively support. However, we don’t want the script
  92. // to wait forever in such cases, so…
  93. timeout = setTimeout(function() {
  94. // …if no piped data arrived after a whole minute, handle shell
  95. // arguments instead.
  96. main();
  97. }, 60000);
  98. }
  99. let data = '';
  100. stdin.on('data', function(chunk) {
  101. clearTimeout(timeout);
  102. data += chunk;
  103. });
  104. stdin.on('end', function() {
  105. strings.push(data.trim());
  106. main();
  107. });
  108. stdin.resume();
  109. }