From db35faa431fad1fc52afe2c0a58d668e720d44a4 Mon Sep 17 00:00:00 2001 From: CHaBou Date: Mon, 19 Jun 2017 23:02:53 +0200 Subject: [PATCH] Show plugin (name and version) loaded (#1826) * Print plugin name and version in devtools * Add plugins informations in About dialog --- app/index.js | 5 +++-- app/menus/menu.js | 25 ++++++++++++++++++++++--- app/menus/menus/darwin.js | 7 +++++-- app/menus/menus/help.js | 13 +++---------- app/plugins.js | 11 +++++++++++ lib/utils/plugins.js | 18 +++++++++++++++++- 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/app/index.js b/app/index.js index 15d80bd5..b97f7f30 100644 --- a/app/index.js +++ b/app/index.js @@ -405,8 +405,9 @@ app.on('ready', () => installDevExtensions(isDev).then(() => { const menu = plugins.decorateMenu( AppMenu(createWindow, () => { plugins.updatePlugins({force: true}); - }) - ); + }, + plugins.getLoadedPluginVersions + )); // If we're on Mac make a Dock Menu if (process.platform === 'darwin') { diff --git a/app/menus/menu.js b/app/menus/menu.js index 463a8d7b..71cde678 100644 --- a/app/menus/menu.js +++ b/app/menus/menu.js @@ -1,4 +1,7 @@ +const {app, dialog} = require('electron'); + const {getKeymaps} = require('../config'); +const {icon} = require('../config/paths'); // menus const viewMenu = require('./menus/view'); @@ -9,16 +12,32 @@ const windowMenu = require('./menus/window'); const helpMenu = require('./menus/help'); const darwinMenu = require('./menus/darwin'); -module.exports = (createWindow, updatePlugins) => { +const appName = app.getName(); +const appVersion = app.getVersion(); + +module.exports = (createWindow, updatePlugins, getLoadedPluginVersions) => { const commands = getKeymaps().commands; + const showAbout = () => { + const loadedPlugins = getLoadedPluginVersions(); + const pluginList = loadedPlugins.length === 0 ? + 'none' : + loadedPlugins.map(plugin => `\n ${plugin.name} (${plugin.version})`); + dialog.showMessageBox({ + title: `About ${appName}`, + message: `${appName} ${appVersion}`, + detail: `Plugins: ${pluginList}\n\nCreated by Guillermo Rauch\nCopyright © 2017 Zeit, Inc.`, + buttons: [], + icon + }); + }; const menu = [ - ...(process.platform === 'darwin' ? [darwinMenu(commands)] : []), + ...(process.platform === 'darwin' ? [darwinMenu(commands, showAbout)] : []), shellMenu(commands, createWindow), editMenu(commands), viewMenu(commands), pluginsMenu(commands, updatePlugins), windowMenu(commands), - helpMenu(commands) + helpMenu(commands, showAbout) ]; return menu; diff --git a/app/menus/menus/darwin.js b/app/menus/menus/darwin.js index 98266ce8..bff2bf0d 100644 --- a/app/menus/menus/darwin.js +++ b/app/menus/menus/darwin.js @@ -3,12 +3,15 @@ const {app} = require('electron'); const {openConfig} = require('../../config'); -module.exports = function (commands) { +module.exports = function (commands, showAbout) { return { label: `${app.getName()}`, submenu: [ { - role: 'about' + label: 'About Hyper', + click() { + showAbout(); + } }, { type: 'separator' diff --git a/app/menus/menus/help.js b/app/menus/menus/help.js index 9bb883c6..6bdc8826 100644 --- a/app/menus/menus/help.js +++ b/app/menus/menus/help.js @@ -1,8 +1,7 @@ const os = require('os'); -const {app, shell, dialog} = require('electron'); -const {icon} = require('../../config/paths'); +const {app, shell} = require('electron'); -module.exports = function () { +module.exports = function (commands, showAbout) { const submenu = [ { label: `${app.getName()} Website`, @@ -31,13 +30,7 @@ module.exports = function () { { role: 'about', click() { - dialog.showMessageBox({ - title: `About ${app.getName()}`, - message: `${app.getName()} ${app.getVersion()}`, - detail: 'Created by Guillermo Rauch', - icon, - buttons: [] - }); + showAbout(); } } ); diff --git a/app/plugins.js b/app/plugins.js index 32926c9c..ec264eb3 100644 --- a/app/plugins.js +++ b/app/plugins.js @@ -161,6 +161,10 @@ function clearCache() { exports.updatePlugins = updatePlugins; +exports.getLoadedPluginVersions = function () { + return modules.map(mod => ({name: mod._name, version: mod._version})); +}; + // we schedule the initial plugins update // a bit after the user launches the terminal // to prevent slowness @@ -309,6 +313,13 @@ function requirePlugins() { // populate the name for internal errors here mod._name = basename(path); + try { + // eslint-disable-next-line import/no-dynamic-require + mod._version = require(resolve(path, 'package.json')).version; + } catch (err) { + console.warn(`No package.json found in ${path}`); + } + console.log(`Plugin ${mod._name} (${mod._version}) loaded.`); return mod; } catch (err) { diff --git a/lib/utils/plugins.js b/lib/utils/plugins.js index f7016ac3..b9912e88 100644 --- a/lib/utils/plugins.js +++ b/lib/utils/plugins.js @@ -73,7 +73,19 @@ const clearModulesCache = () => { } }; -const getPluginName = path => window.require('path').basename(path); +const pathModule = window.require('path'); + +const getPluginName = path => pathModule.basename(path); + +const getPluginVersion = path => { + let version = null; + try { + version = window.require(pathModule.resolve(path, 'package.json')).version; + } catch (err) { + console.warn(`No package.json found in ${path}`); + } + return version; +}; const loadModules = () => { console.log('(re)loading renderer plugins'); @@ -112,6 +124,7 @@ const loadModules = () => { .map(path => { let mod; const pluginName = getPluginName(path); + const pluginVersion = getPluginVersion(path); // window.require allows us to ensure this doesn't get // in the way of our build @@ -126,6 +139,7 @@ const loadModules = () => { for (const i in mod) { if ({}.hasOwnProperty.call(mod, i)) { mod[i]._pluginName = pluginName; + mod[i]._pluginVersion = pluginVersion; } } @@ -209,6 +223,8 @@ const loadModules = () => { mod.onRendererWindow(window); } + console.log(`Plugin ${pluginName} (${pluginVersion}) loaded.`); + return mod; }) .filter(mod => Boolean(mod));