qmk_firmware/flasher/util.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-09-11 03:26:28 +00:00
const { exec, execSync } = require('child_process');
2017-09-11 02:15:00 +00:00
const Chalk = require('chalk');
2017-09-11 03:26:28 +00:00
function IdentifyKeyboard (name = 'Kiibohd DFU', path, limit = false) {
2017-09-11 02:15:00 +00:00
const boards = execSync(`dfu-util --list`).toString()
.split('\n')
.filter(line => line.match(/(\[\w+\:\w+\])/g) != null)
.map(line => ({
name: /name="([\w\s]+)"/g.exec(line)[1],
2017-09-11 03:26:28 +00:00
path: /path="([\w-]+)"/g.exec(line)[1],
2017-09-11 02:15:00 +00:00
id: /(\[\w+\:\w+\])/g.exec(line)[1]
}))
2017-09-11 03:26:28 +00:00
.filter(device => device.name === name || device.path === path);
2017-09-11 02:15:00 +00:00
if (limit) {
return boards[0];
} else {
2017-09-11 03:26:28 +00:00
return boards.length > 0 ? boards : undefined;
2017-09-11 02:15:00 +00:00
}
}
2017-09-11 03:26:28 +00:00
module.exports.IdentifyKeyboard = IdentifyKeyboard;
2017-09-11 02:15:00 +00:00
2017-09-11 03:26:28 +00:00
function Exec (command) {
2017-09-11 02:15:00 +00:00
return new Promise((resolve, reject) => {
2017-09-11 03:26:28 +00:00
console.log(Chalk.blue('] ') + command);
2017-09-11 02:15:00 +00:00
const proc = exec(command, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
const line_start = Chalk.blue(']\t');
proc.stdout.on('data', data => console.log(line_start + data.replace(/\n$/, '').replace(/\n/g, `\n${line_start}`)));
});
}
2017-09-11 03:26:28 +00:00
module.exports.Exec = Exec;
2017-09-11 02:15:00 +00:00
2017-09-11 03:26:28 +00:00
function Echo (...logs) {
2017-09-11 02:15:00 +00:00
for(const log of logs) {
if (typeof log === 'string') {
console.log(log);
} else {
2017-09-11 03:26:28 +00:00
console.log((log && log.toString) ? log.toString() : log);
2017-09-11 02:15:00 +00:00
}
}
}
2017-09-11 03:26:28 +00:00
module.exports.Echo = Echo;