@@ -4,7 +4,7 @@ import { Directory, Document, type Entry } from "@opencode-ai/schema/config"
44import { ConfigPlugin } from "@opencode-ai/schema/config/plugin"
55import { FSUtil } from "@opencode-ai/util/fs-util"
66import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
7- import { Context , Effect , Layer , Option , PubSub , Scope , Stream } from "effect"
7+ import { Context , Effect , Layer , Option , Predicate , PubSub , Schema , Scope , Stream } from "effect"
88import path from "path"
99import { fileURLToPath } from "url"
1010import { Config } from "../../config"
@@ -154,19 +154,67 @@ const scan = Effect.fn("ConfigPluginSource.scan")(function* (
154154} )
155155
156156const sourceDirectories = [ "plugin" , "plugins" ] as const
157+ const Package = Schema . Struct ( {
158+ exports : Schema . optional ( Schema . Unknown ) ,
159+ module : Schema . optional ( Schema . Unknown ) ,
160+ main : Schema . optional ( Schema . Unknown ) ,
161+ } )
162+ const decodePackage = Schema . decodeUnknownOption ( Package )
157163
158164function discoverDirectory ( fs : FSUtil . Interface , directory : string ) {
159165 return Effect . gen ( function * ( ) {
160- const files = yield * fs
161- . scan ( `{${ sourceDirectories . join ( "," ) } }/*.{ts,js}` , {
162- cwd : directory ,
163- absolute : true ,
164- include : "file" ,
165- dot : true ,
166- symlink : true ,
167- } )
168- . pipe ( Effect . orElseSucceed ( ( ) => [ ] ) )
169- return files . sort ( ) . map ( ( target ) : Operation => ( { type : "add" , target, options : { } } ) )
166+ const children = ( yield * Effect . forEach ( sourceDirectories , ( source ) =>
167+ fs . readDirectoryEntries ( path . join ( directory , source ) ) . pipe (
168+ Effect . orElseSucceed ( ( ) => [ ] ) ,
169+ Effect . map ( ( entries ) =>
170+ entries . map ( ( entry ) => ( { ...entry , target : path . join ( directory , source , entry . name ) } ) ) ,
171+ ) ,
172+ ) ,
173+ ) )
174+ . flat ( )
175+ . sort ( ( a , b ) => ( a . target < b . target ? - 1 : a . target > b . target ? 1 : 0 ) )
176+ const targets = yield * Effect . forEach ( children , ( entry ) => discoverChild ( fs , entry ) )
177+ return targets . flatMap ( Option . toArray ) . map ( ( target ) : Operation => ( { type : "add" , target, options : { } } ) )
178+ } )
179+ }
180+
181+ function discoverChild ( fs : FSUtil . Interface , entry : FSUtil . DirEntry & { target : string } ) {
182+ return Effect . gen ( function * ( ) {
183+ const source = entry . target . endsWith ( ".ts" ) || entry . target . endsWith ( ".js" )
184+ if ( entry . type === "file" && source ) return Option . some ( entry . target )
185+ if ( entry . type === "directory" ) return yield * discoverPackage ( fs , entry . target )
186+ if ( entry . type !== "symlink" ) return Option . none < string > ( )
187+ if ( source && ( yield * fs . isFile ( entry . target ) ) ) return Option . some ( entry . target )
188+ if ( yield * fs . isDir ( entry . target ) ) return yield * discoverPackage ( fs , entry . target )
189+ return Option . none < string > ( )
190+ } )
191+ }
192+
193+ function discoverPackage ( fs : FSUtil . Interface , directory : string ) {
194+ return Effect . gen ( function * ( ) {
195+ const root = yield * fs . resolve ( directory )
196+ const manifest = yield * fs
197+ . readJson ( path . join ( directory , "package.json" ) )
198+ . pipe ( Effect . map ( decodePackage ) , Effect . orElseSucceed ( Option . none ) )
199+ const configured = Option . isSome ( manifest )
200+ ? [ manifest . value . exports , manifest . value . module , manifest . value . main ] . filter ( Predicate . isString )
201+ : [ ]
202+ return yield * Effect . findFirst (
203+ [ ...configured , "index.ts" , "index.js" ]
204+ . filter ( ( entry ) => ! path . isAbsolute ( entry ) )
205+ . map ( ( entry ) => path . resolve ( directory , entry ) )
206+ . filter ( ( entry ) => FSUtil . contains ( directory , entry ) ) ,
207+ ( entry ) =>
208+ fs
209+ . isFile ( entry )
210+ . pipe (
211+ Effect . flatMap ( ( exists ) =>
212+ exists
213+ ? fs . resolve ( entry ) . pipe ( Effect . map ( ( resolved ) => FSUtil . contains ( root , resolved ) ) )
214+ : Effect . succeed ( false ) ,
215+ ) ,
216+ ) ,
217+ )
170218 } )
171219}
172220
0 commit comments