-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (114 loc) · 3.9 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fs = require("fs/promises");
(async () => {
// 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 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();
// we already have that file...
return console.log(`The file ${path} already exists.`);
} catch (e) {
// we don't have the file, now we should create it
const newFileHandle = await fs.open(path, "w");
console.log("A new file was successfully created.");
newFileHandle.close();
}
};
const deleteFile = async (path) => {
try {
await fs.unlink(path);
console.log("The file was successfully removed.");
} catch (e) {
if (e.code === "ENOENT") {
console.log("No file at this path to remove.");
} else {
console.log("An error occurred while removing the file: ");
console.log(e);
}
}
};
const renameFile = async (oldPath, newPath) => {
try {
await fs.rename(oldPath, newPath);
console.log("The file was successfully renamed.");
} catch (e) {
if (e.code === "ENOENT") {
console.log(
"No file at this path to rename, or the destination doesn't exist."
);
} else {
console.log("An error occurred while removing the file: ");
console.log(e);
}
}
};
let addedContent;
const addToFile = async (path, content) => {
if (addedContent === content) return;
try {
const fileHandle = await fs.open(path, "a");
fileHandle.write(content);
addedContent = content;
console.log("The content was added successfully.");
} catch (e) {
console.log("An error occurred while removing the file: ");
console.log(e);
}
};
const commandFileHandler = await fs.open("./command.txt", "r");
commandFileHandler.on("change", async () => {
// get the size of our file
const size = (await commandFileHandler.stat()).size;
// allocate our 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 beginning all the way to the end)
await commandFileHandler.read(buff, offset, length, position);
const command = buff.toString("utf-8");
// create a file:
// create a file <path>
if (command.includes(CREATE_FILE)) {
const filePath = command.substring(CREATE_FILE.length + 1);
createFile(filePath);
}
// delete a file
// delete the file <path>
if (command.includes(DELETE_FILE)) {
const filePath = command.substring(DELETE_FILE.length + 1);
deleteFile(filePath);
}
// rename file:
// rename the file <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 file:
// 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);
}
});
// watcher...
const watcher = fs.watch("./command.txt");
for await (const event of watcher) {
if (event.eventType === "change") {
commandFileHandler.emit("change");
}
}
})();