diff --git a/cli/flags.rs b/cli/flags.rs index 80e8565813..7d9ba8fdbd 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -349,6 +349,7 @@ fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) { permission_args_parse(flags, matches); + config_arg_parse(flags, matches); ca_file_arg_parse(flags, matches); unstable_arg_parse(flags, matches); @@ -701,6 +702,7 @@ fn install_subcommand<'a, 'b>() -> App<'a, 'b> { .takes_value(false)) .arg(ca_file_arg()) .arg(unstable_arg()) + .arg(config_arg()) .about("Install script as an executable") .long_about( "Installs a script as an executable in the installation root's bin directory. @@ -2371,6 +2373,32 @@ mod tests { ); } + #[test] + fn install_with_config() { + let r = flags_from_vec_safe(svec![ + "deno", + "install", + "--config", + "tsconfig.json", + "https://deno.land/std/examples/colors.ts" + ]); + + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Install { + name: None, + module_url: "https://deno.land/std/examples/colors.ts".to_string(), + args: svec![], + root: None, + force: false, + }, + config_path: Some("tsconfig.json".to_owned()), + ..Flags::default() + } + ) + } + #[test] fn install_with_args_and_dir_and_force() { let r = flags_from_vec_safe(svec![ diff --git a/cli/installer.rs b/cli/installer.rs index 40e5cf0f45..f975972a3b 100644 --- a/cli/installer.rs +++ b/cli/installer.rs @@ -79,6 +79,17 @@ deno {} "$@" Ok(()) } +fn generate_config_file( + file_path: PathBuf, + config_file_name: String, +) -> Result<(), Error> { + let config_file_copy_path = get_config_file_path(&file_path); + let cwd = std::env::current_dir().unwrap(); + let config_file_path = cwd.join(config_file_name); + fs::copy(config_file_path, config_file_copy_path)?; + Ok(()) +} + fn get_installer_root() -> Result { if let Ok(env_dir) = env::var("DENO_INSTALL_ROOT") { if !env_dir.is_empty() { @@ -211,10 +222,22 @@ pub fn install( executable_args.push("--unstable".to_string()); } + if flags.config_path.is_some() { + let config_file_path = get_config_file_path(&file_path); + let config_file_path_option = config_file_path.to_str(); + if let Some(config_file_path_string) = config_file_path_option { + executable_args.push("--config".to_string()); + executable_args.push(config_file_path_string.to_string()); + } + } + executable_args.push(module_url.to_string()); executable_args.extend_from_slice(&args); generate_executable_file(file_path.to_owned(), executable_args)?; + if let Some(config_path) = flags.config_path { + generate_config_file(file_path.to_owned(), config_path)?; + } println!("✅ Successfully installed {}", name); println!("{}", file_path.to_string_lossy()); @@ -243,6 +266,12 @@ fn is_in_path(dir: &PathBuf) -> bool { false } +fn get_config_file_path(file_path: &PathBuf) -> PathBuf { + let mut config_file_copy_path = PathBuf::from(file_path); + config_file_copy_path.set_extension("tsconfig.json"); + config_file_copy_path +} + #[cfg(test)] mod tests { use super::*; @@ -629,4 +658,36 @@ mod tests { let file_content_2 = fs::read_to_string(&file_path).unwrap(); assert!(file_content_2.contains("cat.ts")); } + + #[test] + fn install_with_config() { + let temp_dir = TempDir::new().expect("tempdir fail"); + let bin_dir = temp_dir.path().join("bin"); + let config_file_path = temp_dir.path().join("test_tsconfig.json"); + let config = "{}"; + let mut config_file = File::create(&config_file_path).unwrap(); + let result = config_file.write_all(config.as_bytes()); + assert!(result.is_ok()); + + let result = install( + Flags { + config_path: Some(config_file_path.to_string_lossy().to_string()), + ..Flags::default() + }, + "http://localhost:4545/cli/tests/cat.ts", + vec![], + Some("echo_test".to_string()), + Some(temp_dir.path().to_path_buf()), + true, + ); + eprintln!("result {:?}", result); + assert!(result.is_ok()); + + let config_file_name = "echo_test.tsconfig.json"; + + let file_path = bin_dir.join(config_file_name.to_string()); + assert!(file_path.exists()); + let content = fs::read_to_string(file_path).unwrap(); + assert!(content == "{}"); + } }