aboutsummaryrefslogtreecommitdiff
path: root/src/lib/tauri.ts
blob: 294f1170424d02dc71f0b10791ca5ea713e89698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { invoke } from '@tauri-apps/api/tauri'
import type { FileEntry } from '@/types'

export async function readFile(path: string): Promise<string> {
  return invoke<string>('read_file', { path })
}

export async function writeFile(path: string, content: string): Promise<void> {
  return invoke<void>('write_file', { path, content })
}

export async function openDir(path: string): Promise<FileEntry[]> {
  return invoke<FileEntry[]>('open_dir', { path })
}

export async function pickDirectory(): Promise<string | null> {
  const { open } = await import('@tauri-apps/api/dialog')
  const selected = await open({
    directory: true,
    multiple: false,
    title: 'Open Folder',
  })
  return selected as string | null
}

export async function createDir(path: string): Promise<void> {
  return invoke<void>('create_dir', { path })
}

export async function pickFile(): Promise<string | null> {
  const { open } = await import('@tauri-apps/api/dialog')
  const selected = await open({
    multiple: false,
    title: 'Open File',
  })
  return (selected as string) ?? null
}

export async function saveDialog(defaultName?: string): Promise<string | null> {
  const { save } = await import('@tauri-apps/api/dialog')
  const selected = await save({
    defaultPath: defaultName,
    title: 'Save File',
  })
  return selected ?? null
}