Advertisement
onezeroz

Untitled

Mar 15th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs/promises");
  2. const watch = require("fs").watch;
  3.  
  4. (async () => {
  5.   const createFile = async (path) => {
  6.     try {
  7.       // we want to check whether or not we already have that file
  8.       const existingFileHandle = await fs.open(path, "r");
  9.       existingFileHandle.close();
  10.       return console.log(`The file ${path} already exists`);
  11.     } catch (err) {
  12.       if (err.code === "ENOENT") {
  13.         const newFileHandle = await fs.open(path, "w");
  14.         console.log("A new file was successfully created");
  15.         newFileHandle.close();
  16.       } else {
  17.         console.log("An error occurred while creating the file: ");
  18.         console.error(err);
  19.       }
  20.     }
  21.   };
  22.  
  23.   const deleteFile = async (path) => {
  24.     try {
  25.       await fs.unlink(path);
  26.       return console.log(`The file ${path} was deleted`);
  27.     } catch (err) {
  28.       if (err.code === "ENOENT") {
  29.         console.log("No file at this path to remove");
  30.       } else {
  31.         console.log("An error occurred while removing the file: ");
  32.         console.error(err);
  33.       }
  34.     }
  35.   };
  36.  
  37.   const renameFile = async (oldPath, newPath) => {
  38.     try {
  39.       const newFileHandle = await fs.open(newPath);
  40.       newFileHandle.close();
  41.       return console.log(`The file ${newPath} already exists`);
  42.     } catch (err) {
  43.       try {
  44.         const oldFileHandle = await fs.open(oldPath);
  45.         oldFileHandle.close();
  46.         await fs.rename(oldPath, newPath);
  47.         console.log(`The file ${oldPath} was renamed to ${newPath}`);
  48.       } catch (err) {
  49.         console.error(err);
  50.         console.log(`No such file ${oldPath}`);
  51.       }
  52.     }
  53.   };
  54.  
  55.   const addToFile = async (path, content) => {
  56.     await fs.appendFile(path, content);
  57.     console.log(`Added ${content} to ${path}`);
  58.   };
  59.  
  60.   // commands
  61.   const CREATE_FILE = "create a file";
  62.   const DELETE_FILE = "delete the file";
  63.   const RENAME_FILE = "rename the file";
  64.   const ADD_TO_FILE = "add to the file";
  65.  
  66.   const commandFileHandler = await fs.open("./command.txt", "r");
  67.  
  68.   commandFileHandler.on("change", async () => {
  69.     // get the size of the file
  70.     const size = (await commandFileHandler.stat()).size;
  71.     // allocate the buffer with the size of the file
  72.     const buff = Buffer.alloc(size);
  73.     // the location at which we want to start filling our buffer
  74.     const offset = 0;
  75.     // how many bytes we want to read
  76.     const length = buff.byteLength;
  77.     // the position that we want to start reading the file from
  78.     const position = 0;
  79.  
  80.     // we always want to read the whole content (from begining all the way to the end)
  81.     await commandFileHandler.read(buff, offset, length, position);
  82.  
  83.     const command = buff.toString("utf-8");
  84.  
  85.     // create a file <path>
  86.     if (command.includes(CREATE_FILE)) {
  87.       const filePath = command.substring(CREATE_FILE.length + 1);
  88.       createFile(filePath);
  89.     }
  90.  
  91.     // delete a file <path>
  92.     if (command.includes(DELETE_FILE)) {
  93.       const filePath = command.substring(DELETE_FILE.length + 1);
  94.       deleteFile(filePath);
  95.     }
  96.  
  97.     // rename the file <old-path> to <new-path>
  98.     if (command.includes(RENAME_FILE)) {
  99.       const _idx = command.indexOf(" to ");
  100.       const oldFilePath = command.substring(RENAME_FILE.length + 1, _idx);
  101.       const newFilePath = command.substring(_idx + 4);
  102.  
  103.       renameFile(oldFilePath, newFilePath);
  104.     }
  105.  
  106.     // add to the file <path> this content: <content>
  107.     if (command.includes(ADD_TO_FILE)) {
  108.       const _idx = command.indexOf(" this content: ");
  109.       const filePath = command.substring(ADD_TO_FILE.length + 1, _idx);
  110.       const content = command.substring(_idx + 15);
  111.       addToFile(filePath, content);
  112.     }
  113.   });
  114.  
  115.   let fsTimeout;
  116.  
  117.   watch("./command.txt", (event) => {
  118.     if (!fsTimeout) {
  119.       console.log(event);
  120.       if (event === "change") {
  121.         commandFileHandler.emit("change");
  122.       }
  123.  
  124.       fsTimeout = setTimeout(() => {
  125.         fsTimeout = null;
  126.       }, 500);
  127.     }
  128.   });
  129. })();
Tags: nodejs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement