-
Notifications
You must be signed in to change notification settings - Fork 0
/
differ.js
169 lines (151 loc) · 3.18 KB
/
differ.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
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
var readline = require("readline");
var fs = require("fs");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var pathDir = "";
var arrFilesDev1 = [];
var arrFilesDev2 = [];
var isDone = [false, false, false];
var arrMenu = [
"1. Input path []",
"2. Save first status []",
"3. Save second status []",
"4. Show difference",
"5. Clear",
"0. Exit"
];
function GetMenuString() {
var menuString = "";
for (var i = 0; i < arrMenu.length; i++) {
menuString += arrMenu[i] + "\n";
}
menuString += "> ";
return menuString;
}
function MainStart() {
rl.question(GetMenuString(), OnAnswer);
return;
}
function OnAnswer(text) {
switch(text) {
case "1":
CheckCorrectPath();
break;
case "2":
SaveArrFilesInDir(parseInt(text));
break;
case "3":
SaveArrFilesInDir(parseInt(text));
break;
case "4":
ShowDiff();
break;
case "5":
Clear();
break;
case "0":
process.exit();
break;
default:
console.log("BAD SELECTION\n");
MainStart();
}
}
function CheckCorrectPath() {
rl.question("# Input correct path (-c to cancel) : ", function(path) {
if (path == "-c") {
console.log("Canceled");
MainStart();
return;
}
fs.stat(path, function(err, stat){
if (err) {
console.log("Incorrect path");
CheckCorrectPath();
return;
}
if (stat.isDirectory()) {
pathDir = path;
OkStep(1);
MainStart();
} else {
console.log("It's not path");
}
});
});
}
function SaveArrFilesInDir(step) {
if (isDone[0]) {
fs.readdir(pathDir, function(err, files){
if (err) {
console.log("Error readdir");
MainStart();
return;
}
console.log("step: ", step);
if (step == 2) {
arrFilesDev1 = files;
OkStep(2);
} else if (step == 3) {
arrFilesDev2 = files;
OkStep(3);
}
});
} else {
console.log("You have not complete the previous step");
MainStart();
}
}
function Clear() {
pathDir = "";
arrFilesDev1 = [];
arrFilesDev2 = [];
isDone1 = isDone2 = isDone3 = false;
console.log("Clear complete");
for (var str in arrMenu) {
arrMenu[str] = arrMenu[str].replace(/\[+.*\]/g, "[]");
}
MainStart();
}
function ShowDiff() {
if (isDone[0] && isDone[1] && isDone[2]) {
var newFiles = [];
var delFiles = [];
for (var file in arrFilesDev1) {
if (arrFilesDev2.indexOf(arrFilesDev1[file]) == -1) {
delFiles.push(arrFilesDev1[file]);
}
}
for (var file in arrFilesDev2) {
if (arrFilesDev1.indexOf( arrFilesDev2[file]) == -1) {
newFiles.push(arrFilesDev2[file]);
}
}
console.log("New files: ", newFiles);
console.log("Deleted files: ", delFiles);
MainStart();
} else {
console.log("You have not complete the previous steps");
MainStart();
}
}
function OkStep(num) {
switch(num) {
case 1:
isDone[num - 1] = true;
arrMenu[num - 1] = arrMenu[num - 1].replace(/\[+.*\]/g, "[" + pathDir + "]");
break;
case 2:
isDone[num - 1] = true;
arrMenu[num - 1] = arrMenu[num - 1].replace(/\[+.*\]/g, "[files: " + arrFilesDev1.length + "]");
break;
case 3:
isDone[num - 1] = true;
arrMenu[num - 1] = arrMenu[num - 1].replace(/\[+.*\]/g, "[files: " + arrFilesDev2.length + "]");
break;
}
MainStart();
}
MainStart();