Skip to content

Commit

Permalink
feat: use shell-quote
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakiyo committed Aug 4, 2023
1 parent a7a9818 commit c915fb8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::*;
use std::ffi::OsString;

#[derive(Debug, Parser)]
#[clap(author, version, about, rename_all = "kebab-case")]
Expand All @@ -19,7 +20,7 @@ pub enum Command {
Run(RunArgs),
/// Execute a shell command in scope of a project.
#[clap(external_subcommand)]
Other(Vec<String>),
Other(Vec<OsString>),
}

/// Runs a defined package script.
Expand All @@ -30,5 +31,5 @@ pub struct RunArgs {
pub script: Option<String>, // Not OsString because it would be compared against package.json#scripts

/// Arguments to pass to the package script.
pub args: Vec<String>,
pub args: Vec<OsString>,
}
24 changes: 17 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use indexmap::IndexMap;
use itertools::Itertools;
use pipe_trait::Pipe;
use serde::{Deserialize, Serialize};
use shell_quote::sh;
use std::{
env,
ffi::OsString,
Expand Down Expand Up @@ -73,20 +74,20 @@ fn run() -> Result<(), MainError> {
eprintln!("> {command}\n");
run_script(name, command, cwd)
};
let command_string = |name: &str, args: &[String]| {
let mut string = name.to_string();
let command_string = |name: &str, args: &[OsString]| {
let mut out: Vec<u8> = name.as_bytes().to_owned();
for arg in args {
string += " ";
string += arg;
out.push(b' ');
sh::escape_into(arg, &mut out);
}
string
std::str::from_utf8(&out).map(str::to_string)
};
match cli.command {
cli::Command::Run(args) => {
let (cwd, manifest) = cwd_and_manifest()?;
if let Some(name) = args.script {
if let Some(command) = manifest.scripts.get(&name) {
let command = command_string(command, &args.args);
let command = command_string(command, &args.args).unwrap();
print_and_run_script(&manifest, &name, &command, &cwd)
} else {
PnError::MissingScript { name }
Expand All @@ -105,14 +106,23 @@ fn run() -> Result<(), MainError> {
cli::Command::Other(args) => {
let (cwd, manifest) = cwd_and_manifest()?;
if let Some(name) = args.first() {
let name = name.to_str().unwrap();
if passed_through::PASSED_THROUGH_COMMANDS.contains(name) {
let args = args
.into_iter()
.map(|f| f.into_string().unwrap())
.collect::<Vec<String>>();
return pass_to_pnpm(&args); // args already contain name, no need to prepend
}
if let Some(command) = manifest.scripts.get(name) {
let command = command_string(command, &args[1..]);
let command = command_string(command, &args[1..]).unwrap();
return print_and_run_script(&manifest, name, &command, &cwd);
}
}
let args = args
.into_iter()
.map(|f| f.into_string().unwrap())
.collect::<Vec<String>>();
pass_to_sub(args.join(" "))
}
}
Expand Down

0 comments on commit c915fb8

Please sign in to comment.