2020-01-21 11:50:06 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-05-05 12:23:15 -04:00
|
|
|
use crate::futures::FutureExt;
|
2020-06-19 06:27:15 -04:00
|
|
|
use crate::tsc::runtime_bundle;
|
2020-05-08 10:18:00 -04:00
|
|
|
use crate::tsc::runtime_compile;
|
|
|
|
use crate::tsc::runtime_transpile;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-09-05 20:34:02 -04:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use serde_json::Value;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::collections::HashMap;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-01-21 11:50:06 -05:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_async(rt, "op_compile", op_compile);
|
|
|
|
super::reg_json_async(rt, "op_transpile", op_transpile);
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CompileArgs {
|
|
|
|
root_name: String,
|
|
|
|
sources: Option<HashMap<String, String>>,
|
|
|
|
bundle: bool,
|
|
|
|
options: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_compile(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-01-21 11:50:06 -05:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_data: BufVec,
|
|
|
|
) -> Result<Value, ErrBox> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state2(&state);
|
|
|
|
cli_state.check_unstable("Deno.compile");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: CompileArgs = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let global_state = cli_state.global_state.clone();
|
|
|
|
let permissions = cli_state.permissions.borrow().clone();
|
2020-08-28 11:08:24 -04:00
|
|
|
let fut = if args.bundle {
|
|
|
|
runtime_bundle(
|
|
|
|
&global_state,
|
|
|
|
permissions,
|
|
|
|
&args.root_name,
|
|
|
|
&args.sources,
|
|
|
|
&args.options,
|
|
|
|
)
|
|
|
|
.boxed_local()
|
|
|
|
} else {
|
|
|
|
runtime_compile(
|
|
|
|
&global_state,
|
|
|
|
permissions,
|
|
|
|
&args.root_name,
|
|
|
|
&args.sources,
|
|
|
|
&args.options,
|
|
|
|
)
|
|
|
|
.boxed_local()
|
|
|
|
};
|
|
|
|
let result = fut.await?;
|
|
|
|
Ok(result)
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct TranspileArgs {
|
|
|
|
sources: HashMap<String, String>,
|
|
|
|
options: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_transpile(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2020-01-21 11:50:06 -05:00
|
|
|
args: Value,
|
2020-08-28 11:08:24 -04:00
|
|
|
_data: BufVec,
|
|
|
|
) -> Result<Value, ErrBox> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state2(&state);
|
|
|
|
cli_state.check_unstable("Deno.transpile");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: TranspileArgs = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let global_state = cli_state.global_state.clone();
|
|
|
|
let permissions = cli_state.permissions.borrow().clone();
|
2020-08-28 11:08:24 -04:00
|
|
|
let result =
|
2020-08-18 12:30:13 -04:00
|
|
|
runtime_transpile(&global_state, permissions, &args.sources, &args.options)
|
2020-08-28 11:08:24 -04:00
|
|
|
.await?;
|
|
|
|
Ok(result)
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|