server: add docs-server, --docs cli option & Docs action in launcher menu

This commit is contained in:
bordun 2020-04-25 13:36:34 +02:00
parent 55e0b3a324
commit bb751cb4cc
8 changed files with 129 additions and 16 deletions

View file

@ -54,6 +54,7 @@
"node-sass": "4.13.1",
"nodemon": "2.0.2",
"nosleep.js": "0.9.0",
"open": "^7.0.3",
"osc": "2.4.0",
"python-shell": "1.0.8",
"replacestream": "^4.0.3",

View file

@ -4,6 +4,7 @@ var {remote, ipcRenderer} = eval('require(\'electron\')'),
terminal = require('./terminal'),
settings = require('./settings'),
midilist = remote.getGlobal('midilist'),
openDocs = remote.getGlobal('openDocs'),
serverStarted = false,
serverStart = ()=>{
if (serverStarted) return
@ -96,6 +97,12 @@ menu.append(new MenuItem({
// app.exit(0)
// }
// }))
menu.append(new MenuItem({
label: 'Documentation',
click: ()=>{
openDocs()
}
}))
menu.append(new MenuItem({
role: 'Quit'
}))

9
src/server/address.js Normal file
View file

@ -0,0 +1,9 @@
var ifaces = require('os').networkInterfaces(),
address = Object.values(ifaces)
.reduce((a,b)=>a.concat(b), [])
.filter(i=>i.family === 'IPv4')
.map(i=>'http://' + i.address + ':')
module.exports = (port)=>{
return address.map(x=>x + port)
}

88
src/server/docs-server.js Normal file
View file

@ -0,0 +1,88 @@
var EventEmitter = require('events').EventEmitter,
http = require('http'),
send = require('send'),
path = require('path'),
fs = require('fs'),
open = require('open'),
address = require('./address')
class DocsServer extends EventEmitter {
constructor() {
super(...arguments)
this.baseUrl = path.resolve(__dirname + '/../../resources/site')
this.home = '/docs/getting-started/introduction/'
this.server = null
this.started = false
this.port = 0
this.available = true
if (!fs.existsSync(this.baseUrl)) {
console.error(`(ERROR, DOCS) Documentation assets not found in the app\'s directory.`)
this.available = false
}
}
start() {
if (this.started || !this.available) return
this.server = http.createServer(this.route.bind(this))
this.server.on('error', (e)=>{
console.error(`(ERROR, DOCS) ${e.message}`)
})
this.server.listen(0).on('listening', ()=>{
this.port = this.server.address().port
console.log('(INFO) Documentation server started, docs available at \n ' + address(this.port).join('\n '))
this.emit('ready')
})
this.started = true
}
open() {
if (!this.started || !this.port) {
this.start()
this.on('ready', ()=>{
open(address(this.port)[0] + this.home)
console.log('(INFO) Opening documentation with default browser...')
})
} else {
open(address(this.port)[0] + this.home)
console.log('(INFO) Opening documentation with default browser...')
}
}
route(req, res) {
var url = req.url.replace(this.baseUrl, '').split('?')[0]
if (url[url.length-1] === '/') url += 'index.html'
var fpath = path.join(this.baseUrl, url)
if (!fs.existsSync(fpath)) {
if (url.indexOf('index.html') !== -1) {
return send(req, path.join(this.baseUrl, '/404.html')).pipe(res)
} else {
res.writeHead(403)
res.end()
}
} else {
return send(req, fpath).pipe(res)
}
}
}
module.exports = DocsServer

View file

@ -1,9 +1,23 @@
require('source-map-support').install({handleUncaughtExceptions: false})
var dev = process.argv[0].includes('node_modules'),
settings = require('./settings')
settings = require('./settings'),
docsServer, serverStarted
function openDocs() {
if (!docsServer) {
var DocsServer = require('./docs-server')
docsServer = new DocsServer()
}
docsServer.open()
}
if (settings.read('docs')) return openDocs()
var serverStarted
function nodeMode() {
@ -106,7 +120,9 @@ if (settings.cli) {
})
app.on('ready',function(){
var doscServer
global.settings = settings
global.openDocs = openDocs
global.midilist = require('./midi').list
global.serverProcess = null
global.clientWindows = []

View file

@ -86,5 +86,6 @@ module.exports = {
'disable-gpu': {type: 'boolean', describe: 'disable hardware acceleration', launcher: false},
'inspect': {type: 'boolean', describe: 'enable node/electron inspector', launcher: false},
'cache-dir': {type: 'string', describe: 'override default cache directory', launcher: false},
'config-file': {type: 'string', describe: 'override default config file (defaults to cache-dir/config.json)', launcher: false}
'config-file': {type: 'string', describe: 'override default config file (defaults to cache-dir/config.json)', launcher: false},
'docs': {type: 'boolean', describe: 'serve documentation website locally and open it with the system\'s default browser', launcher: false}
}

View file

@ -10,7 +10,6 @@ var urlparser = require('url'),
settings = require('./settings'),
theme = require('./theme').init(),
zeroconf = require('./zeroconf'),
appAddresses = settings.appAddresses(),
osc = {},
clients = {},
httpCheckTimeout
@ -112,7 +111,7 @@ function httpCheck(ok){
clearTimeout(httpCheckTimeout)
httpCheckTimeout = null
if (ok) {
console.log('(INFO) Server started, app available at \n ' + appAddresses.join('\n '))
console.log('(INFO) Server started, app available at \n ' + settings.appAddresses().join('\n '))
} else {
console.error('(ERROR, HTTP) Could not setup http server, maybe try a different port ?')
}

View file

@ -1,15 +1,9 @@
var path = require('path'),
fs = require('fs'),
ifaces = require('os').networkInterfaces(),
yargs = require('yargs'),
infos = require('../package.json'),
options = require('./options')
var appAddresses = Object.values(ifaces)
.reduce((a,b)=>a.concat(b), [])
.filter(i=>i.family === 'IPv4')
.map(i=>'http://' + i.address + ':')
options = require('./options'),
address = require('./address')
// This prevents argv parsing to be breaked when the app is packaged (executed without 'electron' prefix)
var firstArgIndex = path.basename(process.argv[0]).match(/electron|node/) ? 2 : 1
@ -162,7 +156,5 @@ module.exports = {
cli: cli,
configPath: baseDir,
infos: infos,
appAddresses: ()=>{
return appAddresses.map(x=>x + (settings.options.port || 8080))
}
appAddresses: ()=>address(settings.options.port || 8080)
}