Tasky - here we go !
Table of Contents
On y va on se lance pour de bon !
On se lance dans les details de la création de Tasky, que tu peut retrouver ICI Tasky
On commence donc par créer le ficher qui va gérer les commandes reçu.
donc on créer le dossier du projet:
mkdir tasky
cd tasky
on créer ensuite le dossier pour les scripts
mkdir src
cd src
et finalement le fichier pour les commandes cli
touch cli.js
Maintenant on va devoir ajouter quelque librairies pour pouvoir faire ce que l’on veut
on va avoir besoin de:
- Commander solution pour les script node en cli
- chalk Pour mettre un peu de couleurs dans nos vies !
- pidusage Pour connaitre le pourcentage d’utilisation du cpu pour chaque process
on va donc installer tout ça !
on va initier le projet et installer les dependences:
npm init
npm install commander chalk pidusage
Initialization cli.js#
On va enfin pouvoir commencer !
On va donc ouvrir le fichier cli.js
, dans lequel on va ajouter la gestion des commandes lancer depuis le terminal.
cli.js
#!/usr/bin/env node
import { Command } from 'commander'; // Import commander for the cli command gestion
import fs from 'fs'; // Import fs for files gestion
import chalk from 'chalk'; // Import chalk for colorization
const program = new Command(); // Initialization of a new Command instance
/**
code for the command we goind to details down there
**/
program.parse(process.argv); // Get all the arguments of the cli command
tasky start#
On va donc rajouter dans notre script la gestion des commandes, on commence par la command
tasky start <name> <script>
cli.js
// Command to start a new process
program
.command('start <name> <script>')
.description('Start a new process')
.action((name, script) => {
console.log(chalk.cyan(`Starting process: ${name} with script ${script}`));
try {
startProcess(name, script);
} catch (error) {
console.error(chalk.red(`Error starting process: ${error.message}`));
}
});
Comme on peut le voir ici on ajoute en premier la commande:
.command('start <name> <script>')
Qui avec la description:
.description('Start a new process')
Ajoute une ligne dans le manuel de notre script.
Elle prend donc comme arguments le nom du process et le script que l’on va lancer.
tasky stop#
tasky stop <pid>
cli.js
// Command to stop a process by PID
program
.command('stop <pid | name>')
.description('Stop a process by PID or name')
.action((pid) => {
console.log(chalk.yellow(`Stopping process with PID: ${pid}`));
try {
stopProcess(Number(pid));
} catch (error) {
console.error(chalk.red(`Error stopping process: ${error.message}`));
}
});
Comme on peut le voir ici on ajoute en premier la commande:
.command('stop <pid | name>')
Qui avec la description:
.description('Stop a process by PID or name')
Ajoute une ligne dans le manuel de notre script.
Elle prend donc comme arguments le pid ou le nom du process a arreter
the entire file#
#!/usr/bin/env node
import { Command } from 'commander';
import fs from 'fs';
import chalk from 'chalk'; // Import chalk for colorization
import { startProcess, stopProcess, listProcesses, realtimeLogs, viewProcessLogs } from './manager.js';
import { monitorProcess } from './monitor.js';
const program = new Command();
// Function to tail the log file and display new logs as they are added
const tailLogs = (logPath) => {
if (!fs.existsSync(logPath)) {
console.log(chalk.red(`Log file does not exist: ${logPath}`));
return;
}
console.log(chalk.green(`Tailing log file: ${logPath}`));
const stream = fs.createReadStream(logPath, { encoding: 'utf-8', start: fs.statSync(logPath).size });
stream.on('data', (chunk) => {
process.stdout.write(chalk.blue(chunk)); // Log output with blue color
});
// Watch for file changes and display new log data as it appears
fs.watchFile(logPath, () => {
const newStream = fs.createReadStream(logPath, { encoding: 'utf-8', start: fs.statSync(logPath).size });
newStream.on('data', (chunk) => {
process.stdout.write(chalk.blue(chunk)); // Output new logs in blue
});
});
// Graceful exit by stopping the file watch
process.on('SIGINT', () => {
fs.unwatchFile(logPath); // Unwatch file when process is terminated
console.log(chalk.yellow('Stopped watching log file.'));
process.exit();
});
};
// Command to start a new process
program
.command('start <name> <script>')
.description('Start a new process')
.action((name, script) => {
console.log(chalk.cyan(`Starting process: ${name} with script ${script}`));
try {
startProcess(name, script);
} catch (error) {
console.error(chalk.red(`Error starting process: ${error.message}`));
}
});
// Command to stop a process by PID
program
.command('stop <pid>')
.description('Stop a process by PID')
.action((pid) => {
console.log(chalk.yellow(`Stopping process with PID: ${pid}`));
try {
stopProcess(Number(pid));
} catch (error) {
console.error(chalk.red(`Error stopping process: ${error.message}`));
}
});
// Command to list all running processes
program
.command('list')
.description('List all running processes')
.action(() => {
console.log(chalk.magenta('Listing all running processes...'));
listProcesses();
});
// Command to monitor a process by PID
program
.command('monitor <pid>')
.description('Monitor a process by PID')
.action((pid) => {
console.log(chalk.green(`Monitoring process with PID: ${pid}`));
monitorProcess(Number(pid));
});
// Command to view real-time logs of a process by PID
program
.command('logs <pid>')
.description('View real-time logs of a process')
.action((pid) => {
const processInfo = realtimeLogs(pid);
if (processInfo && processInfo.logPath) {
console.log(chalk.blue(`Viewing logs for process with PID: ${pid}`));
viewProcessLogs(processInfo.logPath);
} else {
console.log(chalk.red(`No log path available for process with PID ${pid}`));
}
});
program.parse(process.argv);