Skip to content

Commit

Permalink
fix(cli,napi-derive): backward compatible with older cli with #1531 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Mar 22, 2023
1 parent 2f4d9d4 commit 5398b16
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ export class BuildCommand extends Command {
...additionalEnv,
TYPE_DEF_TMP_PATH: intermediateTypeFile,
WASI_REGISTER_TMP_PATH: intermediateWasiRegisterFile,
CARGO_CFG_NAPI_RS_CLI_VERSION: version,
}

try {
Expand Down
6 changes: 5 additions & 1 deletion crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ independent = true
[features]
noop = []
strict = []
type-def = ["regex", "once_cell"]
type-def = ["regex", "once_cell", "semver"]

[dependencies]
convert_case = "0.6"
Expand All @@ -30,3 +30,7 @@ version = "1"
[dependencies.once_cell]
optional = true
version = "1"

[dependencies.semver]
optional = true
version = "1"
2 changes: 2 additions & 0 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub use ast::*;
pub use codegen::*;
pub use error::{BindgenResult, Diagnostic};
#[cfg(feature = "type-def")]
pub use semver;
#[cfg(feature = "type-def")]
pub use typegen::*;

#[derive(Debug)]
Expand Down
22 changes: 19 additions & 3 deletions crates/backend/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ mod r#enum;
mod r#fn;
pub(crate) mod r#struct;

use std::{cell::RefCell, collections::HashMap};
use std::{cell::RefCell, collections::HashMap, env};

use once_cell::sync::Lazy;
use syn::Type;

pub static NAPI_RS_CLI_VERSION: Lazy<semver::Version> = Lazy::new(|| {
let version = env::var("CARGO_CFG_NAPI_RS_CLI_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
semver::Version::parse(&version).unwrap_or_else(|_| semver::Version::new(0, 0, 0))
});

pub static NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX: Lazy<semver::Version> =
Lazy::new(|| semver::Version::new(2, 15, 1));

#[derive(Default, Debug)]
pub struct TypeDef {
pub kind: String,
Expand Down Expand Up @@ -103,9 +111,17 @@ impl ToString for TypeDef {
} else {
"".to_owned()
};
// TODO: remove this in v3
// This is a workaround for lower version of @napi-rs/cli
// See https://github.com/napi-rs/napi-rs/pull/1531
let prefix = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX {
format!("{}:", pkg_name)
} else {
"".to_string()
};
format!(
r#"{}:{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
pkg_name,
r#"{}{{"kind": "{}", "name": "{}", "js_doc": "{}", "def": "{}"{}{}}}"#,
prefix,
self.kind,
self.name,
escape_json(&self.js_doc),
Expand Down
7 changes: 6 additions & 1 deletion crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ pub fn napi(attr: RawStream, input: RawStream) -> RawStream {
// logic on first macro expansion
#[cfg(feature = "type-def")]
if let Ok(ref type_def_file) = env::var("TYPE_DEF_TMP_PATH") {
if let Err(_e) = remove_existed_type_def(type_def_file) {
use napi_derive_backend::{NAPI_RS_CLI_VERSION, NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX};
if let Err(_e) = if *NAPI_RS_CLI_VERSION >= *NAPI_RS_CLI_VERSION_WITH_SHARED_CRATES_FIX {
remove_existed_type_def(type_def_file)
} else {
fs::remove_file(type_def_file)
} {
#[cfg(debug_assertions)]
{
println!("Failed to manipulate type def file: {:?}", _e);
Expand Down

0 comments on commit 5398b16

Please sign in to comment.