-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.mjs
More file actions
68 lines (66 loc) · 2.07 KB
/
async.mjs
File metadata and controls
68 lines (66 loc) · 2.07 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
import fs from "node:fs/promises";
// ASYNC COMPARATOR
async function fileComparator(feedOne,feedTwo){
try {
if (!await access(feedOne,feedTwo)) {
console.log(`file does not exist`)
}
const [fileOne,fileTwo]=await Promise.all([
fs.readFile(feedOne,{encoding:'utf8'}),
fs.readFile(feedTwo,{encoding:'utf8'})
])
//()regular expression for split
const arrayOne=fileOne.split('/\\s+/ ')
const arrayTwo=fileTwo.split(' /\\s+/')
const min=Math.min(arrayOne.length,arrayTwo.length)
for (let index= 0; index < min; index++) {
const elementOne=arrayOne[index]
const elementTwo=arrayTwo[index]
if (elementOne!==elementTwo) {
await writing(`${elementOne} ${elementTwo}`)
if ((index==min-1)&&arrayOne.length>min){
const removed=arrayOne.slice(min).join(' ')
await writing(removed)
}
else if((index==min-1)&&arrayTwo.length>min){
const removed=arrayTwo.slice(min).join(' ')
await writing(removed)
}
}
}
} catch (error) {
console.log(`error in comperator ${error}`)
}
}
async function access(fileOne,fileTwo) {
try {
await fs.access(fileOne,fs.constants.R_OK)
await fs.access(fileTwo,fs.constants.R_OK)
return true
} catch {
console.log(`error no access `)
return false
}
}
async function writing(params) {
try {
const path='./project1/diference.txt'
const content=params
if (await filePath(path)) {
await fs.appendFile(path,content)
} else{
await fs.writeFile(path,content)
}
} catch (error) {
console.log(`error writing ${error}`);
}
}
async function filePath(path) {
try {
await fs.access(path)
return true
} catch {
return false
}
}
fileComparator('./project1/txt/boom.txt','./file-system/copt.txt')