@@ -137,10 +137,6 @@ export class ArduinoApp {
137
137
arduinoChannel . show ( ) ;
138
138
arduinoChannel . start ( `Upload sketch - ${ dc . sketch } ` ) ;
139
139
140
- const serialMonitor = SerialMonitor . getInstance ( ) ;
141
-
142
- const needRestore = await serialMonitor . closeSerialMonitor ( dc . port ) ;
143
- UsbDetector . getInstance ( ) . pauseListening ( ) ;
144
140
await vscode . workspace . saveAll ( false ) ;
145
141
146
142
if ( ! await this . runPreBuildCommand ( dc ) ) {
@@ -205,17 +201,28 @@ export class ArduinoApp {
205
201
const msg = "Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README." ;
206
202
arduinoChannel . warning ( msg ) ;
207
203
}
204
+
205
+ // stop serial monitor when everything is prepared and good
206
+ // what makes restoring of its previous state easier
207
+ const restoreSerialMonitor = await SerialMonitor . getInstance ( ) . closeSerialMonitor ( dc . port ) ;
208
+ UsbDetector . getInstance ( ) . pauseListening ( ) ;
209
+
210
+ const cleanup = async ( ) => {
211
+ UsbDetector . getInstance ( ) . resumeListening ( ) ;
212
+ if ( restoreSerialMonitor ) {
213
+ await SerialMonitor . getInstance ( ) . openSerialMonitor ( ) ;
214
+ }
215
+ }
216
+
208
217
await util . spawn (
209
218
this . _settings . commandPath ,
210
219
arduinoChannel . channel ,
211
220
args ,
212
221
) . then ( async ( ) => {
213
- UsbDetector . getInstance ( ) . resumeListening ( ) ;
214
- if ( needRestore ) {
215
- await serialMonitor . openSerialMonitor ( ) ;
216
- }
222
+ await cleanup ( ) ;
217
223
arduinoChannel . end ( `Uploaded the sketch: ${ dc . sketch } ${ os . EOL } ` ) ;
218
- } , ( reason ) => {
224
+ } , async ( reason ) => {
225
+ await cleanup ( ) ;
219
226
const msg = reason . code ?
220
227
`Exit with code=${ reason . code } ${ os . EOL } ` :
221
228
reason . message ?
@@ -292,16 +299,22 @@ export class ArduinoApp {
292
299
let success = false ;
293
300
const compilerParserContext = makeCompilerParserContext ( dc ) ;
294
301
302
+ const cleanup = async ( ) => {
303
+ await Promise . resolve ( ) ;
304
+ }
305
+
295
306
await util . spawn (
296
307
this . _settings . commandPath ,
297
308
arduinoChannel . channel ,
298
309
args ,
299
310
undefined ,
300
311
compilerParserContext . callback ,
301
- ) . then ( ( ) => {
312
+ ) . then ( async ( ) => {
313
+ await cleanup ( ) ;
302
314
arduinoChannel . end ( `Finished verifying sketch - ${ dc . sketch } ${ os . EOL } ` ) ;
303
315
success = true ;
304
- } , ( reason ) => {
316
+ } , async ( reason ) => {
317
+ await cleanup ( ) ;
305
318
const msg = reason . code ?
306
319
`Exit with code=${ reason . code } ${ os . EOL } ` :
307
320
reason . message ?
0 commit comments