Skip to content

Commit

Permalink
feat: add pacquet -C <path> for setting dir
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jul 31, 2023
1 parent 26e8c03 commit 8a07cbf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
9 changes: 9 additions & 0 deletions crates/cli/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Options

[pnpm documentation](https://pnpm.io/pnpm-cli#options)

| Done | Command | Notes |
|------|-------------------------|-------|
|| -C <path>, --dir <path> | |
| | -w, --workspace-root | |

# Manage dependencies

## `pacquet add <pkg>`
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use pacquet_package_json::DependencyGroup;

Expand All @@ -10,6 +12,10 @@ use pacquet_package_json::DependencyGroup;
pub struct Cli {
#[command(subcommand)]
pub subcommand: Subcommands,

/// Run as if pacquet was started in <path> instead of the current working directory.
#[arg(short = 'C', long = "dir")]
pub current_dir: Option<PathBuf>,
}

#[derive(Subcommand, Debug)]
Expand Down
19 changes: 6 additions & 13 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub async fn run_cli() -> Result<()> {
}

async fn run_commands(cli: Cli) -> Result<()> {
let current_directory = env::current_dir().expect("Getting current directory failed");
let current_directory =
cli.current_dir.unwrap_or(env::current_dir().expect("getting current directory failed"));
let package_json_path = current_directory.join("package.json");

match &cli.subcommand {
Expand Down Expand Up @@ -134,34 +135,26 @@ mod tests {
#[tokio::test]
async fn init_command_should_create_package_json() {
let parent_folder = tempdir().unwrap();
let current_directory = env::current_dir().unwrap();
env::set_current_dir(parent_folder.path()).unwrap();
let cli = Cli::parse_from(["", "init"]);
let cli = Cli::parse_from(["", "-C", parent_folder.path().to_str().unwrap(), "init"]);
run_commands(cli).await.unwrap();
assert!(parent_folder.path().join("package.json").exists());
env::set_current_dir(&current_directory).unwrap();
}

#[tokio::test]
async fn init_command_should_throw_on_existing_file() {
let parent_folder = tempdir().unwrap();
let current_directory = env::current_dir().unwrap();
env::set_current_dir(&parent_folder).unwrap();
let mut file = fs::File::create(parent_folder.path().join("package.json")).unwrap();
file.write_all("{}".as_bytes()).unwrap();
assert!(parent_folder.path().join("package.json").exists());
let cli = Cli::parse_from(["", "init"]);
let cli = Cli::parse_from(["", "-C", parent_folder.path().to_str().unwrap(), "init"]);
run_commands(cli).await.expect_err("should have thrown");
env::set_current_dir(&current_directory).unwrap();
}

#[tokio::test]
async fn should_get_store_path() {
let parent_folder = tempdir().unwrap();
let current_directory = env::current_dir().unwrap();
env::set_current_dir(parent_folder.path()).unwrap();
let cli = Cli::parse_from(["", "store", "path"]);
let cli =
Cli::parse_from(["", "-C", parent_folder.path().to_str().unwrap(), "store", "path"]);
run_commands(cli).await.unwrap();
env::set_current_dir(&current_directory).unwrap();
}
}

0 comments on commit 8a07cbf

Please sign in to comment.