import type { Dirent } from 'fs' ; import * as fs from 'fs' ; import * as path from 'path' ; import { computed , shallowRef , unref } from '@vue/reactivity' ; import { ipcMain } from 'electron' ; import { userData } from '../utils/directory' ; import { provideIPC } from '../utils/hooks' ; import { useEnabledAddons } from './settings' ; interface AddonInfo { entry : string ; manifest : any ; type : 'builtin' | 'user' ; } const discoveredAddonsRef = shallowRef < Record < string , AddonInfo > > ( { } ) ; function useDiscoveredAddons ( ) { return discoveredAddonsRef ; } async function discoverAddons ( ) { const paths = [ { type : 'user' as const , base : userData . file ( 'addons' ) } , { type : 'builtin' as const , base : path . join ( __dirname , '../../addons' ) } , ] ; const result = { } ; for ( const { type , base } of paths ) { let dirents = [ ] ; try { dirents = await fs . promises . readdir ( base , { withFileTypes : true } ) ; } catch { } const directories = dirents . filter ( dirent => dirent . isDirectory ( ) || path . extname ( dirent . name ) === '.asar' ) . map ( dirent => dirent . name ) . filter ( name => ! result [ name ] ) ; for ( const file of directories ) { try { const manifest = require ( path . join ( base , file , 'package.json' ) ) ; const entry = path . join ( base , file , 'index.js' ) ; const name = path . basename ( file , '.asar' ) ; result [ name ] = { type , entry , manifest } ; } catch { } } } discoveredAddonsRef . value = result ; } const enabledAddonsRef = useEnabledAddons ( ) ; const addonsRef = computed ( ( ) => { const discoveredAddons = unref ( discoveredAddonsRef ) ; const enabledAddons = unref ( enabledAddonsRef ) ; return enabledAddons . filter ( item => discoveredAddons [ item ] ) ; } ) ; function useAddons ( ) { return addonsRef ; } function handleAddonMessages ( ) { provideIPC ( 'discovered-addons' , discoveredAddonsRef ) ; provideIPC ( 'addons' , addonsRef ) ; ipcMain . handle ( 'refresh-addons' , ( ) => { discoverAddons ( ) ; } ) ; ipcMain . handle ( 'get-addon-info' , ( event , name ) => { const discoveredAddons = unref ( discoveredAddonsRef ) ; event . returnValue = discoveredAddons [ name ] ; } ) ; discoverAddons ( ) ; } export { useDiscoveredAddons , discoverAddons , useAddons , handleAddonMessages , } ;