cli.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env node
  2. 'use strict';
  3. process.title = 'mime';
  4. let mime = require('.');
  5. let pkg = require('./package.json');
  6. let args = process.argv.splice(2);
  7. if (args.includes('--version') || args.includes('-v') || args.includes('--v')) {
  8. console.log(pkg.version);
  9. process.exit(0);
  10. } else if (args.includes('--name') || args.includes('-n') || args.includes('--n')) {
  11. console.log(pkg.name);
  12. process.exit(0);
  13. } else if (args.includes('--help') || args.includes('-h') || args.includes('--h')) {
  14. console.log(pkg.name + ' - ' + pkg.description + '\n');
  15. console.log(`Usage:
  16. mime [flags] [path_or_extension]
  17. Flags:
  18. --help, -h Show this message
  19. --version, -v Display the version
  20. --name, -n Print the name of the program
  21. Note: the command will exit after it executes if a command is specified
  22. The path_or_extension is the path to the file or the extension of the file.
  23. Examples:
  24. mime --help
  25. mime --version
  26. mime --name
  27. mime -v
  28. mime src/log.js
  29. mime new.py
  30. mime foo.sh
  31. `);
  32. process.exit(0);
  33. }
  34. let file = args[0];
  35. let type = mime.getType(file);
  36. process.stdout.write(type + '\n');