Skip to content

Commit

Permalink
Improve file reading functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
redoC-A2k committed Apr 20, 2024
1 parent a007099 commit d558913
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 48 deletions.
50 changes: 11 additions & 39 deletions JS/wasm/crates/serve/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,18 @@ pub fn add_jsonnet_to_linker(linker: &mut Linker<WasiCtx>) -> 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<String> = 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::<Vec<&str>>()[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(())
},
)?;
Expand Down
14 changes: 7 additions & 7 deletions JS/wasm/examples/ec-wasmjs-hono/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions JS/wasm/examples/ec-wasmjs-hono/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit d558913

Please sign in to comment.