#![warn(missing_docs)]
pub mod auth;
pub mod bunfig;
#[cfg(feature = "cli")]
pub mod cli;
pub mod error;
pub mod fs;
#[cfg(feature = "cli")]
pub mod logger;
pub mod npmrc;
pub mod registry;
pub mod token;
pub mod vcs;
pub mod yarnrc;
pub use error::AuthError;
use std::path::{Path, PathBuf};
pub enum RunOutcome {
Token(String),
Updated {
paths: Vec<PathBuf>,
git_status: Option<vcs::GitStatus>,
bun_env_git_status: Option<vcs::GitStatus>,
},
}
impl std::fmt::Debug for RunOutcome {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Token(_) => formatter.write_str("Token([redacted])"),
Self::Updated {
paths,
git_status,
bun_env_git_status,
} => formatter
.debug_struct("Updated")
.field("paths", paths)
.field("git_status", git_status)
.field("bun_env_git_status", bun_env_git_status)
.finish(),
}
}
}
#[derive(Clone, Default)]
pub struct Options {
pub repo_config: Option<PathBuf>,
pub credential_config: Option<PathBuf>,
pub repo_config_yarn: Option<PathBuf>,
pub credential_config_yarn: Option<PathBuf>,
pub repo_config_bun: Option<PathBuf>,
pub local_credential: bool,
pub token: Option<String>,
pub allow_all_domains: bool,
pub yarn: Option<bool>,
pub bun: Option<bool>,
pub bun_env: bool,
pub print_token: bool,
}
impl std::fmt::Debug for Options {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Options")
.field("repo_config", &self.repo_config)
.field("credential_config", &self.credential_config)
.field("repo_config_yarn", &self.repo_config_yarn)
.field("credential_config_yarn", &self.credential_config_yarn)
.field("repo_config_bun", &self.repo_config_bun)
.field("local_credential", &self.local_credential)
.field("token", &self.token.as_ref().map(|_| "[redacted]"))
.field("allow_all_domains", &self.allow_all_domains)
.field("yarn", &self.yarn)
.field("bun", &self.bun)
.field("bun_env", &self.bun_env)
.field("print_token", &self.print_token)
.finish()
}
}
pub async fn run(options: &Options) -> Result<RunOutcome, AuthError> {
if options.bun_env && options.local_credential {
return Err(AuthError::BunEnvLocalCredentialConflict);
}
if options.bun_env && options.bun == Some(false) {
return Err(AuthError::BunEnvDisabledConflict);
}
let token = auth::get_credentials(options.token.as_deref()).await?;
token::validate_token(&token)?;
if options.print_token {
return Ok(RunOutcome::Token(token));
}
let home_dir = dirs::home_dir()
.ok_or_else(|| AuthError::Config("Unable to determine user home directory".to_string()))?;
let (repo_npmrc, cred_npmrc, git_status) = if options.local_credential {
let cwd = std::env::current_dir()?;
let git_status = vcs::check_git_status(&cwd);
let local_npmrc = cwd.join(".npmrc");
let repo = if let Some(ref r) = options.repo_config {
r.clone()
} else if local_npmrc.exists() {
local_npmrc.clone()
} else {
home_dir.join(".npmrc")
};
(repo, local_npmrc, Some(git_status))
} else {
let repo = options.repo_config.clone().unwrap_or_else(|| {
let local = Path::new(".npmrc");
if local.exists() {
local.to_path_buf()
} else {
home_dir.join(".npmrc")
}
});
let cred = options
.credential_config
.clone()
.unwrap_or_else(|| home_dir.join(".npmrc"));
(repo, cred, None)
};
let repo_yarn = options.repo_config_yarn.clone().unwrap_or_else(|| {
let local = Path::new(".yarnrc.yml");
if local.exists() {
local.to_path_buf()
} else {
home_dir.join(".yarnrc.yml")
}
});
let cred_yarn = options
.credential_config_yarn
.clone()
.unwrap_or_else(|| home_dir.join(".yarnrc.yml"));
let should_update_yarn = match options.yarn {
Some(explicit) => explicit,
None => {
options.repo_config_yarn.is_some()
|| options.credential_config_yarn.is_some()
|| Path::new(".yarnrc.yml").exists()
}
};
let repo_bun = options
.repo_config_bun
.clone()
.unwrap_or_else(|| PathBuf::from("bunfig.toml"));
let should_update_bun = options.bun_env
|| match options.bun {
Some(explicit) => explicit,
None => options.repo_config_bun.is_some() || Path::new("bunfig.toml").exists(),
};
let repo_npmrc = fs::resolve(&repo_npmrc)?;
let cred_npmrc = fs::resolve(&cred_npmrc)?;
let repo_yarn = if should_update_yarn {
Some(fs::resolve(&repo_yarn)?)
} else {
None
};
let cred_yarn = if should_update_yarn {
Some(fs::resolve(&cred_yarn)?)
} else {
None
};
let repo_bun = if should_update_bun {
Some(fs::resolve(&repo_bun)?)
} else {
None
};
let bun_env_path = if options.bun_env {
let parent = repo_bun
.as_ref()
.and_then(|path| path.parent())
.ok_or_else(|| AuthError::Config("bunfig.toml has no parent directory".to_string()))?;
Some(fs::resolve(&parent.join(".env.local"))?)
} else {
None
};
let bun_env_git_status = bun_env_path
.as_ref()
.map(|path| vcs::check_path_status(path.parent().unwrap(), Path::new(".env.local")));
let mut paths = vec![repo_npmrc.clone(), cred_npmrc.clone()];
if let Some(path) = &repo_yarn {
paths.push(path.clone());
}
if let Some(path) = &cred_yarn {
paths.push(path.clone());
}
if let Some(path) = &repo_bun {
paths.push(path.clone());
}
if let Some(path) = &bun_env_path {
paths.push(path.clone());
}
let mut transaction = fs::FileTransaction::new(&paths)?;
let bun_registries = if let Some(path) = &repo_bun {
if !transaction.existed(path)? {
return Err(AuthError::Config(format!(
"Bun configuration does not exist: {}",
path.display()
)));
}
bunfig::registry_urls(transaction.contents(path)?, options.allow_all_domains)?
} else {
Vec::new()
};
let npm_source = transaction.contents(&repo_npmrc)?.to_owned();
let npm_target = transaction.contents(&cred_npmrc)?.to_owned();
let npm_has_registry = npmrc::has_registry(&npm_source, &npm_target, options.allow_all_domains)?;
let bun_npm_registries = if options.local_credential || options.bun_env {
&[][..]
} else {
bun_registries.as_slice()
};
if npm_has_registry || !bun_npm_registries.is_empty() {
let same_file = repo_npmrc == cred_npmrc;
let (cleaned, updated) = npmrc::transform_npmrc_contents_with_registries(
&npm_source,
&npm_target,
&token,
options.allow_all_domains,
same_file,
bun_npm_registries,
)?;
transaction.stage(&cred_npmrc, &updated)?;
if !same_file && transaction.existed(&repo_npmrc)? && cleaned != npm_source {
transaction.stage(&repo_npmrc, &cleaned)?;
}
}
if let (Some(source), Some(target)) = (repo_yarn, cred_yarn) {
let source_content = transaction.contents(&source)?.to_owned();
let target_content = transaction.contents(&target)?.to_owned();
if let Some(updated) = yarnrc::transform_yarnrc_contents_with_policy(
&source_content,
&target_content,
&token,
options.allow_all_domains,
)? {
transaction.stage(&target, &updated)?;
}
}
if options.bun_env {
let bun_path = repo_bun.as_ref().unwrap();
let bun_content = transaction.contents(bun_path)?.to_owned();
let updated_bun = bunfig::transform_bunfig_for_env(&bun_content, options.allow_all_domains)?
.ok_or_else(|| {
AuthError::Config("No eligible registry configuration found in bunfig.toml".to_string())
})?;
if updated_bun != bun_content {
transaction.stage(bun_path, &updated_bun)?;
}
let env_path = bun_env_path.as_ref().unwrap();
let env_content = transaction.contents(env_path)?.to_owned();
let updated_env = bunfig::transform_env_local(&env_content, &token)?;
if updated_env != env_content {
transaction.stage(env_path, &updated_env)?;
}
}
let paths = transaction.commit()?;
if paths.is_empty() {
return Err(AuthError::Config(
"No eligible Artifact Registry configuration found".to_string(),
));
}
Ok(RunOutcome::Updated {
paths,
git_status,
bun_env_git_status,
})
}
pub fn run_blocking(options: &Options) -> Result<RunOutcome, AuthError> {
if tokio::runtime::Handle::try_current().is_ok() {
return Err(AuthError::Config(
"run_blocking cannot be called within an active Tokio runtime; use run().await".to_string(),
));
}
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
runtime.block_on(run(options))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn bun_option_conflicts_return_dedicated_errors() {
let options = Options {
bun_env: true,
local_credential: true,
..Options::default()
};
assert!(matches!(
run(&options).await,
Err(AuthError::BunEnvLocalCredentialConflict)
));
let options = Options {
local_credential: false,
bun: Some(false),
..options
};
assert!(matches!(
run(&options).await,
Err(AuthError::BunEnvDisabledConflict)
));
}
#[test]
fn blocking_run_returns_token_and_propagates_errors() {
let mut options = Options {
token: Some("valid-token".to_string()),
print_token: true,
..Default::default()
};
assert!(
matches!(run_blocking(&options).unwrap(), RunOutcome::Token(token) if token == "valid-token")
);
options.token = Some("invalid\ntoken".to_string());
assert!(run_blocking(&options).is_err());
}
#[test]
fn blocking_run_updates_configuration_without_cli() {
let dir = tempfile::tempdir().unwrap();
let source = dir.path().join("project.npmrc");
let target = dir.path().join("user.npmrc");
std::fs::write(&source, "registry=https://us-npm.pkg.dev/project/repo/\n").unwrap();
let options = Options {
token: Some("valid-token".to_string()),
repo_config: Some(source),
credential_config: Some(target.clone()),
yarn: Some(false),
bun: Some(false),
..Default::default()
};
assert!(
matches!(run_blocking(&options).unwrap(), RunOutcome::Updated { paths, .. } if paths.contains(&fs::resolve(&target).unwrap()))
);
assert!(
std::fs::read_to_string(target)
.unwrap()
.contains("//us-npm.pkg.dev/project/repo/:_authToken=\"valid-token\"")
);
}
#[tokio::test]
async fn blocking_run_rejects_nested_runtime_without_panicking() {
let options = Options {
token: Some("valid-token".to_string()),
print_token: true,
..Default::default()
};
assert!(
matches!(run_blocking(&options), Err(AuthError::Config(message)) if message.contains("active Tokio runtime"))
);
assert!(matches!(run(&options).await.unwrap(), RunOutcome::Token(_)));
}
}