platform-utils
v1.2.0
Published
Helper functions for working with platform names in the Node.js vernacular (darwin|win32|linux). Works in Node.js and browsers.
Downloads
658
Readme
platform-utils
Helper functions for working with platform names in the Node.js vernacular (darwin, win32, linux). Works in Node.js and browsers.
Plays well with the Node.js core modules
os.platform()
and
process.platform
.
Installation
npm install platform-utils --save
Usage
This module exports an object with utility functions.
const {
getPlatformFromFilename,
getPlatformFromUserAgent,
getPlatformLabel
} = require('platform-utils')
API
getPlatformFromFilename(filename)
filename
String - e.g.dist/my-cool-app.dmg
Returns String darwin
, win32
, linux
, or null.
Here's the gist of how the detection works:
if (ext === 'exe') return 'win32'
if (ext === 'zip' && parts.includes('win32')) return 'win32'
if (ext === 'zip' && parts.includes('windows')) return 'win32'
if (ext === 'zip' && parts.includes('win')) return 'win32'
if (ext === 'zip' && parts.includes('ia32')) return 'win32'
if (ext === 'dmg') return 'darwin'
if (ext === 'pkg') return 'darwin'
if (ext === 'zip' && parts.includes('osx')) return 'darwin'
if (ext === 'zip' && parts.includes('mac')) return 'darwin'
if (ext === 'zip' && parts.includes('macos')) return 'darwin'
if (ext === 'zip' && parts.includes('mas')) return 'darwin'
if (ext === 'zip' && parts.includes('darwin')) return 'darwin'
if (ext === 'rpm') return 'linux'
if (ext === 'deb') return 'linux'
if (ext === 'appimage') return 'linux'
if (ext === 'zip' && parts.includes('linux')) return 'linux'
getPlatformFromUserAgent([agent])
agent
String (optional) - Can be omitted when used in the browser, wherenavigator.userAgent
will be used automatically.
Returns String darwin
, win32
, linux
, or null.
getPlatformLabel(platform)
platform
String - Can bedarwin
,win32
, orlinux
.
Returns String macOS
, Windows
, Linux
, or null.
Tests
npm install
npm test
Dependencies
- ua-parser-js: Lightweight JavaScript-based user-agent string parser
Dev Dependencies
- buble: The blazing fast, batteries-included ES2015 compiler
- chai: BDD/TDD assertion library for node.js and the browser. Test framework agnostic.
- mocha: simple, flexible, fun test framework
- random-useragent: Get a random useragent (with an optional filter)
- standard: JavaScript Standard Style
- standard-markdown: Test your Markdown files for Standard JavaScript Style™
License
MIT