Skip to content

Commit db7f4f9

Browse files
refactor(os): refactor EOL const (#427)
Co-authored-by: Lucas Nogueira <[email protected]>
1 parent c8c3191 commit db7f4f9

File tree

8 files changed

+43
-10
lines changed

8 files changed

+43
-10
lines changed

.changes/os-plugin-refactor.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ The os plugin is recieving a few changes to improve consistency and add new feat
99
- Added `family()`,`exe_extension()`, and `hostname()` functions and their equivalents for JS.
1010
- Removed `tempdir()` function and its equivalent on JS, use `std::env::temp_dir` instead of `temp_dir` from `tauri::path::PathResolver::temp_dir` and `path.tempDir` on JS.
1111
- Modified `platform()` implementation to return `windows` instead of `win32` and `macos` instead of `darwin` to align with Rust's `std::env::consts::OS`
12+
- `EOL` const in JS has been modified into a function `eol()` fix import issues in frameworks like `next.js`

.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ target
22
node_modules
33
dist
44
dist-js
5-
api-iife.js
5+
api-iife.js
6+
init.js

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/os/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ thiserror = { workspace = true }
1515
os_info = "3"
1616
sys-locale = "0.3"
1717
gethostname = "0.4"
18+
serialize-to-javascript = "=0.1.1"

plugins/os/guest-js/index.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
declare global {
1212
interface Window {
1313
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
14+
__TAURI__: {
15+
os: { __eol: string };
16+
};
1417
}
1518
}
1619

@@ -41,18 +44,16 @@ type Arch =
4144
| "s390x"
4245
| "sparc64";
4346

44-
function isWindows(): boolean {
45-
return navigator.appVersion.includes("Win");
46-
}
47-
4847
/**
49-
* The operating system-specific end-of-line marker.
48+
* Returns the operating system-specific end-of-line marker.
5049
* - `\n` on POSIX
5150
* - `\r\n` on Windows
5251
*
5352
* @since 2.0.0
5453
* */
55-
const EOL = isWindows() ? "\r\n" : "\n";
54+
function eol() {
55+
return window.__TAURI__.os.__eol;
56+
}
5657

5758
/**
5859
* Returns a string describing the specific operating system in use.
@@ -174,7 +175,7 @@ async function hostname(): Promise<string | null> {
174175
}
175176

176177
export {
177-
EOL,
178+
eol,
178179
platform,
179180
family,
180181
version,

plugins/os/src/api-iife.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/os/src/init.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
__RAW_global_os_api__;
6+
7+
// eslint-disable-next-line
8+
window.__TAURI__.os.__eol = __TEMPLATE_eol__;

plugins/os/src/lib.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::fmt::Display;
66

77
pub use os_info::Version;
8+
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
89
use tauri::{
910
plugin::{Builder, TauriPlugin},
1011
Runtime,
@@ -90,9 +91,28 @@ pub fn hostname() -> String {
9091
gethostname::gethostname().to_string_lossy().to_string()
9192
}
9293

94+
#[derive(Template)]
95+
#[default_template("./init.js")]
96+
struct InitJavascript {
97+
#[raw]
98+
global_os_api: &'static str,
99+
eol: &'static str,
100+
}
101+
93102
pub fn init<R: Runtime>() -> TauriPlugin<R> {
103+
let init_js = InitJavascript {
104+
global_os_api: include_str!("api-iife.js"),
105+
#[cfg(windows)]
106+
eol: "\r\n",
107+
#[cfg(not(windows))]
108+
eol: "\n",
109+
}
110+
.render_default(&Default::default())
111+
// this will never fail with the above global_os_api eol values
112+
.unwrap();
113+
94114
Builder::new("os")
95-
.js_init_script(include_str!("api-iife.js").to_string())
115+
.js_init_script(init_js.to_string())
96116
.invoke_handler(tauri::generate_handler![
97117
commands::platform,
98118
commands::version,

0 commit comments

Comments
 (0)