refactor(os): refactor EOL const (#427)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
pull/407/head^2
Amr Bashir 2 years ago committed by GitHub
parent c8c3191565
commit db7f4f9a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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`

@ -2,4 +2,5 @@ target
node_modules
dist
dist-js
api-iife.js
api-iife.js
init.js

1
Cargo.lock generated

@ -5298,6 +5298,7 @@ dependencies = [
"os_info",
"serde",
"serde_json",
"serialize-to-javascript",
"sys-locale",
"tauri",
"thiserror",

@ -15,3 +15,4 @@ thiserror = { workspace = true }
os_info = "3"
sys-locale = "0.3"
gethostname = "0.4"
serialize-to-javascript = "=0.1.1"

@ -11,6 +11,9 @@
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
__TAURI__: {
os: { __eol: string };
};
}
}
@ -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.
@ -174,7 +175,7 @@ async function hostname(): Promise<string | null> {
}
export {
EOL,
eol,
platform,
family,
version,

@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})}
if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";return n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.eol=function(){return window.__TAURI__.os.__eol},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})}

@ -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__;

@ -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,
@ -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,

Loading…
Cancel
Save