Skip to content

Commit 74a8de3

Browse files
committed
chore: sdk types
1 parent e50ee35 commit 74a8de3

File tree

12 files changed

+155
-42
lines changed

12 files changed

+155
-42
lines changed

modules/tool/build/build-json.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
import { basePath } from '@tool/constants';
1+
import { basePath, UploadToolsS3Path } from '@tool/constants';
22
import type { ToolSetType, ToolType } from '@tool/type';
33
import { ToolTagEnum } from '@tool/type/tags';
44
import { existsSync, writeFileSync } from 'fs';
55
import { readdir } from 'fs/promises';
66
import { join } from 'path';
7+
import { ToolDetailSchema } from 'sdk/client';
78

89
const filterToolList = ['.DS_Store', '.git', '.github', 'node_modules', 'dist', 'scripts'];
910

11+
const S3BasePath = process.env.S3_BASE_PATH;
12+
1013
const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
1114
const tools: ToolType[] = [];
1215

1316
const toolPath = join(basePath, 'modules', 'tool', 'packages', filename);
17+
18+
// get all avatars and push them into s3
1419
const rootMod = (await import(toolPath)).default as ToolSetType | ToolType;
1520

1621
const childrenPath = join(toolPath, 'children');
1722
const isToolSet = existsSync(childrenPath);
1823

1924
const toolsetId = rootMod.toolId || filename;
25+
const parentIcon = rootMod.icon ?? `${S3BasePath}${UploadToolsS3Path}/${toolsetId}/logo`;
2026

2127
if (isToolSet) {
2228
tools.push({
2329
...rootMod,
2430
tags: rootMod.tags || [ToolTagEnum.enum.other],
2531
toolId: toolsetId,
26-
icon: rootMod.icon ?? '',
32+
icon: parentIcon,
2733
toolFilename: filename,
2834
cb: () => Promise.resolve({}),
2935
versionList: []
@@ -35,25 +41,31 @@ const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
3541
const files = await readdir(childrenPath);
3642
for (const file of files) {
3743
const childPath = join(childrenPath, file);
44+
3845
const childMod = (await import(childPath)).default as ToolType;
3946
const toolId = childMod.toolId || `${toolsetId}/${file}`;
47+
48+
const childIcon =
49+
childMod.icon ?? `${S3BasePath}${UploadToolsS3Path}/${toolsetId}/${file}/logo`;
4050
children.push({
4151
...childMod,
4252
toolId,
4353
toolFilename: filename,
44-
icon: childMod.icon ?? ''
54+
icon: childIcon
4555
});
4656
}
4757
}
4858

4959
tools.push(...children);
5060
} else {
5161
// is not toolset
62+
const icon = rootMod.icon ?? `${S3BasePath}${UploadToolsS3Path}/${toolsetId}/logo`;
63+
5264
tools.push({
5365
...(rootMod as ToolType),
5466
tags: rootMod.tags || [ToolTagEnum.enum.other],
5567
toolId: toolsetId,
56-
icon: rootMod.icon ?? '',
68+
icon,
5769
toolFilename: filename,
5870
versionList: []
5971
});
@@ -81,7 +93,7 @@ async function main() {
8193
toolMap.set(tool.toolId, tool);
8294
}
8395

84-
const toolList = Array.from(toolMap.values());
96+
const toolList = Array.from(toolMap.values()).map((item) => ToolDetailSchema.parse(item));
8597
writeFileSync(join(basePath, 'dist', 'tools.json'), JSON.stringify(toolList));
8698

8799
console.log(

modules/tool/contract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import z from 'zod';
22
import { c } from '@/contract/init';
3-
import { ToolDetailSchema, type ToolDetailItemType, ToolTypeListSchema } from './type/api';
3+
import { ToolDetailSchema, type ToolDetailType, ToolTypeListSchema } from './type/api';
44

55
export const toolUploadContract = c.router(
66
{
@@ -87,7 +87,7 @@ export const toolContract = c.router(
8787
method: 'GET',
8888
description: 'Get tools list',
8989
responses: {
90-
200: c.type<Array<ToolDetailItemType>>()
90+
200: c.type<Array<ToolDetailType>>()
9191
}
9292
},
9393
getTool: {

modules/tool/controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ToolTagEnum, ToolTypeMap } from './type/tags';
1+
import { ToolTagEnum, ToolTagsNameMap } from './type/tags';
22
import z from 'zod';
33
import { ToolTypeListSchema } from './type/api';
44
import type { ToolType } from './type';
@@ -19,7 +19,7 @@ export async function getTool(toolId: string): Promise<ToolType | undefined> {
1919
}
2020

2121
export function getToolType(): z.infer<typeof ToolTypeListSchema> {
22-
return Object.entries(ToolTypeMap).map(([type, name]) => ({
22+
return Object.entries(ToolTagsNameMap).map(([type, name]) => ({
2323
type: type as z.infer<typeof ToolTagEnum>,
2424
name
2525
}));

modules/tool/loadToolDev.ts

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ import { isProd } from '@/constants';
22
import { addLog } from '@/utils/log';
33
import { existsSync } from 'fs';
44
import { readdir } from 'fs/promises';
5-
import { join } from 'path/posix';
5+
import { join } from 'path';
6+
import { glob } from 'glob';
67
import { basePath, devToolIds, UploadToolsS3Path } from './constants';
78
import type { ToolType, ToolSetType } from './type';
89
import { ToolTagEnum } from './type/tags';
10+
import { publicS3Server } from '@/s3';
911

1012
/**
1113
* Load Tools in dev mode. Only avaliable in dev mode
1214
* @param filename
1315
*/
14-
1516
export const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
1617
if (isProd) {
1718
addLog.error('Can not load dev tool in prod mode');
@@ -21,19 +22,66 @@ export const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
2122
const tools: ToolType[] = [];
2223

2324
const toolPath = join(basePath, 'modules', 'tool', 'packages', filename);
25+
26+
// get all avatars and push them into s3
27+
try {
28+
// Find logo files using glob pattern
29+
const logoFiles = await glob(`${toolPath}/logo.*`);
30+
const readmeFile = join(toolPath, 'README.md');
31+
32+
// Upload logo files if found
33+
for (const logoPath of logoFiles) {
34+
try {
35+
const logoFilename = logoPath.split('/').pop()!;
36+
const logoNameWithoutExt = logoFilename.split('.').slice(0, -1).join('.');
37+
await publicS3Server.uploadFileAdvanced({
38+
path: logoPath,
39+
defaultFilename: logoNameWithoutExt,
40+
prefix: UploadToolsS3Path + '/' + filename,
41+
keepRawFilename: true
42+
});
43+
addLog.info(
44+
`Uploaded logo file: ${logoPath} to ${UploadToolsS3Path}/${filename}/${logoNameWithoutExt}`
45+
);
46+
} catch (error) {
47+
addLog.warn(`Failed to upload logo file ${logoPath}: ${error}`);
48+
}
49+
}
50+
51+
// Upload README.md if it exists
52+
if (existsSync(readmeFile)) {
53+
try {
54+
await publicS3Server.uploadFileAdvanced({
55+
path: readmeFile,
56+
prefix: UploadToolsS3Path + '/' + filename,
57+
keepRawFilename: true
58+
});
59+
addLog.info(
60+
`Uploaded README.md: ${readmeFile} to ${UploadToolsS3Path}/${filename}/README.md`
61+
);
62+
} catch (error) {
63+
addLog.warn(`Failed to upload README.md ${readmeFile}: ${error}`);
64+
}
65+
}
66+
} catch (error) {
67+
addLog.warn(`Failed to upload static files for ${filename}: ${error}`);
68+
}
2469
const rootMod = (await import(toolPath)).default as ToolSetType | ToolType;
2570

2671
const childrenPath = join(toolPath, 'children');
2772
const isToolSet = existsSync(childrenPath);
2873

2974
const toolsetId = rootMod.toolId || filename;
75+
const parentIcon =
76+
rootMod.icon ??
77+
(await publicS3Server.generateExternalUrl(`${UploadToolsS3Path}/${toolsetId}/logo`));
3078

3179
if (isToolSet) {
3280
tools.push({
3381
...rootMod,
3482
tags: rootMod.tags || [ToolTagEnum.enum.other],
3583
toolId: toolsetId,
36-
icon: rootMod.icon ?? '',
84+
icon: parentIcon,
3785
toolFilename: filename,
3886
cb: () => Promise.resolve({}),
3987
versionList: []
@@ -45,25 +93,80 @@ export const LoadToolsDev = async (filename: string): Promise<ToolType[]> => {
4593
const files = await readdir(childrenPath);
4694
for (const file of files) {
4795
const childPath = join(childrenPath, file);
96+
97+
// Handle static files for child tools
98+
try {
99+
// Find logo files using glob pattern for child tool
100+
const childLogoFiles = await glob(`${childPath}/logo.*`);
101+
const childReadmeFile = join(childPath, 'README.md');
102+
103+
// Upload child logo files if found
104+
for (const logoPath of childLogoFiles) {
105+
try {
106+
const logoFilename = logoPath.split('/').pop()!;
107+
const logoNameWithoutExt = logoFilename.split('.').slice(0, -1).join('.');
108+
await publicS3Server.uploadFileAdvanced({
109+
path: logoPath,
110+
defaultFilename: logoNameWithoutExt,
111+
prefix: UploadToolsS3Path + '/' + toolsetId + '/' + file,
112+
keepRawFilename: true
113+
});
114+
addLog.info(
115+
`Uploaded child logo file: ${logoPath} to ${UploadToolsS3Path}/${toolsetId}/${file}/${logoNameWithoutExt}`
116+
);
117+
} catch (error) {
118+
addLog.warn(`Failed to upload child logo file ${logoPath}: ${error}`);
119+
}
120+
}
121+
122+
// Upload child README.md if it exists
123+
if (existsSync(childReadmeFile)) {
124+
try {
125+
await publicS3Server.uploadFileAdvanced({
126+
path: childReadmeFile,
127+
prefix: UploadToolsS3Path + '/' + toolsetId + '/' + file,
128+
keepRawFilename: true
129+
});
130+
addLog.info(
131+
`Uploaded child README.md: ${childReadmeFile} to ${UploadToolsS3Path}/${toolsetId}/${file}/README.md`
132+
);
133+
} catch (error) {
134+
addLog.warn(`Failed to upload child README.md ${childReadmeFile}: ${error}`);
135+
}
136+
}
137+
} catch (error) {
138+
addLog.warn(`Failed to upload static files for child tool ${file}: ${error}`);
139+
}
140+
48141
const childMod = (await import(childPath)).default as ToolType;
49142
const toolId = childMod.toolId || `${toolsetId}/${file}`;
143+
144+
const childIcon =
145+
childMod.icon ??
146+
(await publicS3Server.generateExternalUrl(
147+
`${UploadToolsS3Path}/${toolsetId}/${file}/logo`
148+
));
50149
children.push({
51150
...childMod,
52151
toolId,
53152
toolFilename: filename,
54-
icon: childMod.icon ?? ''
153+
icon: childIcon
55154
});
56155
}
57156
}
58157

59158
tools.push(...children);
60159
} else {
61160
// is not toolset
161+
const icon =
162+
rootMod.icon ??
163+
(await publicS3Server.generateExternalUrl(`${UploadToolsS3Path}/${toolsetId}/logo`));
164+
62165
tools.push({
63166
...(rootMod as ToolType),
64167
tags: rootMod.tags || [ToolTagEnum.enum.other],
65168
toolId: toolsetId,
66-
icon: rootMod.icon ?? '',
169+
icon,
67170
toolFilename: filename,
68171
versionList: []
69172
});

modules/tool/type/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ export const ToolSimpleSchema = ToolDetailSchema.omit({
2626
versionList: true
2727
});
2828

29-
export type ToolDetailItemType = z.infer<typeof ToolDetailSchema>;
29+
export type ToolDetailType = z.infer<typeof ToolDetailSchema>;
30+
export type ToolSimpleType = z.infer<typeof ToolSimpleSchema>;

modules/tool/type/formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ToolSetType, ToolType } from '.';
2-
import { ToolDetailSchema, type ToolDetailItemType } from './api';
2+
import { ToolDetailSchema, type ToolDetailType } from './api';
33

44
// export const formatToolDetail = (mod: ToolType | ToolSetType): ToolDetailItemType => {
55
// return {

modules/tool/type/tags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const ToolTagEnum = z.enum([
1515
'other'
1616
]);
1717

18-
export const ToolTypeMap = {
18+
export const ToolTagsNameMap = {
1919
[ToolTagEnum.enum.tools]: {
2020
en: 'tools',
2121
'zh-CN': '工具',

modules/tool/utils.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ import { ToolDetailSchema } from './type/api';
2020

2121
const parseMod = async ({
2222
rootMod,
23-
staticFileObjectNames,
2423
filename
2524
}: {
2625
rootMod: ToolSetType | ToolType;
27-
staticFileObjectNames: string[];
2826
filename: string;
2927
}) => {
3028
const tools: ToolType[] = [];
@@ -34,13 +32,9 @@ const parseMod = async ({
3432
if (checkRootModToolSet(rootMod)) {
3533
const toolsetId = rootMod.toolId;
3634

37-
const parentIconName = staticFileObjectNames.find((item) =>
38-
item.startsWith(`${UploadToolsS3Path}/${toolsetId}/logo.`)
39-
);
40-
4135
const parentIcon =
4236
rootMod.icon ||
43-
(parentIconName ? await publicS3Server.generateExternalUrl(parentIconName) : '');
37+
(await publicS3Server.generateExternalUrl(`${UploadToolsS3Path}/${toolsetId}/logo`));
4438

4539
// push parent
4640
tools.push({
@@ -57,13 +51,12 @@ const parseMod = async ({
5751

5852
for (const child of children) {
5953
const childToolId = child.toolId;
60-
const childIconName = staticFileObjectNames.find((item) =>
61-
item.startsWith(`${UploadToolsS3Path}/${toolsetId}/${childToolId}/logo.`)
62-
);
6354

6455
const childIcon =
6556
child.icon ||
66-
(childIconName ? await publicS3Server.generateExternalUrl(childIconName) : '');
57+
(await publicS3Server.generateExternalUrl(
58+
`${UploadToolsS3Path}/${toolsetId}/${childToolId}/logo`
59+
));
6760

6861
tools.push({
6962
...child,
@@ -79,12 +72,9 @@ const parseMod = async ({
7972
} else {
8073
const toolId = rootMod.toolId;
8174

82-
const parentIconName = staticFileObjectNames.find((item) =>
83-
item.startsWith(`${UploadToolsS3Path}/${toolId}/logo.`)
84-
);
8575
const icon =
8676
rootMod.icon ||
87-
(parentIconName ? await publicS3Server.generateExternalUrl(parentIconName) : '');
77+
(await publicS3Server.generateExternalUrl(`${UploadToolsS3Path}/${toolId}/logo`));
8878

8979
tools.push({
9080
...rootMod,
@@ -165,11 +155,7 @@ export const LoadToolsByFilename = async (filename: string): Promise<ToolType[]>
165155
addLog.error(`Can not parse toolId, filename: ${filename}`);
166156
}
167157

168-
const staticFileObjectNames = await publicS3Server.getFiles(
169-
`${UploadToolsS3Path}/${rootMod.toolId}`
170-
);
171-
172-
return parseMod({ rootMod, staticFileObjectNames, filename });
158+
return parseMod({ rootMod, filename });
173159
};
174160

175161
export const parseUploadedTool = async (objectName: string) => {
@@ -200,7 +186,7 @@ export const parseUploadedTool = async (objectName: string) => {
200186
// console.log(path, file);
201187
const { objectName } = await publicS3Server.uploadFileAdvanced({
202188
path,
203-
defaultFilename: file,
189+
defaultFilename: file.split('.').splice(0, -1).join('.'), // remove the extention name
204190
prefix: `${UploadToolsS3Path}/${mod.toolId}`,
205191
keepRawFilename: true
206192
});
@@ -236,7 +222,6 @@ export const parseUploadedTool = async (objectName: string) => {
236222

237223
const tools = await parseMod({
238224
rootMod: mod,
239-
staticFileObjectNames,
240225
filename: join(tempDir, 'index.js')
241226
});
242227
return tools.map((item) => ToolDetailSchema.parse(item));

0 commit comments

Comments
 (0)