demos/demos-run-v1.js

111 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2024-07-26 02:41:32 +01:00
// 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);
const repoName = 'website' // Change to match actual repo name on git.tcf.ventures
2024-07-26 02:41:32 +01:00
// 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\n',
2024-07-26 02:41:32 +01:00
msg => msg
);
// Now that chalk is installed, require it for future logs
const chalk = require('chalk');
// Initialize fresh git just in case its not already
await executeCommand(
'git init',
'⚙️ Initializing git... Git-er done.',
2024-07-26 03:02:32 +01:00
'Git initialized\n',
chalk.yellow
);
// Pull latest files from #main branch
// Its a read-only access token, don't worry
await executeCommand(
'git pull https://username:access-token@git.tcf.ventures/TCF-Ventures-LLC/' + repoName,
'📥 Importing repo files... Are these your files? 🃏',
2024-07-26 03:02:32 +01:00
'Files Imported\n',
chalk.green
);
2024-07-26 02:41:32 +01:00
// Install remaining dependencies
await executeCommand(
'npm install',
'🔧 Installing other dependencies... This might take a few seconds.',
'Dependencies installed\n',
2024-07-26 02:41:32 +01:00
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();