Skip to content

Commit

Permalink
process stdin on raw
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Jul 30, 2021
1 parent 8b67e06 commit 42ff0bb
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/tplight.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const TPLSmartDevice = require('../dist/tplink-lightbulb.cjs')
const yargs = require('yargs')
const util = require('util')

// https://gist.github.com/xenozauros/f6e185c8de2a04cdfecf
function hexToHsl (hex) {
Expand All @@ -26,7 +27,17 @@ function hexToHsl (hex) {
return { h, s, l }
}

const json = process.stdout.isTTY ? s => console.log(s) : s => console.log(JSON.stringify(s, null, 2))
async function readStream (stream = process.stdin) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk))
stream.on('end', () => {
resolve(Buffer.concat(chunks).toString('utf8'))
})
})
}

const json = process.stdout.isTTY ? s => console.log(util.inspect(s, false, null, true)) : s => console.log(JSON.stringify(s, null, 2))

// for pkg support
if (typeof process.pkg !== 'undefined') {
Expand Down Expand Up @@ -221,9 +232,15 @@ module.exports = yargs
.catch(handleError)
})

.command('raw <ip> <json>', 'Send a raw JSON command', {}, argv => {
.command('raw <ip> [json]', 'Send a raw JSON command, use param or stdin', {}, async argv => {
const bulb = new TPLSmartDevice(argv.ip)
bulb.send(JSON.parse(argv.json))
process.stdin.resume()
const msg = argv.json ? argv.json : await readStream()
if (!msg) {
console.error('For raw, you must provide JSON via param or stdin.')
process.exit(1)
}
bulb.send(JSON.parse(msg))
.then(r => argv.quiet || json(r))
.catch(handleError)
})
Expand Down

0 comments on commit 42ff0bb

Please sign in to comment.