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
Show file tree
Hide file tree
Changes from 2 commits
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
15 changes: 15 additions & 0 deletions crates/volta-core/src/error/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 `{}`
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions crates/volta-core/src/hook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -182,6 +188,7 @@ impl HookConfig {
Self {
node: None,
npm: None,
pnpm: None,
yarn: None,
events: None,
}
Expand Down Expand Up @@ -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),
}
Expand Down
4 changes: 4 additions & 0 deletions crates/volta-core/src/hook/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -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>,
}
Expand Down Expand Up @@ -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,
})
Expand Down
10 changes: 10 additions & 0 deletions crates/volta-core/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions crates/volta-core/src/platform/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
}
Expand All @@ -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));
Expand Down
15 changes: 14 additions & 1 deletion crates/volta-core/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
}

Expand All @@ -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),
}
}
Expand All @@ -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),
}
}
Expand All @@ -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),
}
}
Expand All @@ -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>,
}

Expand All @@ -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),
}
}
Expand All @@ -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(),
}),
}
Expand All @@ -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>>,
}

Expand Down Expand Up @@ -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,
})
}
Expand Down
Loading