|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const Board = require('./micropython.js') |
| 4 | + |
| 5 | +const extractArguments = (args) => { |
| 6 | + return args.slice(2) |
| 7 | +} |
| 8 | + |
| 9 | +const extractCommands = (args) => { |
| 10 | + let commands = {} |
| 11 | + let currentCommand = null |
| 12 | + // TODO: Use reduce instead of forEach |
| 13 | + args.forEach((value) => { |
| 14 | + // If it's a command, turn set is as the current one |
| 15 | + if (value.slice(0, 2) === '--') { |
| 16 | + currentCommand = value |
| 17 | + } |
| 18 | + // If there isn't a key initialized for that command, do so |
| 19 | + if (!commands[currentCommand]) { |
| 20 | + commands[currentCommand] = [] |
| 21 | + } else { |
| 22 | + // Otherwise push the values to the current command key |
| 23 | + commands[currentCommand].push(value) |
| 24 | + } |
| 25 | + }) |
| 26 | + return commands |
| 27 | +} |
| 28 | + |
| 29 | +const extractFileArray = (output) => { |
| 30 | + output = output.replace(/'/g, '"'); |
| 31 | + output = output.split('OK') |
| 32 | + let files = output[2] || '' |
| 33 | + files = files.slice(0, files.indexOf(']')+1) |
| 34 | + files = JSON.parse(files) |
| 35 | + return files |
| 36 | +} |
| 37 | + |
| 38 | +function ensurePort(port) { |
| 39 | + if (!port) throw new Error('You must specify a port.') |
| 40 | +} |
| 41 | + |
| 42 | +const listPorts = (args) => {
F42D
code> |
| 43 | + const board = new Board() |
| 44 | + board.listPorts() |
| 45 | + .then((ports) => { |
| 46 | + console.log('available ports', ports) |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +const listFiles = (args, port) => { |
| 51 | + ensurePort(port) |
| 52 | + const board = new Board() |
| 53 | + board.open(port) |
| 54 | + .then(async () => { |
| 55 | + const folder = args[0] || '/' |
| 56 | + try { |
| 57 | + let output = await board.fs_ls(folder) |
| 58 | + let files = extractFileArray(output) |
| 59 | + console.log(`files at "${folder}"`, files) |
| 60 | + } catch(e) { |
| 61 | + console.log('error', e) |
| 62 | + } |
| 63 | + board.close() |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +const executeString = (args, port) => { |
| 68 | + ensurePort(port) |
| 69 | + const board = new Board() |
| 70 | + const code = args[0] || '' |
| 71 | + board.open(port) |
| 72 | + .then(() => board.enter_raw_repl()) |
| 73 | + .then(() => board.exec_raw({ command: code })) |
| 74 | + .then((out) => { |
| 75 | + console.log(out) |
| 76 | + return board.exit_raw_repl() |
| 77 | + }) |
| 78 | + .then(() => board.close()) |
| 79 | + .catch((err) => { |
| 80 | + console.log('error') |
| 81 | + console.log(err) |
| 82 | + board.exit_raw_repl(true) |
| 83 | + board.close() |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +const executeFile = (args, port) => { |
| 88 | + ensurePort(port) |
| 89 | + let board = new Board() |
| 90 | + const filename = args[0] || '' |
| 91 | + board.open(port) |
| 92 | + .then(async () => { |
| 93 | + try { |
| 94 | + await board.execfile(filename) |
| 95 | + } catch(e) { |
| 96 | + console.log('error', e) |
| 97 | + } |
| 98 | + board.close() |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +const putFile = (args, port) => { |
| 103 | + ensurePort(port) |
| 104 | + const board = new Board() |
| 105 | + const [ diskFilename, boardFilename ] = args |
| 106 | + board.open(port) |
| 107 | + .then(async () => { |
| 108 | + try { |
| 109 | + await board.fs_put(diskFilename, boardFilename) |
| 110 | + } catch(e) { |
| 111 | + console.log('error', e) |
| 112 | + } |
| 113 | + board.close() |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +const getFile = (args, port) => { |
| 118 | + ensurePort(port) |
| 119 | + const board = new Board() |
| 120 | + const [ boardFilename, diskFilename ] = args |
| 121 | + board.open(port) |
| 122 | + .then(async () => { |
| 123 | + try { |
| 124 | + let output = await board.fs_cat(boardFilename) |
| 125 | + output = output.split('OK') |
| 126 | + console.log(output[2]) |
| 127 | + fs.writeFileSync(diskFilename, output[2]) |
| 128 | + } catch(e) { |
| 129 | + console.log('error', e) |
| 130 | + } |
| 131 | + board.close() |
| 132 | + }) |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +const operations = { |
| 137 | + '--listports': listPorts, |
| 138 | + '--listfiles': listFiles, |
| 139 | + '--executestring': executeString, |
| 140 | + '--executefile': executeFile, |
| 141 | + '--putfile': putFile, |
| 142 | + '--getfile': getFile |
| 143 | +} |
| 144 | + |
| 145 | +let args = extractArguments(process.argv) |
| 146 | +let commands = extractCommands(args) |
| 147 | +let port = commands['--port'] ? commands['--port'][0] : null |
| 148 | + |
| 149 | +Object.keys(commands) |
| 150 | + .filter((command) => command !== '--port') |
| 151 | + .forEach((command) => { |
| 152 | + operations[command](commands[command], port) |
| 153 | + }) |
| 154 | + |
0 commit comments