Skip to content

Instantly share code, notes, and snippets.

@chintan9
Last active December 26, 2023 08:51
Show Gist options
  • Save chintan9/0375fba3477374616c007fa2866227a0 to your computer and use it in GitHub Desktop.
Save chintan9/0375fba3477374616c007fa2866227a0 to your computer and use it in GitHub Desktop.
Volta node removal script
const fs = require('fs');
const readline = require('readline');
const path = require('path');
/**
* @type {readline.Interface}
*/
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
/**
* @type {string}
*/
const directoryPath = path.join(process.env.HOME, '.volta/tools/image/node/');
/**
* @callback readdirCallback
* @param {Error} err - The error.
* @param {string[]} files - The files in the directory.
*/
/**
* Read the directory and list the files.
* @param {string} directoryPath - The path of the directory.
* @param {readdirCallback} callback - The callback function.
*/
fs.readdir(directoryPath, (err, files) => {
if (err) {
return console.log(`Unable to scan directory: ${err}`);
}
console.log('Select the directories you want to delete from the list below:');
files.forEach((file, index) => {
console.log(`${index + 1}. ${file}`);
});
/**
* @callback questionCallback
* @param {string} answer - The user's answer.
*/
/**
* Ask the user a question and delete the selected directories.
* @param {string} question - The question to ask the user.
* @param {questionCallback} callback - The callback function.
*/
rl.question('Enter the numbers of the directories to delete (separated by commas): ', (answer) => {
const dirsToDelete = answer.split(',');
dirsToDelete.forEach(dirIndex => {
const dir = files[Number(dirIndex.trim()) - 1];
if (dir) {
fs.rm(path.join(directoryPath, dir), { recursive: true, force: true }, (err) => {
if (err) {
console.error(`Error while deleting ${dir}.`);
}
});
}
});
rl.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment