From 8a8f89b386632c27ec72a16e1bed5a55aa9af6c5 Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Sat, 1 Jul 2017 00:55:21 +0200 Subject: [PATCH] wip --- app/src/entry.ts | 2 +- app/src/plugins.ts | 97 +++++++++++++++++++++++++++++-------------- app/webpack.config.js | 2 +- 3 files changed, 68 insertions(+), 33 deletions(-) diff --git a/app/src/entry.ts b/app/src/entry.ts index 6ece8d1b..257d6fbe 100644 --- a/app/src/entry.ts +++ b/app/src/entry.ts @@ -1,4 +1,4 @@ -import 'zone.js/dist/zone.js' +import 'zone.js' import 'core-js/es7/reflect' import 'core-js/core/delay' import 'rxjs' diff --git a/app/src/plugins.ts b/app/src/plugins.ts index d67dc281..58786a11 100644 --- a/app/src/plugins.ts +++ b/app/src/plugins.ts @@ -20,11 +20,7 @@ if (process.env.DEV) { nodeModule.globalPaths.unshift(path.dirname(require('electron').remote.app.getAppPath())) } -const builtinPluginsPath = path.join( - path.dirname(require('electron').remote.app.getPath('exe')), - (process.platform === 'darwin') ? '../Resources' : 'resources', - 'builtin-plugins', -) +const builtinPluginsPath = path.join((process as any).resourcesPath, 'builtin-plugins') const userPluginsPath = path.join( require('electron').remote.app.getPath('appData'), @@ -35,9 +31,9 @@ const userPluginsPath = path.join( Object.assign(window, { builtinPluginsPath, userPluginsPath }) nodeModule.globalPaths.unshift(builtinPluginsPath) nodeModule.globalPaths.unshift(path.join(userPluginsPath, 'node_modules')) - +// nodeModule.globalPaths.unshift(path.join((process as any).resourcesPath, 'app.asar', 'node_modules')) if (process.env.TERMINUS_PLUGINS) { - process.env.TERMINUS_PLUGINS.split(':').map(x => nodeModule.globalPaths.unshift(normalizePath(x))) + process.env.TERMINUS_PLUGINS.split(':').map(x => nodeModule.globalPaths.push(normalizePath(x))) } export declare type ProgressCallback = (current, total) => void @@ -53,9 +49,26 @@ export interface IPluginInfo { info?: any } +const builtinModules = [ + '@angular/animations', + '@angular/common', + '@angular/compiler', + '@angular/core', + '@angular/forms', + '@angular/platform-browser', + '@angular/platform-browser-dynamic', + '@ng-bootstrap/ng-bootstrap', + 'rxjs', + 'terminus-core', + 'terminus-settings', + 'terminus-terminal', + 'zone.js/dist/zone.js', +] + export async function findPlugins (): Promise { let paths = nodeModule.globalPaths let foundPlugins: IPluginInfo[] = [] + let candidateLocations: { pluginDir: string, pluginName: string }[] = [] for (let pluginDir of paths) { pluginDir = normalizePath(pluginDir) @@ -63,32 +76,44 @@ export async function findPlugins (): Promise { continue } let pluginNames = await fs.readdir(pluginDir) - 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)) { + if (await fs.exists(path.join(pluginDir, 'package.json'))) { + candidateLocations.push({ + pluginDir: path.dirname(pluginDir), + pluginName: path.basename(pluginDir) + }) + } + for (let pluginName of pluginNames) { + candidateLocations.push({ pluginDir, pluginName }) + } + } + + for (let { pluginDir, pluginName } of candidateLocations) { + let pluginPath = path.join(pluginDir, pluginName) + let infoPath = path.join(pluginPath, 'package.json') + if (!await fs.exists(infoPath)) { + continue + } + + if (foundPlugins.some(x => x.name === pluginName)) { + console.info(`Plugin ${pluginName} already exists`) + } + + try { + let info = JSON.parse(await fs.readFile(infoPath, {encoding: 'utf-8'})) + if (!info.keywords || info.keywords.indexOf('terminus-plugin') === -1) { continue } - - if (foundPlugins.some(x => x.name === pluginName)) { - console.info(`Plugin ${pluginName} already exists`) - } - - try { - let info = JSON.parse(await fs.readFile(infoPath, {encoding: 'utf-8'})) - console.log(pluginDir, builtinPluginsPath) - foundPlugins.push({ - name: pluginName.substring('terminus-'.length), - packageName: pluginName, - isBuiltin: pluginDir === builtinPluginsPath, - version: info.version, - description: info.description, - path: pluginPath, - info, - }) - } catch (error) { - console.error('Cannot load package info for', pluginName) - } + foundPlugins.push({ + name: pluginName.substring('terminus-'.length), + packageName: pluginName, + isBuiltin: pluginDir === builtinPluginsPath, + version: info.version, + description: info.description, + path: pluginPath, + info, + }) + } catch (error) { + console.error('Cannot load package info for', pluginName) } } @@ -103,6 +128,16 @@ export async function loadPlugins (foundPlugins: IPluginInfo[], progress: Progre for (let foundPlugin of foundPlugins) { console.info(`Loading ${foundPlugin.name}: ${nodeRequire.resolve(foundPlugin.path)}`) progress(index, foundPlugins.length) + // pre-inject builtin modules + builtinModules.forEach(moduleName => { + let mod = nodeRequire(moduleName) + let modPath = nodeRequire.resolve(moduleName) + let modSubpath = modPath.substring(modPath.indexOf(moduleName)) + console.log('injecting', moduleName, modPath) + let targetPath = path.join(foundPlugin.path, 'node_modules', modSubpath) + console.log(targetPath, modPath) + nodeRequire.cache[targetPath] = mod + }) try { let pluginModule = nodeRequire(foundPlugin.path) plugins.push(pluginModule) diff --git a/app/webpack.config.js b/app/webpack.config.js index 25f74a29..0c44a674 100644 --- a/app/webpack.config.js +++ b/app/webpack.config.js @@ -61,7 +61,7 @@ module.exports = { 'mz': 'commonjs mz', 'path': 'commonjs path', 'rxjs': 'commonjs rxjs', - 'zone.js': 'commonjs zone.js', + 'zone.js': 'commonjs zone.js/dist/zone.js', }, plugins: [ new webpack.optimize.ModuleConcatenationPlugin(),