Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

Commit 53a203b

Browse files
authored
fix: Allow comments in .nvmrc/.node-version (#35)
1 parent 2e1ebc0 commit 53a203b

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Added a `dist-url` config setting, allowing the download host to be customized.
88

9+
#### 🐞 Fixes
10+
11+
- Fixed `.nvmrc` and `.node-version` parsing when they contain comments.
12+
913
## 0.10.1
1014

1115
#### 🚀 Updates

crates/node/src/proto.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ pub fn parse_version_file(
5858
}
5959
}
6060
} else {
61-
version = Some(UnresolvedVersionSpec::parse(input.content)?);
61+
for line in input.content.lines() {
62+
let line = line.trim();
63+
64+
if line.is_empty() || line.starts_with('#') {
65+
continue;
66+
} else {
67+
version = Some(UnresolvedVersionSpec::parse(line)?);
68+
break;
69+
}
70+
}
6271
}
6372

6473
Ok(Json(ParseVersionFileOutput { version }))

crates/node/tests/versions_test.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,67 @@ fn parses_volta() {
8080
}
8181
);
8282
}
83+
84+
#[test]
85+
fn parses_nvmrc() {
86+
let sandbox = create_empty_proto_sandbox();
87+
let plugin = sandbox.create_plugin("node-test");
88+
89+
assert_eq!(
90+
plugin.parse_version_file(ParseVersionFileInput {
91+
content: "~20".into(),
92+
file: ".nvmrc".into(),
93+
}),
94+
ParseVersionFileOutput {
95+
version: Some(UnresolvedVersionSpec::parse("~20").unwrap()),
96+
}
97+
);
98+
}
99+
100+
#[test]
101+
fn parses_nvmrc_with_comment() {
102+
let sandbox = create_empty_proto_sandbox();
103+
let plugin = sandbox.create_plugin("node-test");
104+
105+
assert_eq!(
106+
plugin.parse_version_file(ParseVersionFileInput {
107+
content: "# comment\n^20.1".into(),
108+
file: ".nvmrc".into(),
109+
}),
110+
ParseVersionFileOutput {
111+
version: Some(UnresolvedVersionSpec::parse("^20.1").unwrap()),
112+
}
113+
);
114+
}
115+
116+
#[test]
117+
fn parses_node_version() {
118+
let sandbox = create_empty_proto_sandbox();
119+
let plugin = sandbox.create_plugin("node-test");
120+
121+
assert_eq!(
122+
plugin.parse_version_file(ParseVersionFileInput {
123+
content: "~20".into(),
124+
file: ".node-version".into(),
125+
}),
126+
ParseVersionFileOutput {
127+
version: Some(UnresolvedVersionSpec::parse("~20").unwrap()),
128+
}
129+
);
130+
}
131+
132+
#[test]
133+
fn parses_node_version_with_comment() {
134+
let sandbox = create_empty_proto_sandbox();
135+
let plugin = sandbox.create_plugin("node-test");
136+
137+
assert_eq!(
138+
plugin.parse_version_file(ParseVersionFileInput {
139+
content: "# comment\n^20.1".into(),
140+
file: ".node-version".into(),
141+
}),
142+
ParseVersionFileOutput {
143+
version: Some(UnresolvedVersionSpec::parse("^20.1").unwrap()),
144+
}
145+
);
146+
}

0 commit comments

Comments
 (0)