/
index.js
44 lines (32 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* eslint-disable no-console, no-process-exit */
/**
* External dependencies
*/
const EventEmitter = require( 'events' ).EventEmitter;
const process = require( 'process' );
const debug = require( 'debug' )( 'biab:main' );
/**
* External dependencies
*/
const deviceEmitter = new EventEmitter();
const devices = require( 'devices' )( deviceEmitter ); // eslint-disable-line no-unused-vars
if ( process.argv.length < 3 ) {
console.log( process.argv[ 1 ] + ': <command> [data]' );
process.exit( 1 );
}
function runCommand( emitter, command, commandData ) {
debug( 'Command: "' + command + '" with data "' + ( commandData ? commandData : '' ) + '"' );
emitter.on( 'error', message => {
console.log( JSON.stringify( { error: message } ) );
process.exit( 1 );
} );
emitter.on( 'result', json => {
console.log( json );
} );
emitter.emit( command, commandData );
if ( emitter.listenerCount( command ) === 0 ) {
debug( 'No handler for command ' + command );
emitter.emit( 'error', 'No handler' );
}
}
runCommand( deviceEmitter, process.argv[ 2 ], process.argv.length > 3 ? process.argv.slice( 3 ).join( ' ' ) : false );