os.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 `tauri.conf.json > build > withGlobalTauri` is set to true.
  8. *
  9. * The APIs must be allowlisted on `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 { LiteralUnion } from 'type-fest'
  25. import { isWindows } from './helpers/os-check'
  26. import { invokeTauriCommand } from './helpers/tauri'
  27. /**
  28. * The operating system-specific end-of-line marker.
  29. * - `\n` on POSIX
  30. * - `\r\n` on Windows
  31. * */
  32. const EOL = isWindows() ? '\r\n' : '\n'
  33. /**
  34. * Returns a string identifying the operating system platform.
  35. * The value is set at compile time. Possible values are `'linux'`, `'darwin'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'win32'`
  36. * @example
  37. * ```typescript
  38. * import { platform } from '@tauri-apps/api/os';
  39. * const platformName = await platform();
  40. * ```
  41. */
  42. async function platform(): Promise<
  43. LiteralUnion<
  44. | 'linux'
  45. | 'darwin'
  46. | 'ios'
  47. | 'freebsd'
  48. | 'dragonfly'
  49. | 'netbsd'
  50. | 'openbsd'
  51. | 'solaris'
  52. | 'android'
  53. | 'win32',
  54. string
  55. >
  56. > {
  57. return invokeTauriCommand<string>({
  58. __tauriModule: 'Os',
  59. message: {
  60. cmd: 'platform'
  61. }
  62. })
  63. }
  64. /**
  65. * Returns a string identifying the kernel version.
  66. * @example
  67. * ```typescript
  68. * import { version } from '@tauri-apps/api/os';
  69. * const osVersion = await version();
  70. * ```
  71. */
  72. async function version(): Promise<string> {
  73. return invokeTauriCommand<string>({
  74. __tauriModule: 'Os',
  75. message: {
  76. cmd: 'version'
  77. }
  78. })
  79. }
  80. /**
  81. * Returns `'Linux'` on Linux, `'Darwin'` on macOS, and `'Windows_NT'` on Windows.
  82. * @example
  83. * ```typescript
  84. * import { type } from '@tauri-apps/api/os';
  85. * const osType = await type();
  86. * ```
  87. */
  88. async function type(): Promise<
  89. LiteralUnion<'Linux' | 'Darwin' | 'Windows_NT', string>
  90. > {
  91. return invokeTauriCommand<string>({
  92. __tauriModule: 'Os',
  93. message: {
  94. cmd: 'osType'
  95. }
  96. })
  97. }
  98. /**
  99. * Returns the operating system CPU architecture for which the tauri app was compiled.
  100. * Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`.
  101. * @example
  102. * ```typescript
  103. * import { arch } from '@tauri-apps/api/os';
  104. * const archName = await arch();
  105. * ```
  106. */
  107. async function arch(): Promise<
  108. LiteralUnion<
  109. | 'x86'
  110. | 'x86_64'
  111. | 'arm'
  112. | 'aarch64'
  113. | 'mips'
  114. | 'mips64'
  115. | 'powerpc'
  116. | 'powerpc64'
  117. | 'riscv64'
  118. | 's390x'
  119. | 'sparc64',
  120. string
  121. >
  122. > {
  123. return invokeTauriCommand<string>({
  124. __tauriModule: 'Os',
  125. message: {
  126. cmd: 'arch'
  127. }
  128. })
  129. }
  130. /**
  131. * Returns the operating system's default directory for temporary files as a string.
  132. * @example
  133. * ```typescript
  134. * import { tempdir } from '@tauri-apps/api/os';
  135. * const tempdirPath = await tempdir();
  136. * ```
  137. */
  138. async function tempdir(): Promise<string> {
  139. return invokeTauriCommand<string>({
  140. __tauriModule: 'Os',
  141. message: {
  142. cmd: 'tempdir'
  143. }
  144. })
  145. }
  146. export { EOL, platform, version, type, arch, tempdir }