import type { DatabaseSync } from "node:sqlite"; import { Checkbox } from "@cliffy/prompt"; import { Table } from "@cliffy/table"; export default async (db: DatabaseSync) => { const recipes = db.prepare("SELECT * FROM recipes").all(); const choices = await Checkbox.prompt({ message: "Select recipes", options: recipes.map((recipe) => ({ name: recipe.name as string, value: recipe, })), }); const rawIngredients = choices.flatMap((recipe) => JSON.parse(recipe.ingredients as string) ); const ingredients = rawIngredients.reduce((acc, ingredient) => { const existingIngredient = acc.find( (i) => i.name === ingredient.name && i.unit === ingredient.unit ); if (existingIngredient) existingIngredient.value += ingredient.value; else acc.push(ingredient); return acc; }, [] as { name: string; value: number; unit: string }[]); const table = new Table( ...ingredients.map((item) => [item.name, item.value, item.unit]) ).header(["Name", "Value", "Unit"]); console.log(table.border().sort().toString()); };