Add demos-run-v1.js
This commit is contained in:
parent
219fab898b
commit
ec92e7ff64
92
demos-run-v1.js
Normal file
92
demos-run-v1.js
Normal file
@ -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();
|
Loading…
Reference in New Issue
Block a user