Skip to content

Commit

Permalink
feat(electron): add support for displaying server mount points (cncjs…
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
cheton and Cheton Wu committed Mar 25, 2020
1 parent 3250c1b commit 2443396
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
33 changes: 21 additions & 12 deletions src/electron-app/menu-template.darwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://${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:https://${address}:${port}/${routePath}`;
shell.openExternal(url);
}
}))
);
}

const template = [
{
Expand Down
33 changes: 21 additions & 12 deletions src/electron-app/menu-template.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://${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:https://${address}:${port}/${routePath}`;
shell.openExternal(url);
}
}))
);
}

const template = [
{
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://${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();
Expand Down
32 changes: 13 additions & 19 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)}`);

Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 2443396

Please sign in to comment.