Skip to content

Commit f65eb4f

Browse files
committed
fix(cli.js): revert run command to be nonblocking
1 parent 3fbaee4 commit f65eb4f

File tree

10 files changed

+50
-10
lines changed

10 files changed

+50
-10
lines changed

.changes/revert-cli-async.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.js": patch
3+
---
4+
5+
Revert the `run` command to run in a separate thread.

tooling/cli/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli/node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["cdylib"]
1111
napi = { version = "2.5", default-features = false, features = ["napi4"] }
1212
napi-derive = "2.5"
1313
tauri-cli = { path = ".." }
14+
log = "0.4.17"
1415

1516
[build-dependencies]
1617
napi-build = "2.0"

tooling/cli/node/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
/* auto-generated by NAPI-RS */
55

6-
export function run(args: Array<string>, binName?: string | undefined | null): void
6+
export function run(args: Array<string>, binName: string | undefined | null, callback: (...args: any[]) => any): void
7+
export function logError(error: string): void

tooling/cli/node/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ if (!nativeBinding) {
236236
throw new Error(`Failed to load native binding`)
237237
}
238238

239-
const { run } = nativeBinding
239+
const { run, logError } = nativeBinding
240240

241241
module.exports.run = run
242+
module.exports.logError = logError

tooling/cli/node/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { run } = require('./index')
1+
const { run, logError } = require('./index')
22

33
module.exports.run = (args, binName) => {
44
return new Promise((resolve, reject) => {
@@ -11,3 +11,5 @@ module.exports.run = (args, binName) => {
1111
})
1212
})
1313
}
14+
15+
module.exports.logError = logError

tooling/cli/node/src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,30 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
use napi::{
6+
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
7+
Error, JsFunction, Result, Status,
8+
};
9+
10+
#[napi_derive::napi]
11+
pub fn run(args: Vec<String>, bin_name: Option<String>, callback: JsFunction) -> Result<()> {
12+
let function: ThreadsafeFunction<bool, ErrorStrategy::CalleeHandled> = callback
13+
.create_threadsafe_function(0, |ctx| ctx.env.get_boolean(ctx.value).map(|v| vec![v]))?;
14+
15+
// we need to run in a separate thread so Node.js (e.g. vue-cli-plugin-tauri) consumers
16+
// can do work while `tauri dev` is running.
17+
std::thread::spawn(move || match tauri_cli::try_run(args, bin_name) {
18+
Ok(_) => function.call(Ok(true), ThreadsafeFunctionCallMode::Blocking),
19+
Err(e) => function.call(
20+
Err(Error::new(Status::GenericFailure, format!("{:#}", e))),
21+
ThreadsafeFunctionCallMode::Blocking,
22+
),
23+
});
24+
25+
Ok(())
26+
}
27+
528
#[napi_derive::napi]
6-
pub fn run(args: Vec<String>, bin_name: Option<String>) {
7-
tauri_cli::run(args, bin_name);
29+
pub fn log_error(error: String) {
30+
log::error!("{}", error);
831
}

tooling/cli/node/tauri.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ if (binStem === 'node' || binStem === 'nodejs') {
4343
arguments.unshift(bin)
4444
}
4545

46-
cli.run(arguments, binName)
46+
cli.run(arguments, binName).catch((err) => {
47+
cli.logError(err.message)
48+
process.exit(1)
49+
})

tooling/cli/node/test/jest/__tests__/template.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('[CLI] cli.js template', () => {
2323
await move(outPath, cacheOutPath)
2424
}
2525

26-
cli.run(['init', '--directory', process.cwd(), '--force', '--tauri-path', resolve(currentDirName, '../../../../../..'), '--ci'])
26+
await cli.run(['init', '--directory', process.cwd(), '--force', '--tauri-path', resolve(currentDirName, '../../../../../..'), '--ci'])
2727

2828
if (outExists) {
2929
await move(cacheOutPath, outPath)
@@ -39,7 +39,7 @@ describe('[CLI] cli.js template', () => {
3939
const config = readFileSync(configPath).toString()
4040
writeFileSync(configPath, config.replace('com.tauri.dev', 'com.tauri.test'))
4141

42-
cli.run(['build', '--verbose'])
42+
await cli.run(['build', '--verbose'])
4343
process.chdir(cwd)
4444
})
4545
})

tooling/cli/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn format_error<I: IntoApp>(err: clap::Error) -> clap::Error {
7373
err.format(&mut app)
7474
}
7575

76-
/// Run the Tauri CLI with the passed arguments.
76+
/// Run the Tauri CLI with the passed arguments, exiting if an error occurrs.
7777
///
7878
/// The passed arguments should have the binary argument(s) stripped out before being passed.
7979
///
@@ -96,7 +96,10 @@ where
9696
}
9797
}
9898

99-
fn try_run<I, A>(args: I, bin_name: Option<String>) -> Result<()>
99+
/// Run the Tauri CLI with the passed arguments.
100+
///
101+
/// It is similar to [`run`], but instead of exiting on an error, it returns a result.
102+
pub fn try_run<I, A>(args: I, bin_name: Option<String>) -> Result<()>
100103
where
101104
I: IntoIterator<Item = A>,
102105
A: Into<OsString> + Clone,

0 commit comments

Comments
 (0)