detectOs.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var isBrowser = require('./isBrowser');
  2. var isNode = require('./isNode');
  3. exports = function(ua) {
  4. if (!ua && isBrowser) {
  5. ua = navigator.userAgent;
  6. }
  7. function detect(keyword) {
  8. return ua.indexOf(keyword) > -1;
  9. }
  10. if (ua) {
  11. ua = ua.toLowerCase();
  12. if (detect('windows phone')) return 'windows phone';
  13. if (detect('win')) return 'windows';
  14. if (detect('android')) return 'android';
  15. if (detect('ipad') || detect('iphone') || detect('ipod')) return 'ios';
  16. if (detect('mac')) return 'os x';
  17. if (detect('linux')) return 'linux';
  18. } else if (isNode) {
  19. var _process = process,
  20. platform = _process.platform,
  21. env = _process.env;
  22. if (
  23. platform === 'win32' ||
  24. env.OSTYPE === 'cygwin' ||
  25. env.OSTYPE === 'msys'
  26. ) {
  27. return 'windows';
  28. }
  29. if (platform === 'darwin') {
  30. return 'os x';
  31. }
  32. if (platform === 'linux') {
  33. return 'linux';
  34. }
  35. }
  36. return 'unknown';
  37. };
  38. module.exports = exports;