plugins.ts 5.8 KB
Newer Older
E
Eugene Pankov 已提交
1
import * as fs from 'mz/fs'
E
.  
Eugene Pankov 已提交
2
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

E
Eugene Pankov 已提交
17
if (process.env.TERMINUS_DEV) {
E
.  
Eugene Pankov 已提交
18 19 20
    nodeModule.globalPaths.unshift(path.dirname(require('electron').remote.app.getAppPath()))
}

E
Eugene Pankov 已提交
21
const builtinPluginsPath = process.env.TERMINUS_DEV ? path.dirname(require('electron').remote.app.getAppPath()) : path.join((process as any).resourcesPath, 'builtin-plugins')
E
wip  
Eugene Pankov 已提交
22 23

const userPluginsPath = path.join(
E
.  
Eugene Pankov 已提交
24 25 26
    require('electron').remote.app.getPath('appData'),
    'terminus',
    'plugins',
E
wip  
Eugene Pankov 已提交
27 28
)

29 30 31 32
if (!fs.existsSync(userPluginsPath)) {
    fs.mkdir(userPluginsPath)
}

E
wip  
Eugene Pankov 已提交
33 34
Object.assign(window, { builtinPluginsPath, userPluginsPath })
nodeModule.globalPaths.unshift(builtinPluginsPath)
E
.  
Eugene Pankov 已提交
35
nodeModule.globalPaths.unshift(path.join(userPluginsPath, 'node_modules'))
E
wip  
Eugene Pankov 已提交
36
// nodeModule.globalPaths.unshift(path.join((process as any).resourcesPath, 'app.asar', 'node_modules'))
E
.  
Eugene Pankov 已提交
37
if (process.env.TERMINUS_PLUGINS) {
E
wip  
Eugene Pankov 已提交
38
    process.env.TERMINUS_PLUGINS.split(':').map(x => nodeModule.globalPaths.push(normalizePath(x)))
E
.  
Eugene Pankov 已提交
39 40
}

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

E
wip  
Eugene Pankov 已提交
43
export interface IPluginInfo {
E
.  
Eugene Pankov 已提交
44
    name: string
E
wip  
Eugene Pankov 已提交
45 46 47 48
    description: string
    packageName: string
    isBuiltin: boolean
    version: string
E
Eugene Pankov 已提交
49
    author: string
E
wip  
Eugene Pankov 已提交
50 51 52
    homepage?: string
    path?: string
    info?: any
E
.  
Eugene Pankov 已提交
53 54
}

E
wip  
Eugene Pankov 已提交
55 56 57 58 59 60 61 62 63
const builtinModules = [
    '@angular/animations',
    '@angular/common',
    '@angular/compiler',
    '@angular/core',
    '@angular/forms',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    '@ng-bootstrap/ng-bootstrap',
E
Eugene Pankov 已提交
64
    'ngx-toastr',
E
wip  
Eugene Pankov 已提交
65
    'rxjs',
E
Eugene Pankov 已提交
66
    'rxjs/operators',
67
    'rxjs-compat/Subject',
E
wip  
Eugene Pankov 已提交
68 69 70 71 72 73
    'terminus-core',
    'terminus-settings',
    'terminus-terminal',
    'zone.js/dist/zone.js',
]

74 75
const cachedBuiltinModules = {}
builtinModules.forEach(m => {
76 77
    const label = 'Caching ' + m
    console.time(label)
78
    cachedBuiltinModules[m] = nodeRequire(m)
79
    console.timeEnd(label)
80 81
})

82 83
const originalRequire = (global as any).require
;(global as any).require = function (query) {
84 85 86 87 88 89
    if (cachedBuiltinModules[query]) {
        return cachedBuiltinModules[query]
    }
    return originalRequire.apply(this, arguments)
}

E
wip  
Eugene Pankov 已提交
90
export async function findPlugins (): Promise<IPluginInfo[]> {
E
.  
Eugene Pankov 已提交
91
    let paths = nodeModule.globalPaths
E
wip  
Eugene Pankov 已提交
92
    let foundPlugins: IPluginInfo[] = []
E
Eugene Pankov 已提交
93
    let candidateLocations: { pluginDir: string, packageName: string }[] = []
94
    const PREFIX = 'terminus-'
E
.  
Eugene Pankov 已提交
95

E
.  
Eugene Pankov 已提交
96
    for (let pluginDir of paths) {
E
.  
Eugene Pankov 已提交
97
        pluginDir = normalizePath(pluginDir)
E
.  
Eugene Pankov 已提交
98 99 100
        if (!await fs.exists(pluginDir)) {
            continue
        }
E
.  
Eugene Pankov 已提交
101
        let pluginNames = await fs.readdir(pluginDir)
E
wip  
Eugene Pankov 已提交
102 103 104
        if (await fs.exists(path.join(pluginDir, 'package.json'))) {
            candidateLocations.push({
                pluginDir: path.dirname(pluginDir),
E
Eugene Pankov 已提交
105
                packageName: path.basename(pluginDir)
E
wip  
Eugene Pankov 已提交
106 107
            })
        }
E
Eugene Pankov 已提交
108
        for (let packageName of pluginNames) {
109 110 111
            if (packageName.startsWith(PREFIX)) {
                candidateLocations.push({ pluginDir, packageName })
            }
E
wip  
Eugene Pankov 已提交
112 113
        }
    }
E
.  
Eugene Pankov 已提交
114

E
Eugene Pankov 已提交
115 116
    for (let { pluginDir, packageName } of candidateLocations) {
        let pluginPath = path.join(pluginDir, packageName)
E
wip  
Eugene Pankov 已提交
117 118 119 120 121
        let infoPath = path.join(pluginPath, 'package.json')
        if (!await fs.exists(infoPath)) {
            continue
        }

122
        let name = packageName.substring(PREFIX.length)
E
Eugene Pankov 已提交
123 124 125 126

        if (foundPlugins.some(x => x.name === name)) {
            console.info(`Plugin ${packageName} already exists, overriding`)
            foundPlugins = foundPlugins.filter(x => x.name !== name)
E
wip  
Eugene Pankov 已提交
127
        }
E
.  
Eugene Pankov 已提交
128

E
wip  
Eugene Pankov 已提交
129
        try {
E
lint  
Eugene Pankov 已提交
130
            let info = JSON.parse(await fs.readFile(infoPath, { encoding: 'utf-8' }))
E
Eugene Pankov 已提交
131
            if (!info.keywords || !(info.keywords.includes('terminus-plugin') || info.keywords.includes('terminus-builtin-plugin'))) {
E
wip  
Eugene Pankov 已提交
132
                continue
E
.  
Eugene Pankov 已提交
133
            }
E
Eugene Pankov 已提交
134 135
            let author = info.author
            author = author.name || author
E
wip  
Eugene Pankov 已提交
136
            foundPlugins.push({
E
Eugene Pankov 已提交
137 138
                name: name,
                packageName: packageName,
E
wip  
Eugene Pankov 已提交
139 140 141
                isBuiltin: pluginDir === builtinPluginsPath,
                version: info.version,
                description: info.description,
E
Eugene Pankov 已提交
142
                author,
E
wip  
Eugene Pankov 已提交
143 144 145 146
                path: pluginPath,
                info,
            })
        } catch (error) {
E
Eugene Pankov 已提交
147
            console.error('Cannot load package info for', packageName)
E
.  
Eugene Pankov 已提交
148
        }
E
.  
Eugene Pankov 已提交
149
    }
E
.  
Eugene Pankov 已提交
150

E
wip  
Eugene Pankov 已提交
151
    (window as any).installedPlugins = foundPlugins
E
.  
Eugene Pankov 已提交
152 153 154
    return foundPlugins
}

E
wip  
Eugene Pankov 已提交
155
export async function loadPlugins (foundPlugins: IPluginInfo[], progress: ProgressCallback): Promise<any[]> {
E
.  
Eugene Pankov 已提交
156 157
    let plugins: any[] = []
    progress(0, 1)
E
.  
Eugene Pankov 已提交
158 159
    let index = 0
    for (let foundPlugin of foundPlugins) {
E
lint  
Eugene Pankov 已提交
160
        console.info(`Loading ${foundPlugin.name}: ${nodeRequire.resolve(foundPlugin.path)}`)
E
.  
Eugene Pankov 已提交
161 162
        progress(index, foundPlugins.length)
        try {
163 164
            const label = 'Loading ' + foundPlugin.name
            console.time(label)
E
Eugene Pankov 已提交
165 166 167 168
            let packageModule = nodeRequire(foundPlugin.path)
            let pluginModule = packageModule.default.forRoot ? packageModule.default.forRoot() : packageModule.default
            pluginModule['pluginName'] = foundPlugin.name
            pluginModule['bootstrap'] = packageModule.bootstrap
E
.  
Eugene Pankov 已提交
169
            plugins.push(pluginModule)
170
            console.timeEnd(label)
E
.  
Eugene Pankov 已提交
171 172 173
        } catch (error) {
            console.error(`Could not load ${foundPlugin.name}:`, error)
        }
E
.  
Eugene Pankov 已提交
174 175
        index++
    }
E
.  
Eugene Pankov 已提交
176
    progress(1, 1)
E
.  
Eugene Pankov 已提交
177 178
    return plugins
}