code-center/src/load-apps.ts

29 lines
743 B
TypeScript

import path from 'path'
import * as glob from 'fast-glob'
import { fileURLToPath } from 'url'
import { dirname } from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const files = glob.sync(path.resolve(__dirname, './apps/**/index.mjs'))
export const loadApps = async (app: any) => {
const directory = files.map((file: string) => {
const dirname = path.dirname(file)
const lastDirectory = path.basename(dirname)
return lastDirectory
})
console.log('directory', directory)
for (const file of files) {
try {
const module = await import(file)
if (module.render) {
module.render(app)
}
} catch (error) {
console.error(error)
}
}
}