From 2443396f7020e50a1595310404892a5392f192b5 Mon Sep 17 00:00:00 2001 From: Cheton Wu Date: Thu, 26 Mar 2020 00:59:34 +0800 Subject: [PATCH] feat(electron): add support for displaying server mount points (#574) * fix(electron): fix an issue that server mount points are ignored when composing the menu items * fix: fix eslint errors * fix(electron): fix an issue that server mount points are ignored when composing the menu items * chore: revise wording for the menu item label * chore: add menu header for mount points Co-authored-by: Cheton Wu --- src/electron-app/menu-template.darwin.js | 33 ++++++++++++++--------- src/electron-app/menu-template.default.js | 33 ++++++++++++++--------- src/main.js | 4 +-- src/server/index.js | 32 +++++++++------------- 4 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/electron-app/menu-template.darwin.js b/src/electron-app/menu-template.darwin.js index e22306375..27a5cd2f3 100644 --- a/src/electron-app/menu-template.darwin.js +++ b/src/electron-app/menu-template.darwin.js @@ -3,21 +3,30 @@ import trimStart from 'lodash/trimStart'; // https://github.com/electron/electron/blob/master/docs/api/menu/ export default (options) => { - // routes = [ + // mountPoints = [ // { - // path: '/widget', - // directory: '~+/widget' + // route: '/widget', + // target: '~+/widget' // } // ] - const { address, port, routes = [] } = { ...options }; - const menuItems = routes.map(route => ({ - label: `${route.path}: ${route.directory}`, - click: () => { - const path = trimStart(route.path, '/'); - const url = `http://${address}:${port}/${path}`; - shell.openExternal(url); - } - })); + const { address, port, mountPoints = [] } = { ...options }; + let menuItems = []; + + if (mountPoints.length > 0) { + menuItems = [ + { type: 'separator' }, + { label: 'Mount Points', enabled: false }, + ].concat( + mountPoints.map(mountPoint => ({ + label: ` ${mountPoint.route}`, + click: () => { + const routePath = trimStart(mountPoint.route, '/'); + const url = `http://${address}:${port}/${routePath}`; + shell.openExternal(url); + } + })) + ); + } const template = [ { diff --git a/src/electron-app/menu-template.default.js b/src/electron-app/menu-template.default.js index f12e17742..5617af39d 100644 --- a/src/electron-app/menu-template.default.js +++ b/src/electron-app/menu-template.default.js @@ -3,21 +3,30 @@ import trimStart from 'lodash/trimStart'; // https://github.com/electron/electron/blob/master/docs/api/menu/ export default (options) => { - // routes = [ + // mountPoints = [ // { - // path: '/widget', - // directory: '~+/widget' + // route: '/widget', + // target: '~+/widget' // } // ] - const { address, port, routes = [] } = { ...options }; - const menuItems = routes.map(route => ({ - label: `${route.path}: ${route.directory}`, - click: () => { - const path = trimStart(route.path, '/'); - const url = `http://${address}:${port}/${path}`; - shell.openExternal(url); - } - })); + const { address, port, mountPoints = [] } = { ...options }; + let menuItems = []; + + if (mountPoints.length > 0) { + menuItems = [ + { type: 'separator' }, + { label: 'Mount Points', enabled: false }, + ].concat( + mountPoints.map(mountPoint => ({ + label: ` ${mountPoint.route}`, + click: () => { + const routePath = trimStart(mountPoint.route, '/'); + const url = `http://${address}:${port}/${routePath}`; + shell.openExternal(url); + } + })) + ); + } const template = [ { diff --git a/src/main.js b/src/main.js index c4b3a49d5..bdf261d6c 100644 --- a/src/main.js +++ b/src/main.js @@ -63,13 +63,13 @@ const main = () => { app.on('ready', async () => { try { const res = await launchServer(); - const { address, port, routes } = { ...res }; + const { address, port, mountPoints } = { ...res }; if (!(address && port)) { console.error('Unable to start the server at ' + chalk.cyan(`http://${address}:${port}`)); return; } - const menu = Menu.buildFromTemplate(menuTemplate({ address, port, routes })); + const menu = Menu.buildFromTemplate(menuTemplate({ address, port, mountPoints })); Menu.setApplicationMenu(menu); windowManager = new WindowManager(); diff --git a/src/server/index.js b/src/server/index.js index d3afe0aa4..8b8d9a7fe 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -10,9 +10,11 @@ import expandTilde from 'expand-tilde'; import express from 'express'; import httpProxy from 'http-proxy'; import escapeRegExp from 'lodash/escapeRegExp'; +import isEqual from 'lodash/isEqual'; import set from 'lodash/set'; import size from 'lodash/size'; import trimEnd from 'lodash/trimEnd'; +import uniqWith from 'lodash/uniqWith'; import webappengine from 'webappengine'; import settings from './config/settings'; import app from './app'; @@ -101,18 +103,20 @@ const createServer = (options, callback) => { } const { port = 0, host, backlog } = options; - const mountPoints = [ + const mountPoints = uniqWith([ ...ensureArray(options.mountPoints), ...ensureArray(config.get('mountPoints')) - ]; - const routes = []; - - mountPoints.forEach(mount => { + ], isEqual).filter(mount => { if (!mount || !mount.route || mount.route === '/') { log.error(`Must specify a valid route path ${JSON.stringify(mount.route)}.`); - return; + return false; } + return true; + }); + const routes = []; + + mountPoints.forEach(mount => { if (ensureString(mount.target).match(/^(http|https):\/\//i)) { log.info(`Starting a proxy server to proxy all requests starting with ${chalk.yellow(mount.route)} to ${chalk.yellow(mount.target)}`); @@ -235,21 +239,11 @@ const createServer = (options, callback) => { const address = server.address().address; const port = server.address().port; - const filteredRoutes = routes.reduce((acc, r) => { - const { type, route, directory } = r; - if (type === 'static') { - acc.push({ - path: route, - directory: directory - }); - } - return acc; - }, []); callback && callback(null, { - address: address, - port: port, - routes: filteredRoutes + address, + port, + mountPoints, }); if (address !== '0.0.0.0') {