os.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * Provides operating system-related utility methods and properties.
  6. *
  7. * This package is also accessible with `window.__TAURI__.os` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  8. *
  9. * The APIs must be added to [`tauri.allowlist.os`](https://tauri.app/v1/api/config/#allowlistconfig.os) in `tauri.conf.json`:
  10. * ```json
  11. * {
  12. * "tauri": {
  13. * "allowlist": {
  14. * "os": {
  15. * "all": true, // enable all Os APIs
  16. * }
  17. * }
  18. * }
  19. * }
  20. * ```
  21. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  22. * @module
  23. */
  24. import { isWindows } from './helpers/os-check'
  25. import { invokeTauriCommand } from './helpers/tauri'
  26. type Platform =
  27. | 'linux'
  28. | 'darwin'
  29. | 'ios'
  30. | 'freebsd'
  31. | 'dragonfly'
  32. | 'netbsd'
  33. | 'openbsd'
  34. | 'solaris'
  35. | 'android'
  36. | 'win32'
  37. type OsType = 'Linux' | 'Darwin' | 'Windows_NT'
  38. type Arch =
  39. | 'x86'
  40. | 'x86_64'
  41. | 'arm'
  42. | 'aarch64'
  43. | 'mips'
  44. | 'mips64'
  45. | 'powerpc'
  46. | 'powerpc64'
  47. | 'riscv64'
  48. | 's390x'
  49. | 'sparc64'
  50. /**
  51. * The operating system-specific end-of-line marker.
  52. * - `\n` on POSIX
  53. * - `\r\n` on Windows
  54. *
  55. * @since 1.0.0
  56. * */
  57. const EOL = isWindows() ? '\r\n' : '\n'
  58. /**
  59. * Returns a string identifying the operating system platform.
  60. * The value is set at compile time. Possible values are `'linux'`, `'darwin'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'win32'`
  61. * @example
  62. * ```typescript
  63. * import { platform } from '@tauri-apps/api/os';
  64. * const platformName = await platform();
  65. * ```
  66. *
  67. * @since 1.0.0
  68. *
  69. */
  70. async function platform(): Promise<Platform> {
  71. return invokeTauriCommand<Platform>({
  72. __tauriModule: 'Os',
  73. message: {
  74. cmd: 'platform'
  75. }
  76. })
  77. }
  78. /**
  79. * Returns a string identifying the kernel version.
  80. * @example
  81. * ```typescript
  82. * import { version } from '@tauri-apps/api/os';
  83. * const osVersion = await version();
  84. * ```
  85. *
  86. * @since 1.0.0
  87. */
  88. async function version(): Promise<string> {
  89. return invokeTauriCommand<string>({
  90. __tauriModule: 'Os',
  91. message: {
  92. cmd: 'version'
  93. }
  94. })
  95. }
  96. /**
  97. * Returns `'Linux'` on Linux, `'Darwin'` on macOS, and `'Windows_NT'` on Windows.
  98. * @example
  99. * ```typescript
  100. * import { type } from '@tauri-apps/api/os';
  101. * const osType = await type();
  102. * ```
  103. *
  104. * @since 1.0.0
  105. */
  106. async function type(): Promise<OsType> {
  107. return invokeTauriCommand<OsType>({
  108. __tauriModule: 'Os',
  109. message: {
  110. cmd: 'osType'
  111. }
  112. })
  113. }
  114. /**
  115. * Returns the operating system CPU architecture for which the tauri app was compiled.
  116. * Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`.
  117. * @example
  118. * ```typescript
  119. * import { arch } from '@tauri-apps/api/os';
  120. * const archName = await arch();
  121. * ```
  122. *
  123. * @since 1.0.0
  124. */
  125. async function arch(): Promise<Arch> {
  126. return invokeTauriCommand<Arch>({
  127. __tauriModule: 'Os',
  128. message: {
  129. cmd: 'arch'
  130. }
  131. })
  132. }
  133. /**
  134. * Returns the operating system's default directory for temporary files as a string.
  135. * @example
  136. * ```typescript
  137. * import { tempdir } from '@tauri-apps/api/os';
  138. * const tempdirPath = await tempdir();
  139. * ```
  140. *
  141. * @since 1.0.0
  142. */
  143. async function tempdir(): Promise<string> {
  144. return invokeTauriCommand<string>({
  145. __tauriModule: 'Os',
  146. message: {
  147. cmd: 'tempdir'
  148. }
  149. })
  150. }
  151. /**
  152. * Returns a String with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `null` is returned instead.
  153. * @example
  154. * ```typescript
  155. * import { locale } from '@tauri-apps/api/os';
  156. * const locale = await locale();
  157. * if (locale) {
  158. * // use the locale string here
  159. * }
  160. * ```
  161. *
  162. * @since 1.4.0
  163. */
  164. async function locale(): Promise<string | null> {
  165. return invokeTauriCommand<string>({
  166. __tauriModule: 'Os',
  167. message: {
  168. cmd: 'locale'
  169. }
  170. })
  171. }
  172. export { EOL, platform, version, type, arch, tempdir, locale }
  173. export type { Platform, OsType, Arch }