diff options
Diffstat (limited to 'cli/args')
-rw-r--r-- | cli/args/config_file.rs | 13 | ||||
-rw-r--r-- | cli/args/flags.rs | 15 | ||||
-rw-r--r-- | cli/args/mod.rs | 38 | ||||
-rw-r--r-- | cli/args/package_json.rs | 40 |
4 files changed, 55 insertions, 51 deletions
diff --git a/cli/args/config_file.rs b/cli/args/config_file.rs index f7fd85579..3ba88c680 100644 --- a/cli/args/config_file.rs +++ b/cli/args/config_file.rs @@ -2,7 +2,6 @@ use crate::args::ConfigFlag; use crate::args::Flags; -use crate::args::TaskFlags; use crate::util::fs::canonicalize_path; use crate::util::path::specifier_parent; use crate::util::path::specifier_to_file_path; @@ -508,18 +507,6 @@ impl ConfigFile { return Ok(Some(cf)); } } - // attempt to resolve the config file from the task subcommand's - // `--cwd` when specified - if let crate::args::DenoSubcommand::Task(TaskFlags { - cwd: Some(path), - .. - }) = &flags.subcommand - { - let task_cwd = canonicalize_path(&PathBuf::from(path))?; - if let Some(path) = Self::discover_from(&task_cwd, &mut checked)? { - return Ok(Some(path)); - } - }; // From CWD walk up to root looking for deno.json or deno.jsonc Self::discover_from(cwd, &mut checked) } else { diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 825bf96d0..35da62fc8 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -19,6 +19,8 @@ use std::num::NonZeroUsize; use std::path::PathBuf; use std::str::FromStr; +use crate::util::fs::canonicalize_path; + use super::flags_allow_net; static LONG_VERSION: Lazy<String> = Lazy::new(|| { @@ -499,6 +501,16 @@ impl Flags { Some(vec![]) } } + Task(TaskFlags { + cwd: Some(path), .. + }) => { + // attempt to resolve the config file from the task subcommand's + // `--cwd` when specified + match canonicalize_path(&PathBuf::from(path)) { + Ok(path) => Some(vec![path]), + Err(_) => Some(vec![]), + } + } _ => Some(vec![]), } } @@ -533,7 +545,8 @@ impl Flags { .to_file_path() .ok() } - Task(TaskFlags { cwd: None, .. }) => std::env::current_dir().ok(), + Task(_) | Check(_) | Coverage(_) | Cache(_) | Info(_) | Eval(_) + | Test(_) | Bench(_) => std::env::current_dir().ok(), _ => None, } } diff --git a/cli/args/mod.rs b/cli/args/mod.rs index aa1781bd8..fe6883f2c 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -391,49 +391,13 @@ fn discover_package_json( flags: &Flags, maybe_stop_at: Option<PathBuf>, ) -> Result<Option<PackageJson>, AnyError> { - fn discover_from( - start: &Path, - maybe_stop_at: Option<PathBuf>, - ) -> Result<Option<PackageJson>, AnyError> { - const PACKAGE_JSON_NAME: &str = "package.json"; - - // note: ancestors() includes the `start` path - for ancestor in start.ancestors() { - let path = ancestor.join(PACKAGE_JSON_NAME); - - let source = match std::fs::read_to_string(&path) { - Ok(source) => source, - Err(err) if err.kind() == std::io::ErrorKind::NotFound => { - if let Some(stop_at) = maybe_stop_at.as_ref() { - if ancestor == stop_at { - break; - } - } - continue; - } - Err(err) => bail!( - "Error loading package.json at {}. {:#}", - path.display(), - err - ), - }; - - let package_json = PackageJson::load_from_string(path.clone(), source)?; - log::debug!("package.json file found at '{}'", path.display()); - return Ok(Some(package_json)); - } - // No config file found. - log::debug!("No package.json file found"); - Ok(None) - } - // TODO(bartlomieju): discover for all subcommands, but print warnings that // `package.json` is ignored in bundle/compile/etc. if let Some(package_json_dir) = flags.package_json_search_dir() { let package_json_dir = canonicalize_path_maybe_not_exists(&package_json_dir)?; - return discover_from(&package_json_dir, maybe_stop_at); + return package_json::discover_from(&package_json_dir, maybe_stop_at); } log::debug!("No package.json file found"); diff --git a/cli/args/package_json.rs b/cli/args/package_json.rs index 76d353c5e..73bcfb582 100644 --- a/cli/args/package_json.rs +++ b/cli/args/package_json.rs @@ -1,6 +1,8 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use std::collections::HashMap; +use std::path::Path; +use std::path::PathBuf; use deno_core::anyhow::anyhow; use deno_core::anyhow::bail; @@ -83,6 +85,44 @@ pub fn get_local_package_json_version_reqs( Ok(result) } +/// Attempts to discover the package.json file, maybe stopping when it +/// reaches the specified `maybe_stop_at` directory. +pub fn discover_from( + start: &Path, + maybe_stop_at: Option<PathBuf>, +) -> Result<Option<PackageJson>, AnyError> { + const PACKAGE_JSON_NAME: &str = "package.json"; + + // note: ancestors() includes the `start` path + for ancestor in start.ancestors() { + let path = ancestor.join(PACKAGE_JSON_NAME); + + let source = match std::fs::read_to_string(&path) { + Ok(source) => source, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + if let Some(stop_at) = maybe_stop_at.as_ref() { + if ancestor == stop_at { + break; + } + } + continue; + } + Err(err) => bail!( + "Error loading package.json at {}. {:#}", + path.display(), + err + ), + }; + + let package_json = PackageJson::load_from_string(path.clone(), source)?; + log::debug!("package.json file found at '{}'", path.display()); + return Ok(Some(package_json)); + } + + log::debug!("No package.json file found"); + Ok(None) +} + #[cfg(test)] mod test { use pretty_assertions::assert_eq; |