Skip to content

Commit ac1c4f2

Browse files
authored
Merge pull request #798 from particle-iot/feature/sc-133818/add-retry-flash-if-the-device-is-not-found
Feature/sc 133818/add retry flash if the device is not found
2 parents 8d7b2ec + eb46101 commit ac1c4f2

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/cmd/flash.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const { sourcePatterns, binaryPatterns, binaryExtensions } = require('../lib/fil
1616
const deviceOsUtils = require('../lib/device-os-version-util');
1717
const os = require('os');
1818
const semver = require('semver');
19+
1920
const {
2021
createFlashSteps,
2122
filterModulesToFlash,
@@ -120,8 +121,8 @@ module.exports = class FlashCommand extends CLICommandBase {
120121
await qdl.run();
121122
fs.appendFileSync(outputLog, `OS Download complete.${os.EOL}`);
122123
} catch (error) {
123-
fs.appendFileSync(outputLog, 'Download failed with error: ' + error.message);
124-
throw new Error('Download failed with error: ' + error.message);
124+
fs.appendFileSync(outputLog, error.message);
125+
throw error;
125126
}
126127
}
127128

src/lib/qdl.js

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const execa = require('execa');
22
const utilities = require('../lib/utilities');
33
const path = require('path');
44
const fs = require('fs-extra');
5-
const os = require('os');
65
const util = require('util');
76
const temp = require('temp').track();
87
const mkdirTemp = util.promisify(temp.mkdir);
8+
const os = require('os');
99

1010
const TACHYON_STORAGE_TYPE = 'ufs';
1111

@@ -42,18 +42,25 @@ class QdlFlasher {
4242
stdio: 'pipe'
4343
});
4444

45-
const handleStream = (stream) => {
46-
stream.on('data', chunk => {
47-
chunk.toString().split('\n').map(line => line.trim()).filter(Boolean).forEach(line => {
48-
this.processLogLine(line, qdlProcess);
45+
await new Promise((resolve, reject) => {
46+
const handleStream = (stream) => {
47+
stream.on('data', chunk => {
48+
chunk.toString().split('\n').map(line => line.trim()).filter(Boolean).forEach(this.processLogLine.bind(this));
4949
});
50-
});
51-
};
50+
};
5251

53-
handleStream(qdlProcess.stdout);
54-
handleStream(qdlProcess.stderr);
52+
handleStream(qdlProcess.stdout);
53+
handleStream(qdlProcess.stderr);
5554

56-
await qdlProcess;
55+
qdlProcess.on('close', (output) => {
56+
if (output !== 0) {
57+
return reject(new Error('Unable to complete device flashing. See logs for further details.'));
58+
} else {
59+
return resolve();
60+
}
61+
});
62+
qdlProcess.on('error', reject);
63+
});
5764
} finally {
5865
if (this.progressBarInitialized) {
5966
this.progressBar.stop();
@@ -89,25 +96,12 @@ class QdlFlasher {
8996
];
9097
}
9198

92-
processLogLine(line, process) {
99+
processLogLine(line) {
93100
fs.appendFileSync(this.outputLogFile, `${line}\n`);
94-
95101
if (line.includes('Waiting for EDL device')) {
96-
this.handleError(process, `Ensure your device is connected and in EDL mode${os.EOL}`);
97-
} else if (line.includes('[ERROR]')) {
98-
this.handleError(process, `${os.EOL}Error detected: ${line}${os.EOL}`);
99-
} else {
100-
this.processFlashingLogs(line);
101-
}
102-
}
103-
104-
handleError(process, message) {
105-
this.ui.stdout.write(message);
106-
process.kill();
107-
}
108-
109-
processFlashingLogs(line) {
110-
if (line.includes('status=getProgramInfo')) {
102+
const message = `Tachyon not found. Disconnect and reconnect the device, and ensure it is in EDL mode ${os.EOL}`;
103+
this.ui.stdout.write(this.ui.chalk.bold(this.ui.chalk.yellow(message)));
104+
} else if (line.includes('status=getProgramInfo')) {
111105
this.handleProgramInfo(line);
112106
} else if (line.includes('status=Start flashing module')) {
113107
this.handleModuleStart(line);

0 commit comments

Comments
 (0)