@@ -2,6 +2,8 @@ const fs = require('fs')
2
2
const path = require ( 'path' )
3
3
const Board = require ( './micropython.js' )
4
4
5
+ const log = console . log
6
+
5
7
const extractArguments = ( args ) => {
6
8
return args . slice ( 2 )
7
9
}
@@ -34,7 +36,7 @@ const listPorts = (args) => {
34
36
const board = new Board ( )
35
37
board . listPorts ( )
36
38
. then ( ( ports ) => {
37
- console . log ( 'available ports' , ports )
39
+ log ( 'available ports' , ports )
38
40
} )
39
41
}
40
42
@@ -46,77 +48,82 @@ const listFiles = (args, port) => {
46
48
const folder = args [ 0 ] || '/'
47
49
try {
48
50
const output = await board . fs_ls ( folder )
49
- console . log ( `files at "${ folder } "` , output )
51
+ log ( `files at "${ folder } "` , output )
50
52
} catch ( e ) {
51
- console . log ( 'error' , e )
53
+ log ( 'error' , e )
52
54
}
53
55
board . close ( )
54
56
} )
55
57
}
56
58
57
- const executeString = ( args , port ) => {
59
+ const executeString = ( args , port , dataConsumer ) => {
58
60
ensurePort ( port )
59
61
const board = new Board ( )
60
62
const code = args [ 0 ] || ''
61
63
board . open ( port )
62
64
. then ( ( ) => board . enter_raw_repl ( ) )
63
- . then ( ( ) => board . exec_raw ( { command : code } ) )
65
+ . then ( ( ) => board . exec_raw ( { command : code , data_consumer : dataConsumer } ) )
64
66
. then ( async ( out ) => {
65
67
await board . exit_raw_repl ( )
66
68
await board . close ( )
67
- console . log ( out )
69
F987
+ log ( out )
68
70
} )
69
71
. catch ( ( err ) => {
70
- console . log ( 'error' )
71
- console . log ( err )
72
+ log ( 'error' , err )
72
73
board . exit_raw_repl ( true )
73
74
board . close ( )
74
75
} )
75
76
}
76
77
77
- const executeFile = ( args , port ) => {
78
+ const executeFile = ( args , port , dataConsumer ) => {
78
79
ensurePort ( port )
79
80
const board = new Board ( )
80
81
const filename = args [ 0 ] || ''
82
+ const consumer = dataConsumer || function ( ) { }
81
83
board . open ( port )
82
84
. then ( async ( ) => {
83
85
try {
84
- const out = await board . execfile ( filename )
85
- console . log ( out )
86
+ const out = await board . execfile ( filename , consumer )
87
+ log ( out )
86
88
} catch ( e ) {
87
- console . log ( 'error' , e )
89
+ log ( 'error' , e )
88
90
}
89
91
board . close ( )
90
92
} )
91
93
}
92
94
93
- const putFile = ( args , port ) => {
95
+ const putFile = ( args , port , dataConsumer ) => {
94
96
ensurePort ( port )
95
97
const board = new Board ( )
96
98
const [ diskFilename , boardFilename ] = args
97
- board . open ( port )
99
+ const consumer = dataConsumer || function ( ) { }
100
+ return board . open ( port )
98
101
. then ( async ( ) => {
99
102
try {
100
- const out = await board . fs_put ( diskFilename , boardFilename )
101
- console . log ( out )
103
+ const out = await board . fs_put ( diskFilename , boardFilename , consumer )
104
+ log ( out )
102
105
} catch ( e ) {
103
- console . log ( 'error' , e )
106
+ log ( 'error' , e )
104
107
}
105
108
board . close ( )
109
+ return Promise . resolve ( )
106
110
} )
107
111
}
108
112
109
- const getFile = ( args , port ) => {
113
+ const getFile = ( args , port , dataConsumer ) => {
110
114
ensurePort ( port )
111
115
const board = new Board ( )
112
116
const [ boardFilename , diskFilename ] = args
117
+ const consumer = dataConsumer || function ( ) { }
113
118
board . open ( port )
114
119
. then ( async ( ) => {
115
120
try {
116
- let output = await board . fs_cat ( boardFilename )
121
+ let output = await board . fs_cat ( boardFilename , consumer )
117
122
fs . writeFileSync ( diskFilename , output )
123
+ log ( 'output' )
124
+ log ( output )
118
125
} catch ( e ) {
119
- console . log ( 'error' , e )
126
+ log ( 'error' , e )
120
127
}
121
128
board . close ( )
122
129
} )
@@ -131,9 +138,9 @@ const removeFile = (args, port) => {
131
138
. then ( async ( ) => {
132
139
try {
133
140
const out = await board . fs_rm ( boardFilename )
134
- console . log ( out )
141
+ log ( out )
135
142
} catch ( e ) {
136
- console . log ( 'error' , e )
143
+ log ( 'error' , e )
137
144
}
138
145
board . close ( )
139
146
} )
@@ -148,9 +155,9 @@ const removeFolder = (args, port) => {
148
155
. then ( async ( ) => {
149
156
try {
150
157
const out = await board . fs_rmdir ( boardDirname )
151
- console . log ( out )
158
+ log ( out )
152
159
} catch ( e ) {
153
- console . log ( 'error' , e )
160
+ log ( 'error' , e )
154
161
}
155
162
board . close ( )
156
163
} )
@@ -164,16 +171,34 @@ const operations = {
164
171
'--putfile' : putFile ,
165
172
'--getfile' : getFile ,
166
173
'--removefile' : removeFile ,
167
- '--removefolder' : removeFolder
174
+ '--removefolder' : removeFolder ,
175
+ '--verbose' : ( ) => false
168
176
}
169
177
170
178
let args = extractArguments ( process . argv )
171
179
let commands = extractCommands ( args )
172
180
let port = commands [ '--port' ] ? commands [ '--port' ] [ 0 ] : null
173
181
174
- Object . keys ( commands )
175
- . filter ( ( command ) => command !== '--port' )
176
- . forEach ( ( command ) => {
177
- operations [ command ] ( commands [ command ] , port )
178
- } )
182
+ if ( commands [ '--verbose' ] ) {
183
+ log ( 'VERBOSE' )
184
+ Object . keys ( commands )
185
+ . filter ( ( command ) => command !== '--port' )
186
+ . filter ( ( command ) => command !== '--verbose' )
187
+ . forEach ( ( command ) => {
188
+ log ( 'executing command:' )
189
+ log ( 'command' , command )
190
+ log ( 'arguments' , commands [ command ] )
191
+ log ( 'port' , port )
192
+ operations [ command ] ( commands [ command ] , port , log )
193
+ . then ( ( ) => log ( 'command executed' , command , `\r\n` ) )
194
+ . catch ( ( e ) => log ( 'error' , e , `\r\n` ) )
195
+ } )
196
+ } else {
197
+ Object . keys ( commands )
198
+ . filter ( ( command ) => command !== '--port' )
199
+ . forEach ( ( command ) => {
200
+ operations [ command ] ( commands [ command ] , port )
201
+ } )
202
+ }
203
+
179
204
0 commit comments