Skip to content

Commit 9b21a2d

Browse files
[docs] fix node sidecar lab (#3476)
Co-authored-by: Fabian-Lars <[email protected]>
1 parent 2c0bb0c commit 9b21a2d

File tree

4 files changed

+105
-31
lines changed

4 files changed

+105
-31
lines changed

src/content/docs/learn/sidecar-nodejs.mdx

Lines changed: 103 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ This example tutorial is applicable for desktop operating systems only.
1515

1616
We recommend reading the general [sidecar guide] first for a deeper understanding of how Tauri sidecars work.
1717

18+
## Goals
19+
20+
- Package a Node.js application as a binary.
21+
- Integrate this binary as a Tauri sidecar.
22+
23+
## Implementation Details
24+
25+
- For this we use the [pkg] tool, but any other tool that can compile JavaScript or Typescript into a binary application will work.
26+
- You can also embed the Node runtime itself into your Tauri application and ship bundled JavaScript as a resource, but this will ship the JavaScript content as readable-ish files and the runtime is usually larger than a `pkg` packaged application.
27+
1828
In this example we will create a Node.js application that reads input from the command line [process.argv]
1929
and writes output to stdout using [console.log]. <br/>
2030
You can leverage alternative inter-process communication systems such as a localhost server, stdin/stdout or local sockets.
@@ -35,7 +45,6 @@ If you are not an advanced user it's **highly recommended** that you use the opt
3545
- Choose your package manager: `pnpm`
3646
- Choose your UI template: `Vanilla`
3747
- Choose your UI flavor: `Typescript`
38-
- Would you like to setup the project for mobile as well? `yes`
3948

4049
:::
4150

@@ -56,8 +65,8 @@ Without the plugin being initialized and configured the example won't work.
5665

5766
<CommandTabs npm="npm init" yarn="yarn init" pnpm="pnpm init" />
5867

59-
We will compile our Node.js application to a self container binary using [pkg].
60-
Let's install it as a development dependency:
68+
We will compile our Node.js application to a self container binary using [pkg] among other options.
69+
Let's install it as a development dependency into the new `sidecar-app`:
6170

6271
<CommandTabs
6372
npm="npm add @yao-pkg/pkg --save-dev"
@@ -79,9 +88,9 @@ Without the plugin being initialized and configured the example won't work.
7988
const command = process.argv[2];
8089

8190
switch (command) {
82-
case 'ping':
91+
case 'hello':
8392
const message = process.argv[3];
84-
console.log(`pong, ${message}`);
93+
console.log(`Hello ${message}!`);
8594
break;
8695
default:
8796
console.error(`unknown command ${command}`);
@@ -91,18 +100,24 @@ Without the plugin being initialized and configured the example won't work.
91100

92101
1. ##### Package the Sidecar
93102

94-
To package our Node.js application to a self contained binary, we can run the following `pkg` command:
103+
To package our Node.js application into a self contained binary, create a script in `package.json`:
95104

96-
<CommandTabs
97-
npm="npm run pkg -- --output app"
98-
yarn="yarn pkg --output app"
99-
pnpm="pnpm pkg --output app"
100-
/>
105+
```json title="sidecar-app/package.json"
106+
{
107+
"scripts": {
108+
"build": "pkg index.ts --output app"
109+
}
110+
}
111+
```
112+
113+
<CommandTabs npm="npm run build" yarn="yarn build" pnpm="pnpm build" />
101114

102115
This will create the `sidecar-app/app` binary on Linux and macOS, and a `sidecar-app/app.exe` executable on Windows.
103-
To rename this file to the expected Tauri sidecar filename, we can use the following Node.js script:
104116

105-
```js
117+
For sidecar applications, we need to ensure that the binary is named in the correct pattern, for more information read [Embedding External Binaries](https://tauri.app/develop/sidecar/)
118+
To rename this file to the expected Tauri sidecar filename and also move to our Tauri project, we can use the following Node.js script as a starting example:
119+
120+
```js title="sidecar-app/rename.js"
106121
import { execSync } from 'child_process';
107122
import fs from 'fs';
108123

@@ -113,12 +128,42 @@ Without the plugin being initialized and configured the example won't work.
113128
if (!targetTriple) {
114129
console.error('Failed to determine platform target triple');
115130
}
131+
// TODO: create `src-tauri/binaries` dir
116132
fs.renameSync(
117133
`app${ext}`,
118134
`../src-tauri/binaries/app-${targetTriple}${ext}`
119135
);
120136
```
121137

138+
And run `node rename.js` from the `sidecar-app` directory.
139+
140+
At this step the `/src-tauri/binaries` directory should contain the renamed sidecar binary.
141+
142+
1. ##### Setup plugin-shell permission
143+
144+
After installing the [shell plugin](/plugin/shell/) make sure you configure the required capabilities.
145+
146+
Note that we use `"args": true` but you can optionally provide an array `["hello"]`, [read more](/develop/sidecar/#passing-arguments).
147+
148+
```json title='src-tauri/capabilities/default.json'
149+
{
150+
"permissions": [
151+
"core:default",
152+
"opener:default",
153+
{
154+
"identifier": "shell:allow-execute",
155+
"allow": [
156+
{
157+
"args": true,
158+
"name": "binaries/app",
159+
"sidecar": true
160+
}
161+
]
162+
}
163+
]
164+
}
165+
```
166+
122167
1. ##### Configure the Sidecar in the Tauri Application
123168

124169
Now that we have our Node.js application ready, we can connect it to our Tauri application
@@ -142,43 +187,73 @@ Without the plugin being initialized and configured the example won't work.
142187

143188
<TabItem label="JavaScript">
144189

145-
Let's execute the `ping` command in the Node.js sidecar directly:
190+
Let's execute the `hello` command in the Node.js sidecar directly:
146191

147-
```javascript
192+
```js
148193
import { Command } from '@tauri-apps/plugin-shell';
149194

150195
const message = 'Tauri';
151196

152-
const command = Command.sidecar('binaries/app', ['ping', message]);
197+
const command = Command.sidecar('binaries/app', ['hello', message]);
153198
const output = await command.execute();
154-
const response = output.stdout;
199+
// once everything is configured it should log "Hello Tauri" in the browser console.
200+
console.log(output.stdout)
155201
```
156202

157203
</TabItem>
158204

159205
<TabItem label="Rust">
160206

161-
Let's pipe a `ping` Tauri command to the Node.js sidecar:
207+
Let's pipe a `hello` Tauri command to the Node.js sidecar:
162208

163209
```rust
210+
use tauri_plugin_shell::ShellExt;
211+
164212
#[tauri::command]
165-
async fn ping(app: tauri::AppHandle, message: String) -> String {
166-
let sidecar_command = app
167-
.shell()
168-
.sidecar("app")
169-
.unwrap()
170-
.arg("ping")
171-
.arg(message);
172-
let output = sidecar_command.output().unwrap();
173-
let response = String::from_utf8(output.stdout).unwrap();
174-
response
213+
async fn hello(app: tauri::AppHandle, cmd: String, message: String) -> String {
214+
let sidecar_command = app
215+
.shell()
216+
.sidecar("app")
217+
.unwrap()
218+
.arg(cmd)
219+
.arg(message);
220+
let output = sidecar_command.output().await.unwrap();
221+
String::from_utf8(output.stdout).unwrap()
175222
}
176223
```
177224

225+
Register it in `invoke_handler` and call it in the frontend with:
226+
227+
```js
228+
229+
import { invoke } from "@tauri-apps/api/core";
230+
231+
const message = "Tauri"
232+
console.log(await invoke("hello", { cmd: 'hello', message }))
233+
234+
```
235+
178236
</TabItem>
179237

180238
</Tabs>
181239

240+
1. ##### Running
241+
242+
Lets test it
243+
244+
<CommandTabs
245+
npm="npm run tauri dev"
246+
yarn="yarn tauri dev"
247+
pnpm="pnpm tauri dev"
248+
deno="deno task tauri dev"
249+
bun="bun tauri dev"
250+
cargo="cargo tauri dev"
251+
/>
252+
253+
Open the DevTools with F12 (or `Cmd+Option+I` on macOS) and you should see the output of the sidecar command.
254+
255+
If you find any issues, please open an issue on [GitHub](https://github.com/tauri-apps/tauri-docs).
256+
182257
</Steps>
183258

184259
[sidecar guide]: /develop/sidecar/

src/content/docs/learn/splashscreen.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ If you are not an advanced user it's **highly recommended** that you use the opt
3333
- Choose your package manager: `pnpm`
3434
- Choose your UI template: `Vanilla`
3535
- Choose your UI flavor: `Typescript`
36-
- Would you like to setup the project for mobile as well? `yes`
36+
3737
:::
3838

3939
## Steps

src/content/docs/zh-cn/learn/sidecar-nodejs.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import CTA from '@fragments/cta.mdx';
3434
- Choose your package manager: `pnpm`
3535
- Choose your UI template: `Vanilla`
3636
- Choose your UI flavor: `Typescript`
37-
- Would you like to setup the project for mobile as well? `yes`
3837

3938
:::
4039

src/content/docs/zh-cn/learn/splashscreen.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import CTA from '@fragments/cta.mdx';
3232
- Choose your package manager: `pnpm`
3333
- Choose your UI template: `Vanilla`
3434
- Choose your UI flavor: `Typescript`
35-
- Would you like to setup the project for mobile as well? `yes`
35+
3636
:::
3737

3838
## 步骤

0 commit comments

Comments
 (0)