flash.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const prepareBinaries = require('./prepareBinaries');
  2. const request = require('request');
  3. const nodeFetch = require('node-fetch');
  4. const RomComm = require('rom-comm');
  5. function handleBinaryPreparer(binaryPreparer, port, onError, onProgress, onComplete) {
  6. binaryPreparer
  7. .on("error", onError)
  8. .on("progress", progress => {
  9. //For the download/extract progress.
  10. onProgress(progress.details.downloadedBytes / progress.details.downloadSize, progress.display);
  11. })
  12. .on("complete", flashSpec => {
  13. const device = RomComm.serial(port, { baudRate: 115200 }, {
  14. onProgress: progress => onProgress(progress.flashedBytes / progress.totalBytes, 'Flashing')
  15. });
  16. device.open(err => {
  17. if (err) {
  18. onError(err);
  19. } else {
  20. device.flash(flashSpec, err => {
  21. // TODO: This err doesn't come through
  22. if (err) {
  23. onError(err);
  24. } else {
  25. device.close();
  26. onComplete();
  27. }
  28. });
  29. }
  30. });
  31. });
  32. }
  33. function flash(port, manifestURL, onError, onProgress, onComplete) {
  34. nodeFetch(manifestURL)
  35. .then(response => response.json())
  36. .then(prepareBinaries)
  37. .then(binaryPreparer => handleBinaryPreparer(binaryPreparer, port, onError, onProgress, onComplete));
  38. }
  39. module.exports = flash;