Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 .github/actions/prepare-node/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ inputs:
link:
description: 'The link to the local path'
required: false
switch:
description: 'Path to a locally built yarn-switch binary to use in place of the released one'
required: false

runs:
using: composite
steps:
- uses: yarnpkg/setup-action@main
if: inputs.switch == ''

- name: Install the provided Yarn Switch binary
if: inputs.switch != ''
shell: bash
run: |
set -euo pipefail
chmod +x "${{inputs.switch}}"
install_dir="${RUNNER_TEMP}/yarn-switch-bin"
mkdir -p "${install_dir}"
cp -f "${{inputs.switch}}" "${install_dir}/yarn"
echo "${install_dir}" >> "${GITHUB_PATH}"

- name: Add link to the local path
if: inputs.link != ''
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
uses: ./.github/actions/prepare-node
with:
link: artifacts/yarn-bin
switch: artifacts/yarn

- name: Generate the test report
run: |
Expand Down
2 changes: 2 additions & 0 deletions packages/zpm-switch/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ enum SwitchExecCli {
LinkMigrationCommand(switch::link_migration::LinkMigrationCommand),
LinkCommand(switch::link::LinkCommand),
PostinstallCommand(switch::postinstall::PostinstallCommand),
TrustCheckCommand(switch::trust::TrustCheckCommand),
TrustSetCommand(switch::trust::TrustSetCommand),
UnlinkCommand(switch::unlink::UnlinkCommand),
UpCommand(switch::up::UpCommand),
UpdateCommand(switch::update::UpdateCommand),
Expand Down
68 changes: 42 additions & 26 deletions packages/zpm-switch/src/commands/switch/cache_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clipanion::cli;
use zpm_utils::{tree, AbstractValue, IoResultExt, Path, TimeAgo};

use crate::{cache, errors::Error};
use crate::{cache, errors::Error, links::list_configs};

/// List all cached Yarn binaries
#[cli::command]
Expand All @@ -19,41 +19,57 @@ impl CacheListCommand {
let cache_dir
= cache::cache_dir()?;

let Some(cache_entries) = cache_dir.fs_read_dir().ok_missing()? else {
return Ok(());
};
if let Some(cache_entries) = cache_dir.fs_read_dir().ok_missing()? {
for entry in cache_entries {
let entry
= entry?;

for entry in cache_entries {
let entry
= entry?;
let entry_path
= Path::try_from(entry.path())?;
let entry_meta
= cache::cache_metadata(&entry_path);
let entry_age
= cache::cache_last_used(&entry_path);

let entry_path
= Path::try_from(entry.path())?;
let entry_meta
= cache::cache_metadata(&entry_path);
let entry_age
= cache::cache_last_used(&entry_path);
let Ok(entry_meta) = entry_meta else {
continue;
};

let Ok(entry_meta) = entry_meta else {
continue;
};
let Ok(entry_age) = entry_age else {
continue;
};

let Ok(entry_age) = entry_age else {
nodes.push(tree::Node {
label: None,
value: Some(AbstractValue::new(entry_meta.version)),
children: Some(tree::TreeNodeChildren::Map(tree::Map::from([
("path".to_string(), tree::Node {
label: Some("Path".to_string()),
value: Some(AbstractValue::new(entry_path)),
children: None,
}),
("age".to_string(), tree::Node {
label: Some("Age".to_string()),
value: Some(AbstractValue::new(TimeAgo::new(entry_age.elapsed().unwrap()))),
children: None,
}),
]))),
});
}
}

for config in list_configs()? {
let Some(trusted) = config.trusted else {
continue;
};

nodes.push(tree::Node {
label: None,
value: Some(AbstractValue::new(entry_meta.version)),
value: Some(AbstractValue::new(config.project_cwd)),
children: Some(tree::TreeNodeChildren::Map(tree::Map::from([
("path".to_string(), tree::Node {
label: Some("Path".to_string()),
value: Some(AbstractValue::new(entry_path)),
children: None,
}),
("age".to_string(), tree::Node {
label: Some("Age".to_string()),
value: Some(AbstractValue::new(TimeAgo::new(entry_age.elapsed().unwrap()))),
("trusted".to_string(), tree::Node {
label: Some("Trusted".to_string()),
value: Some(AbstractValue::new(trusted)),
children: None,
}),
]))),
Expand Down
1 change: 1 addition & 0 deletions packages/zpm-switch/src/commands/switch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod links_list;
pub mod link_migration;
pub mod link;
pub mod postinstall;
pub mod trust;
pub mod unlink;
pub mod up;
pub mod update;
Expand Down
80 changes: 80 additions & 0 deletions packages/zpm-switch/src/commands/switch/trust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::process::ExitCode;

use clipanion::cli;
use zpm_macro_enum::zpm_enum;
use zpm_utils::Path;

use crate::{errors::Error, links::{get_trusted, set_trusted}};

#[zpm_enum(or_else = |s| Err(Error::InvalidTrustLevel(s.to_string())))]
#[derive(Debug, Copy, Clone)]
enum TrustLevel {
#[literal("true")]
Trusted,

#[literal("false")]
Untrusted,

#[literal("null")]
Unknown,
}

impl TrustLevel {
fn as_option(self) -> Option<bool> {
match self {
Self::Trusted => Some(true),
Self::Untrusted => Some(false),
Self::Unknown => None,
}
}
}

/// Check whether a project has been trusted
#[cli::command]
#[cli::path("switch", "trust")]
#[cli::category("Project trust")]
#[derive(Debug)]
pub struct TrustCheckCommand {
#[cli::option("--check")]
_check: bool,

path: Path,
}

impl TrustCheckCommand {
pub async fn execute(&self) -> Result<ExitCode, Error> {
let path
= self.path.fs_canonicalize()
.unwrap_or_else(|_| self.path.clone());

match get_trusted(&path)? {
Some(true) => Ok(ExitCode::SUCCESS),
Some(false) => Ok(ExitCode::from(2)),
None => Ok(ExitCode::from(3)),
}
}
}

/// Set whether a project is trusted
#[cli::command]
#[cli::path("switch", "trust")]
#[cli::category("Project trust")]
#[derive(Debug)]
pub struct TrustSetCommand {
#[cli::option("--set")]
trusted: TrustLevel,

path: Path,
}

impl TrustSetCommand {
pub async fn execute(&self) -> Result<(), Error> {
let path
= self.path.fs_canonicalize()
.unwrap_or_else(|_| self.path.clone());

set_trusted(&path, self.trusted.as_option())?;

Ok(())
}
}
3 changes: 3 additions & 0 deletions packages/zpm-switch/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub enum Error {
#[error("Invalid version selector: {0}")]
InvalidVersionSelector(String),

#[error("Invalid trust level: {0}; expected true, false, or null")]
InvalidTrustLevel(String),

#[error("Failed to parse manifest: {0}")]
FailedToParseManifest(zpm_parsers::Error),

Expand Down
Loading
Loading