From 7a83f38ca83e83d68a56b9ae572711ffaf84dff5 Mon Sep 17 00:00:00 2001 From: pan93412 Date: Thu, 3 Nov 2022 20:20:16 +0800 Subject: [PATCH] fix(steps/node): Only run global upgrade on Yarn 1.x (#112) --- src/steps/node.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/steps/node.rs b/src/steps/node.rs index ea1fbe4c..e6639223 100644 --- a/src/steps/node.rs +++ b/src/steps/node.rs @@ -93,6 +93,17 @@ impl Yarn { } } + fn has_global_subcmd(&self) -> bool { + // Get the version of Yarn. After Yarn 2.x (berry), + // “yarn global” has been replaced with “yarn dlx”. + // + // As “yarn dlx” don't need to “upgrade”, we + // ignore the whole task if Yarn is 2.x or above. + let version = Command::new(&self.command).args(["--version"]).check_output(); + + matches!(version, Ok(ver) if ver.starts_with('1') || ver.starts_with('0')) + } + #[cfg(target_os = "linux")] fn root(&self) -> Result { let args = ["global", "dir"]; @@ -178,6 +189,11 @@ pub fn run_npm_upgrade(ctx: &ExecutionContext) -> Result<()> { pub fn run_yarn_upgrade(ctx: &ExecutionContext) -> Result<()> { let yarn = require("yarn").map(Yarn::new)?; + if !yarn.has_global_subcmd() { + debug!("Yarn is 2.x or above, skipping global upgrade"); + return Ok(()); + } + #[cfg(target_os = "linux")] { yarn.upgrade(ctx.run_type(), should_use_sudo_yarn(&yarn, ctx)?)