-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlite replaced with swc reading from cookbook.ts
- Loading branch information
1 parent
5ae8da8
commit 82ab7db
Showing
9 changed files
with
938 additions
and
130 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
type Recipe = { | ||
created_at: string; | ||
name: string; | ||
ingredients: string; | ||
instructions: string; | ||
}; | ||
|
||
export default [ | ||
{ | ||
created_at: "2024-10-01", | ||
name: "Pesto and sundried tomato stuffed zucchini boats", | ||
ingredients: `- 3 medium zucchinis | ||
- 1/2 cup of pesto | ||
- 1/4 cup of sundried tomatoes, chopped | ||
- 1/4 cup of grated parmesan cheese | ||
- Salt and pepper to taste | ||
`, | ||
instructions: `1. Preheat your oven to 375°F (190°C). | ||
2. Cut the zucchinis in half lengthwise and scoop out the seeds and flesh from the center to create a hollow "boat". | ||
3. In a mixing bowl, combine the pesto, chopped sundried tomatoes, and grated parmesan cheese. Season with salt and pepper to taste. | ||
4. Spoon the pesto mixture into the hollowed-out zucchini boats, filling them generously. | ||
5. Place the filled zucchini boats on a baking sheet lined with parchment paper. | ||
6. Bake in the preheated oven for about 20-25 minutes, or until the zucchinis are tender and the filling is heated through. | ||
7. Serve the pesto and sundried tomato stuffed zucchini boats hot as a delicious and satisfying meal or side dish. Enjoy! | ||
`, | ||
}, | ||
{ | ||
created_at: "2024-10-01", | ||
name: "Chocolate Chip Cookies", | ||
ingredients: `- 310 g flour | ||
- 1/2 tsp baking soda | ||
- 1/2 tsp salt | ||
- 170 g butter (melted) | ||
- 1 cup brown sugar | ||
- 1/2 cup sugar | ||
- 1 tbsp vanilla | ||
- 1 egg & 1 egg yolk | ||
- 2 cups chocolate chips | ||
`, | ||
instructions: `1. preheat 325 & grease cookie sheets | ||
2. sift flour, baking soda, & salt | ||
3. mix butter & sugar till well blended. beat in vanilla & eggs. mix in dry ingredients. add chocolate chips with spoon, then make big cookies on the sheets. | ||
4. bake 15-17 mins | ||
`, | ||
}, | ||
] satisfies Recipe[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use std::fs; | ||
use swc_common::{sync::Lrc, FileName, SourceMap}; | ||
use swc_ecma_ast::{Expr, Module, ModuleDecl, TsSatisfiesExpr}; | ||
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsSyntax}; | ||
|
||
use crate::utils::SavedRecipe; | ||
|
||
fn extract_default_export_list(module: &Module) -> Option<Vec<SavedRecipe>> { | ||
for item in &module.body { | ||
if let swc_ecma_ast::ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(expr)) = item { | ||
// Check if the export is a "satisfies" expression (type assertion) | ||
let expr = match &*expr.expr { | ||
Expr::TsSatisfies(TsSatisfiesExpr { expr, .. }) => &**expr, // Unwrap `satisfies` type assertion | ||
other => other, | ||
}; | ||
|
||
// Now check if it is an array literal | ||
if let Expr::Array(arr) = expr { | ||
let list = arr | ||
.elems | ||
.iter() | ||
.filter_map(|elem| { | ||
if let Some(Expr::Object(obj)) = elem.as_ref().map(|e| &*e.expr) { | ||
let mut name = String::new(); | ||
let mut created_at = String::new(); | ||
let mut ingredients = String::new(); | ||
let mut instructions = String::new(); | ||
|
||
for prop in &obj.props { | ||
if let swc_ecma_ast::PropOrSpread::Prop(boxed_prop) = prop { | ||
if let swc_ecma_ast::Prop::KeyValue(kv) = &**boxed_prop { | ||
if let Some(key) = kv.key.as_ident() { | ||
match &*kv.value { | ||
Expr::Lit(swc_ecma_ast::Lit::Str(lit)) => { | ||
// Handle regular string | ||
match &*key.sym { | ||
"name" => name = lit.value.to_string(), | ||
"created_at" => { | ||
created_at = lit.value.to_string() | ||
} | ||
"ingredients" => { | ||
ingredients = lit.value.to_string() | ||
} | ||
"instructions" => { | ||
instructions = lit.value.to_string() | ||
} | ||
_ => {} | ||
} | ||
} | ||
Expr::Tpl(tpl) => { | ||
// Handle template literals | ||
let combined_string = tpl | ||
.quasis | ||
.iter() | ||
.map(|quasi| { | ||
quasi | ||
.cooked | ||
.as_ref() | ||
.map(|c| c.to_string()) | ||
.unwrap_or_default() | ||
}) | ||
.collect::<String>(); | ||
|
||
match &*key.sym { | ||
"ingredients" => { | ||
ingredients = combined_string | ||
} | ||
"instructions" => { | ||
instructions = combined_string | ||
} | ||
_ => {} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if !name.is_empty() | ||
&& !ingredients.is_empty() | ||
&& !instructions.is_empty() | ||
&& !created_at.is_empty() | ||
{ | ||
Some(SavedRecipe { | ||
name, | ||
created_at, | ||
ingredients, | ||
instructions, | ||
}) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
|
||
return Some(list); | ||
} | ||
} | ||
} | ||
None | ||
} | ||
|
||
pub fn read_cookbook() -> Vec<SavedRecipe> { | ||
let cm: Lrc<SourceMap> = Default::default(); | ||
|
||
let file_path = "./cookbook.ts"; | ||
let code = fs::read_to_string(file_path).expect("Failed to read file"); | ||
|
||
let fm = cm.new_source_file(FileName::Real(file_path.into()).into(), code); | ||
|
||
let lexer = Lexer::new( | ||
Syntax::Typescript(TsSyntax { | ||
tsx: false, | ||
..Default::default() | ||
}), | ||
Default::default(), | ||
StringInput::from(&*fm), | ||
None, | ||
); | ||
let mut parser = Parser::new_from(lexer); | ||
|
||
let module = parser.parse_module().expect("Failed to parse module"); | ||
|
||
extract_default_export_list(&module).unwrap_or_default() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters