Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs/promises");
- const watch = require("fs").watch;
- (async () => {
- const createFile = async (path) => {
- try {
- // we want to check whether or not we already have that file
- const existingFileHandle = await fs.open(path, "r");
- existingFileHandle.close();
- return console.log(`The file ${path} already exists`);
- } catch (err) {
- if (err.code === "ENOENT") {
- const newFileHandle = await fs.open(path, "w");
- console.log("A new file was successfully created");
- newFileHandle.close();
- } else {
- console.log("An error occurred while creating the file: ");
- console.error(err);
- }
- }
- };
- const deleteFile = async (path) => {
- try {
- await fs.unlink(path);
- return console.log(`The file ${path} was deleted`);
- } catch (err) {
- if (err.code === "ENOENT") {
- console.log("No file at this path to remove");
- } else {
- console.log("An error occurred while removing the file: ");
- console.error(err);
- }
- }
- };
- const renameFile = async (oldPath, newPath) => {
- try {
- const newFileHandle = await fs.open(newPath);
- newFileHandle.close();
- return console.log(`The file ${newPath} already exists`);
- } catch (err) {
- try {
- const oldFileHandle = await fs.open(oldPath);
- oldFileHandle.close();
- await fs.rename(oldPath, newPath);
- console.log(`The file ${oldPath} was renamed to ${newPath}`);
- } catch (err) {
- console.error(err);
- console.log(`No such file ${oldPath}`);
- }
- }
- };
- const addToFile = async (path, content) => {
- await fs.appendFile(path, content);
- console.log(`Added ${content} to ${path}`);
- };
- // commands
- const CREATE_FILE = "create a file";
- const DELETE_FILE = "delete the file";
- const RENAME_FILE = "rename the file";
- const ADD_TO_FILE = "add to the file";
- const commandFileHandler = await fs.open("./command.txt", "r");
- commandFileHandler.on("change", async () => {
- // get the size of the file
- const size = (await commandFileHandler.stat()).size;
- // allocate the buffer with the size of the file
- const buff = Buffer.alloc(size);
- // the location at which we want to start filling our buffer
- const offset = 0;
- // how many bytes we want to read
- const length = buff.byteLength;
- // the position that we want to start reading the file from
- const position = 0;
- // we always want to read the whole content (from begining all the way to the end)
- await commandFileHandler.read(buff, offset, length, position);
- const command = buff.toString("utf-8");
- // create a file <path>
- if (command.includes(CREATE_FILE)) {
- const filePath = command.substring(CREATE_FILE.length + 1);
- createFile(filePath);
- }
- // delete a file <path>
- if (command.includes(DELETE_FILE)) {
- const filePath = command.substring(DELETE_FILE.length + 1);
- deleteFile(filePath);
- }
- // rename the file <old-path> to <new-path>
- if (command.includes(RENAME_FILE)) {
- const _idx = command.indexOf(" to ");
- const oldFilePath = command.substring(RENAME_FILE.length + 1, _idx);
- const newFilePath = command.substring(_idx + 4);
- renameFile(oldFilePath, newFilePath);
- }
- // add to the file <path> this content: <content>
- if (command.includes(ADD_TO_FILE)) {
- const _idx = command.indexOf(" this content: ");
- const filePath = command.substring(ADD_TO_FILE.length + 1, _idx);
- const content = command.substring(_idx + 15);
- addToFile(filePath, content);
- }
- });
- let fsTimeout;
- watch("./command.txt", (event) => {
- if (!fsTimeout) {
- console.log(event);
- if (event === "change") {
- commandFileHandler.emit("change");
- }
- fsTimeout = setTimeout(() => {
- fsTimeout = null;
- }, 500);
- }
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement