Skip to content

Commit

Permalink
fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Apr 24, 2023
1 parent 3663b46 commit e7bbbbf
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ javascript = []
typescript = ["specta/typescript"]

[dependencies]
specta = { version = "1.0.0", features = ["functions", "tauri"] }
specta = { version = "1.0.3", features = ["functions", "tauri"] }
serde = "1.0.152"
serde_json = "1.0.93"
tauri = "1.2.4"
Expand Down
2 changes: 1 addition & 1 deletion example/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tauri-build = { version = "1.2.1", features = [] }

[dependencies]
serde_json = "1.0"
specta = "1.0.0"
specta = "1.0.3"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2.4", features = ["api-all"] }
tauri-specta = { path = "../../", features = ["typescript", "javascript"] }
Expand Down
9 changes: 9 additions & 0 deletions example/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ fn main() {
)
.unwrap();

// This is useful for custom eslint, prettier overrides at the top of the file.
// ts::export_with_cfg_with_header(
// collect_types![hello_world, goodbye_world, nested::some_struct].unwrap(),
// Default::default(),
// "../src/bindings2.ts",
// "// My custom header\n".into(),
// )
// .unwrap();

tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
hello_world,
Expand Down
33 changes: 28 additions & 5 deletions src/js.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use specta::{functions::FunctionDataType, ts::TsExportError, ExportError, TypeDefs};
use std::{
borrow::Cow,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};

use crate::CRINGE_ESLINT_DISABLE;

/// Building blocks for [`export`] and [`export_with_cfg`].
///
/// These are made available for advanced use cases where you may combine Tauri Specta with another
Expand All @@ -13,14 +16,12 @@ pub mod internal {
use heck::ToLowerCamelCase;
use indoc::formatdoc;

use crate::DO_NOT_EDIT;
use specta::{
functions::FunctionDataType,
ts::{self, TsExportError},
};

const DO_NOT_EDIT: &str = "/* eslint-disable */
// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.";

/// Constants that the generated functions rely on
pub fn globals() -> String {
formatdoc! {
Expand Down Expand Up @@ -113,9 +114,26 @@ pub mod internal {
/// Exports the output of [`internal::render`] for a collection of [`FunctionDataType`] into a JavaScript file.
/// Allows for specifying a custom [`ExportConfiguration`](specta::ts::ExportConfiguration).
pub fn export_with_cfg(
result: (Vec<FunctionDataType>, TypeDefs),
export_path: impl AsRef<Path>,
cfg: specta::ts::ExportConfiguration,
) -> Result<(), TsExportError> {
export_with_cfg_with_header(
result,
export_path,
cfg,
Cow::Borrowed(CRINGE_ESLINT_DISABLE), // TODO: Remove this as a default. SemVer moment.
)
}

// TODO: On next major release merge this with `export_with_cfg`
/// Exports the output of [`internal::render`] for a collection of [`FunctionDataType`] into a JavaScript file.
/// Allows for specifying a custom [`ExportConfiguration`](specta::ts::ExportConfiguration).
pub fn export_with_cfg_with_header(
(function_types, _): (Vec<FunctionDataType>, TypeDefs),
export_path: impl AsRef<Path>,
cfg: specta::ts::ExportConfiguration,
header: Cow<'static, str>,
) -> Result<(), TsExportError> {
let export_path = PathBuf::from(export_path.as_ref());

Expand All @@ -125,7 +143,7 @@ pub fn export_with_cfg(

let mut file = File::create(export_path)?;

write!(file, "{}", internal::render(function_types, &cfg)?)?;
write!(file, "{header}{}", internal::render(function_types, &cfg)?)?;

Ok(())
}
Expand All @@ -135,5 +153,10 @@ pub fn export(
macro_data: Result<(Vec<FunctionDataType>, TypeDefs), ExportError>,
export_path: impl AsRef<Path>,
) -> Result<(), TsExportError> {
export_with_cfg(macro_data?, export_path, Default::default())
export_with_cfg_with_header(
macro_data?,
export_path,
Default::default(),
Cow::Borrowed(CRINGE_ESLINT_DISABLE), // TODO: Remove this as a default. SemVer moment.
)
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ macro_rules! collate_types {
}};
}

pub(crate) const DO_NOT_EDIT: &str = "// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.";

pub(crate) const CRINGE_ESLINT_DISABLE: &str = "/* eslint-disable */
";

// TODO
// #[cfg(doctest)]
// doc_comment::doctest!("../README.md");
32 changes: 27 additions & 5 deletions src/ts.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::{
borrow::Cow,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};

use specta::{functions::FunctionDataType, ts::TsExportError, ExportError, TypeDefs};

use crate::CRINGE_ESLINT_DISABLE;

/// Building blocks for [`export`] and [`export_with_cfg`].
///
/// These are made available for advanced use cases where you may combine Tauri Specta with another
Expand All @@ -14,15 +17,13 @@ pub mod internal {
use heck::ToLowerCamelCase;
use indoc::formatdoc;

use crate::DO_NOT_EDIT;
use specta::{
functions::FunctionDataType,
ts::{self, TsExportError},
TypeDefs,
};

const DO_NOT_EDIT: &str = "/* eslint-disable */
// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.";

/// Type definitions and constants that the generated functions rely on
pub fn globals() -> String {
formatdoc! {
Expand Down Expand Up @@ -118,9 +119,25 @@ pub mod internal {
/// Exports the output of [`internal::render`] for a collection of [`FunctionDataType`] into a TypeScript file.
/// Allows for specifying a custom [`ExportConfiguration`](specta::ts::ExportConfiguration).
pub fn export_with_cfg(
result: (Vec<FunctionDataType>, TypeDefs),
cfg: specta::ts::ExportConfiguration,
export_path: impl AsRef<Path>,
) -> Result<(), TsExportError> {
export_with_cfg_with_header(
result,
cfg,
export_path,
Cow::Borrowed(CRINGE_ESLINT_DISABLE), // TODO: Remove this as a default. SemVer moment.
)
}

/// Exports the output of [`internal::render`] for a collection of [`FunctionDataType`] into a TypeScript file.
/// Allows for specifying a custom [`ExportConfiguration`](specta::ts::ExportConfiguration).
pub fn export_with_cfg_with_header(
(function_types, type_map): (Vec<FunctionDataType>, TypeDefs),
cfg: specta::ts::ExportConfiguration,
export_path: impl AsRef<Path>,
header: Cow<'static, str>,
) -> Result<(), TsExportError> {
let export_path = PathBuf::from(export_path.as_ref());

Expand All @@ -132,7 +149,7 @@ pub fn export_with_cfg(

write!(
file,
"{}",
"{header}{}",
internal::render(function_types, type_map, &cfg)?
)?;

Expand All @@ -144,5 +161,10 @@ pub fn export(
macro_data: Result<(Vec<FunctionDataType>, TypeDefs), ExportError>,
export_path: impl AsRef<Path>,
) -> Result<(), TsExportError> {
export_with_cfg(macro_data?, Default::default(), export_path)
export_with_cfg_with_header(
macro_data?,
Default::default(),
export_path,
Cow::Borrowed(CRINGE_ESLINT_DISABLE), // TODO: Remove this as a default. SemVer moment.
)
}

0 comments on commit e7bbbbf

Please sign in to comment.