Skip to content

feat(npm): add support for --reload=npm: and --reload=npm:<package> #15972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions cli/npm/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions cli/npm/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ pub struct NpmRegistryApi {
base_url: Url,
cache: NpmCache,
mem_cache: Arc<Mutex<HashMap<String, Option<NpmPackageInfo>>>>,
reload: bool,
cache_setting: CacheSetting,
progress_bar: ProgressBar,
}
Expand Down Expand Up @@ -133,15 +132,13 @@ impl NpmRegistryApi {
pub fn new(
base_url: Url,
cache: NpmCache,
reload: bool,
cache_setting: CacheSetting,
progress_bar: ProgressBar,
) -> Self {
Self {
base_url,
cache,
mem_cache: Default::default(),
reload,
cache_setting,
progress_bar,
}
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion cli/proc_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down
111 changes: 111 additions & 0 deletions cli/tests/integration/npm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to create a less verbose api for doing this kind of testing in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'll see how many tests could use a helper


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();
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/testdata/npm/reload/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import chalk from "npm:chalk@5";

console.log(chalk);