Skip to content

Commit b8cf030

Browse files
committed
feat(cli): add option to select JS runtime other than node
resolves tree-sitter#465 resolves tree-sitter#1686
1 parent ba9f847 commit b8cf030

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

cli/src/generate/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn generate_parser_in_directory(
4444
abi_version: usize,
4545
generate_bindings: bool,
4646
report_symbol_name: Option<&str>,
47+
runtime: Option<&str>,
4748
) -> Result<()> {
4849
let src_path = repo_path.join("src");
4950
let header_path = src_path.join("tree_sitter");
@@ -56,11 +57,11 @@ pub fn generate_parser_in_directory(
5657
let grammar_json;
5758
match grammar_path {
5859
Some(path) => {
59-
grammar_json = load_grammar_file(path.as_ref())?;
60+
grammar_json = load_grammar_file(path.as_ref(), runtime)?;
6061
}
6162
None => {
6263
let grammar_js_path = grammar_path.map_or(repo_path.join("grammar.js"), |s| s.into());
63-
grammar_json = load_grammar_file(&grammar_js_path)?;
64+
grammar_json = load_grammar_file(&grammar_js_path, runtime)?;
6465
fs::write(&src_path.join("grammar.json"), &grammar_json)?;
6566
}
6667
}
@@ -155,9 +156,9 @@ fn generate_parser_for_grammar_with_opts(
155156
})
156157
}
157158

158-
pub fn load_grammar_file(grammar_path: &Path) -> Result<String> {
159+
pub fn load_grammar_file(grammar_path: &Path, runtime: Option<&str>) -> Result<String> {
159160
match grammar_path.extension().and_then(|e| e.to_str()) {
160-
Some("js") => Ok(load_js_grammar_file(grammar_path)?),
161+
Some("js") => Ok(load_js_grammar_file(grammar_path, runtime)?),
161162
Some("json") => Ok(fs::read_to_string(grammar_path)?),
162163
_ => Err(anyhow!(
163164
"Unknown grammar file extension: {:?}",
@@ -166,14 +167,15 @@ pub fn load_grammar_file(grammar_path: &Path) -> Result<String> {
166167
}
167168
}
168169

169-
fn load_js_grammar_file(grammar_path: &Path) -> Result<String> {
170+
fn load_js_grammar_file(grammar_path: &Path, runtime: Option<&str>) -> Result<String> {
170171
let grammar_path = fs::canonicalize(grammar_path)?;
171-
let mut node_process = Command::new("node")
172+
let js_runtime = runtime.or(Some("node")).unwrap();
173+
let mut node_process = Command::new(js_runtime)
172174
.env("TREE_SITTER_GRAMMAR_PATH", grammar_path)
173175
.stdin(Stdio::piped())
174176
.stdout(Stdio::piped())
175177
.spawn()
176-
.expect("Failed to run `node`");
178+
.expect(format!("Failed to run `{js_runtime}`").as_str());
177179

178180
let mut node_stdin = node_process
179181
.stdin

cli/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ fn run() -> Result<()> {
126126
.long("report-states-for-rule")
127127
.value_name("rule-name")
128128
.takes_value(true),
129+
)
130+
.arg(
131+
Arg::with_name("js-runtime")
132+
.long("js-runtime")
133+
.short("R")
134+
.takes_value(true)
135+
.value_name("binary")
136+
.help("Use a JavaScript runtime other than node"),
129137
),
130138
)
131139
.subcommand(
@@ -327,13 +335,15 @@ fn run() -> Result<()> {
327335
version.parse().expect("invalid abi version flag")
328336
}
329337
});
338+
let runtime = matches.value_of("js-runtime");
330339
let generate_bindings = !matches.is_present("no-bindings");
331340
generate::generate_parser_in_directory(
332341
&current_dir,
333342
grammar_path,
334343
abi_version,
335344
generate_bindings,
336345
report_symbol_name,
346+
runtime
337347
)?;
338348
if build {
339349
if let Some(path) = libdir {

0 commit comments

Comments
 (0)