diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-02-20 19:14:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-20 19:14:06 +0100 |
| commit | 4d1a14ca7fa9496f36470a7771448a9b006b0204 (patch) | |
| tree | de226f6368faec98065bf14382f23541484cfe72 /cli/args/flags.rs | |
| parent | 88f6fc6a1684326ae1f947ea8ec24ad0bff0f449 (diff) | |
feat: auto-discover package.json for npm dependencies (#17272)
This commits adds auto-discovery of "package.json" file when running
"deno run" and "deno task" subcommands. In case of "deno run" the
"package.json" is being looked up starting from the directory of the
script that is being run, stopping early if "deno.json(c)" file is found
(ie. FS tree won't be traversed "up" from "deno.json").
When "package.json" is discovered the "--node-modules-dir" flag is
implied, leading to creation of local "node_modules/" directory - we
did that, because most tools relying on "package.json" will expect
"node_modules/" directory to be present (eg. Vite). Additionally
"dependencies" and "devDependencies" specified in the "package.json"
are downloaded on startup.
This is a stepping stone to supporting bare specifier imports, but
the actual integration will be done in a follow up commit.
---------
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/args/flags.rs')
| -rw-r--r-- | cli/args/flags.rs | 59 |
1 files changed, 53 insertions, 6 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 5d1affb09..2c9f4c09d 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -327,7 +327,7 @@ pub struct Flags { pub cached_only: bool, pub type_check_mode: TypeCheckMode, pub config_flag: ConfigFlag, - pub node_modules_dir: bool, + pub node_modules_dir: Option<bool>, pub coverage_dir: Option<String>, pub enable_testing_features: bool, pub ignore: Vec<PathBuf>, @@ -503,6 +503,33 @@ impl Flags { } } + /// Extract path argument for `package.json` search paths. + /// If it returns Some(path), the `package.json` should be discovered + /// from the `path` dir. + /// If it returns None, the `package.json` file shouldn't be discovered at + /// all. + pub fn package_json_arg(&self) -> Option<PathBuf> { + use DenoSubcommand::*; + + if let Run(RunFlags { script }) = &self.subcommand { + if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) { + if module_specifier.scheme() == "file" { + let p = module_specifier + .to_file_path() + .unwrap() + .parent()? + .to_owned(); + return Some(p); + } else if module_specifier.scheme() == "npm" { + let p = std::env::current_dir().unwrap(); + return Some(p); + } + } + } + + None + } + pub fn has_permission(&self) -> bool { self.allow_all || self.allow_hrtime @@ -2309,7 +2336,12 @@ fn no_npm_arg<'a>() -> Arg<'a> { fn local_npm_arg<'a>() -> Arg<'a> { Arg::new("node-modules-dir") .long("node-modules-dir") - .help("Creates a local node_modules folder") + .min_values(0) + .max_values(1) + .takes_value(true) + .require_equals(true) + .possible_values(["true", "false"]) + .help("Creates a local node_modules folder. This option is implicitly true when a package.json is auto-discovered.") } fn unsafely_ignore_certificate_errors_arg<'a>() -> Arg<'a> { @@ -3247,9 +3279,7 @@ fn no_npm_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } fn local_npm_args_parse(flags: &mut Flags, matches: &ArgMatches) { - if matches.is_present("node-modules-dir") { - flags.node_modules_dir = true; - } + flags.node_modules_dir = optional_bool_parse(matches, "node-modules-dir"); } fn inspect_arg_validate(val: &str) -> Result<(), String> { @@ -5448,7 +5478,24 @@ mod tests { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), - node_modules_dir: true, + node_modules_dir: Some(true), + ..Flags::default() + } + ); + + let r = flags_from_vec(svec![ + "deno", + "run", + "--node-modules-dir=false", + "script.ts" + ]); + assert_eq!( + r.unwrap(), + Flags { + subcommand: DenoSubcommand::Run(RunFlags { + script: "script.ts".to_string(), + }), + node_modules_dir: Some(false), ..Flags::default() } ); |
