Skip to content

Commit e511d39

Browse files
authored
feat(api): Expose application metadata and functions to JS api - fix #1387 (#1445)
1 parent 8a69792 commit e511d39

File tree

16 files changed

+6306
-54
lines changed

16 files changed

+6306
-54
lines changed

.changes/js-app-metadata.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-api": minor
3+
"tauri": minor
4+
---
5+
6+
Added new Javascript API to extract `name`, `version`, `tauri version` from the running application. We exposed `relaunch` and `exit` as well to control your application state.

api/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "./dist/index.js",
66
"exports": {
77
".": "./dist/index.js",
8+
"./app": "./dist/app.js",
89
"./cli": "./dist/cli.js",
910
"./dialog": "./dist/dialog.js",
1011
"./event": "./dist/event.js",

api/rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import pkg from './package.json'
1010
export default [
1111
{
1212
input: {
13+
app: './src/app.ts',
1314
fs: './src/fs.ts',
1415
path: './src/path.ts',
1516
dialog: './src/dialog.ts',

api/src/app.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { invokeTauriCommand } from './helpers/tauri'
2+
3+
/**
4+
* @name getVersion
5+
* @description Get application version
6+
* @returns {Promise<string>} Promise resolving to application version
7+
*/
8+
async function getVersion(): Promise<string> {
9+
return invokeTauriCommand<string>({
10+
__tauriModule: 'App',
11+
mainThread: true,
12+
message: {
13+
cmd: 'getAppVersion'
14+
}
15+
})
16+
}
17+
18+
/**
19+
* @name getName
20+
* @description Get application name
21+
* @returns {Promise<string>} Promise resolving to application name
22+
*/
23+
async function getName(): Promise<string> {
24+
return invokeTauriCommand<string>({
25+
__tauriModule: 'App',
26+
mainThread: true,
27+
message: {
28+
cmd: 'getAppName'
29+
}
30+
})
31+
}
32+
33+
/**
34+
* @name getTauriVersion
35+
* @description Get tauri version
36+
* @returns {Promise<string>} Promise resolving to tauri version
37+
*/
38+
async function getTauriVersion(): Promise<string> {
39+
return invokeTauriCommand<string>({
40+
__tauriModule: 'App',
41+
mainThread: true,
42+
message: {
43+
cmd: 'getTauriVersion'
44+
}
45+
})
46+
}
47+
48+
/**
49+
* @name exit
50+
* @description Exits immediately with exitCode.
51+
* @param [exitCode] defaults to 0.
52+
* @returns {Promise<void>} Application is closing, nothing is returned
53+
*/
54+
async function exit(exitCode: Number = 0): Promise<void> {
55+
return invokeTauriCommand<string>({
56+
__tauriModule: 'App',
57+
mainThread: true,
58+
message: {
59+
cmd: 'exit',
60+
exitCode
61+
}
62+
})
63+
}
64+
65+
/**
66+
* @name relaunch
67+
* @description Relaunches the app when current instance exits.
68+
* @returns {Promise<void>} Application is restarting, nothing is returned
69+
*/
70+
async function relaunch(): Promise<void> {
71+
return invokeTauriCommand<string>({
72+
__tauriModule: 'App',
73+
mainThread: true,
74+
message: {
75+
cmd: 'relaunch'
76+
}
77+
})
78+
}
79+
80+
export { getName, getVersion, getTauriVersion, relaunch, exit }

api/src/bundle.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'regenerator-runtime/runtime'
2+
import * as app from './app'
23
import * as cli from './cli'
34
import * as dialog from './dialog'
45
import * as event from './event'
@@ -13,6 +14,7 @@ import * as notification from './notification'
1314
import * as globalShortcut from './globalShortcut'
1415

1516
export {
17+
app,
1618
cli,
1719
dialog,
1820
event,

0 commit comments

Comments
 (0)