-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
181 lines (163 loc) · 4.58 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
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
#!/usr/bin/env node
import FindFiles from "./FindFiles.js";
import inquirer from "inquirer";
import chalk from "chalk";
import process from "process";
import DeleteFiles from "./DeleteFiles.js";
import FolderSize from "./FolderSize.js";
// Instantiate FindFiles, DeleteFiles, FolderSize classes
const finder = new FindFiles();
const deleter = new DeleteFiles();
const folderSizes = new FolderSize();
/**
* Function to initiate the search for node_modules directories.
* @param {string} path - The starting path for the search.
*/
async function startSearch(path, depth) {
finder.filesList = [];
deleter.filesToDelete = [];
folderSizes.folders = [];
console.log("Searching for node_modules directories...");
await finder.search(path, depth, () => {});
console.log(chalk.green("Search completed successfully."));
console.log(chalk.yellow(`Total files found: ${finder.filesList.length}`));
console.log(chalk.yellow(finder.filesList));
await folderSizes.storeFileSize(finder.filesList);
showOptions();
}
/**
* Function to display the main options menu and handle user choices.
*/
function showOptions() {
inquirer
.prompt([
{
type: "list",
name: "option",
message: "Choose an option:",
choices: [
"Search for node_modules directories",
"Delete found node_modules directories",
"Exit",
],
},
])
.then((answers) => {
if (answers.option === "Search for node_modules directories") {
optionsStartSearch();
} else if (answers.option === "Delete found node_modules directories") {
startDelete();
} else {
console.log(chalk.blue("Goodbye!"));
process.exit(0);
}
});
}
/**
* Array of questions to prompt the user to start searching for node_modules directories.
*/
const choicesForOptionStartSearch = [
{
type: "input",
name: "path",
message: "Enter the path to start searching for node_modules directories:",
validate: (input) => input.trim() !== "",
default: process.cwd(),
},
{
type: "list",
name: "depthChoice",
message: "Enter the depth of the search:",
choices: ["low", "medium", "high", "custom"],
},
{
type: "input",
name: "customDepth",
message: "Enter the custom depth of the search:",
validate: (input) => {
const parsed = parseInt(input);
return (
(!isNaN(parsed) && parsed > 0) || "Please enter a valid positive number"
);
},
when: (answers) => answers.depthChoice === "custom",
default: 10,
},
];
/**
* Function to handle user input for the path to start searching node_modules directories.
*/
function optionsStartSearch() {
inquirer.prompt(choicesForOptionStartSearch).then((answers) => {
const path = answers.path;
let depth =
answers.depthChoice === "custom"
? answers.customDepth
: answers.depthChoice;
depth = getDepth(depth);
startSearch(path, depth);
});
}
/**
* Function to convert the depth string to a number.
*/
function getDepth(depth) {
switch (depth) {
case "low":
return 5;
case "medium":
return 10;
case "high":
return 20;
default:
return parseInt(depth);
}
}
/**
* Function to initiate the deletion of selected node_modules directories.
*/
async function startDelete() {
if (finder.filesList.length === 0) {
console.log(chalk.yellow("No node_modules directories found."));
showOptions();
} else {
// If node_modules directories are found, prompt user to select directories for deletion
selectFilesToDelete();
}
}
/**
* Function to prompt the user to select node_modules directories for deletion.
*/
async function selectFilesToDelete() {
const answers = await inquirer.prompt([
{
type: "checkbox",
name: "filesToDelete",
message:
"Select the node_modules directories to delete: (space to select and enter to confirm)",
choices: finder.filesList,
},
]);
answers.filesToDelete.forEach((path) => {
deleter.addFileToDelete(path);
folderSizes.folders.forEach((folder) => {
if (folder.path === path) {
folder.isDeleted = true;
}
});
});
await deleter.deleteAllFiles();
console.log(
chalk.yellow("Total size of node_modules directories:"),
chalk.red(folderSizes.formatSize(folderSizes.totalSize)),
);
console.log(
chalk.green("Selected node_modules directories deleted successfully."),
);
console.log(
chalk.yellow("Total storage freed:"),
chalk.red(folderSizes.formatSize(folderSizes.getDeletedSize())),
);
showOptions();
}
showOptions();