Skip to content

Commit

Permalink
feat(api): Expose application metadata and functions to JS api - fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
lemarier committed Apr 8, 2021
1 parent 8a69792 commit e511d39
Show file tree
Hide file tree
Showing 16 changed files with 6,306 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .changes/js-app-metadata.md
@@ -0,0 +1,6 @@
---
"tauri-api": minor
"tauri": minor
---

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.
1 change: 1 addition & 0 deletions api/package.json
Expand Up @@ -5,6 +5,7 @@
"main": "./dist/index.js",
"exports": {
".": "./dist/index.js",
"./app": "./dist/app.js",
"./cli": "./dist/cli.js",
"./dialog": "./dist/dialog.js",
"./event": "./dist/event.js",
Expand Down
1 change: 1 addition & 0 deletions api/rollup.config.js
Expand Up @@ -10,6 +10,7 @@ import pkg from './package.json'
export default [
{
input: {
app: './src/app.ts',
fs: './src/fs.ts',
path: './src/path.ts',
dialog: './src/dialog.ts',
Expand Down
80 changes: 80 additions & 0 deletions api/src/app.ts
@@ -0,0 +1,80 @@
import { invokeTauriCommand } from './helpers/tauri'

/**
* @name getVersion
* @description Get application version
* @returns {Promise<string>} Promise resolving to application version
*/
async function getVersion(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'App',
mainThread: true,
message: {
cmd: 'getAppVersion'
}
})
}

/**
* @name getName
* @description Get application name
* @returns {Promise<string>} Promise resolving to application name
*/
async function getName(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'App',
mainThread: true,
message: {
cmd: 'getAppName'
}
})
}

/**
* @name getTauriVersion
* @description Get tauri version
* @returns {Promise<string>} Promise resolving to tauri version
*/
async function getTauriVersion(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'App',
mainThread: true,
message: {
cmd: 'getTauriVersion'
}
})
}

/**
* @name exit
* @description Exits immediately with exitCode.
* @param [exitCode] defaults to 0.
* @returns {Promise<void>} Application is closing, nothing is returned
*/
async function exit(exitCode: Number = 0): Promise<void> {
return invokeTauriCommand<string>({
__tauriModule: 'App',
mainThread: true,
message: {
cmd: 'exit',
exitCode
}
})
}

/**
* @name relaunch
* @description Relaunches the app when current instance exits.
* @returns {Promise<void>} Application is restarting, nothing is returned
*/
async function relaunch(): Promise<void> {
return invokeTauriCommand<string>({
__tauriModule: 'App',
mainThread: true,
message: {
cmd: 'relaunch'
}
})
}

export { getName, getVersion, getTauriVersion, relaunch, exit }
2 changes: 2 additions & 0 deletions api/src/bundle.ts
@@ -1,4 +1,5 @@
import 'regenerator-runtime/runtime'
import * as app from './app'
import * as cli from './cli'
import * as dialog from './dialog'
import * as event from './event'
Expand All @@ -13,6 +14,7 @@ import * as notification from './notification'
import * as globalShortcut from './globalShortcut'

export {
app,
cli,
dialog,
event,
Expand Down

0 comments on commit e511d39

Please sign in to comment.