├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── index.js ├── license ├── 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 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.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 | 6 | const linux = () => linuxBattery().then(res => res[0].state === 'charging'); 7 | const osx = () => osxBattery().then(res => res.externalConnected); 8 | 9 | const win = () => execa.stdout('WMIC', ['Path', 'Win32_Battery', 'Get', 'BatteryStatus']).then(stdout => { 10 | if (!stdout) { 11 | return Promise.reject(new Error('No battery could be found')); 12 | } 13 | 14 | return stdout.includes('2'); 15 | }); 16 | 17 | if (process.platform === 'darwin') { 18 | module.exports = osx; 19 | } else if (process.platform === 'linux') { 20 | module.exports = linux; 21 | } else { 22 | module.exports = win; 23 | } 24 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Andreas Strandman (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 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-charging", 3 | "version": "2.0.0", 4 | "description": "Find out if a computer is charging", 5 | "license": "MIT", 6 | "repository": "gillstrom/is-charging", 7 | "author": { 8 | "name": "Andreas Gillström", 9 | "email": "andreasgillstrom@gmail.com", 10 | "url": "github.com/gillstrom" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "battery", 23 | "charge", 24 | "charging", 25 | "indication", 26 | "is", 27 | "linux", 28 | "mac", 29 | "osx", 30 | "utility", 31 | "win", 32 | "windows" 33 | ], 34 | "dependencies": { 35 | "execa": "^0.8.0", 36 | "linux-battery": "^3.0.1", 37 | "osx-battery": "^4.0.0" 38 | }, 39 | "devDependencies": { 40 | "ava": "*", 41 | "xo": "*" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-charging 2 | 3 | > Find out if a computer is charging 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install is-charging 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const isCharging = require('is-charging'); 17 | 18 | isCharging().then(result => { 19 | console.log(result); 20 | //=> true 21 | }); 22 | ``` 23 | 24 | 25 | ## API 26 | 27 | ### isCharging() 28 | 29 | Returns a promise that resolves in to a `Boolean`. 30 | 31 | 32 | ## Related 33 | 34 | * [is-charging-cli](https://github.com/gillstrom/is-charging-cli) - CLI for this module 35 | * [battery-level](https://github.com/gillstrom/battery-level) - Get current battery level 36 | * [browser-battery](https://github.com/gillstrom/browser-battery) - Get and watch battery information in a browser 37 | 38 | 39 | ## License 40 | 41 | MIT © [Andreas Strandman](https://github.com/gillstrom) 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | test(async t => { 5 | t.is(typeof await m(), 'boolean'); 6 | }); 7 | --------------------------------------------------------------------------------