plugins.ts 1.1 KB
Newer Older
E
.  
Eugene Pankov 已提交
1 2 3 4 5 6 7
import * as fs from 'fs-promise'
import * as path from 'path'

let nodeRequire = (<any>global).require
let module = nodeRequire('module')
nodeRequire.main.paths.map(x => module.globalPaths.push(x))
if (process.env.TERMINUS_PLUGINS) {
E
.  
Eugene Pankov 已提交
8
    process.env.TERMINUS_PLUGINS.split(':').map(x => module.globalPaths.unshift(x))
E
.  
Eugene Pankov 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
}

export async function loadPlugins (): Promise<any[]> {
    let paths = module.globalPaths
    let plugins: any[] = []
    for (let pluginDir of paths) {
        if (!await fs.exists(pluginDir)) {
            continue
        }
        for (let pluginName of await fs.readdir(pluginDir)) {
            if (/^terminus-/.exec(pluginName)) {
                let pluginPath = path.join(pluginDir, pluginName)
                console.info(`Loading ${pluginName}: ${nodeRequire.resolve(pluginPath)}`)
                try {
                    let pluginModule = nodeRequire(pluginPath)
                    plugins.push(pluginModule)
                } catch (error) {
                    console.error(`Could not load ${pluginName}:`, error)
                }
            }
        }
    }
    return plugins
}