Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move npmrc parse method to struct #48

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 71 additions & 71 deletions src/npm_rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,75 @@ pub struct NpmRc {
}

impl NpmRc {
pub fn parse(
input: &str,
get_env_var: &impl Fn(&str) -> Option<String>,
) -> Result<Self, monch::ParseErrorFailureError> {
let kv_or_sections = ini::parse_ini(input)?;
let mut rc_file = Self::default();

for kv_or_section in kv_or_sections {
match kv_or_section {
KeyValueOrSection::KeyValue(kv) => {
if let Key::Plain(key) = &kv.key {
if let Some((left, right)) = key.rsplit_once(':') {
if let Some(scope) = left.strip_prefix('@') {
if right == "registry" {
if let Value::String(text) = &kv.value {
let value = expand_vars(text, get_env_var);
rc_file.scope_registries.insert(scope.to_string(), value);
}
}
} else if let Some(host_and_path) = left.strip_prefix("//") {
if let Value::String(text) = &kv.value {
let value = expand_vars(text, get_env_var);
let config = rc_file
.registry_configs
.entry(host_and_path.to_string())
.or_default();
match right {
"_auth" => {
config.auth = Some(value);
}
"_authToken" => {
config.auth_token = Some(value);
}
"username" => {
config.username = Some(value);
}
"_password" => {
config.password = Some(value);
}
"email" => {
config.email = Some(value);
}
"certfile" => {
config.certfile = Some(value);
}
"keyfile" => {
config.keyfile = Some(value);
}
_ => {}
}
}
}
} else if key == "registry" {
if let Value::String(text) = &kv.value {
let value = expand_vars(text, get_env_var);
rc_file.registry = Some(value);
}
}
}
}
KeyValueOrSection::Section(_) => {
// ignore
}
}
}

Ok(rc_file)
}

pub fn config_for_package<'a>(
&'a self,
package_name: &str,
Expand Down Expand Up @@ -71,75 +140,6 @@ impl NpmRc {
}
}

pub fn parse_npm_rc(
input: &str,
get_env_var: &impl Fn(&str) -> Option<String>,
) -> Result<NpmRc, monch::ParseErrorFailureError> {
let kv_or_sections = ini::parse_ini(input)?;
let mut rc_file = NpmRc::default();

for kv_or_section in kv_or_sections {
match kv_or_section {
KeyValueOrSection::KeyValue(kv) => {
if let Key::Plain(key) = &kv.key {
if let Some((left, right)) = key.rsplit_once(':') {
if let Some(scope) = left.strip_prefix('@') {
if right == "registry" {
if let Value::String(text) = &kv.value {
let value = expand_vars(text, get_env_var);
rc_file.scope_registries.insert(scope.to_string(), value);
}
}
} else if let Some(host_and_path) = left.strip_prefix("//") {
if let Value::String(text) = &kv.value {
let value = expand_vars(text, get_env_var);
let config = rc_file
.registry_configs
.entry(host_and_path.to_string())
.or_default();
match right {
"_auth" => {
config.auth = Some(value);
}
"_authToken" => {
config.auth_token = Some(value);
}
"username" => {
config.username = Some(value);
}
"_password" => {
config.password = Some(value);
}
"email" => {
config.email = Some(value);
}
"certfile" => {
config.certfile = Some(value);
}
"keyfile" => {
config.keyfile = Some(value);
}
_ => {}
}
}
}
} else if key == "registry" {
if let Value::String(text) = &kv.value {
let value = expand_vars(text, get_env_var);
rc_file.registry = Some(value);
}
}
}
}
KeyValueOrSection::Section(_) => {
// ignore
}
}
}

Ok(rc_file)
}

fn expand_vars(
input: &str,
get_env_var: &impl Fn(&str) -> Option<String>,
Expand Down Expand Up @@ -183,7 +183,7 @@ mod test {
#[test]
fn test_parse_basic() {
// https://docs.npmjs.com/cli/v10/configuring-npm/npmrc#auth-related-configuration
let npm_rc = parse_npm_rc(
let npm_rc = NpmRc::parse(
r#"
@myorg:registry=https://example.com/myorg
@another:registry=https://example.com/another
Expand Down Expand Up @@ -284,7 +284,7 @@ registry=https://registry.npmjs.org/

#[test]
fn test_parse_env_vars() {
let npm_rc = parse_npm_rc(
let npm_rc = NpmRc::parse(
r#"
@myorg:registry=${VAR_FOUND}
@another:registry=${VAR_NOT_FOUND}
Expand Down
Loading