From f90bf939a47371c3642a4931036172a6f745638f Mon Sep 17 00:00:00 2001 From: JLEROY Date: Tue, 17 Jun 2025 10:03:07 +0200 Subject: [PATCH] first commit --- .gitignore | 41 ++---------------------- comment.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 17 ++++++++++ 3 files changed, 110 insertions(+), 38 deletions(-) create mode 100644 comment.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore index 8f9bdfe..45b19a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,39 +1,4 @@ -### Angular template -## Angular ## -# compiled output -dist/ -tmp/ -app/**/*.js -app/**/*.js.map - -# dependencies -node_modules/ -bower_components/ - -# IDEs and editors .idea/ - -# misc -.sass-cache/ -connect.lock/ -coverage/ -libpeerconnection.log/ -npm-debug.log -testem.log -typings/ -.angular/ - -# e2e -e2e/*.js -e2e/*.map - -# System Files -.DS_Store/ - -# Certificate files -cert/ -*.key -*.pem - -# Environment files -*.prod +node_modules/ +output/ +package-lock.json \ No newline at end of file diff --git a/comment.js b/comment.js new file mode 100644 index 0000000..b88f82d --- /dev/null +++ b/comment.js @@ -0,0 +1,90 @@ +const fs = require("fs"); +const path = require("path"); +const axios = require("axios"); +const cliProgress = require('cli-progress'); + + +async function commentFile(filePath, outputDir) { + const code = fs.readFileSync(filePath, "utf-8"); + + const prompt = ` +Voici un fichier de code TypeScript ou JavaScript. +Ajoute des commentaires utiles et concis à toutes les fonctions, classes et méthodes. +Garde le code inchangé, seulement ajoute les commentaires en français. + +--- + +${code} +`; + + try { + const response = await axios.post("http://localhost:11434/api/generate", { + model: "llama3", + prompt: prompt, + stream: false, + }); + + const commentedCode = response.data.response; + + const filename = path.basename(filePath).replace(/(\.ts|\.js)$/, ".commented$1"); + const outputPath = path.join(outputDir, filename); + + fs.writeFileSync(outputPath, commentedCode); + console.log(`✅ Fichier commenté : ${outputPath}`); + } catch (error) { + console.error("Erreur lors de l’appel à Ollama :", error.message); + } +} + +function getCodeFilesRecursive(dir) { + let files = []; + for (const item of fs.readdirSync(dir)) { + const fullPath = path.join(dir, item); + const stat = fs.statSync(fullPath); + if (stat.isDirectory()) { + files = files.concat(getCodeFilesRecursive(fullPath)); + } else if (fullPath.endsWith(".ts") || fullPath.endsWith(".js")) { + files.push(fullPath); + } + } + return files; +} + +async function run() { + const inputDir = process.argv[2]; + if (!inputDir || !fs.existsSync(inputDir)) { + console.error("❌ Merci de fournir un dossier valide en argument !"); + process.exit(1); + } + + const absInputDir = path.resolve(inputDir); + const outputDir = path.resolve("./output"); + + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }); + } + + const files = getCodeFilesRecursive(absInputDir); + + console.log(`🔍 ${files.length} fichier(s) trouvé(s) dans ${absInputDir}`); + + const progressBar = new cliProgress.SingleBar({ + format: '📄 {filename} | {bar} {percentage}% | {value}/{total}', + barCompleteChar: '█', + barIncompleteChar: '░', + hideCursor: true + }, cliProgress.Presets.shades_classic); + + progressBar.start(files.length, 0, { filename: '' }); + + for (const file of files) { + progressBar.update(progressBar.value, { filename: path.basename(file) }); + await commentFile(file, outputDir); + progressBar.increment(); + } + + progressBar.stop(); + console.log("🎉 Tous les fichiers ont été traités !"); +} + +run(); diff --git a/package.json b/package.json new file mode 100644 index 0000000..6bd04f6 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "ai-commenter", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "dependencies": { + "axios": "^1.10.0", + "cli-progress": "^3.12.0" + } +}