Skip to content

Commit

Permalink
fix(core): add cache and trash dir
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 7, 2024
1 parent a646dac commit d525c55
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/animegarden/src/resources/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ export class ResourcesCache {

private async loadAnimeResources(anime: Anime): Promise<AnimeCacheSchema | undefined> {
try {
const root = this.animeRoot.join(anime.relativeDirectory);
await root.ensureDir();
return JSON.parse(await root.join('resources.json').readText());
await anime.cacheDirectory.ensureDir();
return JSON.parse(await anime.cacheDirectory.join('resources.json').readText());
} catch {
return undefined;
}
Expand All @@ -133,20 +132,17 @@ export class ResourcesCache {
resp: Awaited<ReturnType<typeof fetchResources>>
): Promise<void> {
try {
const root = this.animeRoot.join(anime.relativeDirectory);

const copied = { ...resp, prefer: { fansub: anime.plan.fansub } };
Reflect.deleteProperty(copied, 'ok');
Reflect.deleteProperty(copied, 'complete');

await root.join('resources.json').writeText(JSON.stringify(copied, null, 2));
await anime.cacheDirectory.join('resources.json').writeText(JSON.stringify(copied, null, 2));
} catch {}
}

public async clearAnimeResources(anime: Anime) {
try {
const root = this.animeRoot.join(anime.relativeDirectory);
await root.join('resources.json').remove();
await anime.cacheDirectory.join('resources.json').remove();
} catch {}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/anime/anime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class Anime {

public readonly libraryDirectory: StoragePath;

public readonly cacheDirectory: StoragePath;

public readonly trashDirectory: StoragePath;

public readonly plan: AnimePlan;

public readonly planFile: PlanFile;
Expand Down Expand Up @@ -62,6 +66,14 @@ export class Anime {
this.libraryDirectory = plan.directory
? space.storage.library.resolve(plan.directory)
: space.storage.library.join(dirname);

this.cacheDirectory = plan.directory
? space.storage.cache.resolve(plan.directory)
: space.storage.cache.join(dirname);

this.trashDirectory = plan.directory
? space.storage.trash.resolve(plan.directory)
: space.storage.trash.join(dirname);
}

public get space(): AnimeSpace {
Expand Down

0 comments on commit d525c55

Please sign in to comment.