cli.js 794 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var fs = require('fs');
  4. var pkg = require('./package.json');
  5. var stripBom = require('./');
  6. var argv = process.argv.slice(2);
  7. var input = argv[0];
  8. function help() {
  9. console.log([
  10. '',
  11. ' ' + pkg.description,
  12. '',
  13. ' Usage',
  14. ' strip-bom <file> > <new-file>',
  15. ' cat <file> | strip-bom > <new-file>',
  16. '',
  17. ' Example',
  18. ' strip-bom unicorn.txt > unicorn-without-bom.txt'
  19. ].join('\n'));
  20. }
  21. if (argv.indexOf('--help') !== -1) {
  22. help();
  23. return;
  24. }
  25. if (argv.indexOf('--version') !== -1) {
  26. console.log(pkg.version);
  27. return;
  28. }
  29. if (process.stdin.isTTY) {
  30. if (!input) {
  31. help();
  32. return;
  33. }
  34. fs.createReadStream(input).pipe(stripBom.stream()).pipe(process.stdout);
  35. } else {
  36. process.stdin.pipe(stripBom.stream()).pipe(process.stdout);
  37. }