1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

feat(install): add --config flag (#6204)

This commits adds support for "--config" flag in "deno install"
subcommand. Specified configuration file is copied alongside
source code to installation directory.
This commit is contained in:
simwipado 2020-07-12 23:05:47 +10:00 committed by GitHub
parent 3374c73fba
commit e1d8140552
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 0 deletions

View file

@ -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![

View file

@ -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<PathBuf, Error> {
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 == "{}");
}
}