-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·69 lines (66 loc) · 1.89 KB
/
cli.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
const debugLib = require('debug');
const program = require('commander');
const { createSerialCommunicator } = require('./build');
program
.command('run <command>')
.description(
'run the given <command> through the serial line and returns result'
)
.option(
'-p, --port-name [PORT_NAME]',
'PORT_NAME is the serial port to communicate with',
'/dev/ttyUSB0'
)
.option(
'-b, --baudrate [BAUD_RATE]',
'BAUD_RATE is the serial baud rate',
115200
)
.option(
'-d, --debug-enabled',
'when set, outputs debug message to the console'
)
.option('--prompt [PROMPT]', 'PROMPT is the serial console prompt', '/ #')
.option(
'-t, --timeout [TIMEOUT]',
'TIMEOUT in ms to be used for lon grun commands such as "du -h ."',
3000
)
.option('--dont-wait-answer', 'execute command and do not wait answer')
.option(
'-l, --line-separator [LINE_SEPARATOR]',
'LINE_SEPARATOR is the serial console line separator',
'\n'
)
.action((command, options) => {
const debug = Object.assign(debugLib('cli'), {
enabled: options.debugEnabled,
});
const communicator = createSerialCommunicator({
debugEnabled: options.debugEnabled,
baudrate: options.baudrate,
prompt: options.prompt,
lineSeparator: options.lineSeparator,
});
communicator
.connect(options.portName)
.then(() =>
communicator.executeCmd(command, {
timeout: options.timeout,
waitAnswer: !options.dontWaitAnswer,
})
)
.then((result) => {
const { output, errorCode } = result;
console.log(output.join(options.lineSeparator));
debug(`ran command: '${command}'`);
debug(`error code: ${errorCode}`);
process.exit(errorCode);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
});
program.parse(process.argv);