os.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2019-2021 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. const EOL = isWindows() ? '\r\n' : '\n'
  56. /**
  57. * Returns a string identifying the operating system platform.
  58. * The value is set at compile time. Possible values are `'linux'`, `'darwin'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'win32'`
  59. * @example
  60. * ```typescript
  61. * import { platform } from '@tauri-apps/api/os';
  62. * const platformName = await platform();
  63. * ```
  64. */
  65. async function platform(): Promise<Platform> {
  66. return invokeTauriCommand<Platform>({
  67. __tauriModule: 'Os',
  68. message: {
  69. cmd: 'platform'
  70. }
  71. })
  72. }
  73. /**
  74. * Returns a string identifying the kernel version.
  75. * @example
  76. * ```typescript
  77. * import { version } from '@tauri-apps/api/os';
  78. * const osVersion = await version();
  79. * ```
  80. */
  81. async function version(): Promise<string> {
  82. return invokeTauriCommand<string>({
  83. __tauriModule: 'Os',
  84. message: {
  85. cmd: 'version'
  86. }
  87. })
  88. }
  89. /**
  90. * Returns `'Linux'` on Linux, `'Darwin'` on macOS, and `'Windows_NT'` on Windows.
  91. * @example
  92. * ```typescript
  93. * import { type } from '@tauri-apps/api/os';
  94. * const osType = await type();
  95. * ```
  96. */
  97. async function type(): Promise<OsType> {
  98. return invokeTauriCommand<OsType>({
  99. __tauriModule: 'Os',
  100. message: {
  101. cmd: 'osType'
  102. }
  103. })
  104. }
  105. /**
  106. * Returns the operating system CPU architecture for which the tauri app was compiled.
  107. * Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`.
  108. * @example
  109. * ```typescript
  110. * import { arch } from '@tauri-apps/api/os';
  111. * const archName = await arch();
  112. * ```
  113. */
  114. async function arch(): Promise<Arch> {
  115. return invokeTauriCommand<Arch>({
  116. __tauriModule: 'Os',
  117. message: {
  118. cmd: 'arch'
  119. }
  120. })
  121. }
  122. /**
  123. * Returns the operating system's default directory for temporary files as a string.
  124. * @example
  125. * ```typescript
  126. * import { tempdir } from '@tauri-apps/api/os';
  127. * const tempdirPath = await tempdir();
  128. * ```
  129. */
  130. async function tempdir(): Promise<string> {
  131. return invokeTauriCommand<string>({
  132. __tauriModule: 'Os',
  133. message: {
  134. cmd: 'tempdir'
  135. }
  136. })
  137. }
  138. export { EOL, platform, version, type, arch, tempdir }
  139. export type { Platform, OsType, Arch }