From 9a216806d514b5f41c73c777010572cdf3c51eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 22 Sep 2022 16:39:58 +0200 Subject: [PATCH] feat(npm): add support for --reload=npm: and --reload=npm: (#15972) --- cli/args/flags.rs | 6 +- cli/file_fetcher.rs | 17 ++++ cli/npm/cache.rs | 1 + cli/npm/registry.rs | 5 +- cli/proc_state.rs | 1 - cli/tests/integration/npm_tests.rs | 111 ++++++++++++++++++++++++++ cli/tests/testdata/npm/reload/main.ts | 3 + 7 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 cli/tests/testdata/npm/reload/main.ts diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 6924907cbf..7922a3c19d 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -1937,7 +1937,11 @@ fn reload_arg<'a>() -> Arg<'a> { --reload=https://deno.land/std Reload only standard modules --reload=https://deno.land/std/fs/utils.ts,https://deno.land/std/fmt/colors.ts - Reloads specific modules", + Reloads specific modules +--reload=npm: + Reload all npm modules +--reload=npm:chalk + Reload specific npm module", ) .value_hint(ValueHint::FilePath) .validator(reload_arg_validate) diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index 3e3776f245..e16b808d97 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -146,6 +146,23 @@ impl CacheSetting { } } } + + pub fn should_use_for_npm_package(&self, package_name: &str) -> bool { + match self { + CacheSetting::ReloadAll => false, + CacheSetting::ReloadSome(list) => { + if list.contains(&"npm:".to_string()) { + return false; + } + let specifier = format!("npm:{}", package_name); + if list.contains(&specifier) { + return false; + } + true + } + _ => true, + } + } } /// Fetch a source file from the local file system. diff --git a/cli/npm/cache.rs b/cli/npm/cache.rs index 71a82fd51e..a733eccebd 100644 --- a/cli/npm/cache.rs +++ b/cli/npm/cache.rs @@ -204,6 +204,7 @@ impl NpmCache { // if this file exists, then the package didn't successfully extract // the first time, or another process is currently extracting the zip file && !package_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME).exists() + && self.cache_setting.should_use_for_npm_package(&id.name) { return Ok(()); } else if self.cache_setting == CacheSetting::Only { diff --git a/cli/npm/registry.rs b/cli/npm/registry.rs index 4fb926287f..a0ffd05447 100644 --- a/cli/npm/registry.rs +++ b/cli/npm/registry.rs @@ -105,7 +105,6 @@ pub struct NpmRegistryApi { base_url: Url, cache: NpmCache, mem_cache: Arc>>>, - reload: bool, cache_setting: CacheSetting, progress_bar: ProgressBar, } @@ -133,7 +132,6 @@ impl NpmRegistryApi { pub fn new( base_url: Url, cache: NpmCache, - reload: bool, cache_setting: CacheSetting, progress_bar: ProgressBar, ) -> Self { @@ -141,7 +139,6 @@ impl NpmRegistryApi { base_url, cache, mem_cache: Default::default(), - reload, cache_setting, progress_bar, } @@ -171,7 +168,7 @@ impl NpmRegistryApi { Ok(info) } else { let mut maybe_package_info = None; - if !self.reload { + if self.cache_setting.should_use_for_npm_package(name) { // attempt to load from the file cache maybe_package_info = self.load_file_cached_package_info(name); } diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 2d2c3a76de..d3c6f2cdd2 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -231,7 +231,6 @@ impl ProcState { let api = NpmRegistryApi::new( registry_url, npm_cache.clone(), - cli_options.reload_flag(), cli_options.cache_setting(), progress_bar.clone(), ); diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index 49bac64fb3..2dcd9824f5 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -266,6 +266,117 @@ fn cached_only_after_first_run() { assert_contains!(stdout, "createChalk: chalk"); } +#[test] +fn reload_flag() { + let _server = http_server(); + + let deno_dir = util::new_deno_dir(); + + let deno = util::deno_cmd_with_deno_dir(&deno_dir) + .current_dir(util::testdata_path()) + .arg("run") + .arg("--unstable") + .arg("--allow-read") + .arg("--allow-env") + .arg("npm/reload/main.ts") + .env("NO_COLOR", "1") + .envs(env_vars()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let output = deno.wait_with_output().unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr); + let stdout = String::from_utf8_lossy(&output.stdout); + assert_contains!(stderr, "Download"); + assert_contains!(stdout, "createChalk: chalk"); + assert!(output.status.success()); + + let deno = util::deno_cmd_with_deno_dir(&deno_dir) + .current_dir(util::testdata_path()) + .arg("run") + .arg("--unstable") + .arg("--allow-read") + .arg("--allow-env") + .arg("--reload") + .arg("npm/reload/main.ts") + .env("NO_COLOR", "1") + .envs(env_vars()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let output = deno.wait_with_output().unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr); + let stdout = String::from_utf8_lossy(&output.stdout); + assert_contains!(stderr, "Download"); + assert_contains!(stdout, "createChalk: chalk"); + assert!(output.status.success()); + + let deno = util::deno_cmd_with_deno_dir(&deno_dir) + .current_dir(util::testdata_path()) + .arg("run") + .arg("--unstable") + .arg("--allow-read") + .arg("--allow-env") + .arg("--reload=npm:") + .arg("npm/reload/main.ts") + .env("NO_COLOR", "1") + .envs(env_vars()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let output = deno.wait_with_output().unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr); + let stdout = String::from_utf8_lossy(&output.stdout); + assert_contains!(stderr, "Download"); + assert_contains!(stdout, "createChalk: chalk"); + assert!(output.status.success()); + + let deno = util::deno_cmd_with_deno_dir(&deno_dir) + .current_dir(util::testdata_path()) + .arg("run") + .arg("--unstable") + .arg("--allow-read") + .arg("--allow-env") + .arg("--reload=npm:chalk") + .arg("npm/reload/main.ts") + .env("NO_COLOR", "1") + .envs(env_vars()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let output = deno.wait_with_output().unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr); + let stdout = String::from_utf8_lossy(&output.stdout); + assert_contains!(stderr, "Download"); + assert_contains!(stdout, "createChalk: chalk"); + assert!(output.status.success()); + + let deno = util::deno_cmd_with_deno_dir(&deno_dir) + .current_dir(util::testdata_path()) + .arg("run") + .arg("--unstable") + .arg("--allow-read") + .arg("--allow-env") + .arg("--reload=npm:foobar") + .arg("npm/reload/main.ts") + .env("NO_COLOR", "1") + .envs(env_vars()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let output = deno.wait_with_output().unwrap(); + let stderr = String::from_utf8_lossy(&output.stderr); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(stderr.is_empty()); + assert_contains!(stdout, "createChalk: chalk"); + assert!(output.status.success()); +} + #[test] fn no_npm_after_first_run() { let _server = http_server(); diff --git a/cli/tests/testdata/npm/reload/main.ts b/cli/tests/testdata/npm/reload/main.ts new file mode 100644 index 0000000000..1ccc441a15 --- /dev/null +++ b/cli/tests/testdata/npm/reload/main.ts @@ -0,0 +1,3 @@ +import chalk from "npm:chalk@5"; + +console.log(chalk);