From ec92e7ff6447df30b53045d050ad35c2e64b15c0 Mon Sep 17 00:00:00 2001 From: Casey B Date: Fri, 26 Jul 2024 01:41:32 +0000 Subject: [PATCH] Add demos-run-v1.js --- demos-run-v1.js | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 demos-run-v1.js diff --git a/demos-run-v1.js b/demos-run-v1.js new file mode 100644 index 0000000..4f8df18 --- /dev/null +++ b/demos-run-v1.js @@ -0,0 +1,92 @@ +// CAUTION ==== CAUTION ==== CAUTION ==== CAUTION ==== +// !!! Modifying this file is NOT recommended !!! +// Incorrect modification of this file may result +// in inability to start this application +// =================================================== + +const { exec } = require('child_process'); +const fs = require('fs'); +const util = require('util'); +const execProm = util.promisify(exec); + +// Simple sleep function using a promise +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +// Simulated progress bar to enhance visual appeal during operations +async function showLoadingBar(chalkColor) { + const progressBarLength = 20; // Length of the progress bar + let progressBar = ''; + process.stdout.write(chalkColor(`\r[${progressBar}${' '.repeat(progressBarLength)}] 0%`)); + return (progress) => { + const filledLength = Math.round(progressBarLength * progress); + progressBar = chalkColor('ā–ˆ'.repeat(filledLength)); + const emptyBar = ' '.repeat(progressBarLength - filledLength); + process.stdout.write(chalkColor(`\r[${progressBar}${emptyBar}] ${Math.round(progress * 100)}%`)); + if (progress === 1) { + console.log(); // Finish with a newline when complete + } + }; +} + +// Function to execute shell commands with progress tracking and minimal logging +async function executeCommand(command, preMessage, successMessage, chalkColor) { + console.log(chalkColor(preMessage)); + + const updateProgress = await showLoadingBar(chalkColor); + try { + await execProm(command, { maxBuffer: 1024 * 5000 }); + updateProgress(1); // Ensure the bar fills to 100% once command execution completes + console.log(chalkColor(`āœØ ${successMessage}`)); + } catch (error) { + updateProgress(1); // Ensure the bar fills to 100% even on error + console.error(chalkColor(`āŒ Error during ${command}: ${error.message}`)); + } +} + +// Read the 'main' script from package.json and run it +async function runMainScript() { + const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); + const mainScript = packageJson.main || 'index.js'; // Default to index.js if not specified + return `node ${mainScript}`; +} + +// Main function to orchestrate the setup and run with enhanced visuals +async function main() { + console.log("\n\nšŸŒŸ Welcome to the Setup Wizard! Let's get started... šŸŒŸ\n"); + + // Install chalk version 4.1.2 specifically + await executeCommand( + 'npm install chalk@4.1.2', + 'šŸŽØ Installing chalk 4.1.2... Please wait as we add some color to your life!.', + 'Chalk 4.1.2 installed', + msg => msg + ); + + // Now that chalk is installed, require it for future logs + const chalk = require('chalk'); + + // Install remaining dependencies + await executeCommand( + 'npm install', + 'šŸ”§ Installing other dependencies... This might take a few seconds.', + 'Dependencies installed', + chalk.blue + ); + + // Run the main script from package.json + const runCommand = await runMainScript(); + await executeCommand( + runCommand, + 'šŸŒˆ Setup Complete! Your application is ready to go! šŸš€\n', + // 'šŸš€ Launching the application... Hold tight, we're almost there!', + //'šŸŒˆ Setup Complete! Your application is ready to go! šŸš€\n', + '\n', + chalk.magentaBright + ); + + console.log(chalk.magentaBright('\n')); +} + +main(); \ No newline at end of file