The Tauri project file ./src-tauri/tauri.conf.json
has the JSON value tauri.bundle.identifier: "com.tauri.dev"
.
How do I access it from within my app?
I need to write to ${dataDir}/${bundleIdentifier}/my-file.txt
. Like so:
const fs = window.__TAURI__.fs;
const path = window.__TAURI__.path;
const id = "com.tauri.dev";
let filename = "my-file.txt";
let _path = await path.join(id, filename);
let data = "Hello World!";
await fs.writeFile({
path: _path,
contents: data
}, { dir: fs.Dir.Data });
Except I want to get the com.tauri.dev
value from from ./src-tauri/tauri.conf.json
rather than being manually inserted.
What's the proper way to do that?
Well, the simples Rust way to do it would be to just include it in your binary like so:
(not tested code)
let config_json = include_str!("../tauri.conf.json");
// then you parse json
let v: Value = serde_json::from_str(config_json)?
let id = v["tauri"]["bundle"]["identifier"];
println!("{id}")
The proper Tauri way is to get App's handle and access config directly.
tauri::Builder::default()
.setup(|app| {
let app_handle = app.handle();
let config = app_handle.config.lock(); // <-- this is tauri config
Ok(())
});
One more version:
fn main() {
let context = tauri::generate_context!();
let tauri_bundle_identifier = context.config().tauri.bundle.identifier;
println!("{tauri_bundle_identifier}");
tauri::Builder::default()
.context(context)
//...... setup, etc
.run("error running tauri app");
}