-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.js
121 lines (108 loc) · 4.17 KB
/
merge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Entrez le chemin du dossier à analyser : ", (directoryPath) => {
if (
!fs.existsSync(directoryPath) ||
!fs.lstatSync(directoryPath).isDirectory()
) {
console.log(
"Le dossier spécifié n'existe pas ou n'est pas un répertoire valide."
);
rl.close();
return;
}
rl.question("Entrez le nom du fichier de sortie : ", (outputFileName) => {
const outputPath = path.join(__dirname, outputFileName);
const ignoredFiles = [
__filename, // Le programme lui-même
outputFileName, // Le fichier de sortie
".gitignore",
"pnpm-lock.yaml",
"package-lock.json",
];
const ignoredDirs = ["node_modules", ".git", ".vscode", ".idea"];
rl.question(
"Entrez les noms des fichiers à ignorer (séparés par des virgules) : ",
(ignoredFilesInput) => {
const customIgnoredFiles = ignoredFilesInput
.split(",")
.map((item) => item.trim());
ignoredFiles.push(...customIgnoredFiles);
rl.question(
"Entrez les noms des dossiers à ignorer (séparés par des virgules) : ",
(ignoredDirsInput) => {
const customIgnoredDirs = ignoredDirsInput
.split(",")
.map((item) => item.trim());
ignoredDirs.push(...customIgnoredDirs);
// Fonction récursive pour parcourir les fichiers et dossiers
function processDirectory(currentDir) {
try {
const files = fs.readdirSync(currentDir);
for (const file of files) {
const filePath = path.join(currentDir, file);
if (
ignoredFiles.includes(file) ||
ignoredFiles.includes(filePath)
) {
continue;
}
const stats = fs.statSync(filePath);
if (stats.isFile()) {
// Si c'est un fichier, ajoutez son contenu au fichier de sortie
const fileContent = fs.readFileSync(filePath, "utf8");
fs.appendFileSync(
outputPath,
`Nom du fichier : ${file}\nChemin du fichier courant : ${filePath}\nContenu du fichier :\n${fileContent}\n\n`
);
} else if (stats.isDirectory()) {
if (
!ignoredDirs.includes(file) &&
!ignoredDirs.includes(filePath)
) {
// Si ce n'est pas un dossier ignoré, récursion pour explorer son contenu
processDirectory(filePath);
}
}
}
} catch (error) {
console.error(
`Erreur lors du traitement de ${currentDir}: ${error.message}`
);
}
}
// Demande de confirmation avant de supprimer le fichier de sortie s'il existe déjà
if (fs.existsSync(outputPath)) {
rl.question(
"Le fichier de sortie existe déjà. Voulez-vous le remplacer ? (Oui/Non): ",
(answer) => {
if (answer.toLowerCase() === "oui" || answer.toLowerCase() === "o" || answer.toLowerCase() === "yes" || answer.toLowerCase() === "y") {
fs.unlinkSync(outputPath);
processDirectory(directoryPath);
console.log(
`La sortie a été écrite dans le fichier : ${outputPath}`
);
} else {
console.log("L'opération a été annulée.");
}
rl.close();
}
);
} else {
processDirectory(directoryPath);
console.log(
`La sortie a été écrite dans le fichier : ${outputPath}`
);
rl.close();
}
}
);
}
);
});
});