add: meal ingestion CLI for images and metadata
All checks were successful
Deploy on push / deploy (push) Has been skipped
All checks were successful
Deploy on push / deploy (push) Has been skipped
This commit is contained in:
@@ -60,6 +60,10 @@ function validateMeals(meals) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!/^\d+$/.test(meal.id)) {
|
||||
throw new Error(`Meal ${index} has a non-numeric id "${meal.id}"`);
|
||||
}
|
||||
|
||||
if (meal.position !== undefined && typeof meal.position !== "string") {
|
||||
throw new Error(`Meal ${index} has a non-string "position" value`);
|
||||
}
|
||||
@@ -82,8 +86,31 @@ function loadMeals() {
|
||||
return meals;
|
||||
}
|
||||
|
||||
function saveMeals(meals) {
|
||||
validateMeals(meals);
|
||||
fs.writeFileSync(mealsPath, `${JSON.stringify(meals, null, 2)}\n`);
|
||||
}
|
||||
|
||||
function getNextMealId(meals) {
|
||||
if (meals.length === 0) {
|
||||
return "01";
|
||||
}
|
||||
|
||||
const nextNumber =
|
||||
Math.max(...meals.map((meal) => Number.parseInt(meal.id, 10))) + 1;
|
||||
const idWidth = Math.max(
|
||||
2,
|
||||
...meals.map((meal) => meal.id.length),
|
||||
String(nextNumber).length
|
||||
);
|
||||
|
||||
return String(nextNumber).padStart(idWidth, "0");
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNextMealId,
|
||||
loadMeals,
|
||||
mealsPath,
|
||||
repoRoot,
|
||||
saveMeals,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user