Files
mixly3-server/src/common/shell-ampy.js

104 lines
3.3 KiB
JavaScript

import mustache from 'mustache';
import Shell from './shell.js';
import { MICROPYTHON, PYTHON } from './config.js';
export default class ShellAmpy extends Shell {
static {
this.TEMPLATE = {
ls: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 ls "{{&folderPath}}"',
get: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 get "{{&filePath}}"',
mkdir: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 mkdir "{{&folderPath}}"',
mkfile: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 mkfile "{{&filePath}}"',
isdir: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 isdir "{{&folderPath}}"',
isfile: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 isfile "{{&filePath}}"',
put: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 put "{{&startPath}}" "{{&endPath}}"',
rm: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 rm "{{&filePath}}"',
rmdir: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 rmdir "{{&folderPath}}"',
rename: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 rename "{{&oldPath}}" "{{&newPath}}"',
run: '{{&ampy}} -p {{&port}} -b {{&baud}} -i 0 run "{{&filePath}}"'
}
this.AMPY_TEMPLATE = mustache.render('"{{&python3}}" "{{&ampy}}"', {
python3: PYTHON.path.cli,
ampy: MICROPYTHON.path.ampy
});
}
constructor() {
super();
}
async ls(port, baud, folderPath) {
return this.exec(this.render('ls', { port, baud, folderPath }), {
encoding: 'utf-8'
});
}
async get(port, baud, filePath) {
return this.exec(this.render('get', { port, baud, filePath }), {
encoding: 'utf-8'
});
}
async mkdir(port, baud, folderPath) {
return this.exec(this.render('mkdir', { port, baud, folderPath }), {
encoding: 'utf-8'
});
}
async mkfile(port, baud, filePath) {
return this.exec(this.render('mkfile', { port, baud, filePath }), {
encoding: 'utf-8'
});
}
async isdir(port, baud, folderPath) {
return this.exec(this.render('isdir', { port, baud, folderPath }), {
encoding: 'utf-8'
});
}
async isfile(port, baud, filePath) {
return this.exec(this.render('isfile', { port, baud, filePath }), {
encoding: 'utf-8'
});
}
async put(port, baud, startPath, endPath) {
return this.exec(this.render('put', { port, baud, startPath, endPath }), {
encoding: 'utf-8'
});
}
async rm(port, baud, filePath) {
return this.exec(this.render('rm', { port, baud, filePath }), {
encoding: 'utf-8'
});
}
async rmdir(port, baud, folderPath) {
return this.exec(this.render('rmdir', { port, baud, folderPath }), {
encoding: 'utf-8'
});
}
async rename(port, baud, oldPath, newPath) {
return this.exec(this.render('rename', { port, baud, oldPath, newPath }), {
encoding: 'utf-8'
});
}
async run(port, baud, filePath) {
return this.exec(this.render('run', { port, baud, filePath }), {
encoding: 'utf-8'
});
}
render(templateName, args) {
return mustache.render(ShellAmpy.TEMPLATE[templateName], {
...args,
ampy: ShellAmpy.AMPY_TEMPLATE
});
}
}