Andrew Chalkley 9 年 前
コミット
3a7a074034
3 ファイル変更60 行追加52 行削除
  1. 34 19
      back-end/prepare_binaries.js
  2. 25 31
      front-end/js/app.js
  3. 1 2
      package.json

+ 34 - 19
back-end/prepare_binaries.js

@@ -17,29 +17,44 @@ function addBufferToBinary(flashSpecification, fileName, buffer) {
     });
 }
 
-function prepareBinaries(manifest, callback) {
+function prepareBinaries(manifest) {
     const eventEmitter = new EventEmitter();
     const flashContents = manifest.flash;
     let body;
-    const downloadRequest = request(manifest.download).on("data", data => {
-        if(body) {
-            body = Buffer.concat([body, data]);
-        } else {
-            body = data;
-        }
-    }).on("complete", ()=>{
-        try {
-         decompress(body, {
+    let contentLength;
+    const downloadRequest = request(manifest.download)
+        .on("response", response => {
+            contentLength = Number(response.headers["content-length"]);
+        })
+        .on("data", data => {
+            if(body) {
+                body = Buffer.concat([body, data]);
+            } else {
+                body = data;
+            }
+            const progress = {
+                details: {
+                    downloadedBytes: body.length,
+                    downloadSize: contentLength
+                },
+                display: "Downloading"
+            };
+            eventEmitter.emit("progress", progress);
+        })
+        .on("complete", () => {
+            decompress(body, {
                  filter: file => isBinaryFileRequired(flashContents, file.path)
-            }).then(files => {
-                files.forEach(file => addBufferToBinary(flashContents, file.path, file.data)
-);
-                callback(null, flashContents);
-            }).catch(callback);
-        } catch(e){
-            alert(e.message);
-        }
-    }).on("error", error => eventEmitter.emit("entry", {display: error.message }));
+            })
+            .then(files => {
+                files.forEach(
+                    file => addBufferToBinary(flashContents, file.path, file.data)
+                );
+                eventEmitter.emit("complete", flashContents);
+            })
+            .catch(err => eventEmitter.emit("error", err));
+      })
+      .on("error", err => eventEmitter.emit("error", err));
+    
     return eventEmitter;
 }
 

+ 25 - 31
front-end/js/app.js

@@ -70,7 +70,7 @@ function processJSON(response) {
  * Handle UI
 ************************/
 
-flashButton.addEventListener("click", (event) => {
+flashButton.addEventListener("click", event => {
     isFlashing = true;
     disableInputs();
     prepareUIForFlashing(() => {
@@ -84,21 +84,21 @@ flashButton.addEventListener("click", (event) => {
  * Manage serial port events
  ************************/
 
- serialScanner.on("ports", (ports) => {
+ serialScanner.on("ports", ports => {
     portsSelect.addAll(ports);
 });
 
-serialScanner.on("deviceAdded", (port) => {
+serialScanner.on("deviceAdded", port => {
     portsSelect.add(port);
     new Notification(`Added: ${port}!`);
 });
 
-serialScanner.on("deviceRemoved", (port) => {
+serialScanner.on("deviceRemoved", port => {
     portsSelect.remove(port);
     new Notification(`Removed: ${port}!`);
 });
 
-serialScanner.on("error", (err) => {
+serialScanner.on("error", err => {
     if(err.message === "No serial ports detected.") {
         if(portsSelect.children[0]) {
             portsSelect.remove(portsSelect.children[0].textContent);
@@ -147,8 +147,8 @@ function onError(error){
 }
 
 function generateManifestList(manifestsJSON) {
-    manifestsJSON.options.forEach((option) => {
-        option.versions.forEach((version) => {
+    manifestsJSON.options.forEach(option => {
+        option.versions.forEach(version => {
             const optionElement = document.createElement("option");
             optionElement.textContent = `${option.name} - ${version.version}`;
             optionElement.value = version.manifest;
@@ -170,28 +170,31 @@ function getManifests() {
     });
 }
 
+function updateProgressUI(percent, display) {
+        percent = Math.round(percent * 100);
+        updateProgressBar(percent, svg);
+        appStatus.textContent = `${display} - ${percent}%`;
+}
+
 function flashWithManifest(manifest) {
     appStatus.textContent = `Flashing ${portsSelect.value}`;
-    const numberOfSteps = manifest.flash.length * 2;
-    let correctStepNumber = 1;
-    prepareBinaries(manifest, (err, flashSpec) => {
-        if(err) throw err;
-
+    prepareBinaries(manifest)
+    .on("error", onError)
+    .on("progress", progress => {
+        //For the download/extract progress.
+        updateProgressUI(progress.details.downloadedBytes / progress.details.downloadSize, progress.display);
+    })
+    .on("complete", flashSpec => {
         const esp = new RomComm({
             portName: portsSelect.value,
             baudRate: 115200
         });
 
-        esp.on('progress', (progress) => {
-            const flashPercent = Math.round((progress.details.flashedBytes/progress.details.totalBytes) * 100);
-            const processSoFar = 50; //From download and extracting.
-            const flashProcess = flashPercent / 2; //To add to the overall progress
-            updateProgressBar(processSoFar + flashProcess, svg);
-            appStatus.textContent = `${progress.display} - ${flashPercent}%`;
-
+        esp.on('progress', progress => {
+            updateProgressUI(progress.details.flashedBytes/progress.details.totalBytes, progress.display);
         });
 
-        esp.open().then((result) => {
+        esp.open().then(result => {
             appStatus.textContent = `Flashing device connected to ${portsSelect.value}`;
             let promise = Promise.resolve();
             return esp.flashSpecifications(flashSpec)
@@ -202,23 +205,14 @@ function flashWithManifest(manifest) {
                     restoreUI();
                     log.info("Flashed to latest Espruino build!", result);
                 });
-        }).catch((error) => {
+        }).catch(error => {
             esp.close();
             new Notification("An error occured during flashing.");
             isFlashing = false;
             log.error("Oh noes!", error);
             restoreUI();
         });
-    })
-    .on("error", onError)
-    .on("entry", (progress) => {
-        //For the download/extract progress. The other half is flashing.
-        const extractPercent = Math.round((correctStepNumber++/numberOfSteps) * 50);
-        updateProgressBar(extractPercent, svg);
-        appStatus.textContent = progress.display;
     });
-    
-    
 }
 
 function cloneSVGNode(node) {
@@ -243,7 +237,7 @@ function updateProgressBar(percent, svg){
                                     .map(cloneSVGNode)
                                     .map(updateClass);
 
-        backgroundElements.forEach(node => g.insertBefore(node, line));
+        backgroundElements.forEach(element => g.insertBefore(element, line));
     }
 
     const bgLine = backgroundElements[0];

+ 1 - 2
package.json

@@ -45,8 +45,7 @@
     "electron-squirrel-startup": "^1.0.0",
     "node-binary": "^1.1.0",
     "request": "^2.72.0",
-    "serialport": "^2.0.7-beta1",
-    "tar.gz": "^1.0.3"
+    "serialport": "^2.0.7-beta1"
   },
   "devDependencies": {
     "chai": "^3.5.0",