-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deno specifier and workspace support (#289)
- Loading branch information
Showing
21 changed files
with
2,523 additions
and
173 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use std::collections::BTreeMap; | ||
use std::collections::HashMap; | ||
|
||
use deno_semver::package::PackageNv; | ||
use deno_semver::package::PackageReq; | ||
use deno_semver::Version; | ||
use deno_semver::VersionReq; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
use crate::ModuleInfo; | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct DenoPackageInfo { | ||
pub versions: HashMap<Version, DenoPackageInfoVersion>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Default)] | ||
pub struct DenoPackageInfoVersion { | ||
// currently not supported because it doesn't work in workspaces | ||
// pub main: Option<String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Default)] | ||
pub struct DenoPackageVersionInfo { | ||
#[serde(rename = "moduleGraph1")] | ||
pub module_graph: Option<serde_json::Value>, | ||
} | ||
|
||
impl DenoPackageVersionInfo { | ||
pub fn module_info(&self, specifier: &str) -> Option<ModuleInfo> { | ||
let module_graph = self.module_graph.as_ref()?.as_object()?; | ||
let module_info = module_graph.get(specifier)?; | ||
serde_json::from_value(module_info.clone()).ok() | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Default, Serialize)] | ||
pub struct DenoSpecifierSnapshot { | ||
#[serde(flatten)] | ||
package_reqs: BTreeMap<PackageReq, PackageNv>, | ||
#[serde(skip_serializing)] | ||
packages_by_name: HashMap<String, Vec<PackageNv>>, | ||
} | ||
|
||
impl DenoSpecifierSnapshot { | ||
pub fn is_empty(&self) -> bool { | ||
self.package_reqs.is_empty() | ||
} | ||
|
||
pub fn add(&mut self, package_req: PackageReq, nv: PackageNv) { | ||
let nvs = self | ||
.packages_by_name | ||
.entry(package_req.name.clone()) | ||
.or_default(); | ||
if !nvs.contains(&nv) { | ||
nvs.push(nv.clone()); | ||
} | ||
self.package_reqs.insert(package_req, nv); | ||
} | ||
|
||
pub fn versions_by_name(&self, name: &str) -> Option<&Vec<PackageNv>> { | ||
self.packages_by_name.get(name) | ||
} | ||
|
||
pub fn mappings(&self) -> impl Iterator<Item = (&PackageReq, &PackageNv)> { | ||
self.package_reqs.iter() | ||
} | ||
} | ||
|
||
pub fn resolve_version<'a>( | ||
version_req: &VersionReq, | ||
versions: impl Iterator<Item = &'a Version>, | ||
) -> Option<&'a Version> { | ||
let mut maybe_best_version: Option<&Version> = None; | ||
for version in versions { | ||
if version_req.matches(version) { | ||
let is_best_version = maybe_best_version | ||
.as_ref() | ||
.map(|best_version| (*best_version).cmp(version).is_lt()) | ||
.unwrap_or(true); | ||
if is_best_version { | ||
maybe_best_version = Some(version); | ||
} | ||
} | ||
} | ||
maybe_best_version | ||
} |
Oops, something went wrong.