diff --git a/.dockerignore b/.dockerignore index e2f885aa..8472437d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -45,6 +45,8 @@ Dockerfile docker-compose.yml docker-compose.example.yml +/target + /dev /docs /neko diff --git a/.gitignore b/.gitignore index 714d3f6f..22230212 100644 --- a/.gitignore +++ b/.gitignore @@ -73,9 +73,10 @@ yarn* .devenv .direnv -# Cargo cache for Docker +# Cargo /.cargo-cache /.cargo-target +/target # Custom custom/ diff --git a/packages/backend-rs/Cargo.lock b/Cargo.lock similarity index 100% rename from packages/backend-rs/Cargo.lock rename to Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..53e135b5 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,35 @@ +[workspace] +members = ["packages/backend-rs"] +resolver = "2" + +[workspace.dependencies] +napi = { version = "2.15.0", default-features = false } +napi-build = "2.1.0" +napi-derive = "2.15.0" + +argon2 = "0.5.3" +async-trait = "0.1.77" +basen = "0.1.0" +bcrypt = "0.15.0" +cfg-if = "1.0.0" +chrono = "0.4.33" +cuid2 = "0.1.2" +idna = "0.5.0" +jsonschema = "0.17.1" +once_cell = "1.19.0" +parse-display = "0.8.2" +pretty_assertions = "1.4.0" +rand = "0.8.5" +regex = "1.10.3" +schemars = "0.8.16" +sea-orm = "0.12.12" +serde ="1.0.196" +serde_json = "1.0.113" +serde_yaml = "0.9.31" +thiserror = "1.0.56" +tokio = "1.35.1" +url = "2.5.0" +urlencoding = "2.1.3" + +[profile.release] +lto = true diff --git a/Makefile b/Makefile index 46911aab..ea86c8f0 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,13 @@ PACKAGE_JSONS += packages/firefish-js/package.json PACKAGE_JSONS += packages/megalodon/package.json PACKAGE_JSONS += packages/sw/package.json -RUST_DIR := packages/backend-rs -RUSTY_STUFF := $(call recursive_wildcard, $(RUST_DIR)/src, *) -RUSTY_STUFF += $(RUST_DIR)/Cargo.toml +CARGO_TOMLS := Cargo.toml +CARGO_TOMLS += packages/backend-rs/Cargo.toml + +RUST_BE_DIR := packages/backend-rs +RUST_BE_SRC := Cargo.toml +RUST_BE_SRC += $(RUST_BE_DIR)/Cargo.toml +RUST_BE_SRC += $(call recursive_wildcard, $(RUST_BE_DIR)/src, *) MIGRATIONS := $(foreach dir, packages/backend/migration, $(wildcard $(dir)/*)) MIGRATIONS += $(foreach dir, packages/backend/migration-neko, $(wildcard $(dir)/*)) @@ -50,13 +54,13 @@ $(T)/test-revert: neko/revert.sql $(MIGRATIONS) .PHONY: update-lockfiles -update-lockfiles: pnpm-lock.yaml $(RUST_DIR)/Cargo.lock +update-lockfiles: pnpm-lock.yaml Cargo.lock pnpm-lock.yaml: pnpm-workspace.yaml $(PACKAGE_JSONS) pnpm install -$(RUST_DIR)/Cargo.lock: $(RUST_DIR)/Cargo.toml - cd $(RUST_DIR) && cargo update +Cargo.lock: $(CARGO_TOMLS) + cargo update .PHONY: run @@ -81,9 +85,9 @@ format: .PHONY: rust-lint -rust-lint: $(T)/rust-lint +rust-lint: $(T)/rust-be-lint -$(T)/rust-lint: $(RUSTY_STUFF) +$(T)/rust-be-lint: $(RUST_BE_SRC) pnpm --filter='backend-rs' run lint touch $@ @@ -93,7 +97,7 @@ regenerate-entities: $(T)/regenerate-entities $(T)/regenerate-entities: $(MIGRATIONS) pnpm run migrate - cd $(RUST_DIR) && \ + cd $(RUST_BE_DIR) && \ sea-orm-cli generate entity \ --output-dir='src/model/entity' \ --database-url='postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST_PORT)/$(POSTGRES_DB)' \ @@ -106,15 +110,15 @@ $(T)/regenerate-entities: $(MIGRATIONS) sed -i "s/NAPI_EXTRA_ATTR_PLACEHOLDER/$${attribute}/" "$${file}"; \ done sed -i 's/#\[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)\]/#[derive(Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]\n#[napi_derive::napi]/' \ - $(RUST_DIR)/src/model/entity/sea_orm_active_enums.rs + $(RUST_BE_DIR)/src/model/entity/sea_orm_active_enums.rs touch $@ -neko/index.js: $(RUSTY_STUFF) +neko/index.js: $(RUST_BE_SRC) pnpm --filter='backend-rs' run build:debug - [ -f $(RUST_DIR)/built/index.js ] + [ -f $(RUST_BE_DIR)/built/index.js ] rm neko/index.js - cp $(RUST_DIR)/built/index.js neko/index.js + cp $(RUST_BE_DIR)/built/index.js neko/index.js .PHONY: build-image diff --git a/neko/update/patch.sh b/neko/update/patch.sh index 3b363f84..4b302666 100755 --- a/neko/update/patch.sh +++ b/neko/update/patch.sh @@ -14,3 +14,6 @@ if [ -d packages/backend/native-utils ]; then rm --recursive --force packages/backend/native-utils/target rmdir packages/backend/native-utils || : fi + +# create virtual workspace +rm --recursive --force packages/backend-rs/target diff --git a/package.json b/package.json index bd306d1e..1109fae5 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "build:debug": "pnpm clean && pnpm --filter backend-rs run build:debug && pnpm --filter firefish-js run build:debug && pnpm --filter firefish-js run build:types && pnpm --filter !firefish-js --filter !backend-rs -r --parallel run build:debug && pnpm run gulp", "format": "pnpm -r --parallel run format", "clean": "pnpm node ./scripts/clean-built.mjs", - "clean-cargo": "pnpm node ./scripts/clean-cargo.mjs", + "clean-cargo": "cargo clean", "clean-npm": "pnpm node ./scripts/clean-npm.mjs", "clean-all": "pnpm run clean && pnpm run clean-cargo && pnpm run clean-npm", "cleanall": "pnpm run clean-all" diff --git a/packages/backend-rs/Cargo.toml b/packages/backend-rs/Cargo.toml index a102b7fc..b52800e5 100644 --- a/packages/backend-rs/Cargo.toml +++ b/packages/backend-rs/Cargo.toml @@ -3,46 +3,38 @@ edition = "2021" name = "backend-rs" version = "0.0.0" -[features] -default = ["napi"] -napi = ["dep:napi-derive"] - [lib] crate-type = ["cdylib", "lib"] [dependencies] -async-trait = "0.1.77" -cfg-if = "1.0.0" -chrono = "0.4.33" -cuid2 = "0.1.2" -idna = "0.5.0" -jsonschema = "0.17.1" -once_cell = "1.19.0" -parse-display = "0.8.2" -rand = "0.8.5" -regex = "1.10.3" -schemars = { version = "0.8.16", features = ["chrono"] } -sea-orm = { version = "0.12.12", features = ["sqlx-postgres", "runtime-tokio-rustls"] } -serde = { version = "1.0.196", features = ["derive"] } -serde_json = "1.0.113" -serde_yaml = "0.9.31" -thiserror = "1.0.56" -tokio = { version = "1.35.1", features = ["full"] } -url = "2.5.0" +napi = { workspace = true, default-features = false, features = ["napi9", "tokio_rt", "chrono_date", "serde-json"] } +napi-derive = { workspace = true } -# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix -napi = { version = "2.15.0", default-features = false, features = ["napi9", "tokio_rt", "chrono_date", "serde-json"] } -napi-derive = { version = "2.15.0", optional = true } -basen = "0.1.0" -urlencoding = "2.1.3" -argon2 = "0.5.3" -bcrypt = "0.15.0" +argon2 = { workspace = true } +async-trait = { workspace = true } +basen = { workspace = true } +bcrypt = { workspace = true } +cfg-if = { workspace = true } +chrono = { workspace = true } +cuid2 = { workspace = true } +idna = { workspace = true } +jsonschema = { workspace = true } +once_cell = { workspace = true } +parse-display = { workspace = true } +rand = { workspace = true } +regex = { workspace = true } +schemars = { workspace = true, features = ["chrono"] } +sea-orm = { workspace = true, features = ["sqlx-postgres", "runtime-tokio-rustls"] } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +serde_yaml = { workspace = true } +thiserror = { workspace = true } +tokio = { workspace = true, features = ["full"] } +url = { workspace = true } +urlencoding = { workspace = true } [dev-dependencies] -pretty_assertions = "1.4.0" +pretty_assertions = { workspace = true } [build-dependencies] -napi-build = "2.1.0" - -[profile.release] -lto = true +napi-build = { workspace = true } diff --git a/packages/backend-rs/package.json b/packages/backend-rs/package.json index 06d7c6fe..4c7f8a2b 100644 --- a/packages/backend-rs/package.json +++ b/packages/backend-rs/package.json @@ -31,8 +31,8 @@ }, "scripts": { "artifacts": "napi artifacts", - "build": "napi build --features napi --platform --release ./built/", - "build:debug": "napi build --features napi --platform ./built/", + "build": "napi build --platform --release ./built/", + "build:debug": "napi build --platform ./built/", "prepublishOnly": "napi prepublish -t npm", "universal": "napi universal", "version": "napi version", diff --git a/packages/backend-rs/src/lib.rs b/packages/backend-rs/src/lib.rs index e6ec2545..6bfbb464 100644 --- a/packages/backend-rs/src/lib.rs +++ b/packages/backend-rs/src/lib.rs @@ -1,9 +1,7 @@ pub mod config; pub mod database; pub mod macros; +pub mod mastodon_api; pub mod model; pub mod prelude; pub mod util; - -// #[cfg(feature = "napi")] -pub mod mastodon_api; diff --git a/packages/backend-rs/src/macros.rs b/packages/backend-rs/src/macros.rs index b2549227..2f6b6109 100644 --- a/packages/backend-rs/src/macros.rs +++ b/packages/backend-rs/src/macros.rs @@ -1,7 +1,6 @@ #[macro_export] macro_rules! impl_napi_error_from { ($a:ty) => { - #[cfg(feature = "napi")] impl From<$a> for napi::Error { fn from(reason: $a) -> napi::Error { napi::Error::from_reason(reason.to_string()) diff --git a/scripts/clean-cargo.mjs b/scripts/clean-cargo.mjs deleted file mode 100644 index df48e5fd..00000000 --- a/scripts/clean-cargo.mjs +++ /dev/null @@ -1,12 +0,0 @@ -import path, { join } from "node:path"; -import { fileURLToPath } from "node:url"; -import { execa } from "execa"; - -(async () => { - const __dirname = path.dirname(fileURLToPath(import.meta.url)); - - execa("cargo", ["clean"], { - cwd: join(__dirname, "/../packages/backend-rs"), - stdio: "inherit", - }); -})();