From 2c2f38deb619183a6dd2d2465bf66c07e43da84c Mon Sep 17 00:00:00 2001 From: Louis Orleans Date: Sun, 10 Sep 2017 20:26:28 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Flashing=20works?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flasher/commands.js | 60 ++++++++++++++++++++++++++++----------------- flasher/index.js | 34 ++++++++++++++++--------- flasher/util.js | 19 ++++++++------ 3 files changed, 71 insertions(+), 42 deletions(-) diff --git a/flasher/commands.js b/flasher/commands.js index f11c0e3c55..204834cd59 100644 --- a/flasher/commands.js +++ b/flasher/commands.js @@ -1,47 +1,61 @@ -module.exports.Upload = async function Upload (keymap, target, right = false) { - await build(right); +const Inquirer = require('inquirer'); + +const { Echo, Exec, IdentifyKeyboard } = require('./util'); + +async function Upload (keymap, path, right = false) { + await Build(keymap, path, right); let board; while (board == null) { - board = IdentifyKeyboard(); + board = IdentifyKeyboard(undefined, path); if (board == null) { Echo(`Put your keyboard in flash mode`); } } + if (board.length > 1) { + const answer = await Inquirer.prompt({ + name: 'path', + type: 'list', + message: `Select a the keyboard's ${right ? 'right' : 'left'} half`, + choices: board.map(board => ({ + name: `${board.name} (${board.path})`, + value: board.path + })) + }); + board = board.find(board => board.path === answer.path); + } else if (board.length === 1) { + board = board[0]; + } - Echo(`Building for ${board.name}`); - await flash(right, board); + Echo(`Building for ${board.name} (${board.path})`); + await Flash(keymap, board.path, right); } +module.exports.Upload = Upload; -module.exports.Build = async function Build (right = false) { +async function Build (keymap, path, right = false) { Echo(`Building ${right ? 'right' : 'left'}`); - await Exec(`make ergodox_infinity-${KEYMAP} ${right ? 'MASTER=right' : ''}`); + await Exec(`make ergodox_infinity-${keymap} ${right ? 'MASTER=right' : ''}`); + await Exec(`make ergodox_infinity-${keymap}-.build/ergodox_infinity_${keymap}.bin ${right ? 'MASTER=right' : ''}`); Echo(`Built ${right ? 'right' : 'left'}`, ''); } +module.exports.Build = Build; -module.exports.Flash = function Flash (right = false, board) { +function Flash (keymap, path, right = false) { return new Promise(async resolve => { Echo(`Flashing ${right ? 'right' : 'left'}`); - try { - await Exec(`make ergodox_infinity-${KEYMAP}-dfu-util ${right ? 'MASTER=right' : ''}`); - } catch (err) { - if (err.message && err.message.includes(`More than one DFU capable USB device found`)) { - Echo(`QMK flash failed!`); - Echo(`Flashing manually with dfu-util`); - - await Exec(`dfu-util --device ${board.id} -D .build/ergodox_infinity_default.bin`); - } else { - throw err; - } - } + // make infinity--.build/ergodox_infinity_.bin + // await Exec(`make ergodox_infinity-${keymap}-dfu-util ${right ? 'MASTER=right' : ''}`); + await Exec(`dfu-util --path ${path} -D .build/ergodox_infinity_default.bin`); Echo(`Flashed ${right ? 'right' : 'left'}`, ''); - // while (IdentifyKeyboard()) { + // while (IdentifyKeyboard(board.path)) { // Echo(`Unplug your keyboard!`); // } // TODO: Check to see if the script is done. If it is, then don't require an unplug // TODO: Make this actually look for the keyboard to be unplugged - Echo(`Unplug your keyboard! (you have 15 seconds to do so)`); - setTimeout(() => resolve(), 15000); + // Echo(`Unplug your keyboard! (you have 15 seconds to do so)`); + // setTimeout(() => resolve(), 15000); + resolve(); }); } +module.exports.Flash = Flash; diff --git a/flasher/index.js b/flasher/index.js index cea1e1cb34..9eadf40204 100755 --- a/flasher/index.js +++ b/flasher/index.js @@ -1,29 +1,39 @@ #!/usr/bin/env node const { execSync, exec } = require('child_process'); +const Commander = require('commander'); const { Echo, Exec, IdentifyKeyboard } = require('./util'); const { Upload } = require('./commands'); -const KEYMAP = process.env.KEYMAP || 'default'; const LEFT_TARGET = process.env.LEFT; const RIGHT_TARGET = process.env.RIGHT; -(async () => { - Echo(`Keymap: ${KEYMAP}`); +if (!process.cwd().endsWith('/qmk_firmware')) { + process.chdir(`${__dirname}/../`) + console.log(process.cwd()); +} + +Commander + .option('-m --keymap [name]', 'Keymap name', 'default') + .option('-p --path [path]', 'DFU device path') + .option('--half [half]', 'Which half to program (left, right)') + .parse(process.argv); - if (process.env.LEFT || process.env.RIGHT) { - if (process.env.LEFT) { - await Upload(KEYMAP); - } +(async () => { + Echo(`Keymap: ${Commander.keymap}`); - if (process.env.RIGHT) { - await Upload(KEYMAP, undefined, true); - } + if (Commander.half != null) { + await Upload(Commander.keymap, Commander.path, Commander.half === 'right'); } else { // Build both halves - await Upload(KEYMAP); + await Upload(Commander.keymap); - await Upload(KEYMAP, undefined, true); + await Upload(Commander.keymap, undefined, true); } })(); + +process.on('unhandledRejection', err => { + console.error(err); + process.exit(1); +}); diff --git a/flasher/util.js b/flasher/util.js index 0874b56d06..970ef19e3e 100644 --- a/flasher/util.js +++ b/flasher/util.js @@ -1,24 +1,27 @@ +const { exec, execSync } = require('child_process'); const Chalk = require('chalk'); -module.exports.IdentifyKeyboard = function IdentifyKeyboard (name = 'Kiibohd DFU', path, limit = true) { +function IdentifyKeyboard (name = 'Kiibohd DFU', path, limit = false) { 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], + path: /path="([\w-]+)"/g.exec(line)[1], id: /(\[\w+\:\w+\])/g.exec(line)[1] })) - .filter(device => device.name === name && path != null ? device.path === path : true); + .filter(device => device.name === name || device.path === path); if (limit) { return boards[0]; } else { - return boards; + return boards.length > 0 ? boards : undefined; } } +module.exports.IdentifyKeyboard = IdentifyKeyboard; -module.exports.Exec = function Exec (command) { +function Exec (command) { return new Promise((resolve, reject) => { - console.log(command); + console.log(Chalk.blue('] ') + command); const proc = exec(command, (err) => { if (err) { reject(err); @@ -30,13 +33,15 @@ module.exports.Exec = function Exec (command) { proc.stdout.on('data', data => console.log(line_start + data.replace(/\n$/, '').replace(/\n/g, `\n${line_start}`))); }); } +module.exports.Exec = Exec; -module.exports.Echo = function Echo (...logs) { +function Echo (...logs) { for(const log of logs) { if (typeof log === 'string') { console.log(log); } else { - console.log(log.toString ? log.toString() : log); + console.log((log && log.toString) ? log.toString() : log); } } } +module.exports.Echo = Echo;