fix(core/path): change sep and delimiter to functions (#7160)

* fix(core/path): change `sep` and `delimiter` to functions

* fix impl

* semicolons

* return types

* generated

* fix init js

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2023-06-08 03:40:14 +03:00 committed by GitHub
parent bfaf624a4c
commit 6d3f3138b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 220 additions and 294 deletions

View File

@ -0,0 +1,5 @@
---
'@tauri-apps/api': 'minor:enhance'
---
Changed `sep` and `delimiter` from `path` module into functions to fix import in frameworks like `next.js`

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,10 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
if (!('path' in window.__TAURI__)) {
window.__TAURI__.path = {}
}
window.__TAURI__.path.__sep = __TEMPLATE_sep__
window.__TAURI__.path.__delimiter = __TEMPLATE_delimiter__

View File

@ -11,6 +11,7 @@ use crate::{
use serde::{de::Error as DeError, Deserialize, Deserializer};
use serde_repr::{Deserialize_repr, Serialize_repr};
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
#[cfg(any(path_all, test))]
mod commands;
@ -331,6 +332,13 @@ fn resolve_path<R: Runtime>(
Ok(base_dir_path)
}
#[derive(Template)]
#[default_template("./init.js")]
struct InitJavascript {
sep: &'static str,
delimiter: &'static str,
}
/// Initializes the plugin.
pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
#[allow(unused_mut)]
@ -350,7 +358,18 @@ pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
]);
}
#[cfg(windows)]
let (sep, delimiter) = ("\\", ";");
#[cfg(not(windows))]
let (sep, delimiter) = ("/", ":");
let init_js = InitJavascript { sep, delimiter }
.render_default(&Default::default())
// this will never fail with the above sep and delimiter values
.unwrap();
builder
.js_init_script(init_js.to_string())
.setup(|app, _api| {
#[cfg(target_os = "android")]
{

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -1,19 +0,0 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/** @ignore */
function isLinux(): boolean {
return navigator.appVersion.includes('Linux')
}
function isWindows(): boolean {
return navigator.appVersion.includes('Win')
}
function isMacOS(): boolean {
return navigator.appVersion.includes('Mac')
}
export { isLinux, isWindows, isMacOS }

View File

@ -12,7 +12,6 @@
*/
import { invoke } from './tauri'
import { isWindows } from './helpers/os-check'
/**
* @since 2.0.0
@ -44,6 +43,17 @@ enum BaseDirectory {
Template
}
declare global {
interface Window {
__TAURI__: {
path: {
__sep: string
__delimiter: string
}
}
}
}
/**
* Returns the path to the suggested directory for your app's config files.
* Resolves to `${configDir}/${bundleIdentifier}`, where `bundleIdentifier` is the value [`tauri.bundle.identifier`](https://tauri.app/v1/api/config/#bundleconfig.identifier) is configured in `tauri.conf.json`.
@ -541,23 +551,26 @@ async function tempDir(path: string): Promise<string> {
}
/**
* Provides the platform-specific path segment separator:
* Returns the platform-specific path segment separator:
* - `\` on Windows
* - `/` on POSIX
*
* @since 1.0.0
* @since 2.0.0
*/
const sep = isWindows() ? '\\' : '/'
function sep(): string {
return window.__TAURI__.path.__sep
}
/**
* Provides the platform-specific path segment delimiter:
* Returns the platform-specific path segment delimiter:
* - `;` on Windows
* - `:` on POSIX
*
* @since 1.0.0
* @since 2.0.0
*/
const delimiter = isWindows() ? ';' : ':'
function delimiter(): string {
return window.__TAURI__.path.__delimiter
}
/**
* Resolves a sequence of `paths` or `path` segments into an absolute path.
* @example