@@ -2,16 +2,17 @@ import { isProd } from '@/constants';
22import { addLog } from '@/utils/log' ;
33import { existsSync } from 'fs' ;
44import { readdir } from 'fs/promises' ;
5- import { join } from 'path/posix' ;
5+ import { join } from 'path' ;
6+ import { glob } from 'glob' ;
67import { basePath , devToolIds , UploadToolsS3Path } from './constants' ;
78import type { ToolType , ToolSetType } from './type' ;
89import { 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-
1516export 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 } ) ;
0 commit comments