@@ -29,6 +29,12 @@ const PluginModule = Schema.Struct({
2929 ] ) ,
3030} )
3131
32+ const PluginPackage = Schema . Struct ( {
33+ exports : Schema . optional ( Schema . Unknown ) ,
34+ main : Schema . optional ( Schema . String ) ,
35+ module : Schema . optional ( Schema . String ) ,
36+ } )
37+
3238export const Plugin = define ( {
3339 id : "config-plugin" ,
3440 effect : Effect . fn ( function * ( ctx ) {
@@ -65,8 +71,30 @@ export const Plugin = define({
6571 symlink : true ,
6672 } )
6773 . pipe ( Effect . orElseSucceed ( ( ) => [ ] ) )
74+ const directories = yield * fs
75+ . glob ( "{plugin,plugins}/*" , {
76+ cwd : entry . path ,
77+ absolute : true ,
78+ include : "all" ,
79+ dot : true ,
80+ symlink : true ,
81+ } )
82+ . pipe (
83+ Effect . flatMap ( ( items ) =>
84+ Effect . filter ( items , ( item ) => fs . isDir ( item ) , {
85+ concurrency : "unbounded" ,
86+ } ) ,
87+ ) ,
88+ Effect . orElseSucceed ( ( ) => [ ] ) ,
89+ )
90+ const packages = yield * Effect . forEach (
91+ directories . sort ( ) ,
92+ ( directory ) => resolvePackageEntrypoint ( fs , directory ) ,
93+ { concurrency : "unbounded" } ,
94+ ) . pipe ( Effect . map ( ( items ) => items . filter ( ( item ) : item is string => item !== undefined ) ) )
6895 files . sort ( )
6996 for ( const file of files ) configured . push ( { package : file } )
97+ for ( const file of packages ) configured . push ( { package : file } )
7098 }
7199 }
72100
@@ -89,3 +117,18 @@ export const Plugin = define({
89117 } )
90118 } ) ,
91119} )
120+
121+ const resolvePackageEntrypoint = Effect . fnUntraced ( function * ( fs : FSUtil . Interface , directory : string ) {
122+ const pkg = yield * fs . readJson ( path . join ( directory , "package.json" ) ) . pipe (
123+ Effect . flatMap ( Schema . decodeUnknownEffect ( PluginPackage ) ) ,
124+ Effect . catch ( ( ) => Effect . succeed ( undefined ) ) ,
125+ )
126+ const exported = typeof pkg ?. exports === "string" ? pkg . exports : undefined
127+ const entries = [ exported , pkg ?. module , pkg ?. main , "index.ts" , "index.js" ]
128+
129+ return yield * Effect . forEach ( entries , ( entry ) => {
130+ if ( ! entry ) return Effect . succeed ( undefined )
131+ const file = path . resolve ( directory , entry )
132+ return fs . isFile ( file ) . pipe ( Effect . map ( ( exists ) => ( exists ? file : undefined ) ) )
133+ } ) . pipe ( Effect . map ( ( items ) => items . find ( ( item ) : item is string => item !== undefined ) ) )
134+ } )
0 commit comments