-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.js
More file actions
54 lines (45 loc) · 1.39 KB
/
utils.js
File metadata and controls
54 lines (45 loc) · 1.39 KB
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
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;
function filterDevices(devices) {
return Object.keys(devices)
.filter(port => devices[port].runtime === "espruino")
.map(port => Object.assign({port}, devices[port]));
}
function runEspruino(device, ...cmdLineArgs) {
let output = function(msg) {
msg = "" + msg;
// Avoid cases where it is re-printing exactly what we are typing
if (msg.endsWith("\n")) {
msg = `${device.runtime}: ${msg}`;
}
process.stdout.write(msg);
};
let espruinoCmd = spawn('node', [
path.join('node_modules', 'espruino', 'bin', 'espruino-cli'),
'-b', device.baud_rate,
'-p', device.port,
'--no-ble',
...cmdLineArgs
], {
// Because spawned processes don't support setRawMode we need to pass our stdin thru
stdio: ['inherit', 'pipe', 'pipe']
});
espruinoCmd.stdout.on('data', data => {
output(data.toString());
});
espruinoCmd.stderr.on('data', data => {
output(data.toString());
});
espruinoCmd.on('close', code => {
output(`Exited with status ${code}`);
});
espruinoCmd.on('error', err => {
console.error(`Error: ${err.message}`);
});
return espruinoCmd;
}
module.exports = {
filterDevices,
runEspruino
};