-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
277 lines (251 loc) · 5.88 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const fs = require("fs");
const path = require("path");
const { exit } = require("process");
// Parse the command-line arguments
const args = process.argv.slice(2); // Remove the first two arguments (node and script.js)
//Fallback Folder
let directoryPath = "D:/Dropbox/Rezepte";
// Process command line arguments
for (const arg of args) {
const [key, value] = arg.split("=");
if (key === "dir" && value) {
directoryPath = value;
}
}
// Array to store recipe titles
const recipeTitleList = [];
// Function to search the directory and extract recipe titles
function extractRecipeTitles(directory) {
try {
const files = fs.readdirSync(directory);
files.forEach((file) => {
const filePath = path.join(directory, file);
if (fs.statSync(filePath).isFile()) {
// Extract filename from file path
const fileName = path.basename(filePath);
// Add the filename to the recipeTitleList
recipeTitleList.push(fileName);
}
});
} catch (e) {
console.log(
"\x1b[31m",
"Could not find or missing path argument. Please change the path with dir='path/to/recipes' as argument.",
"\x1b[0m"
);
process.exit(1);
}
}
function deleteFolderRecursive(folderPath) {
if (fs.existsSync(folderPath)) {
fs.readdirSync(folderPath).forEach((file) => {
const curPath = path.join(folderPath, file);
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(folderPath);
}
}
// Function to create a weekly menu from the recipe titles
function createWeeklyMenu(recipeTitles, numberOfDays, selectedCategories) {
if (
recipeTitles.length < numberOfDays ||
selectedCategories.length < numberOfDays
) {
return "Not enough recipes or categories available for the weekly menu.";
}
const balancedMenu = [];
const usedCategories = new Set();
for (let day = 0; day < numberOfDays; day++) {
const availableDishes = [];
recipeTitles.forEach((title) => {
selectedCategories.forEach((category) => {
if (title.includes(category) && !usedCategories.has(category)) {
availableDishes.push({ title, category });
}
});
});
if (availableDishes.length === 0) {
// If no available dishes, add a message
balancedMenu.push("No matching dish found");
} else {
// Randomly select a dish from available dishes
const randomIndex = Math.floor(Math.random() * availableDishes.length);
const selectedDish = availableDishes[randomIndex];
balancedMenu.push(selectedDish.title);
// Add the used category to the set
usedCategories.add(selectedDish.category);
}
}
return balancedMenu;
}
// Search the directory and extract recipe titles
extractRecipeTitles(directoryPath);
// Create a weekly menu
// Selected categories for the menu (example)
const selectedCategories = [
"Hühnchen",
"Gemüse",
"Reis",
"Nudeln",
"Fleisch",
"Fisch",
"Obst",
"Vollkorn",
"Hülsenfrüchte",
"Joghurt",
"Eier",
"Magermilch",
"Nüsse",
"Samen",
"Käse",
"Olivenöl",
"Avocado",
"Beeren",
"Spinat",
"Karotten",
"Haferflocken",
"Brokkoli",
"Tomaten",
"Quinoa",
"Mandeln",
"Lachs",
"Tofu",
"Griechischer Joghurt",
"Bananen",
"Spargel",
"Rucola",
"Möhren",
"Linsen",
"Hummus",
"Chia-Samen",
"Rote Beete",
"Blumenkohl",
"Zitrusfrüchte",
"Erdnüsse",
"Mandelmilch",
"Schwarzer Tee",
"Grüner Tee",
"Vollkornbrot",
"Vollkornnudeln",
"Vollkornreis",
"Walnüsse",
"Erdbeeren",
"Himbeeren",
"Blaubeeren",
"Chinakohl",
"Süßkartoffeln",
"Knoblauch",
"Zwiebeln",
"Ingwer",
"Paprika",
"Radieschen",
"Johannisbeeren",
"Brombeeren",
"Ananas",
"Gurken",
"Melone",
"Kirschen",
"Zucchini",
"Pilze",
"Petersilie",
"Basilikum",
"Oregano",
"Rosmarin",
"Thymian",
"Minze",
"Koriander",
"Dill",
"Senf",
"Hühnerbrust",
"Hühnerschenkel",
"Putenschnitzel",
"Rindfleisch",
"Schweinefleisch",
"Lammfleisch",
"Ente",
"Garnelen",
"Krabben",
"Hummer",
"Schalentiere",
"Tintenfisch",
"Scholle",
"Thunfisch",
"Forelle",
"Kabeljau",
"Schinken",
"Salami",
"Pfefferoni",
"Sardellen",
"Sardinen",
"Makrele",
"Forelle",
"Heilbutt",
"Hering",
"Hühnereier",
"Wachteleier",
"Rindersteak",
"Hähnchenschenkel",
"Kaninchenfleisch",
"Wildschwein",
"Lachsforelle",
"Forellenfilet",
"Bisonfleisch",
"Lachsfilet",
"Steinbutt",
"Schwertfisch",
"Seelachs",
"Schellfisch",
"Kaninchenfilet",
];
// Create a weekly menu
const numberOfDaysInMenu = 7; // For example, 7 days for a week
const weeklyMenu = createWeeklyMenu(
recipeTitleList,
numberOfDaysInMenu,
selectedCategories
);
function copyFilesToWeeklyMenu(sourceDir, destinationDir, menu) {
deleteFolderRecursive(destinationDir);
fs.readdir(sourceDir, (err, files) => {
if (err) {
console.error("Fehler beim Lesen des Quellverzeichnisses:", err);
return;
}
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir);
}
menu.forEach((file) => {
const sourcePath = path.join(sourceDir, file);
const destinationPath = path.join(destinationDir, file);
fs.copyFile(sourcePath, destinationPath, (err) => {
if (err) {
console.error("Fehler beim Kopieren der Datei:", err);
} else {
}
});
});
console.log(
"\x1b[32m",
"You find your menu here: " + directoryPath + "/weekly-menu",
"\x1b[0m"
);
});
}
copyFilesToWeeklyMenu(
directoryPath,
directoryPath + "/weekly-menu",
weeklyMenu
);
// Print the weekly menu in the console
console.log("\x1b[32m", "--Weekly Menu--", "\x1b[0m");
if (Array.isArray(weeklyMenu)) {
weeklyMenu.forEach((dish, index) => {
console.log(`Day ${index + 1}: ${dish}`);
});
} else {
console.log(weeklyMenu);
}