import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import chalk = require('chalk'); import { confirmMessage, defaultMenuSettings } from '../../../../globals'; const { AutoComplete, Confirm } = require('enquirer'); const { spawn } = require('child_process'); module.exports = { name: 'clear-prints', alias: ['cp'], description: 'Clear prints-dev bucket on S3 (a s3 cp)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { const { print, system, strings } = toolbox; // List all the folders on root level in bucket photowall-prints-dev const allFiles = await system.run( `aws s3api list-objects-v2 --bucket photowall-prints-dev --output json` ); const allFilesArray = JSON.parse(allFiles); // Organize all the files in different folder if the folder name starts with station const stations = []; for (const file of allFilesArray.Contents) { if (file.Key.startsWith('station')) { // Get the station name const stationName = file.Key.split('/')[0]; if (!stations[stationName]) { stations[stationName] = []; } // Then push the file into the station stations[stationName].push(file); } } // Take all the station and create another list with the station and the number of files const stationList = []; for (const station in stations) { stationList.push(`${station} (${stations[station].length} files)`); } // Now show these in a enquiere prompt where the user can search and choose a folder const prompt = new AutoComplete({ message: 'Choose a folder', choices: stationList, }); const folder = await prompt.run(); const stationName = folder.split(' ')[0]; console.log(`stationName`, stationName); // Ask the user if he wants to delete all the files in that station const confirm = new Confirm({ message: `Do you want to delete all the files in ${chalk.yellow( stationName )}?`, }); const deleteAll = await confirm.run(); if (deleteAll) { // Delete all the files in that station for (const file of stations[stationName]) { await system.run(`aws s3 rm s3://photowall-prints-dev/${file.Key}`); } } if (toolbox.fromMenu()) { await toolbox.menu.showMenu('aws s3', defaultMenuSettings); } }, };