-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file.js
More file actions
59 lines (48 loc) · 1.65 KB
/
Copy pathread_file.js
File metadata and controls
59 lines (48 loc) · 1.65 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
const fsp = require('fs').promises;
const fs = require('fs')
let fileName = ".gitignores"
const readFileAsync = async (fileName) => {
console.log("Reading file asynchronously | Promise version")
try {
let data = await fsp.readFile(fileName)
console.log("Content of the file is: \n")
console.log(data.toString())
return data
} catch (error) {
console.log("[!] Error: ", error.message);
return ''
}
}
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Sync version of readFile
const readFileSync = (fileName) => {
try {
console.log("\nReading file synchronously")
let data = fs.readFileSync(fileName)
console.log("Content of the file is: \n")
console.log(data.toString())
} catch (error) {
console.log("[!] Error: ", error.message);
return '';
}
}
readFileAsync(fileName).then(() => {
readFileSync(fileName);
})
fileName = ".gitignore"
// Practice: Replace text in a file
const replaceInFile = (pathToFile, textToReplace, textToInsert) => {
// Read file content as string
let contents = fs.readFileSync(pathToFile, 'utf-8');
// Replace all occurrences of textToReplace (as a string, not the literal 'textToReplace')
// If textToReplace is a string, we need to escape special regex chars and create a global regex
const escaped = textToReplace.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(escaped, 'g');
contents = contents.replace(regex, textToInsert);
fs.writeFileSync(pathToFile, contents);
console.log(`Replaced ${textToReplace} with ${textToInsert} in ${pathToFile}`);
return contents;
}
replaceInFile('./test.txt', 'test', 'test2');