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

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
E
lint  
Eugene Pankov 已提交
13
}
E
.  
Eugene Pankov 已提交
14

E
lint  
Eugene Pankov 已提交
15
nodeRequire.main.paths.map(x => nodeModule.globalPaths.push(normalizePath(x)))
E
.  
Eugene Pankov 已提交
16 17 18 19 20 21 22

if (process.env.DEV) {
    nodeModule.globalPaths.unshift(path.dirname(require('electron').remote.app.getAppPath()))
}

nodeModule.globalPaths.unshift(path.join(
    path.dirname(require('electron').remote.app.getPath('exe')),
E
.  
Eugene Pankov 已提交
23
    (process.platform === 'darwin') ? '../Resources' : 'resources',
E
ю  
Eugene Pankov 已提交
24
    'builtin-plugins/node_modules',
E
.  
Eugene Pankov 已提交
25 26 27 28 29 30
))
nodeModule.globalPaths.unshift(path.join(
    require('electron').remote.app.getPath('appData'),
    'terminus',
    'plugins',
))
E
.  
Eugene Pankov 已提交
31 32

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

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

E
.  
Eugene Pankov 已提交
38
interface IPluginEntry {
E
.  
Eugene Pankov 已提交
39 40
    name: string
    path: string
E
.  
Eugene Pankov 已提交
41
    info: any
E
.  
Eugene Pankov 已提交
42 43
}

E
.  
Eugene Pankov 已提交
44
export async function findPlugins (): Promise<IPluginEntry[]> {
E
.  
Eugene Pankov 已提交
45
    let paths = nodeModule.globalPaths
E
.  
Eugene Pankov 已提交
46
    let foundPlugins: IPluginEntry[] = []
E
.  
Eugene Pankov 已提交
47

E
.  
Eugene Pankov 已提交
48
    for (let pluginDir of paths) {
E
.  
Eugene Pankov 已提交
49
        pluginDir = normalizePath(pluginDir)
E
.  
Eugene Pankov 已提交
50 51 52
        if (!await fs.exists(pluginDir)) {
            continue
        }
E
.  
Eugene Pankov 已提交
53
        let pluginNames = await fs.readdir(pluginDir)
E
.  
Eugene Pankov 已提交
54 55 56 57 58 59
        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
            }
E
.  
Eugene Pankov 已提交
60 61 62 63 64

            if (foundPlugins.some(x => x.name === pluginName)) {
                console.info(`Plugin ${pluginName} already exists`)
            }

E
.  
Eugene Pankov 已提交
65 66 67 68 69 70 71 72 73 74
            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 已提交
75
    }
E
.  
Eugene Pankov 已提交
76 77 78 79

    return foundPlugins
}

E
.  
Eugene Pankov 已提交
80
export async function loadPlugins (foundPlugins: IPluginEntry[], progress: ProgressCallback): Promise<any[]> {
E
.  
Eugene Pankov 已提交
81 82
    let plugins: any[] = []
    progress(0, 1)
E
.  
Eugene Pankov 已提交
83 84
    let index = 0
    for (let foundPlugin of foundPlugins) {
E
lint  
Eugene Pankov 已提交
85
        console.info(`Loading ${foundPlugin.name}: ${nodeRequire.resolve(foundPlugin.path)}`)
E
.  
Eugene Pankov 已提交
86 87
        progress(index, foundPlugins.length)
        try {
E
lint  
Eugene Pankov 已提交
88
            let pluginModule = nodeRequire(foundPlugin.path)
E
.  
Eugene Pankov 已提交
89 90 91 92
            plugins.push(pluginModule)
        } catch (error) {
            console.error(`Could not load ${foundPlugin.name}:`, error)
        }
E
.  
Eugene Pankov 已提交
93 94 95
        await delay(1)
        index++
    }
E
.  
Eugene Pankov 已提交
96
    progress(1, 1)
E
.  
Eugene Pankov 已提交
97 98
    return plugins
}