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-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
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-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::BufVec;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
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,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
let cli_state = super::global_state2(&state);
|
2020-09-10 09:57:45 -04:00
|
|
|
cli_state.check_unstable("Deno.compile");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: CompileArgs = serde_json::from_value(args)?;
|
2020-09-19 19:17:35 -04:00
|
|
|
let global_state = cli_state.clone();
|
|
|
|
let permissions = {
|
|
|
|
let state = state.borrow();
|
|
|
|
state.borrow::<Permissions>().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,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
let cli_state = super::global_state2(&state);
|
2020-09-10 09:57:45 -04:00
|
|
|
cli_state.check_unstable("Deno.transpile");
|
2020-01-21 11:50:06 -05:00
|
|
|
let args: TranspileArgs = serde_json::from_value(args)?;
|
2020-09-19 19:17:35 -04:00
|
|
|
let global_state = cli_state.clone();
|
|
|
|
let permissions = {
|
|
|
|
let state = state.borrow();
|
|
|
|
state.borrow::<Permissions>().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
|
|
|
}
|