app-wizard-cli
Version:
CLI pour gérer votre projet Nest/Angular
206 lines (205 loc) • 6.81 kB
JavaScript
import { execSync } from 'child_process';
import axios from 'axios';
import chalk from 'chalk';
import prompts from 'prompts';
/**
* Vérifie la connexion Internet en tentant d'accéder à google.com.
*
* @returns Promise<boolean> Vrai si la connexion fonctionne.
*/
export async function checkInternet() {
try {
await axios.get('https://google.com', { timeout: 5000 });
return true;
}
catch {
return false;
}
}
/**
* Vérifie si Git est installé en exécutant "git --version".
*
* @returns boolean Vrai si Git est disponible.
*/
export function checkGit() {
try {
execSync('git --version', { stdio: 'ignore' });
return true;
}
catch {
return false;
}
}
/**
* Vérifie l'accès au dépôt GitHub "app-template".
*
* @returns Promise<boolean> Vrai si le dépôt est accessible.
*/
export async function checkRepo() {
try {
await axios.get('https://github.com/Kactus83/app-template', { timeout: 5000 });
return true;
}
catch {
return false;
}
}
/**
* Vérifie si Docker est installé en exécutant "docker --version".
*
* @returns boolean Vrai si Docker est disponible.
*/
export function checkDocker() {
try {
execSync('docker --version', { stdio: 'ignore' });
return true;
}
catch {
return false;
}
}
/**
* Vérifie si Docker Compose est installé en exécutant "docker-compose --version".
*
* @returns boolean Vrai si Docker Compose est disponible.
*/
export function checkDockerCompose() {
try {
execSync('docker-compose --version', { stdio: 'ignore' });
return true;
}
catch {
return false;
}
}
/**
* Ouvre une URL dans le navigateur par défaut, selon le système.
*
* @param url L'URL à ouvrir.
*/
export function openUrl(url) {
const platform = process.platform;
try {
if (platform === 'win32') {
execSync(`start "" "${url}"`);
}
else if (platform === 'darwin') {
execSync(`open "${url}"`);
}
else {
execSync(`xdg-open "${url}"`);
}
}
catch (error) {
console.error(chalk.red(`Erreur lors de l'ouverture de ${url}:`), error);
}
}
/**
* Exécute un diagnostic complet de l'environnement et affiche les résultats avec des conseils.
*
* @returns Promise<void> Une fois le diagnostic terminé.
*/
export async function runDetailedDiagnostic() {
console.clear();
console.log(chalk.magenta('🔍 Diagnostic détaillé de l\'environnement\n'));
const internet = await checkInternet();
const git = checkGit();
const repo = await checkRepo();
const docker = checkDocker();
const dockerCompose = checkDockerCompose();
console.log(`Connexion Internet : ${internet ? chalk.green('OK') : chalk.red('ÉCHEC')}`);
console.log(`Git : ${git ? chalk.green('OK') : chalk.red('ÉCHEC')}`);
console.log(`Dépôt GitHub : ${repo ? chalk.green('OK') : chalk.red('ÉCHEC')}`);
console.log(`Docker : ${docker ? chalk.green('OK') : chalk.red('ÉCHEC')}`);
console.log(`Docker Compose : ${dockerCompose ? chalk.green('OK') : chalk.red('ÉCHEC')}`);
if (!internet) {
console.log(chalk.yellow('→ Vérifiez votre connexion internet et vos paramètres réseau.'));
}
if (!git) {
console.log(chalk.yellow('→ Installez Git depuis : https://git-scm.com/downloads'));
}
if (!repo) {
console.log(chalk.yellow('→ Vérifiez votre accès à GitHub ou l\'URL du dépôt "app-template".'));
}
if (!docker) {
console.log(chalk.yellow('→ Installez Docker depuis : https://docs.docker.com/get-docker/'));
}
if (!dockerCompose) {
console.log(chalk.yellow('→ Installez Docker Compose depuis : https://docs.docker.com/compose/install/'));
}
if (internet && git && repo && docker && dockerCompose) {
console.log(chalk.bold.green('\n🎉 Diagnostic : Tout est opérationnel.'));
}
else {
console.log(chalk.bold.red('\n❗ Diagnostic : Certains prérequis ne sont pas satisfaits.'));
}
await prompts({
type: 'text',
name: 'pause',
message: 'Appuyez sur Entrée pour continuer...',
});
}
/**
* Commande interactive "helpers" proposant deux options principales :
* 1. Effectuer un diagnostic complet de l'environnement (similaire à doctor).
* 2. Sélectionner puis ouvrir d'un coup les fenêtres web du projet.
*
* @returns Promise<void> Une fois l'opération terminée.
*/
export async function helpersCommand() {
console.clear();
console.log(chalk.magenta('🔧 Menu Helpers - Outils d\'assistance\n'));
// Sélection de l'action principale
const main = await prompts({
type: 'select',
name: 'choice',
message: 'Que souhaitez-vous faire ?',
choices: [
{ title: '1. Diagnostic complet de l\'environnement', value: 'diagnostic' },
{ title: '2. Ouvrir les fenêtres web du projet', value: 'openWeb' },
{ title: '3. Retour', value: 'return' },
],
initial: 0,
});
const choice = main.choice;
if (choice === 'diagnostic') {
await runDetailedDiagnostic();
return;
}
if (choice === 'openWeb') {
console.clear();
console.log(chalk.magenta('🌐 Sélection des fenêtres web à ouvrir\n'));
const pages = [
{ title: 'Mailhog (http://localhost:8025)', url: 'http://localhost:8025' },
{ title: 'Frontend (http://localhost:4200)', url: 'http://localhost:4200' },
{ title: 'Documentation (http://localhost:3000/docs)', url: 'http://localhost:3000/docs' },
{ title: 'Swagger (http://localhost:3000/api-docs)', url: 'http://localhost:3000/api-docs' },
];
// Sélection multiple
const sel = await prompts({
type: 'multiselect',
name: 'urls',
message: 'Sélectionnez les fenêtres à ouvrir :',
choices: pages.map(p => ({ title: p.title, value: p.url, selected: true })),
instructions: false,
});
const urls = sel.urls || [];
if (urls.length > 0) {
console.log();
for (const url of urls) {
openUrl(url);
console.log(chalk.green(`✅ ${url} ouvert.`));
}
}
else {
console.log(chalk.yellow('Aucune fenêtre sélectionnée.'));
}
await prompts({
type: 'text',
name: 'pause',
message: 'Appuyez sur Entrée pour continuer...',
});
return;
}
console.log(chalk.green('\nRetour au menu principal.'));
}