Skip to content

Commit

Permalink
feat: add test command
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jul 18, 2023
1 parent 95637fd commit a6b7806
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Experimental package manager for node.js written in rust.
- [ ] `why`
- [ ] `licenses`
- [ ] `run`
- [ ] `test`
- [x] `test`
- [ ] `exec`

## Debugging
Expand Down
2 changes: 2 additions & 0 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum Subcommands {
Init,
/// Add a package
Add(AddArgs),
/// Runs a package's "test" script, if one was provided.
Test,
}

#[derive(Parser, Debug)]
Expand Down
3 changes: 3 additions & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub async fn run_commands() -> Result<()> {
// the existing entry.
registry_manager.add_dependency(&args.package, args.get_dependency_group()).await?;
}
Subcommands::Test => {
PackageJson::from_path(&package_json_path)?.execute_command("test")?;
}
}

Ok(())
Expand Down
4 changes: 4 additions & 0 deletions crates/package_json/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ pub enum PackageJsonError {
AlreadyExist,
#[error("invalid attribute: {0}")]
InvalidAttribute(String),
#[error("No package.json was found in {0}")]
NoImporterManifestFound(String),
#[error("Missing script: \"{0}\"")]
NoScript(String),
}
34 changes: 34 additions & 0 deletions crates/package_json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
fs,
io::{Read, Write},
path::PathBuf,
process::{Command, Stdio},
};

use serde_json::{json, Map, Value};
Expand Down Expand Up @@ -77,6 +78,14 @@ impl PackageJson {
Ok(())
}

pub fn from_path(path: &PathBuf) -> Result<PackageJson, PackageJsonError> {
if !path.exists() {
return Err(PackageJsonError::NoImporterManifestFound(path.display().to_string()));
}

Ok(PackageJson { path: path.to_path_buf(), value: PackageJson::read_from_file(path)? })
}

pub fn create_if_needed(path: &PathBuf) -> Result<PackageJson, PackageJsonError> {
let value = if path.exists() {
PackageJson::read_from_file(path)?
Expand Down Expand Up @@ -116,4 +125,29 @@ impl PackageJson {
}
Ok(())
}

pub fn execute_command(&self, command: &str) -> Result<(), PackageJsonError> {
match self
.value
.get("scripts")
.unwrap_or(&Value::default())
.get(command)
.unwrap_or(&Value::default())
.as_str()
{
Some(command) => {
let mut cmd = Command::new(command)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()
.unwrap();

cmd.wait().unwrap();

Ok(())
}
None => Err(PackageJsonError::NoScript(command.to_string())),
}
}
}

0 comments on commit a6b7806

Please sign in to comment.