-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: Error when installing a global with npm/pnpm/yarn directly. (#110)
- Loading branch information
Showing
5 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod go; | ||
pub mod node; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::tools::{create_tool, ToolType}; | ||
use proto_core::{color, ProtoError}; | ||
|
||
pub async fn pre_run(tool_type: ToolType, args: &[String]) -> Result<(), ProtoError> { | ||
let mut is_install_command = false; | ||
let mut is_global = false; | ||
|
||
match tool_type { | ||
// npm install -g <dep> | ||
// pnpm add -g <dep> | ||
ToolType::Npm | ToolType::Pnpm => { | ||
is_install_command = args[0] == "install" || args[0] == "i" || args[0] == "add"; | ||
|
||
for arg in args { | ||
if arg == "--global" || arg == "-g" || arg == "--location=global" { | ||
is_global = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// yarn global add <dep> | ||
ToolType::Yarn => { | ||
is_global = args[0] == "global"; | ||
is_install_command = args[1] == "add"; | ||
} | ||
_ => {} | ||
}; | ||
|
||
if is_install_command && is_global { | ||
let tool = create_tool(&tool_type).await?; | ||
|
||
return Err(ProtoError::Message(format!( | ||
"Global binaries must be installed with {} and {} should be added to your {}!\nLearn more: {}", | ||
color::shell(format!("proto install-global {}", tool.get_id())), | ||
color::path(tool.get_globals_bin_dir()?), | ||
color::shell("PATH"), | ||
color::url("https://moonrepo.dev/docs/proto/faq#how-can-i-install-a-global-binary-for-a-language") | ||
)))?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
mod utils; | ||
|
||
use starbase_sandbox::predicates::prelude::*; | ||
use utils::*; | ||
|
||
mod npm { | ||
use super::*; | ||
|
||
#[test] | ||
fn errors_if_installing_global() { | ||
let temp = create_empty_sandbox(); | ||
|
||
let mut cmd = create_proto_command(temp.path()); | ||
cmd.arg("install") | ||
.arg("npm") | ||
.arg("latest") | ||
.assert() | ||
.success(); | ||
|
||
let mut cmd = create_proto_command(temp.path()); | ||
let assert = cmd | ||
.arg("run") | ||
.arg("npm") | ||
.arg("latest") | ||
.args(["--", "install", "-g", "typescript"]) | ||
.assert(); | ||
|
||
assert.stderr(predicate::str::contains( | ||
"Global binaries must be installed with proto install-global npm", | ||
)); | ||
} | ||
} | ||
|
||
mod pnpm { | ||
use super::*; | ||
|
||
#[test] | ||
fn errors_if_installing_global() { | ||
let temp = create_empty_sandbox(); | ||
|
||
let mut cmd = create_proto_command(temp.path()); | ||
cmd.arg("install").arg("pnpm").assert().success(); | ||
|
||
let mut cmd = create_proto_command(temp.path()); | ||
let assert = cmd | ||
.arg("run") | ||
.arg("pnpm") | ||
.args(["--", "add", "-g", "typescript"]) | ||
.assert(); | ||
|
||
assert.stderr(predicate::str::contains( | ||
"Global binaries must be installed with proto install-global pnpm", | ||
)); | ||
} | ||
} | ||
|
||
mod yarn { | ||
use super::*; | ||
|
||
#[test] | ||
fn errors_if_installing_global() { | ||
let temp = create_empty_sandbox(); | ||
|
||
let mut cmd = create_proto_command(temp.path()); | ||
cmd.arg("install").arg("yarn").assert().success(); | ||
|
||
let mut cmd = create_proto_command(temp.path()); | ||
let assert = cmd | ||
.arg("run") | ||
.arg("yarn") | ||
.args(["--", "global", "add", "typescript"]) | ||
.assert(); | ||
|
||
assert.stderr(predicate::str::contains( | ||
"Global binaries must be installed with proto install-global yarn", | ||
)); | ||
} | ||
} |