refactor (backend-rs): read version from package.json at compile time
This commit is contained in:
parent
7dd3b8ec5a
commit
ada5ff7e75
6 changed files with 25 additions and 59 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1745,6 +1745,8 @@ dependencies = [
|
|||
"napi",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn 2.0.64",
|
||||
"thiserror",
|
||||
]
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
extern crate napi_build;
|
||||
|
||||
fn main() {
|
||||
// watch the version in the project root package.json
|
||||
println!("cargo:rerun-if-changed=../../package.json");
|
||||
|
||||
// napi
|
||||
napi_build::setup();
|
||||
}
|
||||
|
|
12
packages/backend-rs/index.d.ts
vendored
12
packages/backend-rs/index.d.ts
vendored
|
@ -191,18 +191,6 @@ export interface Config {
|
|||
authUrl: string
|
||||
driveUrl: string
|
||||
userAgent: string
|
||||
clientEntry: Manifest
|
||||
}
|
||||
export interface Manifest {
|
||||
file: string
|
||||
name: string
|
||||
src: string
|
||||
isEntry: boolean
|
||||
isDynamicEntry: boolean
|
||||
imports: Array<string>
|
||||
dynamicImports: Array<string>
|
||||
css: Array<string>
|
||||
assets: Array<string>
|
||||
}
|
||||
export function loadConfig(): Config
|
||||
export interface Acct {
|
||||
|
|
|
@ -231,34 +231,6 @@ pub struct Config {
|
|||
pub auth_url: String,
|
||||
pub drive_url: String,
|
||||
pub user_agent: String,
|
||||
pub client_entry: Manifest,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct Meta {
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
||||
struct ManifestJson {
|
||||
#[serde(rename = "src/init.ts")]
|
||||
pub init_ts: Manifest,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[crate::export(object, use_nullable = false)]
|
||||
pub struct Manifest {
|
||||
pub file: String,
|
||||
pub name: String,
|
||||
pub src: String,
|
||||
pub is_entry: bool,
|
||||
pub is_dynamic_entry: bool,
|
||||
pub imports: Vec<String>,
|
||||
pub dynamic_imports: Vec<String>,
|
||||
pub css: Vec<String>,
|
||||
pub assets: Vec<String>,
|
||||
}
|
||||
|
||||
fn read_config_file() -> ServerConfig {
|
||||
|
@ -280,28 +252,12 @@ fn read_config_file() -> ServerConfig {
|
|||
data
|
||||
}
|
||||
|
||||
fn read_meta() -> Meta {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
let meta_json = fs::File::open(cwd.join("../../built/meta.json"))
|
||||
.expect("Failed to open 'built/meta.json'");
|
||||
serde_json::from_reader(meta_json).expect("Failed to parse built/meta.json")
|
||||
}
|
||||
|
||||
fn read_manifest() -> Manifest {
|
||||
let cwd = env::current_dir().unwrap();
|
||||
let manifest_json = fs::File::open(cwd.join("../../built/_client_dist_/manifest.json"))
|
||||
.expect("Failed to open 'built/_client_dist_/manifest.json'");
|
||||
let manifest: ManifestJson = serde_json::from_reader(manifest_json)
|
||||
.expect("Failed to parse built/_client_dist_/manifest.json");
|
||||
|
||||
manifest.init_ts
|
||||
}
|
||||
const VERSION: &str = macro_rs::read_version_from_package_json!();
|
||||
|
||||
#[crate::export]
|
||||
pub fn load_config() -> Config {
|
||||
let server_config = read_config_file();
|
||||
let version = read_meta().version;
|
||||
let manifest = read_manifest();
|
||||
let version = VERSION.to_owned();
|
||||
let url = url::Url::parse(&server_config.url).expect("Config url is invalid");
|
||||
let hostname = url
|
||||
.host_str()
|
||||
|
@ -379,7 +335,6 @@ pub fn load_config() -> Config {
|
|||
redis_key_prefix,
|
||||
scheme,
|
||||
ws_scheme,
|
||||
client_entry: manifest,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@ proc-macro = true
|
|||
convert_case = { workspace = true }
|
||||
proc-macro2 = { workspace = true }
|
||||
quote = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
syn = { workspace = true, features = ["full", "extra-traits"] }
|
||||
|
||||
[dev-dependencies]
|
||||
|
|
|
@ -2,6 +2,21 @@ use convert_case::{Case, Casing};
|
|||
use proc_macro2::{TokenStream, TokenTree};
|
||||
use quote::{quote, ToTokens};
|
||||
|
||||
/// Read the version field in the project root package.json at compile time
|
||||
#[proc_macro]
|
||||
pub fn read_version_from_package_json(_item: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
#[derive(serde::Deserialize)]
|
||||
struct PackageJson {
|
||||
version: String,
|
||||
}
|
||||
|
||||
let file = std::fs::File::open("package.json").expect("Failed to open package.json");
|
||||
let json: PackageJson = serde_json::from_reader(file).unwrap();
|
||||
let version = &json.version;
|
||||
|
||||
quote! { #version }.into()
|
||||
}
|
||||
|
||||
/// Export this function, struct, enum, const, etc. to TypeScript.
|
||||
#[proc_macro_attribute]
|
||||
pub fn export(
|
||||
|
|
Loading…
Reference in a new issue