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=https://deno.land/std
|
||||||
Reload only standard modules
|
Reload only standard modules
|
||||||
--reload=https://deno.land/std/fs/utils.ts,https://deno.land/std/fmt/colors.ts
|
--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)
|
.value_hint(ValueHint::FilePath)
|
||||||
.validator(reload_arg_validate)
|
.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.
|
/// 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
|
// if this file exists, then the package didn't successfully extract
|
||||||
// the first time, or another process is currently extracting the zip file
|
// the first time, or another process is currently extracting the zip file
|
||||||
&& !package_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME).exists()
|
&& !package_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME).exists()
|
||||||
|
&& self.cache_setting.should_use_for_npm_package(&id.name)
|
||||||
{
|
{
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else if self.cache_setting == CacheSetting::Only {
|
} else if self.cache_setting == CacheSetting::Only {
|
||||||
|
|
|
@ -105,7 +105,6 @@ pub struct NpmRegistryApi {
|
||||||
base_url: Url,
|
base_url: Url,
|
||||||
cache: NpmCache,
|
cache: NpmCache,
|
||||||
mem_cache: Arc<Mutex<HashMap<String, Option<NpmPackageInfo>>>>,
|
mem_cache: Arc<Mutex<HashMap<String, Option<NpmPackageInfo>>>>,
|
||||||
reload: bool,
|
|
||||||
cache_setting: CacheSetting,
|
cache_setting: CacheSetting,
|
||||||
progress_bar: ProgressBar,
|
progress_bar: ProgressBar,
|
||||||
}
|
}
|
||||||
|
@ -133,7 +132,6 @@ impl NpmRegistryApi {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
base_url: Url,
|
base_url: Url,
|
||||||
cache: NpmCache,
|
cache: NpmCache,
|
||||||
reload: bool,
|
|
||||||
cache_setting: CacheSetting,
|
cache_setting: CacheSetting,
|
||||||
progress_bar: ProgressBar,
|
progress_bar: ProgressBar,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -141,7 +139,6 @@ impl NpmRegistryApi {
|
||||||
base_url,
|
base_url,
|
||||||
cache,
|
cache,
|
||||||
mem_cache: Default::default(),
|
mem_cache: Default::default(),
|
||||||
reload,
|
|
||||||
cache_setting,
|
cache_setting,
|
||||||
progress_bar,
|
progress_bar,
|
||||||
}
|
}
|
||||||
|
@ -171,7 +168,7 @@ impl NpmRegistryApi {
|
||||||
Ok(info)
|
Ok(info)
|
||||||
} else {
|
} else {
|
||||||
let mut maybe_package_info = None;
|
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
|
// attempt to load from the file cache
|
||||||
maybe_package_info = self.load_file_cached_package_info(name);
|
maybe_package_info = self.load_file_cached_package_info(name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,7 +231,6 @@ impl ProcState {
|
||||||
let api = NpmRegistryApi::new(
|
let api = NpmRegistryApi::new(
|
||||||
registry_url,
|
registry_url,
|
||||||
npm_cache.clone(),
|
npm_cache.clone(),
|
||||||
cli_options.reload_flag(),
|
|
||||||
cli_options.cache_setting(),
|
cli_options.cache_setting(),
|
||||||
progress_bar.clone(),
|
progress_bar.clone(),
|
||||||
);
|
);
|
||||||
|
|
|
@ -266,6 +266,117 @@ fn cached_only_after_first_run() {
|
||||||
assert_contains!(stdout, "createChalk: chalk");
|
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]
|
#[test]
|
||||||
fn no_npm_after_first_run() {
|
fn no_npm_after_first_run() {
|
||||||
let _server = http_server();
|
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