├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── index.js ├── lib ├── linux.js ├── osx.js └── win.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_size = 2 12 | indent_style = space 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | if (process.platform === 'darwin') { 4 | module.exports = require('./lib/osx'); 5 | module.exports.sync = require('./lib/osx').sync; 6 | } else if (process.platform === 'win32') { 7 | module.exports = require('./lib/win'); 8 | module.exports.sync = require('./lib/win').sync; 9 | } else { 10 | module.exports = require('./lib/linux'); 11 | module.exports.sync = require('./lib/linux').sync; 12 | } 13 | -------------------------------------------------------------------------------- /lib/linux.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const execa = require('execa'); 3 | 4 | module.exports = () => { 5 | const cmd = 'iwgetid'; 6 | const args = ['--raw']; 7 | 8 | return execa.stdout(cmd, args).then(stdout => { 9 | const ret = stdout.replace('\n', ''); 10 | 11 | if (!ret) { 12 | throw new Error('Could not get SSID'); 13 | } 14 | 15 | return ret; 16 | }); 17 | }; 18 | 19 | module.exports.sync = () => { 20 | const cmd = 'iwgetid'; 21 | const args = ['--raw']; 22 | const ret = execa.sync(cmd, args).stdout.replace('\n', ''); 23 | 24 | if (!ret) { 25 | throw new Error('Could not get SSID'); 26 | } 27 | 28 | return ret; 29 | }; 30 | -------------------------------------------------------------------------------- /lib/osx.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const execa = require('execa'); 3 | 4 | module.exports = () => { 5 | const cmd = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'; 6 | const args = ['-I']; 7 | 8 | return execa.stdout(cmd, args).then(stdout => { 9 | if (stdout.includes('AirPort: Off')) { 10 | throw new Error('Wi-Fi is turned off'); 11 | } 12 | 13 | let ret; 14 | 15 | ret = /^\s*SSID: (.+)\s*$/gm.exec(stdout); 16 | ret = ret && ret.length ? ret[1] : null; 17 | 18 | if (!ret) { 19 | throw new Error('Could not get SSID'); 20 | } 21 | 22 | return ret; 23 | }); 24 | }; 25 | 26 | module.exports.sync = () => { 27 | const cmd = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'; 28 | const args = ['-I']; 29 | const stdout = execa.sync(cmd, args).stdout; 30 | 31 | if (stdout.includes('AirPort: Off')) { 32 | throw new Error('Wi-Fi is turned off'); 33 | } 34 | 35 | let ret; 36 | 37 | ret = /^\s*SSID: (.+)\s*$/gm.exec(stdout); 38 | ret = ret && ret.length ? ret[1] : null; 39 | 40 | if (!ret) { 41 | throw new Error('Could not get SSID'); 42 | } 43 | 44 | return ret; 45 | }; 46 | -------------------------------------------------------------------------------- /lib/win.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const execa = require('execa'); 3 | 4 | module.exports = () => { 5 | const cmd = 'netsh'; 6 | const args = ['wlan', 'show', 'interface']; 7 | 8 | return execa.stdout(cmd, args).then(stdout => { 9 | let ret; 10 | 11 | ret = /^\s*SSID\s*: (.+)\s*$/gm.exec(stdout); 12 | ret = ret && ret.length ? ret[1] : null; 13 | 14 | if (!ret) { 15 | throw new Error('Could not get SSID'); 16 | } 17 | 18 | return ret; 19 | }); 20 | }; 21 | 22 | module.exports.sync = () => { 23 | const cmd = 'netsh'; 24 | const args = ['wlan', 'show', 'interface']; 25 | const stdout = execa.sync(cmd, args).stdout; 26 | 27 | let ret; 28 | 29 | ret = /^\s*SSID\s*: (.+)\s*$/gm.exec(stdout); 30 | ret = ret && ret.length ? ret[1] : null; 31 | 32 | if (!ret) { 33 | throw new Error('Could not get SSID'); 34 | } 35 | 36 | return ret; 37 | }; 38 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 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": "wifi-name", 3 | "version": "3.1.1", 4 | "description": "Get current wifi name", 5 | "license": "MIT", 6 | "repository": "kevva/wifi-name", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "https://github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js", 20 | "lib" 21 | ], 22 | "keywords": [ 23 | "name", 24 | "network", 25 | "wifi" 26 | ], 27 | "dependencies": { 28 | "execa": "^0.7.0" 29 | }, 30 | "devDependencies": { 31 | "ava": "*", 32 | "xo": "*" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # wifi-name 2 | 3 | > Get current wifi name 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install wifi-name 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const wifiName = require('wifi-name'); 17 | 18 | wifiName().then(name => { 19 | console.log(name); 20 | //=> 'wu-tang lan' 21 | }); 22 | ``` 23 | 24 | 25 | ## API 26 | 27 | ### wifiName() 28 | 29 | Returns a `Promise` for a `string` with the current wifi name. 30 | 31 | ### wifiName.sync() 32 | 33 | Returns a `string` with the current wifi name. 34 | 35 | 36 | ## Related 37 | 38 | * [wifi-name-cli](https://github.com/kevva/wifi-name-cli) - CLI for this module 39 | 40 | 41 | ## License 42 | 43 | MIT © [Kevin Mårtensson](https://github.com/kevva) 44 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | test('async', async t => { 5 | t.truthy(await m()); 6 | }); 7 | 8 | test('sync', t => { 9 | t.truthy(m.sync()); 10 | }); 11 | --------------------------------------------------------------------------------