├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── index.js ├── license ├── media └── logo.png ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const execa = require('execa'); 3 | const linuxBattery = require('linux-battery'); 4 | const osxBattery = require('osx-battery'); 5 | const toDecimal = require('to-decimal'); 6 | 7 | const linux = async () => { 8 | const [battery] = await linuxBattery(); 9 | const {percentage} = battery; 10 | 11 | return toDecimal(parseFloat(percentage.slice(0, percentage.length))); 12 | }; 13 | 14 | const osx = async () => { 15 | const {currentCapacity, maxCapacity} = await osxBattery(); 16 | return parseFloat((currentCapacity / maxCapacity).toFixed(2)); 17 | }; 18 | 19 | const win = async () => { 20 | const {stdout} = await execa('WMIC', ['Path', 'Win32_Battery', 'Get', 'EstimatedChargeRemaining']); 21 | const level = parseFloat(stdout.split('\n')[1]); 22 | 23 | return toDecimal(level > 100 ? 100 : level); 24 | }; 25 | 26 | if (process.platform === 'darwin') { 27 | module.exports = osx; 28 | } else if (process.platform === 'linux') { 29 | module.exports = linux; 30 | } else { 31 | module.exports = win; 32 | } 33 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Andreas Gillström (github.com/gillstrom) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /media/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gillstrom/battery-level/6776119ff09fa1998b362e08833f819d0dad70c4/media/logo.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "battery-level", 3 | "version": "3.0.0", 4 | "description": "Get current battery level", 5 | "license": "MIT", 6 | "repository": "gillstrom/battery-level", 7 | "author": { 8 | "email": "andreasgillstrom@gmail.com", 9 | "name": "Andreas Gillström", 10 | "url": "github.com/gillstrom" 11 | }, 12 | "engines": { 13 | "node": ">=10" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "app", 23 | "battery", 24 | "cli", 25 | "cli-app", 26 | "level", 27 | "linux", 28 | "mac", 29 | "os x", 30 | "osx", 31 | "windows" 32 | ], 33 | "dependencies": { 34 | "execa": "^2.0.4", 35 | "linux-battery": "^3.0.1", 36 | "osx-battery": "^4.0.0", 37 | "to-decimal": "^2.0.0" 38 | }, 39 | "devDependencies": { 40 | "ava": "^2.4.0", 41 | "xo": "^0.25.3" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

2 |
3 | battery-level 4 |
5 |
6 |

7 | 8 | > Get current battery level 9 | 10 | 11 | ## Install 12 | 13 | ``` 14 | $ npm install battery-level 15 | ``` 16 | 17 | 18 | ## Usage 19 | 20 | ```js 21 | const batteryLevel = require('battery-level'); 22 | 23 | (async () => { 24 | console.log(await batteryLevel()); 25 | //=> 0.55 26 | })(); 27 | ``` 28 | 29 | 30 | ## API 31 | 32 | ### batteryLevel() 33 | 34 | Returns a `Promise` with the battery level. 35 | 36 | 37 | ## Related 38 | 39 | * [battery-level-cli](https://github.com/gillstrom/battery-level-cli) - CLI for this module 40 | * [browser-battery](https://github.com/gillstrom/browser-battery) - Get battery information in a browser 41 | * [is-charging](https://github.com/gillstrom/is-charging) - Find out if a computer is charging 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import batteryLevel from '.'; 3 | 4 | test('main', async t => { 5 | const level = await batteryLevel(); 6 | 7 | t.is(typeof level, 'number'); 8 | t.true(level >= 0 && level <= 1); 9 | }); 10 | --------------------------------------------------------------------------------