Skip to content

Commit

Permalink
fix: 🐛 tagsGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
meetqy committed Feb 28, 2023
1 parent 95133d3 commit 6e74e09
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/transform-eagle/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dotenv from "dotenv";
import watchImage from "./image";
import watchFloder from "./folder";
import watchMetadata from "./metadata";
import { logger } from "@eagleuse/utils";
import { getNSFW } from "./image/nsfw";

Expand All @@ -16,6 +16,6 @@ export const transformEagle = async () => {
logger.info("Complete init nsfw.");
}

watchFloder(LIBRARY);
watchMetadata(LIBRARY);
watchImage(LIBRARY);
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { join } from "path";
import * as chokidar from "chokidar";
import { getPrisma } from "./prisma";
import { readJsonSync } from "fs-extra";
import { getPrisma } from "../prisma";
import { Folder } from "@prisma/client";
import { logger } from "@eagleuse/utils";
import * as _ from "lodash";
import { trigger } from "./trigger";
import { trigger } from "../trigger";

const prisma = getPrisma();
const _wait = 3000;

// 多级嵌套转为一级
const demotionFolder = (folders: EagleUse.Folder[]): Folder[] => {
Expand All @@ -29,9 +24,8 @@ const demotionFolder = (folders: EagleUse.Folder[]): Folder[] => {
return newFolders;
};

const handleFloder = async (file: string) => {
const json = readJsonSync(file);
const folders = demotionFolder(json["folders"]);
export const handleFloder = async (metadataFolders: EagleUse.Folder[]) => {
const folders = demotionFolder(metadataFolders);

folders.forEach((folder) => {
prisma.folder
Expand Down Expand Up @@ -67,23 +61,10 @@ const deleteUnnecessary = (localFolder: Folder[]) => {
},
},
})
.then((res) => {
logger.debug(res, "Delete unnecessary folder: ");
})
.catch((e) => {
logger.error(e, "Delete unnecessary folder");
logger.error(e, "Delete folder error: ");
});
}
});
trigger();
};

const _debounce = _.debounce(handleFloder, _wait);

const watchFloder = (LIBRARY: string) => {
const file = join(LIBRARY, "./metadata.json");

chokidar.watch(file).on("add", _debounce).on("change", _debounce);
};

export default watchFloder;
24 changes: 24 additions & 0 deletions packages/transform-eagle/lib/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { join } from "path";
import * as chokidar from "chokidar";
import _ from "lodash";
import { readJsonSync } from "fs-extra";
import { handleFloder } from "./folder";
import { handleTagsGroups } from "./tags-groups";

const _wait = 5000;

const handleMetadata = (file: string) => {
const json = readJsonSync(file);
handleFloder(json["folders"]);
handleTagsGroups(json["tagsGroups"]);
};

const _debounce = _.debounce(handleMetadata, _wait);

const watchMetaData = (LIBRARY: string) => {
const file = join(LIBRARY, "./metadata.json");

chokidar.watch(file).on("add", _debounce).on("change", _debounce);
};

export default watchMetaData;
57 changes: 57 additions & 0 deletions packages/transform-eagle/lib/metadata/tags-groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { logger } from "@eagleuse/utils";
import { getPrisma } from "../prisma";
import { trigger } from "../trigger";

const prisma = getPrisma();

export const handleTagsGroups = (localTagsGroups: EagleUse.TagsGroupsItem[]) => {
localTagsGroups.forEach((item) => {
const tags = (item.tags as string[]) || [];

const data = {
...item,
tags: {
connect: tags.map((tag) => ({ id: tag })),
},
};

prisma.tagsGroups
.upsert({
where: { id: item.id },
create: data,
update: data,
})
.catch((e) => logger.info(e, "TagsGroups error: "));

trigger();
});

deleteUnnecessary(localTagsGroups);
};

const deleteUnnecessary = (localTagsGroup: EagleUse.TagsGroupsItem[]) => {
prisma.tagsGroups
.findMany({
where: {
id: {
notIn: localTagsGroup.map((item) => item.id),
},
},
})
.then((folders) => {
if (folders && folders.length > 0) {
prisma.tagsGroups
.deleteMany({
where: {
id: {
in: folders.map((item) => item.id),
},
},
})
.catch((e) => {
logger.error(e, "TagsGroups Delete error: ");
});
}
});
trigger();
};
2 changes: 1 addition & 1 deletion packages/transform-eagle/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ declare namespace EagleUse {
id: string;
name: string;
color?: string;
tags: Tag[];
tags: (Tag | string)[];
}

export interface SearchParams {
Expand Down

0 comments on commit 6e74e09

Please sign in to comment.