Skip to content

Commit

Permalink
Add medic-check-node corepack-shim-installed
Browse files Browse the repository at this point in the history
  • Loading branch information
sax committed Jun 30, 2023
1 parent a853b4f commit f65a254
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions medic-check-node/src/cli/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct CliArgs {

#[derive(Debug, Subcommand)]
pub enum Command {
/// Checks whether a corepack shim (such as pnpm) has been installed.
CorepackShimInstalled(CorepackShimArgs),
/// Checks that NPM exists in the PATH.
NpmExists,
/// Checks that all NPM dependencies are installed.
Expand All @@ -33,6 +35,19 @@ pub struct PackageArgs {
pub prefix: Option<String>,
}

#[derive(Args, Debug)]
pub struct CorepackShimArgs {
/// Name of a shim
#[clap(value_parser)]
#[arg(short, long, value_hint = clap::ValueHint::CommandString)]
pub name: String,

/// Version of the shim
#[clap(value_parser)]
#[arg(short, long, value_hint = clap::ValueHint::CommandString)]
pub version: String,
}

impl Default for CliArgs {
fn default() -> Self {
Self::new()
Expand Down
49 changes: 49 additions & 0 deletions medic-check-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,52 @@ pub fn packages_installed(cd: Option<String>, prefix: Option<String>) -> CheckRe
),
}
}

pub fn corepack_shim_installed(name: String, version: String) -> CheckResult {
enable_corepack()?;

let shim = format!("{name}@{version}");

let mut corepack_remedy = Command::new("corepack");
corepack_remedy.args(["prepare", &shim, "--activate"]);
let mut npm_remedy = Command::new("npm");
npm_remedy.args(["install", "-g", &shim]);

let remedy = format!("{corepack_remedy:?} && {npm_remedy:?}");

match Command::new("npm").args(["ls", "-g", &name]).output() {
Ok(output) => {
let stdout = std_to_string(output.stdout);
let stderr = std_to_string(output.stderr);

if output.status.success() && stdout.contains(&shim) {
CheckOk
} else {
CheckError(
"Node corepack shim out of date.".into(),
Some(stdout),
Some(stderr),
Some(remedy),
)
}
}
Err(_err) => CheckError(
"Node corepack shim not installed.".into(),
None,
None,
Some(remedy),
),
}
}

fn enable_corepack() -> CheckResult {
match Command::new("corepack").arg("enable").output() {
Ok(_) => CheckOk,
Err(error) => CheckError(
"Unable to enable corepack".into(),
None,
Some(format!("{error}")),
None,
),
}
}
3 changes: 3 additions & 0 deletions medic-check-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ fn main() -> CheckResult {
let cli = CliArgs::new();

match cli.command {
Command::CorepackShimInstalled(args) => {
medic_check_node::corepack_shim_installed(args.name, args.version)?
}
Command::NpmExists => medic_check_node::npm_exists()?,
Command::PackagesInstalled(args) => {
medic_check_node::packages_installed(args.cd, args.prefix)?
Expand Down

0 comments on commit f65a254

Please sign in to comment.