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 /ext/node/package_json.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 'ext/node/package_json.rs')
-rw-r--r-- | ext/node/package_json.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/ext/node/package_json.rs b/ext/node/package_json.rs index e40448930..b05362890 100644 --- a/ext/node/package_json.rs +++ b/ext/node/package_json.rs @@ -33,6 +33,8 @@ pub struct PackageJson { pub path: PathBuf, pub typ: String, pub types: Option<String>, + pub dependencies: Option<HashMap<String, String>>, + pub dev_dependencies: Option<HashMap<String, String>>, } impl PackageJson { @@ -49,6 +51,8 @@ impl PackageJson { path, typ: "none".to_string(), types: None, + dependencies: None, + dev_dependencies: None, } } @@ -86,6 +90,13 @@ impl PackageJson { return Ok(PackageJson::empty(path)); } + Self::load_from_string(path, source) + } + + pub fn load_from_string( + path: PathBuf, + source: String, + ) -> Result<PackageJson, AnyError> { let package_json: Value = serde_json::from_str(&source) .map_err(|err| anyhow::anyhow!("malformed package.json {}", err))?; @@ -114,6 +125,25 @@ impl PackageJson { let version = version_val.and_then(|s| s.as_str()).map(|s| s.to_string()); let module = module_val.and_then(|s| s.as_str()).map(|s| s.to_string()); + let dependencies = package_json.get("dependencies").and_then(|d| { + if d.is_object() { + let deps: HashMap<String, String> = + serde_json::from_value(d.to_owned()).unwrap(); + Some(deps) + } else { + None + } + }); + let dev_dependencies = package_json.get("devDependencies").and_then(|d| { + if d.is_object() { + let deps: HashMap<String, String> = + serde_json::from_value(d.to_owned()).unwrap(); + Some(deps) + } else { + None + } + }); + // Ignore unknown types for forwards compatibility let typ = if let Some(t) = type_val { if let Some(t) = t.as_str() { @@ -147,6 +177,8 @@ impl PackageJson { exports, imports, bin, + dependencies, + dev_dependencies, }; CACHE.with(|cache| { |