cssesc 3.2 KB

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