Skip to content

Commit

Permalink
refactor: wifi binary
Browse files Browse the repository at this point in the history
Rewrite wifi binary to make it compliant with ES6.
  • Loading branch information
friedrith committed Jul 3, 2020
1 parent 6d71c73 commit 096027d
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 139 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
72 changes: 72 additions & 0 deletions bin/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const optionDefinitions = [
{
name: 'scan',
type: Boolean,
description:
'Scan for wifi networks. It returns a JSON formatted response with ssid, bssid, frequency, signal level and security for every network detected.'
},
{
name: 'connect',
type: Boolean,
description:
'Connect to a wifi network. It needs options {bold --ssid} and {bold --password}. A specific interface may be selected by adding option {bold --iface}'
},
{
name: 'disconnect',
type: Boolean,
description:
'Disconnect from a wifi network. A specific interface may be selected by adding option {bold --iface}'
},
{
name: 'current',
type: Boolean,
description:
'List the current wifi connections. A specific interface may be selected by adding option {bold --iface}'
},
{
name: 'ssid',
type: String,
typeLabel: '{underline ssid}',
description: 'Ssid to connect to the wifi.'
},
{
name: 'password',
type: String,
typeLabel: '{underline password}',
description: 'Password to connect to the wifi.'
},
{
name: 'iface',
type: String,
typeLabel: '{underline interface}',
description: 'Network interface to connect to the wifi.'
},
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Show the help.'
},
{
name: 'version',
alias: 'v',
type: Boolean,
description: 'Display the current version of node-wifi.'
}
];

const sections = [
{
header: 'Wifi',
content: 'Multi-OS tool to manage wifi.'
},
{
header: 'Options',
optionList: optionDefinitions
}
];

module.exports = {
sections,
optionDefinitions
};
197 changes: 62 additions & 135 deletions bin/wifi.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,35 @@
#!/usr/bin/env node
/* eslint-disable no-process-exit */

'use strict';

const commandLineArgs = require('command-line-args')
const getUsage = require('command-line-usage')

const packageJson = require('../package.json')

var wifi = require('../src/wifi');

const optionDefinitions = [
{
name: 'scan',
type: Boolean,
description: 'Scan for wifi networks. It returns a JSON formatted response with ssid, bssid, frequency, signal level and security for every network detected.'
},
{
name: 'connect',
type: Boolean,
description: 'Connect to a wifi network. It needs options {bold --ssid} and {bold --password}. A specific interface may be selected by adding option {bold --iface}'
},
{
name: 'disconnect',
type: Boolean,
description: 'Disconnect from a wifi network. A specific interface may be selected by adding option {bold --iface}'
},
{
name: 'current',
type: Boolean,
description: 'List the current wifi connections. A specific interface may be selected by adding option {bold --iface}'
},
{
name: 'ssid',
type: String,
typeLabel: '{underline ssid}',
description: 'Ssid to connect to the wifi.'
},
{
name: 'password',
type: String,
typeLabel: '{underline password}',
description: 'Password to connect to the wifi.'
},
{
name: 'iface',
type: String,
typeLabel: '{underline interface}',
description: 'Network interface to connect to the wifi.'
},
{
name: 'help',
alias: 'h',
type: Boolean,
description: "Show the help."
},
{
name: 'version',
alias: 'v',
type: Boolean,
description: 'Display the current version of node-wifi.'
}
];

const sections = [
{
header: 'Wifi',
content: 'Multi-OS tool to manage wifi.'
},
{
header: 'Options',
optionList: optionDefinitions
}
];
const commandLineArgs = require('command-line-args');
const getUsage = require('command-line-usage');

const packageJson = require('../package.json');

const usage = getUsage(sections)
const wifi = require('../src/wifi');
const { sections, optionDefinitions } = require('./help');

const usage = getUsage(sections);

var options = null;

try {
options = commandLineArgs(optionDefinitions)
options = commandLineArgs(optionDefinitions);
} catch (e) {
console.log('Bad options, please see the help with option -h', e);
process.exit(2);
console.error('Bad options, please see the help with option -h', e);
process.exit(2);
}

if (options.version) {
console.log('Version '+packageJson.version);
console.log('Version ' + packageJson.version);
process.exit(0);
}

if (options.help) {
console.log(usage);
process.exit(0);
console.log(usage);
process.exit(0);
}

var cmds = 0;
Expand All @@ -101,76 +39,65 @@ if (options.disconnect) cmds++;
if (options.scan) cmds++;
if (options.current) cmds++;


if (cmds > 1) {
console.log('You cannot do several operations at the same time');
process.exit(2);
// throw new Error();
console.error('You cannot do several operations at the same time');
process.exit(2);
}

if (!options.connect && !options.scan && !options.disconnect && !options.current) {
console.log(usage);
process.exit(2);
if (
!options.connect &&
!options.scan &&
!options.disconnect &&
!options.current
) {
console.log(usage);
process.exit(2);
}

wifi.init({
iface : options.iface
iface: options.iface
});

if (options.scan) {
wifi.scan((error, resp) => {
if (error) {
console.error(error);
process.exit(2);
} else {
console.log(resp);
}
});
} else if (options.connect) {
if (!options.ssid || !options.password) {
console.error(usage);
process.exit(2);
}

wifi.scan(function(err, resp) {
if (err) {
console.log(err);
process.exit(2);
} else {
console.log(resp);
}
});
}

if (options.connect) {
const accessPoint = {
ssid: options.ssid,
password: options.password
};

if (!options.ssid || !options.password) {
console.log(usage);
process.exit(2);
wifi.connect(accessPoint, error => {
if (error) {
console.error(error);
process.exit(2);
}

var ap = {
ssid : options.ssid,
password : options.password
});
} else if (options.disconnect) {
wifi.disconnect(error => {
if (error) {
console.error(error);
process.exit(2);
}

wifi.connect(ap, function(err) {
if (err) {
console.log(err);
process.exit(2);
}
});
};

if (options.connect) {

var ap = {
ssid : options.ssid,
password : options.password
});
} else if (options.current) {
wifi.getCurrentConnections((error, currentConnections) => {
if (error) {
console.log(error);
process.exit(2);
} else {
console.log(currentConnections);
}

wifi.connect(ap, function(err) {
if (err) {
console.log(err);
process.exit(2);
}
});
}

if (options.current) {
wifi.getCurrentConnections(function(err, currentConnections) {
if (err) {
console.log(err);
process.exit(2);
} else {
console.log(currentConnections);
}
});
});
}
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@
"scripts": {
"test": "jest",
"commitlint": "commitlint --from 8005de94dd1d0d1220d6fa09a163f97c4a22deda",
"lint": "eslint src/**/*.js",
"lint:fix": "eslint --fix src/**/*.js",
"format": "prettier --check 'src/**/*.js'",
"format:fix": "prettier --write 'src/**/*.js'",
"lint": "eslint {src,bin}/**/*.js",
"format": "prettier --check '{src,bin}/**/*.js'",
"format:fix": "prettier --write '{src,bin}/**/*.js'",
"release": "standard-version"
},
"husky": {
Expand Down

0 comments on commit 096027d

Please sign in to comment.