plugins.ts 2.3 KB
Newer Older
E
.  
Eugene Pankov 已提交
1 2
import * as fs from 'fs-promise'
import * as path from 'path'
E
.  
Eugene Pankov 已提交
3 4 5 6 7 8 9 10 11 12 13 14
const nodeModule = (<any>global).require('module')

function normalizePath (path: string): string {
    const cygwinPrefix = '/cygdrive/'
    if (path.startsWith(cygwinPrefix)) {
        path = path.substring(cygwinPrefix.length).replace('/', '\\')
        path = path[0] + ':' + path.substring(1)
    }
    return path
};

(<any>global).require.main.paths.map(x => nodeModule.globalPaths.push(normalizePath(x)))
E
.  
Eugene Pankov 已提交
15 16

if (process.env.TERMINUS_PLUGINS) {
E
.  
Eugene Pankov 已提交
17
    process.env.TERMINUS_PLUGINS.split(':').map(x => nodeModule.globalPaths.unshift(normalizePath(x)))
E
.  
Eugene Pankov 已提交
18 19
}

E
.  
Eugene Pankov 已提交
20 21
export declare type ProgressCallback = (current, total) => void

E
.  
Eugene Pankov 已提交
22
interface IPluginEntry {
E
.  
Eugene Pankov 已提交
23 24
    name: string
    path: string
E
.  
Eugene Pankov 已提交
25
    info: any
E
.  
Eugene Pankov 已提交
26 27
}

E
.  
Eugene Pankov 已提交
28
export async function findPlugins (): Promise<IPluginEntry[]> {
E
.  
Eugene Pankov 已提交
29
    let paths = nodeModule.globalPaths
E
.  
Eugene Pankov 已提交
30
    let foundPlugins: IPluginEntry[] = []
E
.  
Eugene Pankov 已提交
31

E
.  
Eugene Pankov 已提交
32
    for (let pluginDir of paths) {
E
.  
Eugene Pankov 已提交
33
        pluginDir = normalizePath(pluginDir)
E
.  
Eugene Pankov 已提交
34 35 36
        if (!await fs.exists(pluginDir)) {
            continue
        }
E
.  
Eugene Pankov 已提交
37
        let pluginNames = await fs.readdir(pluginDir)
E
.  
Eugene Pankov 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        for (let pluginName of pluginNames.filter(x => /^terminus-/.exec(x))) {
            let pluginPath = path.join(pluginDir, pluginName)
            let infoPath = path.join(pluginPath, 'package.json')
            if (!await fs.exists(infoPath)) {
                continue
            }
            try {
                foundPlugins.push({
                    name: pluginName,
                    path: pluginPath,
                    info: await fs.readJson(infoPath),
                })
            } catch (error) {
                console.error('Cannot load package info for', pluginName)
            }
        }
E
.  
Eugene Pankov 已提交
54
    }
E
.  
Eugene Pankov 已提交
55 56 57 58 59 60 61

    return foundPlugins
}

export function loadPlugins (foundPlugins: IPluginEntry[], progress: ProgressCallback): any[] {
    let plugins: any[] = []
    progress(0, 1)
E
.  
Eugene Pankov 已提交
62 63 64 65 66 67 68 69 70 71 72
    foundPlugins.forEach((foundPlugin, index) => {
        console.info(`Loading ${foundPlugin.name}: ${(<any>global).require.resolve(foundPlugin.path)}`)
        progress(index, foundPlugins.length)
        try {
            let pluginModule = (<any>global).require(foundPlugin.path)
            plugins.push(pluginModule)
        } catch (error) {
            console.error(`Could not load ${foundPlugin.name}:`, error)
        }
    })
    progress(1, 1)
E
.  
Eugene Pankov 已提交
73 74
    return plugins
}