Skip to content

Commit 888c976

Browse files
committed
automatically create missing directories during write operations
1 parent 237d3a9 commit 888c976

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

src-tauri/capabilities/default.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
"path": "$APPLOCALDATA/*"
4343
}
4444
]
45+
},
46+
{
47+
"identifier": "fs:allow-mkdir",
48+
"allow": [
49+
{
50+
"path": "$APPLOCALDATA/*"
51+
}
52+
]
4553
}
4654
]
4755
}

src/lib/fs.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
1-
import { BaseDirectory, readTextFile, writeTextFile, readDir, rename, remove } from "@tauri-apps/plugin-fs";
1+
import {
2+
BaseDirectory,
3+
readTextFile,
4+
writeTextFile,
5+
readDir,
6+
rename,
7+
remove,
8+
mkdir,
9+
exists
10+
} from "@tauri-apps/plugin-fs";
11+
import { dirname } from "@tauri-apps/api/path";
212
import { toKebabCase, toSentenceCase } from "./strings";
313
import { RecipeEntries } from "~/types";
414

5-
export async function readJSONFile<T>(path:string) {
15+
export async function readJSONFile<T>(path: string) {
616
const data = await readTextFile(path, {
717
baseDir: BaseDirectory.AppLocalData
818
});
919

1020
return JSON.parse(data) as T;
1121
}
1222

23+
export async function createDirectory(path: string) {
24+
const dir = await dirname(path);
25+
const dirExists = await exists(dir, {
26+
baseDir: BaseDirectory.AppLocalData,
27+
});
28+
29+
if (!dirExists) {
30+
await mkdir(dir, {
31+
baseDir: BaseDirectory.AppLocalData,
32+
recursive: true,
33+
});
34+
}
35+
}
36+
1337
export async function writeJSONFile<T>(path: string, data: T) {
38+
await createDirectory(path);
1439
const _data = typeof data === "string" ? data : JSON.stringify(data);
1540
return await writeTextFile(path, _data, {
1641
baseDir: BaseDirectory.AppLocalData,
@@ -43,11 +68,12 @@ export async function listRecipes(path: string = "") {
4368

4469
export function deleteFile(path: string) {
4570
return remove(path, {
46-
baseDir: BaseDirectory.AppLocalData
71+
baseDir: BaseDirectory.AppLocalData,
4772
});
4873
}
4974

50-
export function renameFile(oldPath: string, newPath: string) {
75+
export async function renameFile(oldPath: string, newPath: string) {
76+
await createDirectory(newPath);
5177
return rename(oldPath, newPath, {
5278
newPathBaseDir: BaseDirectory.AppLocalData,
5379
oldPathBaseDir: BaseDirectory.AppLocalData

src/lib/image.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { convertFileSrc } from "@tauri-apps/api/core";
22
import { appLocalDataDir, join } from "@tauri-apps/api/path";
33
import { writeFile, BaseDirectory } from "@tauri-apps/plugin-fs";
4+
import { createDirectory } from "./fs";
45

56
export async function getLocalImage(imageSrc: string) {
67
const localDir = await appLocalDataDir();
@@ -14,8 +15,11 @@ async function getAsByteArray(file: File) {
1415
}
1516

1617
export async function copyImage(name: string, file: File) {
18+
const path = `images/${name}`;
19+
await createDirectory(path);
20+
1721
const content = await getAsByteArray(file);
18-
return writeFile(`images/${name}`, content, {
22+
return writeFile(path, content, {
1923
baseDir: BaseDirectory.AppLocalData,
2024
});
2125
}

src/views/new-recipe.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ export function NewRecipeView() {
2323
}
2424
};
2525

26-
const recipeEntries = await listRecipes("recipes");
27-
const fileExists = recipeEntries.find((entry) => entry.id === recipeIdentifier);
26+
const recipeEntries = await listRecipes("recipes").catch(() => []);
27+
const fileExists = recipeEntries.find((entry) => {
28+
return entry.id === recipeIdentifier;
29+
});
30+
2831
if (fileExists) {
2932
toast.error({
3033
title: "Nombre de receta en uso",

0 commit comments

Comments
 (0)