-
Notifications
You must be signed in to change notification settings - Fork 0
/
organise.js
73 lines (63 loc) · 2.5 KB
/
organise.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
#!/usr/bin/env node
let fs = require("fs")
let path = require("path")
let utility = require("./utility")
// check whether given path is a file or directory
function checkWhetherFile(src){
return fs.lstatSync(src).isFile()
}
// get extension of the file path
function getExtension(src){
let ext = src.split(".").pop()
return ext
}
// get category based on file extension
function getCategory(ext){
let types = utility.types
for (let category in types) {
for (let i = 0; i < types[category].length; i++) {
if (ext == types[category][i]) {
return category
}
}
}
return "others"
}
// copy the file from src to desired location
function sendFile(dest, category, src) {
let categoryPath = path.join(dest, category) // create category directory path
if (fs.existsSync(categoryPath) == false) { // if category directory not exists
fs.mkdirSync(categoryPath) // create category directory
}
let fName = path.basename(src) // returns the filename part of a file path.
let cPath = path.join(categoryPath, fName) // create the destination path
fs.copyFileSync(src, cPath)
}
// get content of the directory path
function getContent(src) {
return fs.readdirSync(src) // return the contents of a given directory as an array
}
// MAIN FUNCTION
function organizer(src, dest) {
if(checkWhetherFile(src) == true){ // if path is a file
let ext = getExtension(src) // get extension of the file
let category = getCategory(ext) // get category of the file
sendFile(dest, category, src) // copy file from src to dest
}
else{ // if path is a directory
let childNames = getContent(src) // get content of the directory
for (let i = 0; i < childNames.length; i++) {
if (childNames[i] == "organized_files") { // ignoring the dest directory
continue;
}
let childPath = path.join(src, childNames[i])
organizer(childPath, dest) // recursively organise for directory content
}
}
}
let src = process.argv[2]||process.cwd() // cwd path
let dest = path.join(src,"organized_files") // create des directory path
if (fs.existsSync(dest) == false){ // if des directory not exists
fs.mkdirSync(dest) // create des directory
}
organizer(src,dest)