10000 implement `ilistdir` and tighten the indentation · arduino/micropython.js@754ad86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 754ad86

Browse files
committed
implement ilistdir and tighten the indentation
1 parent a1ce71b commit 754ad86

File tree

2 files changed

+203
-164
lines changed

2 files changed

+203
-164
lines changed

cli.js

Lines changed: 178 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -5,207 +5,223 @@ const Board = require('./micropython.js')
55
const log = console.log
66

77
const extractArguments = (args) => {
8-
return args.slice(2)
8+
return args.slice(2)
99
}
1010

1111
const extractCommands = (args) => {
12-
let commands = {}
13-
let currentCommand = null
14-
// TODO: Use reduce instead of forEach
15-
args.forEach((value) => {
16-
// If it's a command, turn set is as the current one
17-
if (value.slice(0, 2) === '--') {
18-
currentCommand = value
19-
}
20-
// If there isn't a key initialized for that command, do so
21-
if (!commands[currentCommand]) {
22-
commands[currentCommand] = []
23-
} else {
24-
// Otherwise push the values to the current command key
25-
commands[currentCommand].push(value)
26-
}
27-
})
28-
return commands
12+
let commands = {}
13+
let currentCommand = null
14+
// TODO: Use reduce instead of forEach
15+
args.forEach((value) => {
16+
// If it's a command, turn set is as the current one
17+
if (value.slice(0, 2) === '--') {
18+
currentCommand = value
19+
}
20+
// If there isn't a key initialized for that command, do so
21+
if (!commands[currentCommand]) {
22+
commands[currentCommand] = []
23+
} else {
24+
// Otherwise push the values to the current command key
25+
commands[currentCommand].push(value)
26+
}
27+
})
28+
return commands
2929
}
3030

3131
function ensurePort(port) {
32-
if (!port) throw new Error('You must specify a port.')
32+
if (!port) throw new Error('You must specify a port.')
3333
}
3434

3535
const listPorts = (args) => {
36-
const board = new Board()
37-
return board.listPorts()
38-
.then((ports) => {
39-
log('available ports', ports)
40-
return Promise.resolve()
41-
})
36+
const board = new Board()
37+
return board.listPorts()
38+
.then((ports) => {
39+
log('available ports', ports)
40+
return Promise.resolve()
41+
})
4242
}
4343

4444
const listFiles = (args, port) => {
45-
ensurePort(port)
46-
const board = new Board()
47-
return board.open(port)
48-
.then(async () => {
49-
const folder = args[0] || '/'
50-
try {
51-
const output = await board.fs_ls(folder)
52-
log(`files at "${folder}"`, output)
53-
} catch(e) {
54-
log('error', e)
55-
}
56-
board.close()
57-
return Promise.resolve()
58-
})
45+
ensurePort(port)
46+
const board = new Board()
47+
return board.open(port)
48+
.then(async () => {
49+
const folder = args[0] || '/'
50+
try {
51+
const output = await board.fs_ls(folder)
52+
log(`files at "${folder}"`, output)
53+
} catch(e) {
54+
log('error', e)
55+
}
56+
board.close()
57+
return Promise.resolve()
58+
})
59+
}
60+
61+
const ilistFiles = (args, port) => {
62+
ensurePort(port)
63+
const board = new Board()
64+
return board.open(port)
65+
.then(async () => {
66+
const folder = args[0] || '/'
67+
try {
68+
const output = await board.fs_ils(folder)
69+
log(`files at "${folder}"`, output)
70+
} catch(e) {
71+
log('error', e)
72+
}
73+
board.close()
74+
return Promise.resolve()
75+
})
5976
}
6077

6178
const executeString = (args, port, dataConsumer) => {
62-
ensurePort(port)
63-
const board = new Board()
64-
const code = args[0] || ''
65-
return board.open(port)
66-
.then(() => board.enter_raw_repl())
67-
.then(() => board.exec_raw({ command: code, data_consumer: dataConsumer }))
68-
.then(async (out) => {
69-
await board.exit_raw_repl()
70-
await board.close()
71-
log(out)
72-
return Promise.resolve()
73-
})
74-
.catch((err) => {
75-
log('error', err)
76-
board.exit_raw_repl(true)
77-
board.close()
78-
})
79+
ensurePort(port)
80+
const board = new Board()
81+
const code = args[0] || ''
82+
return board.open(port)
83+
.then(() => board.enter_raw_repl())
84+
.then(() => board.exec_raw({ command: code, data_consumer: dataConsumer }))
85+
.then(async (out) => {
86+
await board.exit_raw_repl()
87+
await board.close()
88+
log(out)
89+
return Promise.resolve()
90+
})
91+
.catch((err) => {
92+
log('error', err)
93+
board.exit_raw_repl(true)
94+
board.close()
95+
})
7996
}
8097

8198
const executeFile = (args, port, dataConsumer) => {
82-
ensurePort(port)
83-
const board = new Board()
84-
const filename = args[0] || ''
85-
const consumer = dataConsumer || function() {}
86-
return board.open(port)
87-
.then(async () => {
88-
try {
89-
const out = await board.execfile(filename, consumer)
90-
log(out)
91-
} catch(e) {
92-
log('error', e)
93-
}
94-
board.close()
95-
return Promise.resolve()
96-
})
99+
ensurePort(port)
100+
const board = new Board()
101+
const filename = args[0] || ''
102+
const consumer = dataConsumer || function() {}
103+
return board.open(port)
104+
.then(async () => {
105+
try {
106+
const out = await board.execfile(filename, consumer)
107+
log(out)
108+
} catch(e) {
109+
log('error', e)
110+
}
111+
board.close()
112+
return Promise.resolve()
113+
})
97114
}
98115

99116
const putFile = (args, port, dataConsumer) => {
100-
ensurePort(port)
101-
const board = new Board()
102-
const [ diskFilename, boardFilename ] = args
103-
const consumer = dataConsumer || function() {}
104-
return board.open(port)
105-
.then(async () => {
106-
try {
107-
const out = await board.fs_put(diskFilename, boardFilename, consumer)
108-
log(out)
109-
} catch(e) {
110-
log('error', e)
111-
}
112-
board.close()
113-
return Promise.resolve()
114-
})
117+
ensurePort(port)
118+
const board = new Board()
119+
const [ diskFilename, boardFilename ] = args
120+
const consumer = dataConsumer || function() {}
121+
return board.open(port)
122+
.then(async () => {
123+
try {
124+
const out = await board.fs_put(diskFilename, boardFilename, consumer)
125+
log(out)
126+
} catch(e) {
127+
log('error', e)
128+
}
129+
board.close()
130+
return Promise.resolve()
131+
})
115132
}
116133

117134
const getFile = (args, port, dataConsumer) => {
118-
ensurePort(port)
119-
const board = new Board()
120-
const [ boardFilename, diskFilename ] = args
121-
const consumer = dataConsumer || function() {}
122-
return board.open(port)
123-
.then(async () => {
124-
try {
125-
let output = await board.fs_cat(boardFilename, consumer)
126-
fs.writeFileSync(diskFilename, output)
127-
log('output')
128-
log(output)
129-
} catch(e) {
130-
log('error', e)
131-
}
132-
board.close()
133-
return Promise.resolve()
134-
})
135+
ensurePort(port)
136+
const board = new Board()
137+
const [ boardFilename, diskFilename ] = args
138+
const consumer = dataConsumer || function() {}
139+
return board.open(port)
140+
.then(async () => {
141+
try {
142+
let output = await board.fs_cat(boardFilename, consumer)
143+
fs.writeFileSync(diskFilename, output)
144+
log('output')
145+
log(output)
146+
} catch(e) {
147+
log('error', e)
148+
}
149+
board.close()
150+
return Promise.resolve()
151+
})
135152
}
136153

137154
const removeFile = (args, port) => {
138-
ensurePort(port)
139-
const board = new Board()
140-
const [ boardFilename ] = args
141-
142-
return board.open(port)
143-
.then(async () => {
144-
try {
145-
const out = await board.fs_rm(boardFilename)
146-
log(out)
147-
} catch(e) {
148-
log('error', e)
149-
}
150-
board.close()
151-
return Promise.resolve()
152-
})
155+
ensurePort(port)
156+
const board = new Board()
157+
const [ boardFilename ] = args
158+
159+
return board.open(port)
160+
.then(async () => {
161+
try {
162+
const out = await board.fs_rm(boardFilename)
163+
log(out)
164+
} catch(e) {
165+
log('error', e)
166+
}
167+
board.close()
168+
return Promise.resolve()
169+
})
153170
}
154171

155172
const removeFolder = (args, port) => {
156-
ensurePort(port)
157-
const board = new Board()
158-
const [ boardDirname ] = args
159-
160-
return board.open(port)
161-
.then(async () => {
162-
try {
163-
const out = await board.fs_rmdir(boardDirname)
164-
log(out)
165-
} catch(e) {
166-
log('error', e)
167-
}
168-
board.close()
169-
return Promise.resolve()
170-
})
173+
ensurePort(port)
174+
const board = new Board()
175+
const [ boardDirname ] = args
176+
177+
return board.open(port)
178+
.then(async () => {
179+
try {
180+
const out = await board.fs_rmdir(boardDirname)
181+
log(out)
182+
} catch(e) {
183+
log('error', e)
184+
}
185+
board.close()
186+
return Promise.resolve()
187+
})
171188
}
172189

173190
const operations = {
174-
'--listports': listPorts,
175-
'--listfiles': listFiles,
176-
'--executestring': executeString,
177-
'--executefile': executeFile,
178-
'--putfile': putFile,
179-
'--getfile': getFile,
180-
'--removefile': removeFile,
181-
'--removefolder': removeFolder,
182-
'--verbose': () => false
191+
'--listports': listPorts,
192+
'--listfiles': listFiles,
193+
'--ilistfiles': ilistFiles,
194+
'--executestring': executeString,
195+
'--executefile': executeFile,
196+
'--putfile': putFile,
197+
'--getfile': getFile,
198+
'--removefile': removeFile,
199+
'--removefolder': removeFolder,
200+
'--verbose': () => false
183201
}
184202

185203
let args = extractArguments(process.argv)
186204
let commands = extractCommands(args)
187205
let port = commands['--port'] ? commands['--port'][0] : null
188206

189207
if (commands['--verbose']) {
190-
log('VERBOSE')
191-
Object.keys(commands)
192-
.filter((command) => command !== '--port')
193-
.filter((command) => command !== '--verbose')
194-
.forEach((command) => {
195-
log('executing command:')
196-
log('command', command)
197-
log('arguments', commands[command])
198-
log('port', port)
199-
operations[command](commands[command], port, log)
200-
.then(() => log('command executed', command, `\r\n`))
201-
.catch((e) => log('error', e, `\r\n`))
202-
})
208+
log('VERBOSE')
209+
Object.keys(commands)
210+
.filter((command) => command !== '--port')
211+
.filter((command) => command !== '--verbose')
212+
.forEach((command) => {
213+
log('executing command:')
214+
log('command', command)
215+
log('arguments', commands[command])
216+
log('port', port)
217+
operations[command](commands[command], port, log)
218+
.then(() => log('command executed', command, `\r\n`))
219+
.catch((e) => log('error', e, `\r\n`))
220+
})
203221
} else {
204-
Object.keys(commands)
205-
.filter((command) => command !== '--port')
206-
.forEach((command) => {
207-
operations[command](commands[command], port)
208-
})
222+
Object.keys(commands)
223+
.filter((command) => command !== '--port')
224+
.forEach((command) => {
225+
operations[command](commands[command], port)
226+
})
209227
}
210-
211-

0 commit comments

Comments
 (0)
0