diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2019-04-29 15:58:31 +0100 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-04-29 07:58:31 -0700 |
commit | 1a0f53a807abad0e9ebfcf437f3dade6b01d7f84 (patch) | |
tree | 013eb8d15383a22a4e2c4c5502ba37035b397903 /cli/state.rs | |
parent | 73be183864d0983821e683198d9a6ea9008f070a (diff) |
Add support for custom tsconfig.json (#2089)
Use `--config`
Diffstat (limited to 'cli/state.rs')
-rw-r--r-- | cli/state.rs | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/cli/state.rs b/cli/state.rs index f10f3b7e0..5aefe7d90 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -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; @@ -51,6 +52,12 @@ pub struct State { pub argv: Vec<String>, pub permissions: DenoPermissions, pub flags: flags::DenoFlags, + /// When flags contains a `.config_path` option, the content of the + /// configuration file will be resolved and set. + pub config: Option<Vec<u8>>, + /// When flags contains a `.config_path` option, the fully qualified path + /// name of the passed path will be resolved and set. + pub config_path: Option<String>, pub metrics: Metrics, pub worker_channels: Mutex<WorkerChannels>, pub global_timer: Mutex<GlobalTimer>, @@ -97,11 +104,52 @@ impl ThreadSafeState { let external_channels = (worker_in_tx, worker_out_rx); let resource = resources::add_worker(external_channels); + // take the passed flag and resolve the file name relative to the cwd + let config_file = match &flags.config_path { + 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, + }; + + // Convert the PathBuf to a canonicalized string. This is needed by the + // compiler to properly deal with the configuration. + let config_path = match &config_file { + Some(config_file) => Some( + config_file + .canonicalize() + .unwrap() + .to_str() + .unwrap() + .to_owned(), + ), + _ => None, + }; + + // Load the contents of the configuration file + 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(), + dir: deno_dir::DenoDir::new(custom_root, &config).unwrap(), argv: argv_rest, permissions: DenoPermissions::from_flags(&flags), flags, + config, + config_path, metrics: Metrics::default(), worker_channels: Mutex::new(internal_channels), global_timer: Mutex::new(GlobalTimer::new()), |