@@ -19,6 +19,7 @@ function getHelp() {
19
19
" -e command : Evaluate the given expression on Espruino" ,
20
20
" If no file to upload is specified but you use -e," ,
21
21
" Espruino will not be reset" ,
22
+ " --sleep 10 : Sleep for the given number of seconds after uploading code" ,
22
23
" -n : Do not connect to Espruino to upload code" ,
23
24
" --board BRDNAME/BRD.json : Rather than checking on connect, use the given board name or file" ,
24
25
" --ide [8080] : Serve up the Espruino Web IDE on the given port. If not specified, 8080 is the default." ,
@@ -64,6 +65,9 @@ var args = {
64
65
storageContents : { } // Storage files to save when using ohex
65
66
} ;
66
67
68
+ var isNextValidNumber = function ( next ) {
69
+ return next && isFinite ( parseFloat ( next ) ) ;
70
+ }
67
71
var isNextValidPort = function ( next ) {
68
72
return next && next [ 0 ] !== '-' && next . indexOf ( ".js" ) == - 1 ;
69
73
}
@@ -154,6 +158,9 @@ for (var i=2;i<process.argv.length;i++) {
154
158
args . ideServer = parseInt ( next ) ;
155
159
i ++ ;
156
160
}
161
+ } else if ( arg == "--sleep" ) {
162
+ i ++ ; args . sleepAfterUpload = parseFloat ( next ) ;
163
+ if ( ! isNextValidNumber ( next ) ) throw new Error ( "Expecting a number argument to --sleep" ) ;
157
164
} else throw new Error ( "Unknown Argument '" + arg + "', try --help" ) ;
158
165
} else {
159
166
if ( "file" in args )
@@ -440,7 +447,15 @@ function sendCode(callback) {
440
447
require ( "fs" ) . writeFileSync ( args . outputHEX , toIntelHex ( storage ) ) ;
441
448
}
442
449
if ( ! args . nosend )
443
- Espruino . Core . CodeWriter . writeToEspruino ( code , callback ) ;
450
+ Espruino . Core . CodeWriter . writeToEspruino ( code , function ( ) {
451
+ if ( args . sleepAfterUpload ) {
452
+ log ( "Upload Complete. Sleeping for " + args . sleepAfterUpload + "s" ) ;
453
+ setTimeout ( callback , args . sleepAfterUpload * 1000 ) ;
454
+ } else {
455
+ log ( "Upload Complete" ) ;
456
+ callback ( ) ;
457
+ }
458
+ } ) ;
444
459
else
445
460
callback ( ) ;
446
461
} ) ;
@@ -455,10 +470,9 @@ function connect(devicePath, exitCallback) {
455
470
if ( ! args . quiet ) if ( ! args . nosend ) log ( "Connecting to '" + devicePath + "'" ) ;
456
471
var currentLine = "" ;
457
472
var exitTimeout ;
473
+ // Handle received data
458
474
Espruino . Core . Serial . startListening ( function ( data ) {
459
- // convert ArrayBuffer to string
460
475
data = String . fromCharCode . apply ( null , new Uint8Array ( data ) ) ;
461
- // Now handle...
462
476
currentLine += data ;
463
477
while ( currentLine . indexOf ( "\n" ) >= 0 ) {
464
478
var i = currentLine . indexOf ( "\n" ) ;
@@ -684,13 +698,18 @@ function getPortPath(port, callback) {
684
698
} else throw new Error ( "Unknown port type! " + JSON . stringify ( port ) ) ;
685
699
}
686
700
701
+ function tasksComplete ( ) {
702
+ console . log ( "Done" ) ;
703
+ process . exit ( 0 ) ;
704
+ }
705
+
687
706
function startConnect ( ) {
688
707
if ( ( ! args . file && ! args . updateFirmware && ! args . expr ) || ( args . file && args . watchFile ) ) {
689
708
if ( args . ports . length != 1 )
690
709
throw new Error ( "Can only have one port when using terminal mode" ) ;
691
710
692
- getPortPath ( args . ports [ 0 ] , function ( path ) {
693
- terminal ( path , function ( ) { process . exit ( 0 ) ; } ) ;
711
+ getPortPath ( args . ports [ 0 ] , function ( path ) {
712
+ terminal ( path , tasksComplete ) ;
694
713
} ) ;
695
714
} else {
696
715
//closure for stepping through each port
@@ -700,7 +719,7 @@ function startConnect() {
700
719
this . idx = 0 ;
701
720
this . connect = connect ;
702
721
this . iterate = function ( ) {
703
- if ( idx >= ports . length ) process . exit ( 0 ) ;
722
+ if ( idx >= ports . length ) tasksComplete ( ) ;
704
723
else getPortPath ( ports [ idx ++ ] , function ( path ) {
705
724
connect ( path , iterate ) ;
706
725
} ) ;
@@ -716,10 +735,7 @@ function main() {
716
735
if ( args . ports . length == 0 && ( args . outputJS || args . outputHEX ) ) {
717
736
console . log ( "No port supplied, but output file listed - not connecting" ) ;
718
737
args . nosend = true ;
719
- sendCode ( function ( ) {
720
- log ( "File written. Exiting." ) ;
721
- process . exit ( 1 ) ;
722
- } ) ;
738
+ sendCode ( tasksComplete ) ;
723
739
} else if ( args . ports . length == 0 || args . showDevices ) {
724
740
console . log ( "Searching for serial ports..." ) ;
725
741
Espruino . Core . Serial . getPorts ( function ( ports , shouldCallAgain ) {
0 commit comments