diff --git a/src/commands/aws/s3/clear_prints/clear_prints.ts b/src/commands/aws/s3/clear_prints/clear_prints.ts index 4867b7e..2539504 100644 --- a/src/commands/aws/s3/clear_prints/clear_prints.ts +++ b/src/commands/aws/s3/clear_prints/clear_prints.ts @@ -1,8 +1,8 @@ import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import chalk = require('chalk'); -import { confirmMessage, defaultMenuSettings } from '../../../../globals'; +import { defaultMenuSettings } from '../../../../globals'; +import { createPrintBucketStationList } from '../../../../services/aws_s3'; const { AutoComplete, Confirm } = require('enquirer'); -const { spawn } = require('child_process'); module.exports = { name: 'clear-prints', @@ -10,43 +10,20 @@ module.exports = { description: 'Clear prints-dev bucket on S3 (a s3 cp)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { - const { print, system, strings } = toolbox; + const { system } = 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 stationListDev = await createPrintBucketStationList( + toolbox, + 'photowall-prints-dev' ); - 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, + choices: stationListDev.stationNames, }); 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({ @@ -58,7 +35,7 @@ module.exports = { if (deleteAll) { // Delete all the files in that station - for (const file of stations[stationName]) { + for (const file of stationListDev.stationFiles[stationName]) { await system.run(`aws s3 rm s3://photowall-prints-dev/${file.Key}`); } } diff --git a/src/commands/aws/s3/print_on_station/print_on_station.ts b/src/commands/aws/s3/print_on_station/print_on_station.ts new file mode 100644 index 0000000..1a2f328 --- /dev/null +++ b/src/commands/aws/s3/print_on_station/print_on_station.ts @@ -0,0 +1,74 @@ +import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; +import chalk = require('chalk'); +import { defaultMenuSettings } from '../../../../globals'; +import { createPrintBucketStationList } from '../../../../services/aws_s3'; +const { AutoComplete, Confirm } = require('enquirer'); + +module.exports = { + name: 'print-on-station', + alias: ['pos'], + description: 'Print on a real station (a s3 pos)', + hidden: false, + run: async (toolbox: GluegunMenuToolbox) => { + const { system } = toolbox; + + const stationListDev = await createPrintBucketStationList( + toolbox, + 'photowall-prints-dev' + ); + + // Now show these in a enquiere prompt where the user can search and choose a folder + const promptDev = new AutoComplete({ + message: 'Choose a dev folder to copy from', + choices: stationListDev.stationNames, + }); + const folderDev = await promptDev.run(); + const stationNameDev = folderDev.split(' ')[0]; + + const promptProd = new AutoComplete({ + message: 'Choose a live folder to copy to', + choices: [ + 'station1', + 'station3', + 'station5', + 'station6 (Amanda)', + 'station80 (Samples 1)', + 'station81 (Samples 2)', + 'station90 (Paint 1)', + 'station91 (Paint 2)', + ], + }); + const folderProd = await promptProd.run(); + const stationNameProd = folderProd.split(' ')[0]; + + // Ask the user if he wants to delete all the files in that station + const confirm = new Confirm({ + message: `Are u sure you want to copy files from s3://photowall-prints-dev/${chalk.yellow( + stationNameDev + )} to s3://photowall-prints/${chalk.blue( + stationNameProd + )} printing them in production?`, + }); + const confirmResult1 = await confirm.run(); + + const confirmAgain = new Confirm({ + message: `${chalk.black.bgYellow.bold( + 'Doing this will disrupt the packing process and cause confusion. Are you physically present at the target station?' + )}\n\n${chalk.bold('Files to be copied:')}\n${stationListDev.stationFiles[ + stationNameDev + ].map((file) => chalk.yellow(file.Key) + '\n')}\n\n${chalk.yellow.bold( + 'Are you really sure?' + )}`, + }); + const confirmResult2 = await confirmAgain.run(); + + if (confirmResult1 && confirmResult2) { + const cmd = `aws s3 sync s3://photowall-prints-dev/${stationNameDev} s3://photowall-prints/${stationNameProd}/`; + await system.run(cmd); + } + + if (toolbox.fromMenu()) { + await toolbox.menu.showMenu('aws s3', defaultMenuSettings); + } + }, +}; diff --git a/src/commands/aws/s3/test_station_print/test_station_print.ts b/src/commands/aws/s3/test_station_print/test_station_print.ts deleted file mode 100644 index eed225d..0000000 --- a/src/commands/aws/s3/test_station_print/test_station_print.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; -import chalk = require('chalk'); -import { defaultMenuSettings } from '../../../../globals'; -const { AutoComplete, Confirm } = require('enquirer'); - -module.exports = { - name: 'test-station-print', - alias: ['tsp'], - description: 'Test a station print', - hidden: false, - run: async (toolbox: GluegunMenuToolbox) => { - const { system } = 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) { - if (station !== 'station100') { - 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 dev folder to copy from', - 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: `Are u sure you want to copy files from ${chalk.yellow( - stationName - )} to ${chalk.blue( - 'station100' - )} printing them on station 6 in production?`, - }); - const copyFiles = await confirm.run(); - - if (copyFiles) { - const cmd = `aws s3 sync s3://photowall-prints-dev/${stationName} s3://photowall-prints-dev/station100/`; - await system.run(cmd); - } - - if (toolbox.fromMenu()) { - await toolbox.menu.showMenu('aws s3', defaultMenuSettings); - } - }, -}; diff --git a/src/services/aws_s3.ts b/src/services/aws_s3.ts new file mode 100644 index 0000000..63d0e5d --- /dev/null +++ b/src/services/aws_s3.ts @@ -0,0 +1,31 @@ +export const createPrintBucketStationList = async (toolbox, bucket) => { + const { system } = toolbox; + // List all the folders on root level in bucket photowall-prints-dev + const allFiles = await system.run( + `aws s3api list-objects-v2 --bucket ${bucket} --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)`); + } + + return { stationNames: stationList, stationFiles: stations }; +};