From bd8c8d9e2b2288eec756b03e506f9f02fbe2f466 Mon Sep 17 00:00:00 2001 From: Andreas Holstenson Date: Tue, 23 Jan 2018 18:08:25 +0100 Subject: [PATCH] Cleaning up lint errors --- cli/commands/configure.js | 7 +++---- cli/commands/control.js | 10 ++++------ cli/commands/discover.js | 6 +++--- cli/commands/inspect.js | 9 ++++----- cli/commands/protocol.js | 2 +- cli/commands/protocol/call.js | 10 ++++------ cli/commands/protocol/json-dump.js | 4 ++-- cli/commands/protocol/packet.js | 2 -- cli/commands/tokens.js | 2 +- cli/commands/tokens/update.js | 8 +++----- cli/device-finder.js | 2 +- cli/index.js | 3 ++- cli/log.js | 10 +++++----- lib/device.js | 9 --------- lib/devices/air-purifier.js | 2 +- lib/devices/vacuum.js | 4 ++-- lib/discovery.js | 7 +------ lib/index.js | 1 - lib/infoFromHostname.js | 3 --- lib/management.js | 2 +- lib/network.js | 11 +++++++++-- 21 files changed, 47 insertions(+), 67 deletions(-) diff --git a/cli/commands/configure.js b/cli/commands/configure.js index 049951d..2d3ce3d 100644 --- a/cli/commands/configure.js +++ b/cli/commands/configure.js @@ -1,6 +1,5 @@ 'use strict'; -const chalk = require('chalk'); const log = require('../log'); const deviceFinder = require('../device-finder'); @@ -51,16 +50,16 @@ exports.handler = function(argv) { }) .then(() => { pending--; - process.exit(0); + process.exit(0); // eslint-disable-line }); }); const doneHandler = () => { - if(pending == 0) { + if(pending === 0) { if(! foundDevice) { log.warn('Could not find device'); } - process.exit(0); + process.exit(0); // eslint-disable-line } }; setTimeout(doneHandler, 5000); diff --git a/cli/commands/control.js b/cli/commands/control.js index 2aaf987..d197cd4 100644 --- a/cli/commands/control.js +++ b/cli/commands/control.js @@ -1,6 +1,5 @@ 'use strict'; -const chalk = require('chalk'); const log = require('../log'); const deviceFinder = require('../device-finder'); @@ -21,8 +20,7 @@ exports.handler = function(argv) { browser.on('available', device => { if(! device[argv.method]) { log.error('The method ' + argv.method + ' is not available'); - process.exit(0); - return; + process.exit(0); // eslint-disable-line } pending++; @@ -39,16 +37,16 @@ exports.handler = function(argv) { }) .then(() => { pending--; - process.exit(0); + process.exit(0); // eslint-disable-line }); }); const doneHandler = () => { - if(pending == 0) { + if(pending === 0) { if(! foundDevice) { log.warn('Could not find device'); } - process.exit(0); + process.exit(0); // eslint-disable-line } }; setTimeout(doneHandler, 5000); diff --git a/cli/commands/discover.js b/cli/commands/discover.js index c9b5d5e..1f8eeb3 100644 --- a/cli/commands/discover.js +++ b/cli/commands/discover.js @@ -14,7 +14,7 @@ exports.builder = { }; exports.handler = function(argv) { - log.info('Discovering devices. Press Ctrl+C to stop.') + log.info('Discovering devices. Press Ctrl+C to stop.'); log.plain(); const browser = deviceFinder(); @@ -22,14 +22,14 @@ exports.handler = function(argv) { try { log.device(device); } catch(ex) { - console.log(ex); + log.error(ex); } const mgmt = device.management; if(argv.sync && mgmt.token && mgmt.autoToken) { tokens.update(device.id, mgmt.token) .catch(err => { - error('Could not update token for', device.id, ':', err); + log.error('Could not update token for', device.id, ':', err); }); } }); diff --git a/cli/commands/inspect.js b/cli/commands/inspect.js index 972de70..01b4f61 100644 --- a/cli/commands/inspect.js +++ b/cli/commands/inspect.js @@ -3,7 +3,6 @@ const chalk = require('chalk'); const log = require('../log'); const deviceFinder = require('../device-finder'); -const Tokens = require('../../lib/tokens'); exports.command = 'inspect '; exports.description = 'Inspect a device'; @@ -80,7 +79,7 @@ exports.handler = function(argv) { log.plain(chalk.bold('Parent:')); log.group(() => { log.device(mgmt.parent); - }) + }); } }) .catch(err => { @@ -88,16 +87,16 @@ exports.handler = function(argv) { }) .then(() => { pending--; - process.exit(0); + process.exit(0); // eslint-disable-line }); }); const doneHandler = () => { - if(pending == 0) { + if(pending === 0) { if(! foundDevice) { log.warn('Could not find device'); } - process.exit(0); + process.exit(0); // eslint-disable-line } }; setTimeout(doneHandler, 5000); diff --git a/cli/commands/protocol.js b/cli/commands/protocol.js index a37f0ec..1222095 100644 --- a/cli/commands/protocol.js +++ b/cli/commands/protocol.js @@ -2,7 +2,7 @@ const path = require('path'); -exports.command = 'protocol ' +exports.command = 'protocol '; exports.description = 'Inspect and test raw miIO-commands'; exports.builder = yargs => yargs.commandDir(path.join(__dirname, 'protocol')); exports.handler = () => {}; diff --git a/cli/commands/protocol/call.js b/cli/commands/protocol/call.js index af084d0..a76740b 100644 --- a/cli/commands/protocol/call.js +++ b/cli/commands/protocol/call.js @@ -1,9 +1,7 @@ 'use strict'; -const chalk = require('chalk'); const log = require('../../log'); const deviceFinder = require('../../device-finder'); -const tokens = require('../../../lib/tokens'); exports.command = 'call [params]'; exports.description = 'Call a raw method on a device'; @@ -29,7 +27,7 @@ exports.handler = function(argv) { const parsedArgs = argv.params ? JSON.parse(argv.params) : []; device.miioCall(argv.method, parsedArgs) .then(result => { - log.info('Got result:') + log.info('Got result:'); log.plain(JSON.stringify(result, null, ' ')); }) .catch(err => { @@ -40,16 +38,16 @@ exports.handler = function(argv) { }) .then(() => { pending--; - process.exit(0); + process.exit(0); // eslint-disable-line }); }); const doneHandler = () => { - if(pending == 0) { + if(pending === 0) { if(! foundDevice) { log.warn('Could not find device'); } - process.exit(0); + process.exit(0); // eslint-disable-line } }; setTimeout(doneHandler, 5000); diff --git a/cli/commands/protocol/json-dump.js b/cli/commands/protocol/json-dump.js index d9e738f..9eef86a 100644 --- a/cli/commands/protocol/json-dump.js +++ b/cli/commands/protocol/json-dump.js @@ -31,10 +31,10 @@ exports.handler = function(argv) { if(! udp) return; let out; - if(udp['udp.dstport'] == '54321') { + if(udp['udp.dstport'] === '54321') { // Packet that is being sent to the device out = true; - } else if(udp['udp.srcport'] == '54321') { + } else if(udp['udp.srcport'] === '54321') { // Packet coming from the device out = false; } else { diff --git a/cli/commands/protocol/packet.js b/cli/commands/protocol/packet.js index e886631..28430f9 100644 --- a/cli/commands/protocol/packet.js +++ b/cli/commands/protocol/packet.js @@ -1,7 +1,5 @@ 'use strict'; -const fs = require('fs'); -const chalk = require('chalk'); const log = require('../../log'); const Packet = require('../../../lib/packet'); diff --git a/cli/commands/tokens.js b/cli/commands/tokens.js index d56b217..56665e7 100644 --- a/cli/commands/tokens.js +++ b/cli/commands/tokens.js @@ -2,7 +2,7 @@ const path = require('path'); -exports.command = 'tokens ' +exports.command = 'tokens '; exports.description = 'Manage tokens of devices'; exports.builder = yargs => yargs.commandDir(path.join(__dirname, 'tokens')); exports.handler = () => {}; diff --git a/cli/commands/tokens/update.js b/cli/commands/tokens/update.js index 3123c79..5a26541 100644 --- a/cli/commands/tokens/update.js +++ b/cli/commands/tokens/update.js @@ -1,9 +1,7 @@ 'use strict'; -const chalk = require('chalk'); const log = require('../../log'); const deviceFinder = require('../../device-finder'); -const tokens = require('../../../lib/tokens'); exports.command = 'update '; exports.description = 'Update the token to use for the given device'; @@ -43,16 +41,16 @@ exports.handler = function(argv) { }) .then(() => { pending--; - process.exit(0); + process.exit(0); // eslint-disable-line }); }); const doneHandler = () => { - if(pending == 0) { + if(pending === 0) { if(! foundDevice) { log.warn('Could not find device'); } - process.exit(0); + process.exit(0); // eslint-disable-line } }; setTimeout(doneHandler, 5000); diff --git a/cli/device-finder.js b/cli/device-finder.js index 9704992..3c5ed4d 100644 --- a/cli/device-finder.js +++ b/cli/device-finder.js @@ -1,7 +1,7 @@ 'use strict'; const { EventEmitter } = require('events'); -const { Browser, Devices } = require('../lib/discovery'); +const { Devices } = require('../lib/discovery'); const connectToDevice = require('../lib/connectToDevice'); const IP = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; diff --git a/cli/index.js b/cli/index.js index 35c1622..9ff98fd 100755 --- a/cli/index.js +++ b/cli/index.js @@ -1,7 +1,8 @@ #!/usr/bin/env node +'use strict'; const path = require('path'); -const argv = require('yargs') +require('yargs') .commandDir(path.join(__dirname, 'commands')) .recommendCommands() .demandCommand() diff --git a/cli/log.js b/cli/log.js index 191766c..8f5e267 100644 --- a/cli/log.js +++ b/cli/log.js @@ -6,19 +6,19 @@ module.exports = { indent: '', info(...args) { - console.log(this.indent + chalk.bgWhite.black(' INFO '), args.join(' ')); + console.log(this.indent + chalk.bgWhite.black(' INFO '), args.join(' ')); // eslint-disable-line }, error(...args) { - console.log(this.indent + chalk.bgRed.white(' ERROR '), args.join(' ')); + console.log(this.indent + chalk.bgRed.white(' ERROR '), args.join(' ')); // eslint-disable-line }, warn(...args) { - console.log(this.indent + chalk.bgYellow.black(' WARNING '), args.join(' ')); + console.log(this.indent + chalk.bgYellow.black(' WARNING '), args.join(' ')); // eslint-disable-line }, plain(...args) { - console.log(this.indent + args.join(' ')); + console.log(this.indent + args.join(' ')); // eslint-disable-line }, group(g) { @@ -50,7 +50,7 @@ module.exports = { if(mgmt.token) { this.plain(chalk.bold('Token:'), mgmt.token, mgmt.autoToken ? chalk.green('via auto-token') : chalk.yellow('via stored token')); } else if(! mgmt.parent) { - this.plain(chalk.bold('Token:'), '???') + this.plain(chalk.bold('Token:'), '???'); } else { this.plain(chalk.bold('Token:'), chalk.green('Automatic via parent device')); } diff --git a/lib/device.js b/lib/device.js index 0f7662e..23a19eb 100644 --- a/lib/device.js +++ b/lib/device.js @@ -3,19 +3,10 @@ const isDeepEqual = require('deep-equal'); const { Thing, Polling } = require('abstract-things'); -const dgram = require('dgram'); -const Packet = require('./packet'); - const DeviceManagement = require('./management'); const IDENTITY_MAPPER = v => v; -const ERRORS = { - '-5001': (method, args, err) => err.message === 'invalid_arg' ? 'Invalid argument' : err.message, - '-5005': (method, args, err) => err.message === 'params error' ? 'Invalid argument' : err.message, - '-10000': (method) => 'Method `' + method + '` is not supported' -}; - module.exports = Thing.type(Parent => class extends Parent.with(Polling) { static get type() { return 'miio'; diff --git a/lib/devices/air-purifier.js b/lib/devices/air-purifier.js index deee50e..f974a7c 100644 --- a/lib/devices/air-purifier.js +++ b/lib/devices/air-purifier.js @@ -128,7 +128,7 @@ module.exports = class extends AirPurifier return Promise.resolve(this.property('favoriteLevel')); } - return setFavoriteLevel(level); + return this.setFavoriteLevel(level); } /** diff --git a/lib/devices/vacuum.js b/lib/devices/vacuum.js index 3bd13f8..98ddd98 100644 --- a/lib/devices/vacuum.js +++ b/lib/devices/vacuum.js @@ -2,7 +2,7 @@ const { EasyNameable, ChargingState, AutonomousCharging } = require('abstract-things'); const { - Vacuum, CleaningState, AdjustableFanSpeed, AutonomousCleaning, SpotCleaning + Vacuum, AdjustableFanSpeed, AutonomousCleaning, SpotCleaning } = require('abstract-things/climate'); const MiioApi = require('../device'); @@ -20,7 +20,7 @@ function checkResult(r) { */ module.exports = class extends Vacuum.with( MiioApi, EasyNameable, BatteryLevel, AutonomousCharging, AutonomousCleaning, - SpotCleaning, AdjustableFanSpeed + SpotCleaning, AdjustableFanSpeed, ChargingState ) { static get type() { return 'miio:vacuum'; diff --git a/lib/discovery.js b/lib/discovery.js index 6809bdb..f9de04c 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -2,20 +2,15 @@ const { TimedDiscovery, BasicDiscovery, search, addService, removeService } = require('tinkerhub-discovery'); const { Children } = require('abstract-things'); -const { ThingDiscovery } = require('abstract-things/discovery'); const util = require('util'); -const dgram = require('dgram'); const dns = require('dns'); const network = require('./network'); const infoFromHostname = require('./infoFromHostname'); -const Packet = require('./packet'); const connectToDevice = require('./connectToDevice'); -const PORT = 54321; - const tryAdd = Symbol('tryAdd'); const Browser = module.exports.Browser = class Browser extends TimedDiscovery { @@ -118,7 +113,7 @@ class Devices extends BasicDiscovery { .then(device => { reg.device = device; return reg; - }) + }); }); this._browser.on('available', s => { diff --git a/lib/index.js b/lib/index.js index e9b80db..2722d39 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,6 @@ 'use strict'; const discovery = require('./discovery'); -const Device = require('./device'); /** * Get information about the models supported. Can be used to extend the models diff --git a/lib/infoFromHostname.js b/lib/infoFromHostname.js index 340777e..6d073a7 100644 --- a/lib/infoFromHostname.js +++ b/lib/infoFromHostname.js @@ -1,8 +1,5 @@ 'use strict'; - -const models = require('./models'); - module.exports = function(hostname) { // Extract info via hostname structure const m = /(.+)_miio(\d+)/g.exec(hostname); diff --git a/lib/management.js b/lib/management.js index 2cdf5ba..a67aa01 100644 --- a/lib/management.js +++ b/lib/management.js @@ -76,7 +76,7 @@ class DeviceManagement { updateToken(token) { if(token instanceof Buffer) { token = token.toString('hex'); - } else if(! token instanceof String) { + } else if(! (token instanceof String)) { return Promise.resolve(new Error('Token must be a hex-string or a Buffer')); } diff --git a/lib/network.js b/lib/network.js index 9dd7716..ac66eb9 100644 --- a/lib/network.js +++ b/lib/network.js @@ -10,6 +10,13 @@ const tokens = require('./tokens'); const PORT = 54321; +const ERRORS = { + '-5001': (method, args, err) => err.message === 'invalid_arg' ? 'Invalid argument' : err.message, + '-5005': (method, args, err) => err.message === 'params error' ? 'Invalid argument' : err.message, + '-10000': (method) => 'Method `' + method + '` is not supported' +}; + + /** * Class for keeping track of the current network of devices. This is used to * track a few things: @@ -322,7 +329,7 @@ class DeviceInfo { } let data = this.packet.data; - if(data == null) { + if(data === null) { this.debug('<-', 'Handshake reply:', this.packet.checksum); this.packet.handleHandshakeReply(); @@ -381,7 +388,7 @@ class DeviceInfo { this.handshakeTimeout = null; this.handshakePromise = null; - if(this.id != this.packet.deviceId) { + if(this.id !== this.packet.deviceId) { // Update the identifier if needed this.id = this.packet.deviceId; this.debug = debug('thing:miio:' + this.id);