1
import { Keywords } from "../flows/Keywords";
2
import { appendFile, getBuffer, readFile } from "../utils/Util";
3
import { Command } from "./Command";
5
export class Echo extends Command {
6
async execute(): Promise<string | null> {
7
if(!this.isValid(Keywords.ECHO)) return `Wrong sintax of ${Keywords.ECHO}`;
9
const fileData: string = await this.storage.downloadFromBuffer(this.getFileName());
10
await this.storage.uploadBuffer(this.getFileName(), fileData + "\n" + this.getValue(), "");
11
return `${this.getFileName()} is updated`;
13
return this.getValue();
16
isValid(keyword: Keywords): boolean {
17
if(!this.getCurrentText().startsWith(keyword) ||
18
this.getCurrentText().length < 6 ||
19
this.getCurrentText().split(" ").length < 1 ||
20
this.getValue() === null
25
getValue(): string | null {
26
const matched: string[] | null = this.getTail().match(/(["'])(?:(?=(\\?))\2.)*?\1/);
27
return matched ? matched[0].slice(1, -1) : null;
30
isAddable(): boolean {
31
return this.getCurrentText().includes(">>");
34
getFileName(): string {
35
return this.getCurrentText().split(">>")[1].trim();
38
getFileNameNoPath(): string {
39
const lastIndex: number = this.getFileName().lastIndexOf("/");
40
return lastIndex === -1 ? this.getFileName() : this.getFileName().substring(lastIndex, this.getTail().length - 1);