Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
- name: Lint Rust
run: cargo clippy --workspace --all-targets --locked -- -D warnings

- name: Build Native Binding
run: pnpm --filter rstack build:native

- name: Lint
run: node --run lint

Expand Down
185 changes: 182 additions & 3 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/rstack-binding"]
members = ["crates/rstack-binding", "crates/rstack-ignore"]
resolver = "2"

[workspace.package]
Expand All @@ -9,9 +9,12 @@ repository = "https://github.com/rstackjs/rstack-cli"
rust-version = "1.88"

[workspace.dependencies]
ignore = { version = "0.4.33", default-features = false }
napi = { version = "3.12.0", default-features = false, features = ["napi9"] }
napi-build = "2.4.0"
napi-derive = "3.6.2"
pathdiff = "0.2.3"
rstack-ignore = { path = "crates/rstack-ignore" }

[profile.release]
lto = true
Expand Down
2 changes: 2 additions & 0 deletions crates/rstack-binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ publish = false

[lib]
crate-type = ["cdylib"]
test = false

[dependencies]
napi.workspace = true
napi-derive.workspace = true
rstack-ignore.workspace = true

[build-dependencies]
napi-build.workspace = true
42 changes: 34 additions & 8 deletions crates/rstack-binding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
#![deny(clippy::all)]

use std::path::Path;

use napi::Error;
use napi_derive::napi;
use rstack_ignore::{IgnoreMatcher as CoreIgnoreMatcher, IgnoreSource as CoreIgnoreSource};

/// A Gitignore-compatible pattern source received from JavaScript.
#[napi(object, object_to_js = false)]
pub struct IgnoreSource {
/// Directory that patterns are resolved from.
pub root_path: String,
/// Newline-delimited Gitignore patterns.
pub patterns: String,
}

/// JavaScript-facing wrapper around the compiled Rust matcher.
#[napi]
pub fn native_ping(input: String) -> String {
format!("pong:{input}")
pub struct IgnoreMatcher {
inner: CoreIgnoreMatcher,
}

#[cfg(test)]
mod tests {
use super::native_ping;
#[napi]
impl IgnoreMatcher {
/// Compiles all pattern sources once and keeps the result for repeated path checks.
#[napi(constructor)]
pub fn new(sources: Vec<IgnoreSource>) -> napi::Result<Self> {
let sources = sources
.into_iter()
.map(|source| CoreIgnoreSource::new(source.root_path, source.patterns));
let inner = CoreIgnoreMatcher::new(sources).map_err(|error| {
Error::from_reason(format!("Failed to compile ignore patterns: {error}"))
})?;

Ok(Self { inner })
}

#[test]
fn formats_ping_response() {
assert_eq!(native_ping("rstack".to_string()), "pong:rstack");
/// Returns whether a file or directory is ignored by any source.
#[napi]
pub fn is_ignored(&mut self, file_path: String, is_directory: bool) -> bool {
self.inner.is_ignored(Path::new(&file_path), is_directory)
}
}
12 changes: 12 additions & 0 deletions crates/rstack-ignore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rstack-ignore"
version = "0.1.0"
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
publish = false

[dependencies]
ignore.workspace = true
pathdiff.workspace = true
Loading