os.js 3.7 KB

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