Skip to content

Commit

Permalink
refactor: make shared util modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Apr 25, 2024
1 parent 3aaedaa commit 9235f68
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 55 deletions.
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/cli/env_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use dotenvy::Result;

#[derive(Clone, Debug)]
pub struct EnvFile {
inner: Vec<(String, String)>,
}

impl From<Vec<(String, String)>> for EnvFile {
fn from(inner: Vec<(String, String)>) -> Self {
Self { inner }
}
}

impl EnvFile {
pub fn from_path(path: &str) -> Result<Self> {
let file = dotenvy::from_filename_iter(path)?;
let env: Vec<(String, String)> = file.collect::<Result<_>>()?;
Ok(Self { inner: env })
}

pub fn iter(&self) -> impl Iterator<Item = (&String, &String)> {
self.inner.iter().map(|(a, b)| (a, b))
}
}
38 changes: 4 additions & 34 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
use clap::{Args, CommandFactory, Parser, Subcommand};

use color_eyre::Result;
use owo_colors::{OwoColorize as _, Stream};
use std::env;

use std::{env, sync::OnceLock};
use self::env_file::EnvFile;

mod env_file;
mod exec;
mod list;
mod run;

#[derive(Clone, Debug)]
pub struct EnvFile {
inner: Vec<(String, String)>,
}

impl From<Vec<(String, String)>> for EnvFile {
fn from(inner: Vec<(String, String)>) -> Self {
Self { inner }
}
}

impl EnvFile {
pub fn from_path(path: &str) -> dotenvy::Result<Self> {
let file = dotenvy::from_filename_iter(path)?;
let env = file.collect::<dotenvy::Result<Vec<(String, String)>>>()?;
Ok(Self { inner: env })
}

pub fn iter(&self) -> impl Iterator<Item = (&String, &String)> {
self.inner.iter().map(|(a, b)| (a, b))
}
}

#[derive(Parser, Clone)]
#[command(author, version, about, long_about = None)]
#[command(args_conflicts_with_subcommands = true)]
Expand Down Expand Up @@ -112,16 +92,6 @@ pub struct ExecArgs {
pub env_file: Option<EnvFile>,
}

pub fn get_level() -> &'static usize {
static ONCE_LOCK: OnceLock<usize> = OnceLock::new();
ONCE_LOCK.get_or_init(|| {
std::env::var("__NRR_LEVEL")
.ok()
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(1)
})
}

impl Cli {
pub fn execute(&self) -> Result<()> {
let current_dir = env::current_dir()?;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ mod cli;
mod package_json;
mod run;
mod suggest;
mod util;

use clap::Parser as _;
use color_eyre::eyre::Result;
use std::env;

use crate::cli::{Cli, NrxCli};
use cli::get_level;

fn main() -> Result<()> {
color_eyre::install()?;
Expand Down
12 changes: 9 additions & 3 deletions src/run/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use std::{env, path::Path, process::Command};
#[cfg(unix)]
use std::os::unix::process::CommandExt as _;

use crate::{cli::ExecArgs, package_json::PackageJson, run::util};
use crate::{
cli::ExecArgs,
package_json::PackageJson,
util::{get_level, itoa},
};

use super::util::make_patched_path;

pub fn exec(package_path: &Path, package_data: &PackageJson, args: &ExecArgs) -> Result<()> {
let package_folder = package_path.parent().unwrap();
Expand All @@ -18,8 +24,8 @@ pub fn exec(package_path: &Path, package_data: &PackageJson, args: &ExecArgs) ->
}

subproc
.env("PATH", util::make_patched_path(package_path)?)
.env("__NRR_LEVEL", util::itoa(crate::get_level() + 1));
.env("PATH", make_patched_path(package_path)?)
.env("__NRR_LEVEL", itoa(get_level() + 1));

subproc
.env("npm_execpath", env::current_exe()?)
Expand Down
18 changes: 12 additions & 6 deletions src/run/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ use std::{env, path::Path};
use color_eyre::Result;
use owo_colors::{OwoColorize as _, Stream};

use crate::{cli::SharedRunOptions, package_json::PackageJson, run::util};
use crate::{
cli::SharedRunOptions,
package_json::PackageJson,
util::{get_level, itoa},
};

use super::util::{make_patched_path, make_shell_cmd};

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ScriptType {
Expand Down Expand Up @@ -49,7 +55,7 @@ fn single_script(
}

if !options.silent {
let cmd_prefix = script_type.prefix() + &"$".repeat(*crate::get_level());
let cmd_prefix = script_type.prefix() + &"$".repeat(*get_level());

eprintln!(
"{} {}",
Expand All @@ -60,16 +66,16 @@ fn single_script(
);
}

let mut subproc = util::make_shell_cmd();
let mut subproc = make_shell_cmd();
subproc.current_dir(package_folder).arg(&full_cmd);

if let Some(env_file) = &options.env_file {
subproc.envs(env_file.iter());
}

subproc
.env("PATH", util::make_patched_path(package_path)?)
.env("__NRR_LEVEL", util::itoa(crate::get_level() + 1));
.env("PATH", make_patched_path(package_path)?)
.env("__NRR_LEVEL", itoa(get_level() + 1));

subproc
.env("npm_execpath", env::current_exe()?)
Expand Down Expand Up @@ -113,7 +119,7 @@ pub fn script(
eprint!(
"{}",
package_data.make_prefix(
match crate::get_level() {
match get_level() {
1 => None,
_ => Some(script_name),
},
Expand Down
5 changes: 0 additions & 5 deletions src/run/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ use std::{
process::Command,
};

#[must_use]
pub fn itoa(input: impl itoa::Integer) -> String {
itoa::Buffer::new().format(input).to_owned()
}

#[must_use]
pub fn make_patched_paths(package_path: &Path) -> Vec<PathBuf> {
let mut patched_path = package_path
Expand Down
17 changes: 17 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::sync::OnceLock;

#[must_use]
#[inline]
pub fn itoa(input: impl itoa::Integer) -> String {
itoa::Buffer::new().format(input).to_owned()
}

pub fn get_level() -> &'static usize {
static ONCE_LOCK: OnceLock<usize> = OnceLock::new();
ONCE_LOCK.get_or_init(|| {
std::env::var("__NRR_LEVEL")
.ok()
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(1)
})
}

0 comments on commit 9235f68

Please sign in to comment.