From d5589138d533c1b9a9087d74f5014d89a3cf92f7 Mon Sep 17 00:00:00 2001 From: afshan ahmed khan Date: Sat, 20 Apr 2024 19:58:43 +0530 Subject: [PATCH] Improve file reading functionality --- JS/wasm/crates/serve/src/binding.rs | 50 +++++--------------- JS/wasm/examples/ec-wasmjs-hono/build.js | 14 +++--- JS/wasm/examples/ec-wasmjs-hono/src/index.js | 11 ++++- 3 files changed, 27 insertions(+), 48 deletions(-) diff --git a/JS/wasm/crates/serve/src/binding.rs b/JS/wasm/crates/serve/src/binding.rs index b5cda47e6..b1ca79d59 100644 --- a/JS/wasm/crates/serve/src/binding.rs +++ b/JS/wasm/crates/serve/src/binding.rs @@ -155,46 +155,18 @@ pub fn add_jsonnet_to_linker(linker: &mut Linker) -> anyhow::Result<()> let vm = jsonnet_make(); - let entries = - fs::read_dir(env::current_dir().expect("Unable to get current directory")) - .expect("unable to read path"); - let mut file: Option = None; - for entry in entries { - let filepath = entry.expect("Panic in entry").path(); - if (filepath.is_file()) { - let filename = filepath - .file_name() - .expect("Panic in filename") - .to_str() - .expect("Panic in to_str") - .to_owned(); - if (filename.starts_with(path.split('.').collect::>()[0]) - && filename.ends_with(".jsonnet")) - { - file = Some(filename); - break; - } - } - } - match file { - Some(filename) => { - for (key, value) in var_json.as_object().unwrap() { - ext_string( - vm, - key, - value.as_str().expect("ext_string value is not a string"), - ); - } - let code = fs::read_to_string(filename)?; - let out = jsonnet_evaluate_snippet(vm, "deleteme", &code); - let mut output: std::sync::MutexGuard<'_, String> = output.lock().unwrap(); - *output = out; - } - None => { - let mut output = output.lock().unwrap(); - *output = r#""Empty file""#.to_string(); - } + for (key, value) in var_json.as_object().unwrap() { + ext_string( + vm, + key, + value.as_str().expect("ext_string value is not a string"), + ); } + let code = fs::read_to_string(path).expect("File not found"); + let out = jsonnet_evaluate_snippet(vm, "deleteme", &code); + let mut output: std::sync::MutexGuard<'_, String> = output.lock().unwrap(); + *output = out; + Ok(()) }, )?; diff --git a/JS/wasm/examples/ec-wasmjs-hono/build.js b/JS/wasm/examples/ec-wasmjs-hono/build.js index 5f3fc6ad2..edb1b6ba9 100644 --- a/JS/wasm/examples/ec-wasmjs-hono/build.js +++ b/JS/wasm/examples/ec-wasmjs-hono/build.js @@ -16,23 +16,23 @@ let runtime = process.argv[2]; // } build({ - // entryPoints: ["src/index.js", "src/example.jsonnet"], - entryPoints: ["src/index.js"], + entryPoints: ["src/index.js", "src/example.jsonnet"], + // entryPoints: ["src/index.js"], bundle: true, // minify: true, minifySyntax: true, - outfile: "bin/app.js", - // outdir: "bin", + // outfile: "bin/app.js", + outdir: "bin", format: "esm", target: "esnext", platform: "node", // external: ["arakoo"], + loader:{ + ".jsonnet":"copy" + }, define: { "process.env.arakoo": JSON.stringify(runtime === "arakoo"), }, - // loader: { - // ".jsonnet": "text", - // }, }).catch((error) => { console.error(error); process.exit(1); diff --git a/JS/wasm/examples/ec-wasmjs-hono/src/index.js b/JS/wasm/examples/ec-wasmjs-hono/src/index.js index 16a473502..29a141826 100644 --- a/JS/wasm/examples/ec-wasmjs-hono/src/index.js +++ b/JS/wasm/examples/ec-wasmjs-hono/src/index.js @@ -23,8 +23,15 @@ app.get("/", (c) => { }); app.get('/file', (c)=>{ - let result = jsonnet.extString("extName","Mohan").evaluateFile("example.jsonnet"); - return c.json(JSON.parse(result)); + try { + let result = jsonnet.extString("extName","Mohan").evaluateFile("example.jsonnet"); + return c.json(JSON.parse(result)); + } catch (error) { + console.log("Error occured"); + console.log(error); + return c.json("Unable to evaluate File") + } + }) app.get("/:username", (c) => {