Skip to content

Commit cd9a20b

Browse files
authored
refactor(cli.js): run on separate thread (#3436)
1 parent 9c50bda commit cd9a20b

File tree

9 files changed

+63
-22
lines changed

9 files changed

+63
-22
lines changed

.changes/async-cli.js.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.js": patch
3+
---
4+
5+
Change the `run` function to take a callback and run asynchronously instead of blocking the event loop.

examples/api/src-tauri/Cargo.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli/node/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export class ExternalObject<T> {
99
[K: symbol]: T
1010
}
1111
}
12-
export function run(args: Array<string>, binName?: string | undefined | null): void
12+
export function run(args: Array<string>, binName: string | undefined | null, callback: (...args: any[]) => any): void

tooling/cli/node/main.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
4+
export function run(args: Array<string>, binName: string | undefined | null): Promise<void>

tooling/cli/node/main.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { run } = require('./index')
2+
3+
module.exports.run = (args, binName) => {
4+
return new Promise((resolve, reject) => {
5+
run(args, binName, res => {
6+
if (res instanceof Error) {
7+
reject(res)
8+
} else {
9+
resolve(res)
10+
}
11+
})
12+
})
13+
}

tooling/cli/node/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"publishConfig": {
2222
"access": "public"
2323
},
24-
"main": "index.js",
25-
"types": "index.d.ts",
24+
"main": "main.js",
25+
"types": "main.d.ts",
2626
"napi": {
2727
"name": "cli",
2828
"triples": {

tooling/cli/node/src/lib.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use napi::{Error, Result, Status};
5+
use napi::{
6+
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
7+
Error, JsFunction, Result, Status,
8+
};
69

710
#[napi_derive::napi]
8-
pub fn run(args: Vec<String>, bin_name: Option<String>) -> Result<()> {
9-
tauri_cli::run(args, bin_name).map_err(|e| Error::new(Status::GenericFailure, e.to_string()))
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+
std::thread::spawn(move || match tauri_cli::run(args, bin_name) {
16+
Ok(_) => function.call(Ok(true), ThreadsafeFunctionCallMode::Blocking),
17+
Err(e) => function.call(
18+
Err(Error::new(Status::GenericFailure, e.to_string())),
19+
ThreadsafeFunctionCallMode::Blocking,
20+
),
21+
});
22+
Ok(())
1023
}

tooling/cli/node/tauri.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
const cli = require('./index')
3+
const cli = require('./main')
44
const path = require('path')
55

66
const [bin, script, ...arguments] = process.argv
@@ -43,8 +43,7 @@ if (binStem === 'node' || binStem === 'nodejs') {
4343
arguments.unshift(bin)
4444
}
4545

46-
try {
47-
cli.run(arguments, binName)
48-
} catch (e) {
49-
console.log(`Error running CLI: ${e.message}`)
50-
}
46+
cli.run(arguments, binName).catch((err) => {
47+
console.log(`Error running CLI: ${err.message}`)
48+
process.exit(1)
49+
})

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fixtureSetup = require('../fixtures/app-test-setup.js')
22
const { resolve } = require('path')
33
const { existsSync, readFileSync, writeFileSync } = require('fs')
44
const { move } = require('fs-extra')
5-
const cli = require('~/index.js')
5+
const cli = require('~/main.js')
66

77
const currentDirName = __dirname
88

@@ -23,7 +23,11 @@ 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'])
27+
.catch(err => {
28+
console.error(err)
29+
throw err
30+
})
2731

2832
if (outExists) {
2933
await move(cacheOutPath, outPath)
@@ -35,7 +39,10 @@ describe('[CLI] cli.js template', () => {
3539
const manifestFile = readFileSync(manifestPath).toString()
3640
writeFileSync(manifestPath, `workspace = { }\n${manifestFile}`)
3741

38-
cli.run(['build', '--verbose'])
42+
await cli.run(['build', '--verbose']).catch(err => {
43+
console.error(err)
44+
throw err
45+
})
3946
process.chdir(cwd)
4047
})
4148
})

0 commit comments

Comments
 (0)