mirror of
https://github.com/denoland/deno.git
synced 2024-11-28 16:20:57 -05:00
feat(npm): add support for --reload=npm: and --reload=npm:<package> (#15972)
This commit is contained in:
parent
06c77a30f9
commit
9a216806d5
7 changed files with 138 additions and 6 deletions
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
);
|
||||
|
|
|
@ -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();
|
||||
|
|
3
cli/tests/testdata/npm/reload/main.ts
vendored
Normal file
3
cli/tests/testdata/npm/reload/main.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
import chalk from "npm:chalk@5";
|
||||
|
||||
console.log(chalk);
|
Loading…
Reference in a new issue