os.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. */
  37. async function platform(): Promise<
  38. LiteralUnion<
  39. | 'linux'
  40. | 'darwin'
  41. | 'ios'
  42. | 'freebsd'
  43. | 'dragonfly'
  44. | 'netbsd'
  45. | 'openbsd'
  46. | 'solaris'
  47. | 'android'
  48. | 'win32',
  49. string
  50. >
  51. > {
  52. return invokeTauriCommand<string>({
  53. __tauriModule: 'Os',
  54. message: {
  55. cmd: 'platform'
  56. }
  57. })
  58. }
  59. /**
  60. * Returns a string identifying the kernel version.
  61. */
  62. async function version(): Promise<string> {
  63. return invokeTauriCommand<string>({
  64. __tauriModule: 'Os',
  65. message: {
  66. cmd: 'version'
  67. }
  68. })
  69. }
  70. /**
  71. * Returns `'Linux'` on Linux, `'Darwin'` on macOS, and `'Windows_NT'` on Windows.
  72. */
  73. async function type(): Promise<
  74. LiteralUnion<'Linux' | 'Darwin' | 'Windows_NT', string>
  75. > {
  76. return invokeTauriCommand<string>({
  77. __tauriModule: 'Os',
  78. message: {
  79. cmd: 'osType'
  80. }
  81. })
  82. }
  83. /**
  84. * Returns the operating system CPU architecture for which the tauri app was compiled. Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`
  85. */
  86. async function arch(): Promise<
  87. LiteralUnion<
  88. | 'x86'
  89. | 'x86_64'
  90. | 'arm'
  91. | 'aarch64'
  92. | 'mips'
  93. | 'mips64'
  94. | 'powerpc'
  95. | 'powerpc64'
  96. | 'riscv64'
  97. | 's390x'
  98. | 'sparc64',
  99. string
  100. >
  101. > {
  102. return invokeTauriCommand<string>({
  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. */
  112. async function tempdir(): Promise<string> {
  113. return invokeTauriCommand<string>({
  114. __tauriModule: 'Os',
  115. message: {
  116. cmd: 'tempdir'
  117. }
  118. })
  119. }
  120. export { EOL, platform, version, type, arch, tempdir }