-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSystem.js
More file actions
241 lines (202 loc) · 8.77 KB
/
fileSystem.js
File metadata and controls
241 lines (202 loc) · 8.77 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* Created by Boaz on 19/01/2017.
*/
const readlineSync = require('readline-sync');
const colors = require('colors');
var exit = false;//global variable to control the exit command
var uniqueID = 0;// an ID of each file in the storage
var path = 'root >';
var level = 0;// level distance from root folder
var storage = [//basic folder system
/*[id, parentID , whatAmI?, content===NULL]*/
[0,0,'root(i\'m the son of myself)'],
[1,0,'subfolder1'],
[2,0,'subfolder2'],
[3,0,'subfolder3'],
[4,1,'subfolder4'],
[5,4,'subfolder5' ],
[6,5,'file1.txt','content'],
[7,5,'file2.txt','content'],
[8,0, 'test.txt', 'hello world']
];
var menu = [//user menu
' Print Current folder.',
' Change(go back or forward) ',
' Create file or folder',
' Open file',
' Delete file',
' Exit(suit yourself out from the program)'
];
console.log(colors.red(path));
while (!exit){
showMenu();
}
function showMenu() {
var userMenuInput = readlineSync.keyInSelect(menu, colors.red('Chose your menu option:'));
userMenuInput++;
switch (userMenuInput){//calling functions according to user menu input
case 1 :
printRootSorted();
break;
case 2 :
changeDirectory();
break;
case 3 :
createNewFile();
break;
case 4 :
openFile();
break;
case 5 :
deleteFile();
break;
case 6 :
exitProgram();
break;
default:
console.log(colors.red("What is wrong with you?? chose menu item between 1 to 6"));
}
}
function exitProgram() {// exit the program safely using the process object
var exitProgram = readlineSync.question(colors.red("Are you sure you want to exit? (y / n)"));
if(exitProgram.toLowerCase() === 'y'){
exit = true;
process.exit();
} else if(exitProgram.toLowerCase() === 'n' ) {
showMenu();
}
}
function printRootSorted(){// print the first level sons under folder sorted way
console.log(path);
var foldersArr = [];
var filesArr = [];
for(var i = 1; i <storage.length; i++){
if( uniqueID === storage[i][1])
{
if (isFolder(i) ) {
foldersArr.push(storage[i][2]);
} else {
filesArr.push(storage[i][2]);
}
}
}
foldersArr.sort();
filesArr.sort();
for(var j = 0; j<foldersArr.length ; j++){
console.log(colors.blue(" " +foldersArr[j]));
}
for(var k = 0; k<filesArr.length ; k++){
console.log(colors.yellow(" " +filesArr[k]));
}
console.log("number of files: " + (filesArr.length+foldersArr.length)+".");
}
function changeDirectory(){// move backward or forward from current directory
var goTo = readlineSync.question(colors.red("Where would you like to go?"));
if(goTo === '..'){//backward case
if(uniqueID > 0){
uniqueID = storage[uniqueID][1];
var name = storage[uniqueID][2];
path = path.slice(0,(path.length-name.length));
console.log(path);
level--;
} else {// check if the current location is the root folder
console.log(colors.red("You are in the root , no where to go back"));
}
} else if(!checkIfFolderExist(goTo)) {
console.log(colors.red("No directory called " + goTo));
}
}
function checkIfFolderExist(folderName) {//help function in order to check whether user input of folder is exist
for(var i = 0 ; i < storage.length ; i++){
if(folderName === storage[i][2] && uniqueID === storage[i][1]){
path += "/" + folderName;
uniqueID = storage[i][0] ;
level++;
console.log(path);
return true;
}
}
return false;
}
function isFolder(id){// check if the needed to open is file or folder, (help function)
if(storage[id].length === 3){
return true;
}
return false;
}
function openFile(){// show content of a file if exist
var file = readlineSync.question(colors.red("Which file would you like to open?"));
file = file.toLowerCase();
if (isExist(file) ){
if(isFolder(getIndex(file))){
console.log(colors.red("File not to be found"));
} else {
console.log(colors.red(storage[getIndex(file)][3]));
}
} else {
console.log("File not to be found.");
}
}
function isExist(name) {
for(var i = 0 ; i < storage.length ; i++){
if(name === storage[i][2] && uniqueID === storage[i][1]){
return true;
}
}
return false;
}
function getIndex(name) {//return the uniqueID of specific file
for(var i = 0 ; i < storage.length ; i++){
if(name === storage[i][2] && uniqueID === storage[i][1]){
return storage[i][0];
}
}
return -1;
}
function createNewFile(){
var fileToCreate = readlineSync.question(colors.red("Please name the file you want to create:"));
fileToCreate = fileToCreate.toLowerCase();
if(!isExist(fileToCreate)){
if(fileToCreate.indexOf(".") > -1){
var content = readlineSync.question(colors.red("what content would you like to enter to your file?"));
storage.push([storage.length, uniqueID, fileToCreate, content]);
} else {
storage.push([storage.length, uniqueID, fileToCreate]);
}
} else {
console.log(colors.red(fileToCreate +" is already exist under the current folder."))
}
}
function deleteFile(){
var fileToDelete = readlineSync.question(colors.red("Which file would you like to delete?"));
fileToDelete = fileToDelete.toLowerCase();
if(isExist(fileToDelete)){
deleteId(getIndex(fileToDelete));
} else {
console.log(colors.red(fileToDelete + " dosen't exit under the current folder"));
}
}
function deleteId(id) {
if(!isFolder(id)){ // File to delete
console.log(colors.red(storage[id][2] + " deleted."));
storage.splice(id, 1);
} else { // Folder to delete
//
var stackToDelete = [];//stack that will hold all files need to be deleted
for(var i = 1; i <storage.length; i++){
if (id === storage[i][1]){
stackToDelete.push(storage[i][0]);
}
}
if(stackToDelete.length === 0){
console.log(colors.red(storage[id][2] + " deleted."));
storage.splice(id, 1);
} else {
while (stackToDelete.length > 0){
deleteId(stackToDelete.pop()); //recursion call with the id on top of stack
}
console.log(colors.red(storage[id][2] + " deleted."));
storage.splice(id, 1);
}
}
}