Skip to content

Commit d0f38df

Browse files
authored
enahnce(cli/init): skip prompts automatically in non interactive environments (#15997)
1 parent f9ed1a3 commit d0f38df

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'tauri-cli': 'patch:enhance'
3+
'@tauri-apps/cli': 'patch:enhance'
4+
---
5+
6+
When stdin is not a terminal, `tauri init` now automatically skips prompts, avoiding IO errors in CI and scripts. This eliminates the need to pass `--ci` explicitly in non-interactive environments.

‎crates/tauri-cli/src/init.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
collections::BTreeMap,
1515
env::current_dir,
1616
fs::{read_to_string, remove_dir_all},
17+
io::IsTerminal,
1718
path::PathBuf,
1819
};
1920

@@ -75,6 +76,9 @@ struct InitDefaults {
7576

7677
impl Options {
7778
fn load(mut self) -> Result<Self> {
79+
if !std::io::stdin().is_terminal() {
80+
self.ci = true;
81+
}
7882
let package_json_path = PathBuf::from(&self.directory).join("package.json");
7983

8084
let init_defaults = if package_json_path.exists() {

‎crates/tauri-cli/tests/init.rs‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
use std::fs;
6+
use std::process::Command;
7+
8+
#[test]
9+
fn init_non_interactive_works() {
10+
let temp_dir = tempfile::tempdir().unwrap();
11+
let dir = temp_dir.path();
12+
13+
// Determine the path to the cargo-tauri binary.
14+
// The test binary is in target/debug/deps/, so we navigate up to target/debug/
15+
let current_exe = std::env::current_exe().unwrap();
16+
let target_debug = current_exe.parent().unwrap().parent().unwrap();
17+
let cargo_tauri = if cfg!(windows) {
18+
target_debug.join("cargo-tauri.exe")
19+
} else {
20+
target_debug.join("cargo-tauri")
21+
};
22+
23+
assert!(
24+
cargo_tauri.exists(),
25+
"cargo-tauri binary not found at {:?}",
26+
cargo_tauri
27+
);
28+
29+
// Run `init` without --force or --ci; since stdin is not a terminal in tests,
30+
// it should automatically skip prompts and use defaults.
31+
let output = Command::new(&cargo_tauri)
32+
.arg("init")
33+
.arg("--app-name")
34+
.arg("testapp")
35+
.arg("--frontend-dist")
36+
.arg("../dist")
37+
.arg("--dev-url")
38+
.arg("http://localhost:3000")
39+
.current_dir(dir)
40+
.output()
41+
.expect("failed to run cargo-tauri init");
42+
43+
assert!(output.status.success(), "init failed: {:?}", output);
44+
45+
// Verify that src-tauri was created.
46+
let src_tauri = dir.join("src-tauri");
47+
assert!(src_tauri.exists(), "src-tauri directory was not created");
48+
49+
// Verify that the tauri.conf.json contains the app name we set.
50+
let conf_path = src_tauri.join("tauri.conf.json");
51+
assert!(conf_path.exists(), "tauri.conf.json not found");
52+
let content = fs::read_to_string(&conf_path).unwrap();
53+
assert!(content.contains("testapp"), "app name not found in config");
54+
}

0 commit comments

Comments
 (0)