cli.js 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var fs = require('fs');
  4. var stdin = require('get-stdin');
  5. var argv = require('minimist')(process.argv.slice(2));
  6. var pkg = require('./package.json');
  7. var detectIndent = require('./');
  8. var input = argv._[0];
  9. function help() {
  10. console.log([
  11. '',
  12. ' ' + pkg.description,
  13. '',
  14. ' Usage',
  15. ' detect-indent <file>',
  16. ' echo <string> | detect-indent',
  17. '',
  18. ' Example',
  19. ' echo \' foo\\n bar\' | detect-indent | wc --chars',
  20. ' 2'
  21. ].join('\n'));
  22. }
  23. function init(data) {
  24. var indent = detectIndent(data).indent;
  25. if (indent !== null) {
  26. process.stdout.write(indent);
  27. } else {
  28. console.error('Indentation could not be detected');
  29. process.exit(2);
  30. }
  31. }
  32. if (argv.help) {
  33. help();
  34. return;
  35. }
  36. if (argv.version) {
  37. console.log(pkg.version);
  38. return;
  39. }
  40. if (process.stdin.isTTY) {
  41. if (!input) {
  42. help();
  43. return;
  44. }
  45. init(fs.readFileSync(input, 'utf8'));
  46. } else {
  47. stdin(init);
  48. }