os.cjs 3.9 KB

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