Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
new: Support proto v0.22 release. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Nov 4, 2023
1 parent e460f8f commit f5140a3
Show file tree
Hide file tree
Showing 21 changed files with 540 additions and 526 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
- uses: moonrepo/setup-rust@v1
with:
bins: cargo-wasi, cargo-nextest
cache: false
- uses: moonrepo/setup-toolchain@v0
- run: cargo wasi build -p node_plugin
- run: cargo wasi build -p node_depman_plugin
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## 0.5.0

#### 💥 Breaking

- Updated the `npm` tool to create the `npx` shim instead of the `node` tool.
- Updated executable detection for package managers to use the shell scripts instead of the source `.js` files (when applicable).
- Previously we would execute the JS file with node: `node ./bin/npm-cli.js`
- Now we execute the shell script: `./bin/npm` (unix), `./bin/npm.cmd` (windows)

#### 🚀 Updates

- Updated to support proto v0.22 release.

#### ⚙️ Internal

- Updated dependencies.

## 0.4.3

#### 🐞 Fixes
Expand Down
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ members = ["crates/*"]

[workspace.dependencies]
extism-pdk = "0.3.4"
proto_pdk = { version = "0.9.0" } # , path = "../../proto/crates/pdk" }
proto_pdk_api = { version = "0.9.0" } # , path = "../../proto/crates/pdk-api" }
proto_pdk_test_utils = { version = "0.9.1" } # , path = "../../proto/crates/pdk-test-utils" }
proto_pdk = { version = "0.10.0" } # , path = "../../proto/crates/pdk" }
proto_pdk_api = { version = "0.10.0" } # , path = "../../proto/crates/pdk-api" }
proto_pdk_test_utils = { version = "0.10.0" } # , path = "../../proto/crates/pdk-test-utils" }
regex = { version = "1.10.2", default-features = false, features = [
"std",
"unicode",
Expand Down
2 changes: 1 addition & 1 deletion crates/node-depman/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_depman_plugin"
version = "0.4.3"
version = "0.5.0"
edition = "2021"
license = "MIT"
publish = false
Expand Down
6 changes: 6 additions & 0 deletions crates/node-depman/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// WASM cannot be executed through the test runner and we need to avoid building
// WASM code for non-WASM targets. We can solve both of these with a cfg flag.

#[cfg(not(test))]
mod npm_registry;

#[cfg(not(test))]
mod package_manager;

#[cfg(not(test))]
mod proto;

Expand Down
32 changes: 32 additions & 0 deletions crates/node-depman/src/npm_registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![allow(dead_code)]

use extism_pdk::{json, Error, HttpResponse};
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Deserialize)]
pub struct RegistryVersion {
pub version: String, // No v prefix
}

#[derive(Deserialize)]
pub struct RegistryResponse {
#[serde(rename = "dist-tags")]
pub dist_tags: HashMap<String, String>,
pub versions: HashMap<String, RegistryVersion>,
}

pub fn parse_registry_response(
res: HttpResponse,
is_yarn: bool,
) -> Result<RegistryResponse, Error> {
if !is_yarn {
return res.json();
}

// https://github.com/moonrepo/proto/issues/257
let pattern = regex::bytes::Regex::new("[\u{0000}-\u{001F}]+").unwrap();
let body = res.body();

Ok(json::from_slice(&pattern.replace_all(&body, b""))?)
}
63 changes: 63 additions & 0 deletions crates/node-depman/src/package_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#![allow(dead_code)]

use proto_pdk::{get_tool_id, UnresolvedVersionSpec};
use std::fmt;

#[derive(PartialEq)]
pub enum PackageManager {
Npm,
Pnpm,
Yarn,
}

impl PackageManager {
pub fn detect() -> PackageManager {
let id = get_tool_id();

if id.to_lowercase().contains("yarn") {
PackageManager::Yarn
} else if id.to_lowercase().contains("pnpm") {
PackageManager::Pnpm
} else {
PackageManager::Npm
}
}

pub fn get_package_name(&self, version: impl AsRef<UnresolvedVersionSpec>) -> String {
if self.is_yarn_berry(version.as_ref()) {
"@yarnpkg/cli-dist".into()
} else {
self.to_string()
}
}

pub fn is_yarn_classic(&self, version: impl AsRef<UnresolvedVersionSpec>) -> bool {
matches!(self, PackageManager::Yarn)
&& match version.as_ref() {
UnresolvedVersionSpec::Alias(alias) => alias == "legacy" || alias == "classic",
UnresolvedVersionSpec::Version(ver) => ver.major == 1,
UnresolvedVersionSpec::Req(req) => req.comparators.iter().any(|c| c.major == 1),
_ => false,
}
}

pub fn is_yarn_berry(&self, version: impl AsRef<UnresolvedVersionSpec>) -> bool {
matches!(self, PackageManager::Yarn)
&& match version.as_ref() {
UnresolvedVersionSpec::Alias(alias) => alias == "berry" || alias == "latest",
UnresolvedVersionSpec::Version(ver) => ver.major > 1,
UnresolvedVersionSpec::Req(req) => req.comparators.iter().any(|c| c.major > 1),
_ => false,
}
}
}

impl fmt::Display for PackageManager {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PackageManager::Npm => write!(f, "npm"),
PackageManager::Pnpm => write!(f, "pnpm"),
PackageManager::Yarn => write!(f, "yarn"),
}
}
}
Loading

0 comments on commit f5140a3

Please sign in to comment.