-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
207 lines (164 loc) · 6.4 KB
/
app.js
File metadata and controls
207 lines (164 loc) · 6.4 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as fs from "node:fs/promises";
import { Buffer } from "node:buffer";
// open (32) file description
// read or write
const CREATE_FILE_COMMAND = "create a file";
const DELETE_FILE_COMMAND = "delete the file";
const RENAME_FILE_COMMAND = "rename the file";
const ADD_TO_FILE_COMMAND = "add to the file";
(async () => {
const createFile = async (path) => {
try {
const existingFileHandle = await fs.open(path, "r");
await existingFileHandle.close();
return console.log(`${path} adlı dosya zaten var!`);
} catch (error) {
const newFileHandle = await fs.open(path, "w");
console.log("Yeni Dosya Başarıyla Oluşturuldu");
await newFileHandle.close();
}
};
const deleteFile = async (path) => {
try {
await fs.unlink(path);
console.log("Dosya başarıyla silindi.");
} catch (error) {
if (error.code === "ENOENT") {
console.log(`${path} dosyası bulunamadı (zaten silinmiş olabilir).`);
} else {
console.log("Bir hatayla karşılaşıldı", error.message);
}
}
};
async function renameFile(oldPath, newPath){
try{
await fs.rename(oldPath, newPath);
console.log("Dosya adı başarıyla değiştirildi");
} catch (error) {
if(error.code === "ENOENT"){
console.log(`${oldPath} dosyası bulunamadı (silinmiş olabilir).`);
} else {
console.log("bir hatayla karşılaşıldı", error.message);
}
}
}
let addedContent;
async function addToFile(path, content) {
let fileHandle;
if (addedContent === content) return;
try {
fileHandle = await fs.open(path, "a");
await fileHandle.write(content);
addedContent = content;
console.log("içerik başarıyla eklendi");
} catch (error) {
console.log("bir hatayla karşılaşıldı", error.message);
} finally {
if (fileHandle) {
await fileHandle.close();
}
}
}
// kolay ama döngü içerisinde yazma yapılacaksa performans verimsiz
// async function addToFile(path, content) {
// try {
// await fs.appendFile(path, content, { encoding:"utf8" }); // defaultu utf-8
// console.log("Dosyaya yeni veriler eklendi");
// } catch (error) {
// console.log("bir hatayla karşılaşıldı", error.message);
// }
// }
const commandFileHandler = await fs.open("./command.txt", "r");
// flags parametresinin default değeri r
commandFileHandler.on("change", async () => {
// dosyayı okumak istiyoruz
// dosyanın boyutunu alalım
// console.log(await commandFileHandler.stat());
const size = (await commandFileHandler.stat()).size;
// dosyanın boyutuna göre Buffer ayırıyoruz
const buffer = Buffer.alloc(size);
// Buffer'ı doldurmaya başlamak istediğimiz konum.
const offset = 0;
// Okunacak bayt sayısı.
const length = buffer.byteLength - offset;
// Dosyayı okumak istediğimiz konum.
const position = 0;
// Baştan sona kadar bütün içeriği okumak istiyorum.
await commandFileHandler.read({
buffer: buffer,
offset: offset,
length: length,
position: position}
);
// console.log(buffer.toString("utf-8")); // toString default olarak utf-8 decode eder
const command = buffer.toString("utf-8");
// dosya oluştur
// create a file <path>
if(command.includes(CREATE_FILE_COMMAND)){
const filePath = command.substring(CREATE_FILE_COMMAND.length + 1);
await createFile(filePath);
}
// dosyayı sil
// delete the file <path>
else if (command.includes(DELETE_FILE_COMMAND)) {
const filePath = command.substring(DELETE_FILE_COMMAND.length + 1);
await deleteFile(filePath);
}
// dosyanın adını değiştir
// rename the file <path> to <new-path>
else if (command.includes(RENAME_FILE_COMMAND)) {
const _index = command.indexOf(" to ");
const newPath = command.substring(_index + 4);
const oldPath = command.substring(RENAME_FILE_COMMAND.length + 1, _index);
await renameFile(oldPath, newPath);
}
// dosyaya ekle
// add to the file <path> this content: <content>
else if (command.includes(ADD_TO_FILE_COMMAND)) {
const _index = command.indexOf(" this content: ");
const filePath = command.substring(ADD_TO_FILE_COMMAND.length + 1, _index);
const content = command.substring(_index + " this content: ".length - 1);
await addToFile(filePath, content);
}
else {
console.log(
"Geçersiz Komut, lütfen bunlardan birini deneyin:\n\n" +
"yeni dosya oluşturmak için: create a file <path>\n" +
"dosya silmek için: delete the file <path>\n" +
"dosya adını değiştirmek için: rename the file <path> to <new-path>\n" +
"dosyaya yeni içerik eklemek için: add to the file <path> this content: <content>\n"
);
}
});
const watcher = fs.watch("./command.txt", { encoding: "utf-8" });
// encoding paramateresi default'u utf-8
for await (const event of watcher) {
if (event.eventType === "change" && event.filename === "command.txt") {
commandFileHandler.emit(event.eventType); // "change" eventini tetikliyoruz.
// // console.log("Dosya değişikliğe uğradı.");
// // dosyayı okumak istiyoruz
// // dosyanın boyutunu alalım
// // console.log(await commandFileHandler.stat());
// const size = (await commandFileHandler.stat()).size;
// // dosyanın boyutuna göre Buffer ayırıyoruz
// const buffer = Buffer.alloc(size);
// // Buffer'ı doldurmaya başlamak istediğimiz konum.
// const offset = 0;
// // Okunacak bayt sayısı.
// const length = buffer.byteLength - offset;
// // Dosyayı okumak istediğimiz konum.
// const position = 0;
// // Baştan sona kadar bütün içeriği okumak istiyorum.
// const content = await commandFileHandler.read({
// buffer: buffer,
// offset: offset,
// length: length,
// position: position}
// );
// console.log(content.buffer.toString());
}
}
})();
// Tüm <FileHandle> nesneleri <EventEmitter> nesneleridir.
// flag'lar resmi dokümantasyonda File System'in en altında
// chokidar kütüphanesine bak https://www.npmjs.com/package/chokidar github.com/paulmillr/chokidar