Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(os): refactor EOL const #427

Merged
merged 10 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/os-plugin-refactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ The os plugin is recieving a few changes to improve consistency and add new feat
- Added `family()`,`exe_extension()`, and `hostname()` functions and their equivalents for JS.
- 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.
- Modified `platform()` implementation to return `windows` instead of `win32` and `macos` instead of `darwin` to align with Rust's `std::env::consts::OS`
- `EOL` const in JS has been modified into a function `eol()` fix import issues in frameworks like `next.js`
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target
node_modules
dist
dist-js
api-iife.js
api-iife.js
init.js
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ thiserror = { workspace = true }
os_info = "3"
sys-locale = "0.3"
gethostname = "0.4"
serialize-to-javascript = "=0.1.1"
15 changes: 8 additions & 7 deletions plugins/os/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
__TAURI__: {
os: { __eol: string };
};
}
}

Expand Down Expand Up @@ -41,18 +44,16 @@ type Arch =
| "s390x"
| "sparc64";

function isWindows(): boolean {
return navigator.appVersion.includes("Win");
}

/**
* The operating system-specific end-of-line marker.
* Returns the operating system-specific end-of-line marker.
* - `\n` on POSIX
* - `\r\n` on Windows
*
* @since 2.0.0
* */
const EOL = isWindows() ? "\r\n" : "\n";
function eol() {
return window.__TAURI__.os.__eol;
}

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

export {
EOL,
eol,
platform,
family,
version,
Expand Down
2 changes: 1 addition & 1 deletion plugins/os/src/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions plugins/os/src/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

__RAW_global_os_api__;

// eslint-disable-next-line
window.__TAURI__.os.__eol = __TEMPLATE_eol__;
22 changes: 21 additions & 1 deletion plugins/os/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::fmt::Display;

pub use os_info::Version;
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
use tauri::{
plugin::{Builder, TauriPlugin},
Runtime,
Expand Down Expand Up @@ -90,9 +91,28 @@ pub fn hostname() -> String {
gethostname::gethostname().to_string_lossy().to_string()
}

#[derive(Template)]
#[default_template("./init.js")]
struct InitJavascript {
#[raw]
global_os_api: &'static str,
eol: &'static str,
}

pub fn init<R: Runtime>() -> TauriPlugin<R> {
let init_js = InitJavascript {
global_os_api: include_str!("api-iife.js"),
#[cfg(windows)]
eol: "\r\n",
#[cfg(not(windows))]
eol: "\n",
}
.render_default(&Default::default())
// this will never fail with the above global_os_api eol values
.unwrap();

Builder::new("os")
.js_init_script(include_str!("api-iife.js").to_string())
.js_init_script(init_js.to_string())
.invoke_handler(tauri::generate_handler![
commands::platform,
commands::version,
Expand Down