2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-12-13 07:14:04 -05:00
|
|
|
|
2023-03-13 13:50:23 -04:00
|
|
|
use anyhow::Context;
|
2021-12-13 07:14:04 -05:00
|
|
|
use deno_core::anyhow::Error;
|
|
|
|
use deno_core::FsModuleLoader;
|
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::RuntimeOptions;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
if args.len() < 2 {
|
|
|
|
println!("Usage: target/examples/debug/fs_module_loader <path_to_module>");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2023-01-05 14:29:50 -05:00
|
|
|
let main_url = &args[1];
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("Run {main_url}");
|
2021-12-13 07:14:04 -05:00
|
|
|
|
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
module_loader: Some(Rc::new(FsModuleLoader)),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()?;
|
|
|
|
|
2023-03-13 13:50:23 -04:00
|
|
|
let main_module = deno_core::resolve_path(
|
|
|
|
main_url,
|
|
|
|
&std::env::current_dir().context("Unable to get CWD")?,
|
|
|
|
)?;
|
2021-12-13 07:14:04 -05:00
|
|
|
|
|
|
|
let future = async move {
|
|
|
|
let mod_id = js_runtime.load_main_module(&main_module, None).await?;
|
2022-06-20 08:40:57 -04:00
|
|
|
let result = js_runtime.mod_evaluate(mod_id);
|
2021-12-13 07:14:04 -05:00
|
|
|
js_runtime.run_event_loop(false).await?;
|
2022-06-20 08:40:57 -04:00
|
|
|
result.await?
|
2021-12-13 07:14:04 -05:00
|
|
|
};
|
|
|
|
runtime.block_on(future)
|
|
|
|
}
|