|
| 1 | +import { isProd } from '@/constants'; |
| 2 | +import { addLog } from '@/utils/log'; |
| 3 | +import { basePath, devToolIds } from '@tool/constants'; |
| 4 | +import { LoadToolsByFilename } from '@tool/loadToolProd'; |
| 5 | +import { getIconPath } from '@tool/parseMod'; |
| 6 | +import type { ToolSetType, ToolType } from '@tool/type'; |
| 7 | +import { ToolTagEnum } from '@tool/type/tags'; |
| 8 | +import { existsSync } from 'fs'; |
| 9 | +import { readdir } from 'fs/promises'; |
| 10 | +import { join } from 'path'; |
| 11 | + |
| 12 | +const LoadToolsDev = async (filename: string): Promise<ToolType[]> => { |
| 13 | + if (isProd) { |
| 14 | + addLog.error('Can not load dev tool in prod mode'); |
| 15 | + return []; |
| 16 | + } |
| 17 | + |
| 18 | + const tools: ToolType[] = []; |
| 19 | + |
| 20 | + const toolPath = join(basePath, 'modules', 'tool', 'packages', filename); |
| 21 | + |
| 22 | + const rootMod = (await import(toolPath)).default as ToolSetType | ToolType; |
| 23 | + |
| 24 | + const childrenPath = join(toolPath, 'children'); |
| 25 | + const isToolSet = existsSync(childrenPath); |
| 26 | + |
| 27 | + const toolsetId = rootMod.toolId || filename; |
| 28 | + const parentIcon = rootMod.icon ?? getIconPath(`${toolsetId}/logo`); |
| 29 | + |
| 30 | + if (isToolSet) { |
| 31 | + tools.push({ |
| 32 | + ...rootMod, |
| 33 | + tags: rootMod.tags || [ToolTagEnum.enum.other], |
| 34 | + toolId: toolsetId, |
| 35 | + icon: parentIcon, |
| 36 | + toolFilename: filename, |
| 37 | + cb: () => Promise.resolve({}), |
| 38 | + versionList: [] |
| 39 | + }); |
| 40 | + |
| 41 | + const children: ToolType[] = []; |
| 42 | + |
| 43 | + { |
| 44 | + const files = await readdir(childrenPath); |
| 45 | + for (const file of files) { |
| 46 | + const childPath = join(childrenPath, file); |
| 47 | + |
| 48 | + const childMod = (await import(childPath)).default as ToolType; |
| 49 | + const toolId = childMod.toolId || `${toolsetId}/${file}`; |
| 50 | + |
| 51 | + const childIcon = childMod.icon ?? rootMod.icon ?? getIconPath(`${toolsetId}/${file}/logo`); |
| 52 | + children.push({ |
| 53 | + ...childMod, |
| 54 | + toolId, |
| 55 | + toolFilename: filename, |
| 56 | + icon: childIcon, |
| 57 | + parentId: toolsetId |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + tools.push(...children); |
| 63 | + } else { |
| 64 | + // is not toolset |
| 65 | + const icon = rootMod.icon ?? getIconPath(`${toolsetId}/logo`); |
| 66 | + |
| 67 | + tools.push({ |
| 68 | + ...(rootMod as ToolType), |
| 69 | + tags: rootMod.tags || [ToolTagEnum.enum.other], |
| 70 | + toolId: toolsetId, |
| 71 | + icon, |
| 72 | + toolFilename: filename |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + tools.forEach((tool) => devToolIds.add(tool.toolId)); |
| 77 | + return tools; |
| 78 | +}; |
| 79 | + |
| 80 | +export const loadTool = async (filename: string, dev: boolean) => { |
| 81 | + return dev ? await LoadToolsDev(filename) : await LoadToolsByFilename(filename); |
| 82 | +}; |
0 commit comments