-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (98 loc) · 3.71 KB
/
index.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
#!/usr/bin/env node
import fs from "fs";
import chalk from "chalk";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// laoding configuratiuon from config file to variables
const config = JSON.parse(
fs.readFileSync(path.join(__dirname, "config.json"), "utf8")
);
const movingConfig = config.configuration;
const directoryPath = config.directories;
// this function will run automatically
(async () => {
directoryPath.map((path) => {
console.log(
chalk.grey("Moving files of directory: ") + chalk.bold.green(path)
);
moveFilesFromDirectory(path);
});
console.log(chalk.green("Successfully moved files form all directories"));
})();
// Take a directory path a parameter and move all the files from that directory
function moveFilesFromDirectory(dir) {
let allFiles = getAllFilesFromDirectory(dir);
allFiles.map(function (file) {
const { oldPath, newPath, fileName } = file;
moveFileToNewDirectory(oldPath, newPath);
console.log(chalk.bold("Moving file: " + chalk.magenta(fileName)));
});
console.log(chalk.yellow("Directory Cleaned"));
}
// get all the files with thier oldPath and newPath
function getAllFilesFromDirectory(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir);
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) {
const name = `${dir}/${file}`;
// Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) {
// If it is a directory, recursively call the getAllFilesFromDirectory function with the directory path and the files array
console.log(
chalk.grey("Searching Sub-directory: ") + chalk.bold.green(name)
);
getAllFilesFromDirectory(name, files);
removeEmptyDirectory(name);
} else {
// If it is a file, push the full path to the files array
let extension = file.split(".").pop();
let finalPath = getNewDirectoryPath(extension);
if (finalPath != undefined) {
files.push({
oldPath: name,
newPath: `${finalPath}/${file}`,
fileName: file,
});
}
}
}
return files;
}
function removeEmptyDirectory(directory) {
fs.readdir(directory, function (err, files) {
if (err) {
console.error(err.message);
} else {
if (!files.length) {
fs.rmdirSync(directory);
console.log(
`${chalk.grey("Removing empty directory :")} ${chalk.red(
directory
)}`
);
}
}
});
}
// Returns the expected path for a given file according to the file extension
function getNewDirectoryPath(ext) {
let finalPathKey = Object.keys(movingConfig).filter((key) => {
return movingConfig[key].extension.includes(ext);
});
// if the path for a file extension is not specified it will not be returned
if (finalPathKey && movingConfig[finalPathKey]) {
let finalPath = movingConfig[finalPathKey].directoryPath;
return finalPath;
}
}
// move the file to new directory based on the finguration
async function moveFileToNewDirectory(oldPath, newPath) {
fs.rename(oldPath, newPath, function (err) {
if (err) {
console.error(err.message);
}
});
}