Skip to content

Commit

Permalink
Add support for loading tsconfig.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Apr 26, 2019
1 parent 40d8ef1 commit 81d86e3
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 7 deletions.
23 changes: 23 additions & 0 deletions cli/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ fn req(specifier: &str, referrer: &str, cmd_id: u32) -> Buf {
.into_boxed_bytes()
}

/// Returns an optional tuple which represents the state of the compiler
/// configuration where the first is canonical name for the configuration file
/// and a vector of the bytes of the contents of the configuration file.
pub fn get_compiler_config(
parent_state: &ThreadSafeState,
_compiler_type: &str,
) -> Option<(String, Vec<u8>)> {
match (&parent_state.config_file_name, &parent_state.config) {
(Some(config_file_name), Some(config)) => {
Some((config_file_name.to_string(), config.to_vec()))
}
_ => None,
}
}

pub fn compile_async(
parent_state: ThreadSafeState,
specifier: &str,
Expand Down Expand Up @@ -306,4 +321,12 @@ mod tests {

assert_eq!(parse_cmd_id(res_json), cmd_id);
}

#[test]
fn test_get_compiler_config_no_flag() {
let compiler_type = "typescript";
let state = ThreadSafeState::mock();
let out = get_compiler_config(&state, compiler_type);
assert_eq!(out, None);
}
}
22 changes: 22 additions & 0 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct DenoFlags {
pub log_debug: bool,
pub version: bool,
pub reload: bool,
pub config: Option<String>,
pub allow_read: bool,
pub allow_write: bool,
pub allow_net: bool,
Expand Down Expand Up @@ -79,6 +80,13 @@ pub fn create_cli_app<'a, 'b>() -> App<'a, 'b> {
.short("r")
.long("reload")
.help("Reload source code cache (recompile TypeScript)"),
).arg(
Arg::with_name("config")
.short("c")
.long("config")
.value_name("FILE")
.help("Load compiler configuration file")
.takes_value(true),
).arg(
Arg::with_name("v8-options")
.long("v8-options")
Expand Down Expand Up @@ -143,6 +151,7 @@ pub fn parse_flags(matches: ArgMatches) -> DenoFlags {
if matches.is_present("reload") {
flags.reload = true;
}
flags.config = matches.value_of("config").map(ToOwned::to_owned);
if matches.is_present("allow-read") {
flags.allow_read = true;
}
Expand Down Expand Up @@ -350,4 +359,17 @@ mod tests {
}
)
}

#[test]
fn test_set_flags_11() {
let flags =
flags_from_vec(svec!["deno", "-c", "tsconfig.json", "script.ts"]);
assert_eq!(
flags,
DenoFlags {
config: Some("tsconfig.json".to_owned()),
..DenoFlags::default()
}
)
}
}
11 changes: 11 additions & 0 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ union Any {
Chdir,
Chmod,
Close,
CompilerConfig,
CompilerConfigRes,
CopyFile,
Cwd,
CwdRes,
Expand Down Expand Up @@ -174,6 +176,15 @@ table StartRes {
no_color: bool;
}

table CompilerConfig {
compiler_type: string;
}

table CompilerConfigRes {
path: string;
data: [ubyte];
}

table FormatError {
error: string;
}
Expand Down
37 changes: 37 additions & 0 deletions cli/ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use atty;
use crate::ansi;
use crate::compiler::get_compiler_config;
use crate::errors;
use crate::errors::{DenoError, DenoResult, ErrorKind};
use crate::fs as deno_fs;
Expand Down Expand Up @@ -146,6 +147,8 @@ pub fn dispatch_all(

pub fn op_selector_compiler(inner_type: msg::Any) -> Option<OpCreator> {
match inner_type {
msg::Any::CompilerConfig => Some(op_compiler_config),
msg::Any::Cwd => Some(op_cwd),
msg::Any::FetchModuleMetaData => Some(op_fetch_module_meta_data),
msg::Any::WorkerGetMessage => Some(op_worker_get_message),
msg::Any::WorkerPostMessage => Some(op_worker_post_message),
Expand Down Expand Up @@ -443,6 +446,40 @@ fn op_fetch_module_meta_data(
}()))
}

fn op_compiler_config(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: deno_buf,
) -> Box<OpWithError> {
assert_eq!(data.len(), 0);
let inner = base.inner_as_compiler_config().unwrap();
let cmd_id = base.cmd_id();
let compiler_type = inner.compiler_type().unwrap();

Box::new(futures::future::result(|| -> OpResult {
let builder = &mut FlatBufferBuilder::new();
let (path, out) = match get_compiler_config(state, compiler_type) {
Some(val) => val,
_ => ("".to_owned(), "".as_bytes().to_owned()),
};
let data_off = builder.create_vector(&out);
let msg_args = msg::CompilerConfigResArgs {
path: Some(builder.create_string(&path)),
data: Some(data_off),
};
let inner = msg::CompilerConfigRes::create(builder, &msg_args);
Ok(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::CompilerConfigRes,
..Default::default()
},
))
}()))
}

fn op_chdir(
_state: &ThreadSafeState,
base: &msg::Base<'_>,
Expand Down
40 changes: 40 additions & 0 deletions cli/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use futures::future::Shared;
use std;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::ops::Deref;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -51,6 +52,8 @@ pub struct State {
pub argv: Vec<String>,
pub permissions: DenoPermissions,
pub flags: flags::DenoFlags,
pub config: Option<Vec<u8>>,
pub config_file_name: Option<String>,
pub metrics: Metrics,
pub worker_channels: Mutex<WorkerChannels>,
pub global_timer: Mutex<GlobalTimer>,
Expand Down Expand Up @@ -97,11 +100,48 @@ impl ThreadSafeState {
let external_channels = (worker_in_tx, worker_out_rx);
let resource = resources::add_worker(external_channels);

let config_file = match &flags.config {
Some(config_file_name) => {
debug!("Compiler config file: {}", config_file_name);
let cwd = std::env::current_dir().unwrap();
Some(cwd.join(config_file_name))
}
_ => None,
};

let config_file_name = match &config_file {
Some(config_file) => Some(
config_file
.canonicalize()
.unwrap()
.to_str()
.unwrap()
.to_owned(),
),
_ => None,
};

let config = match &config_file {
Some(config_file) => {
debug!("Attempt to load config: {}", config_file.to_str().unwrap());
match fs::read(&config_file) {
Ok(config_data) => Some(config_data.to_owned()),
_ => panic!(
"Error retrieving compiler config file at \"{}\"",
config_file.to_str().unwrap()
),
}
}
_ => None,
};

ThreadSafeState(Arc::new(State {
dir: deno_dir::DenoDir::new(custom_root).unwrap(),
argv: argv_rest,
permissions: DenoPermissions::from_flags(&flags),
flags,
config,
config_file_name,
metrics: Metrics::default(),
worker_channels: Mutex::new(internal_channels),
global_timer: Mutex::new(GlobalTimer::new()),
Expand Down
Loading

0 comments on commit 81d86e3

Please sign in to comment.