@rescript-tauri/plugin-fs¶
Tauri 2.x filesystem プラグイン の ReScript バインディングです。単発のファイル IO(read / write / dir / stat)を対象としており、FileHandle、watch、readTextFileLines、iOS 専用の security-scoped API は後続のパッケージ反復に持ち越しています。
注釈
本パッケージは main で機能完備済みです。初回 npm 公開は他のパッケージと合わせて予定されています。それまでは、ソースリポジトリ経由かワークスペースリンクで利用してください。
インストール¶
pnpm add @rescript-tauri/plugin-fs @tauri-apps/plugin-fs
@rescript-tauri/plugin-fs は @rescript-tauri/core と @tauri-apps/plugin-fs の両方を peerDependencies として宣言しているため、上流のバージョンは利用者側で制御できます。
rescript.json の dependencies にパッケージを追加します:
{
"dependencies": [
"@rescript-tauri/core",
"@rescript-tauri/plugin-fs"
]
}
Rust 側ではプラグインクレートを追加し、builder に登録します:
# src-tauri/Cargo.toml
[dependencies]
tauri-plugin-fs = "2"
// src-tauri/src/main.rs
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_fs::init())
.run(tauri::generate_context!())
.expect("error while running app");
}
Capabilities¶
Tauri 2.x ではファイルシステム操作ごとに capability の付与が必要です。examples/plugin-fs-demo で使用している最小セットは次のとおりです:
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"windows": ["main"],
"permissions": [
"core:default",
"fs:default",
"fs:allow-app-local-data-recursive"
]
}
fs:allow-app-local-data-recursive はプラグインが提供する permission エイリアスで、$APPLOCALDATA 配下を全面的に開放します。別のサンドボックスが必要な場合は fs:allow-home-recursive、fs:allow-document-recursive などに差し替えてください。
最小例¶
open RescriptTauriPluginFs
let baseDir = PluginFs.BaseDirectory.appLocalData
await PluginFs.mkdir(
"notes",
~options={baseDir: baseDir, recursive: true},
)
await PluginFs.writeTextFile(
"notes/hello.txt",
"Hello from rescript-tauri.\n",
~options={baseDir: baseDir, create: true},
)
let body = await PluginFs.readTextFile(
"notes/hello.txt",
~options={baseDir: baseDir},
)
Console.log(body)
公開 API¶
14 個の単発 IO 関数はすべて PluginFs の下に公開されています:
関数 |
備考 |
|---|---|
|
UTF-8 テキスト読み込み |
|
UTF-8 テキスト書き込み |
|
生バイト列( |
|
生バイト列 |
|
パスの存在確認 |
|
ファイルまたはディレクトリ(ツリー全体は |
|
ファイルシステム内での移動 |
|
ディレクトリ作成(親も含めて作るには |
|
ディレクトリの直下のエントリ一覧 |
|
メタデータ取得。シンボリックリンクを辿る |
|
メタデータ取得。シンボリックリンクは 辿らない |
|
ファイルを N バイトにリサイズ |
|
コピー。base ディレクトリをまたいだパスにも対応 |
|
バイト数(数値) |
関連するオプションレコード(readFileOptions、writeFileOptions、mkdirOptions、removeOptions、renameOptions、copyFileOptions、statOptions、existsOptions、readDirOptions、truncateOptions)は すべて PluginFs から再エクスポートされており、上流の FileInfo / DirEntry 形状は PluginFs.fileInfo / PluginFs.dirEntry として利用できます。
BaseDirectory は @rescript-tauri/core から再エクスポート されているため、別途インポートする必要はありません:
let baseDir = PluginFs.BaseDirectory.appLocalData
// equivalent to RescriptTauriCore.Path.BaseDirectory.appLocalData
落とし穴¶
単一フィールドレコードの punning¶
ReScript では {baseDir} は punning された 1 フィールドのレコードではなく、文 1 つを含む ブロック として扱われます。単一フィールドのオプションは必ず {baseDir: baseDir} のように明示してください:
// ❌ compile error: expected record, got block
await PluginFs.exists("foo.txt", ~options={baseDir})
// ✅
await PluginFs.exists("foo.txt", ~options={baseDir: baseDir})
複数フィールドのレコード({baseDir, recursive: true})は期待通りに動作します。
Uint8Array の長さ¶
@rescript/core の Uint8Array.t は TypedArray.t<int> のエイリアスです。length のゲッタは親モジュールに存在します:
let bytes = await PluginFs.readFile("data.bin", ~options={baseDir: baseDir})
Console.log("byte count: " ++ Int.toString(TypedArray.length(bytes)))
互換性¶
コンポーネント |
サポート範囲 |
|---|---|
上流の |
|
Rust 側 |
|
|
|
ReScript |
|
|
|
OS |
Linux / macOS / Windows |
関連情報¶
動作デモ:
examples/plugin-fs-demoソース:
packages/plugin-fs上流ドキュメント: Tauri 2.x file-system プラグイン