cli.js 866 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var pkg = require('./package.json');
  4. var hasAnsi = require('./');
  5. var input = process.argv[2];
  6. function stdin(cb) {
  7. var ret = '';
  8. process.stdin.setEncoding('utf8');
  9. process.stdin.on('data', function (data) {
  10. ret += data;
  11. });
  12. process.stdin.on('end', function () {
  13. cb(ret);
  14. });
  15. }
  16. function help() {
  17. console.log([
  18. pkg.description,
  19. '',
  20. 'Usage',
  21. ' $ has-ansi <string>',
  22. ' $ echo <string> | has-ansi',
  23. '',
  24. 'Exits with code 0 if input has ANSI escape codes and 1 if not'
  25. ].join('\n'));
  26. }
  27. function init(data) {
  28. process.exit(hasAnsi(data) ? 0 : 1);
  29. }
  30. if (process.argv.indexOf('--help') !== -1) {
  31. help();
  32. return;
  33. }
  34. if (process.argv.indexOf('--version') !== -1) {
  35. console.log(pkg.version);
  36. return;
  37. }
  38. if (process.stdin.isTTY) {
  39. if (!input) {
  40. help();
  41. return;
  42. }
  43. init(input);
  44. } else {
  45. stdin(init);
  46. }