cli.js 823 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var fs = require('fs');
  4. var stdin = require('get-stdin');
  5. var pkg = require('./package.json');
  6. var stripIndent = require('./');
  7. var argv = process.argv.slice(2);
  8. var input = argv[0];
  9. function help() {
  10. console.log([
  11. '',
  12. ' ' + pkg.description,
  13. '',
  14. ' Usage',
  15. ' strip-indent <file>',
  16. ' echo <string> | strip-indent',
  17. '',
  18. ' Example',
  19. ' echo \'\\tunicorn\\n\\t\\tcake\' | strip-indent',
  20. ' unicorn',
  21. ' \tcake'
  22. ].join('\n'));
  23. }
  24. function init(data) {
  25. console.log(stripIndent(data));
  26. }
  27. if (argv.indexOf('--help') !== -1) {
  28. help();
  29. return;
  30. }
  31. if (argv.indexOf('--version') !== -1) {
  32. console.log(pkg.version);
  33. return;
  34. }
  35. if (process.stdin.isTTY) {
  36. if (!input) {
  37. help();
  38. return;
  39. }
  40. init(fs.readFileSync(input, 'utf8'));
  41. } else {
  42. stdin(init);
  43. }