-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
173 lines (151 loc) · 4.07 KB
/
main.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
170
171
172
173
//command line mei jo bhi input tu dega wo ek array mei store kar lega process.argv use karne ka argv array ka naam hai
let inputArr= process.argv.slice(2);
console.log(inputArr);
let fs=require("fs");
let path=require("path");
//node main.js tree "directoryPath"
//node main.js organize "directoryPath"
//node main.js help "directoryPath"
let command=inputArr[0];
let types={
media:["mp4","mkv"],
archive:["zip","7z","rar","tar",'gz',"ar","iso","xz"],
documents:["docx","doc","pdf","xlsx","odt","ods","odg","odp","odf","txt","ps","tex"],
app:["exe","dmg","pkg","deb"]
}
switch(command)
{
case "tree":
treeFn(inputArr[1]);
break;
case "organize":
organizeFn(inputArr[1])
break;
case "help":
helpFn();
break;
default:
console.log("Please🤞🤞 input correct command");
break;
}
function treeFn(dirPath) {
// let destPath;
if (dirPath == undefined) {
treeHelper(process.cwd(), "");
return;
} else {
let doesExist = fs.existsSync(dirPath);
if (doesExist) {
treeHelper(dirPath, "");
} else {
console.log("Kindly enter the correct path");
return;
}
}
}
function treeHelper(dirPath, indent) {
// is file or folder
let isFile = fs.lstatSync(dirPath).isFile();
if (isFile == true) {
let fileName = path.basename(dirPath);
console.log(indent + "├──" + fileName);
} else {
let dirName = path.basename(dirPath)
console.log(indent + "└──" + dirName);
let childrens = fs.readdirSync(dirPath);
for (let i = 0; i < childrens.length; i++) {
let childPath = path.join(dirPath, childrens[i]);
treeHelper(childPath, indent + "\t");
}
}
}
module.exports = {
treeKey: treeFn
}
function organizeFn(dirPath)
{
//5- agar aap path nhi karte ho toh wo path undefined hoga aur undefined
// pth ke liye bhi call lag jayegi aisa n aho iss liye undefined path ke liye check karenge.
let destPath;
if (dirPath==undefined)
{
console.log("Kindly enter the path");
return;
}
else{
// 1- given directory ka path
//check karo ki jo path mila wo kisi directory ka hai bhi ya nhi?
//iske liye fs ka lstat use karenge u
let doesExist=fs.existsSync(dirPath);
if(doesExist)
{
//2- iss directory path mei ja ke ek organized file ke naam se directory banao
//iske liye fs modle ka mkdirsync use karte hain
destPath=path.join(dirPath,"organizedFiles");
if(fs.existsSync(destPath)==false)
{
fs.mkdirSync(destPath);
}
}else{
//path exist nhi kara to user ko bolo sahi path daale
console.log("Kindly enter the correct path");
return;
}
}
organizeHelper(dirPath,destPath);
//console.log("Organize command implemented for ",dirPath);
}
function organizeHelper(src,dest){
//identify categories of all files in path directory
let childNames=fs.readdirSync(src);
// console.log(childNames);
for(let i in childNames)
{
let childAddress=path.join(src,childNames[i]);
let isFile=fs.lstatSync(childAddress).isFile();
if(isFile)
{
let category=getCategory(childNames[i]);
//4- copy/cut to that organized directory ,put them inside their organized category
console.log(childNames[i],"belongs to--->",category);
sendFiles(childAddress,dest,category);
}
}
}
function sendFiles(srcFilePath,dest,category)
{
let categoryPath=path.join(dest,category);
if(fs.existsSync(categoryPath)==false)
{
fs.mkdirSync(categoryPath);
}
let fileName=path.basename(srcFilePath);
let destFilePath=path.join(categoryPath,fileName);
fs.copyFileSync(srcFilePath,destFilePath );
fs.unlinkSync(srcFilePath);
console.log(fileName,"copied to",category);
}
function getCategory(name)
{
let ext=path.extname(name);
ext=ext.slice(1);
for(let type in types)
{
let cTypeArray=types[type];
for(let i=0;i<cTypeArray.length;i++)
{
if(cTypeArray[i]===ext)
return type;
}
}
return "others";
}
function helpFn()
{
console.log(`
List of all the commands:
node main.js tree "directoryPath"
node main.js organize "directoryPath"
node main.js help "directoryPath"
`);``
}