Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Initial pnpm support #1273

Merged
merged 21 commits into from
Nov 3, 2022
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Initial pnpm support
Signed-off-by: Chawye Hsu <chawyehsu@hotmail.com>
  • Loading branch information
chawyehsu committed Jul 30, 2022
commit 402e177bb9fd1740debcd291d3f4322ae2435c08
15 changes: 15 additions & 0 deletions crates/volta-core/src/error/kind.rs
Original file line number Diff line number Diff line change
@@ -330,6 +330,11 @@ pub enum ErrorKind {
tool: String,
},

/// Thrown when there is no Pnpm version matching a requested semver specifier.
PnpmVersionNotFound {
matching: String,
},

/// Thrown when executing a project-local binary fails
ProjectLocalBinaryExecError {
command: String,
@@ -1096,6 +1101,13 @@ Please supply a spec in the format `<tool name>[@<version>]`.",
{}",
tool, PERMISSIONS_CTA
),
ErrorKind::PnpmVersionNotFound { matching } => write!(
f,
r#"Could not find Pnpm version matching "{}" in the version registry.

Please verify that the version is correct."#,
matching
),
ErrorKind::ProjectLocalBinaryExecError { command } => write!(
f,
"Could not execute `{}`
@@ -1299,12 +1311,14 @@ Please ensure it is installed with `{} {0}`"#,
package,
match manager {
PackageManager::Npm => "npm i -g",
PackageManager::Pnpm => "pnpm add -g",
PackageManager::Yarn => "yarn global add",
}
),
ErrorKind::UpgradePackageWrongManager { package, manager } => {
let (name, command) = match manager {
PackageManager::Npm => ("npm", "npm update -g"),
PackageManager::Pnpm => ("pnpm", "pnpm update -g"),
PackageManager::Yarn => ("Yarn", "yarn global upgrade"),
};
write!(
@@ -1490,6 +1504,7 @@ impl ErrorKind {
ErrorKind::ParsePackageConfigError => ExitCode::UnknownError,
ErrorKind::ParsePlatformError => ExitCode::ConfigurationError,
ErrorKind::PersistInventoryError { .. } => ExitCode::FileSystemError,
ErrorKind::PnpmVersionNotFound { .. } => ExitCode::NoVersionMatch,
ErrorKind::ProjectLocalBinaryExecError { .. } => ExitCode::ExecutionFailure,
ErrorKind::ProjectLocalBinaryNotFound { .. } => ExitCode::FileSystemError,
ErrorKind::PublishHookBothUrlAndBin => ExitCode::ConfigurationError,
8 changes: 8 additions & 0 deletions crates/volta-core/src/hook/mod.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ use std::path::Path;
use crate::error::{Context, ErrorKind, Fallible};
use crate::layout::volta_home;
use crate::project::Project;
use crate::tool::pnpm::Pnpm;
use crate::tool::{Node, Npm, Tool};
use lazycell::LazyCell;
use log::debug;
@@ -50,6 +51,7 @@ impl LazyHookConfig {
pub struct HookConfig {
node: Option<ToolHooks<Node>>,
npm: Option<ToolHooks<Npm>>,
pnpm: Option<ToolHooks<Pnpm>>,
yarn: Option<YarnHooks>,
events: Option<EventHooks>,
}
@@ -118,6 +120,10 @@ impl HookConfig {
self.npm.as_ref()
}

pub fn pnpm(&self) -> Option<&ToolHooks<Pnpm>> {
self.pnpm.as_ref()
}

pub fn yarn(&self) -> Option<&YarnHooks> {
self.yarn.as_ref()
}
@@ -182,6 +188,7 @@ impl HookConfig {
Self {
node: None,
npm: None,
pnpm: None,
yarn: None,
events: None,
}
@@ -214,6 +221,7 @@ impl HookConfig {
Self {
node: merge_hooks!(self, other, node),
npm: merge_hooks!(self, other, npm),
pnpm: merge_hooks!(self, other, pnpm),
yarn: merge_hooks!(self, other, yarn),
events: merge_hooks!(self, other, events),
}
4 changes: 4 additions & 0 deletions crates/volta-core/src/hook/serial.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ use std::path::Path;
use super::tool;
use super::RegistryFormat;
use crate::error::{ErrorKind, Fallible, VoltaError};
use crate::tool::pnpm::Pnpm;
use crate::tool::{Node, Npm, Tool};
chawyehsu marked this conversation as resolved.
Show resolved Hide resolved
use serde::{Deserialize, Serialize};

@@ -129,6 +130,7 @@ impl TryFrom<RawPublishHook> for super::Publish {
pub struct RawHookConfig {
pub node: Option<RawToolHooks<Node>>,
pub npm: Option<RawToolHooks<Npm>>,
pub pnpm: Option<RawToolHooks<Pnpm>>,
pub yarn: Option<RawYarnHooks>,
pub events: Option<RawEventHooks>,
}
@@ -172,11 +174,13 @@ impl RawHookConfig {
pub fn into_hook_config(self, base_dir: &Path) -> Fallible<super::HookConfig> {
let node = self.node.map(|n| n.into_tool_hooks(base_dir)).transpose()?;
let npm = self.npm.map(|n| n.into_tool_hooks(base_dir)).transpose()?;
let pnpm = self.pnpm.map(|p| p.into_tool_hooks(base_dir)).transpose()?;
let yarn = self.yarn.map(|y| y.into_yarn_hooks(base_dir)).transpose()?;
let events = self.events.map(|e| e.try_into()).transpose()?;
Ok(super::HookConfig {
node,
npm,
pnpm,
yarn,
events,
})
10 changes: 10 additions & 0 deletions crates/volta-core/src/inventory.rs
Original file line number Diff line number Diff line change
@@ -38,6 +38,16 @@ pub fn npm_versions() -> Fallible<BTreeSet<Version>> {
volta_home().and_then(|home| read_versions(home.npm_image_root_dir()))
}

/// Checks if a given Pnpm version image is available on the local machine
pub fn pnpm_available(version: &Version) -> Fallible<bool> {
volta_home().map(|home| home.pnpm_image_dir(&version.to_string()).exists())
}

/// Collects a set of all Pnpm versions fetched on the local machine
pub fn pnpm_versions() -> Fallible<BTreeSet<Version>> {
volta_home().and_then(|home| read_versions(home.pnpm_image_root_dir()))
}

/// Checks if a given Yarn version image is available on the local machine
pub fn yarn_available(version: &Version) -> Fallible<bool> {
volta_home().map(|home| home.yarn_image_dir(&version.to_string()).exists())
7 changes: 7 additions & 0 deletions crates/volta-core/src/platform/image.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ pub struct Image {
pub node: Sourced<Version>,
/// The custom version of npm, if any. `None` represents using the npm that is bundled with Node
pub npm: Option<Sourced<Version>>,
/// The pinned version of Pnpm, if any.
pub pnpm: Option<Sourced<Version>>,
/// The pinned version of Yarn, if any.
pub yarn: Option<Sourced<Version>>,
}
@@ -27,6 +29,11 @@ impl Image {
bins.push(home.npm_image_bin_dir(&npm_str));
}

if let Some(pnpm) = &self.pnpm {
let pnpm_str = pnpm.value.to_string();
bins.push(home.pnpm_image_bin_dir(&pnpm_str));
}

if let Some(yarn) = &self.yarn {
let yarn_str = yarn.value.to_string();
bins.push(home.yarn_image_bin_dir(&yarn_str));
15 changes: 14 additions & 1 deletion crates/volta-core/src/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use std::fmt;

use crate::error::{ErrorKind, Fallible};
use crate::session::Session;
use crate::tool::{Node, Npm, Yarn};
use crate::tool::{Node, Npm, Pnpm, Yarn};
use semver::Version;

mod image;
@@ -161,6 +161,7 @@ impl<T> Default for InheritOption<T> {
pub struct PlatformSpec {
pub node: Version,
pub npm: Option<Version>,
pub pnpm: Option<Version>,
pub yarn: Option<Version>,
}

@@ -170,6 +171,7 @@ impl PlatformSpec {
Platform {
node: Sourced::with_default(self.node.clone()),
npm: self.npm.clone().map(Sourced::with_default),
pnpm: self.pnpm.clone().map(Sourced::with_default),
yarn: self.yarn.clone().map(Sourced::with_default),
}
}
@@ -179,6 +181,7 @@ impl PlatformSpec {
Platform {
node: Sourced::with_project(self.node.clone()),
npm: self.npm.clone().map(Sourced::with_project),
pnpm: self.pnpm.clone().map(Sourced::with_project),
yarn: self.yarn.clone().map(Sourced::with_project),
}
}
@@ -188,6 +191,7 @@ impl PlatformSpec {
Platform {
node: Sourced::with_binary(self.node.clone()),
npm: self.npm.clone().map(Sourced::with_binary),
pnpm: self.pnpm.clone().map(Sourced::with_binary),
yarn: self.yarn.clone().map(Sourced::with_binary),
}
}
@@ -198,6 +202,7 @@ impl PlatformSpec {
pub struct CliPlatform {
pub node: Option<Version>,
pub npm: InheritOption<Version>,
pub pnpm: InheritOption<Version>,
pub yarn: InheritOption<Version>,
}

@@ -207,6 +212,7 @@ impl CliPlatform {
Platform {
node: self.node.map_or(base.node, Sourced::with_command_line),
npm: self.npm.map(Sourced::with_command_line).inherit(base.npm),
pnpm: self.pnpm.map(Sourced::with_command_line).inherit(base.pnpm),
yarn: self.yarn.map(Sourced::with_command_line).inherit(base.yarn),
}
}
@@ -220,6 +226,7 @@ impl From<CliPlatform> for Option<Platform> {
Some(node) => Some(Platform {
node: Sourced::with_command_line(node),
npm: base.npm.map(Sourced::with_command_line).into(),
pnpm: base.pnpm.map(Sourced::with_command_line).into(),
yarn: base.yarn.map(Sourced::with_command_line).into(),
}),
}
@@ -231,6 +238,7 @@ impl From<CliPlatform> for Option<Platform> {
pub struct Platform {
pub node: Sourced<Version>,
pub npm: Option<Sourced<Version>>,
pub pnpm: Option<Sourced<Version>>,
pub yarn: Option<Sourced<Version>>,
}

@@ -268,13 +276,18 @@ impl Platform {
Npm::new(version.clone()).ensure_fetched(session)?;
}

if let Some(Sourced { value: version, .. }) = &self.pnpm {
Pnpm::new(version.clone()).ensure_fetched(session)?;
}

if let Some(Sourced { value: version, .. }) = &self.yarn {
Yarn::new(version.clone()).ensure_fetched(session)?;
}

Ok(Image {
node: self.node,
npm: self.npm,
pnpm: self.pnpm,
yarn: self.yarn,
})
}
Loading