diff --git a/package.json b/package.json index 7c781568..171eadae 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/launcher/toolbar.js b/src/launcher/toolbar.js index 2c8ca12a..2e7fd2d0 100644 --- a/src/launcher/toolbar.js +++ b/src/launcher/toolbar.js @@ -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' })) diff --git a/src/server/address.js b/src/server/address.js new file mode 100644 index 00000000..c8580aad --- /dev/null +++ b/src/server/address.js @@ -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) +} diff --git a/src/server/docs-server.js b/src/server/docs-server.js new file mode 100644 index 00000000..3ca235b6 --- /dev/null +++ b/src/server/docs-server.js @@ -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 diff --git a/src/server/index.js b/src/server/index.js index 3632a6b4..86f27f21 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -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 = [] diff --git a/src/server/options.js b/src/server/options.js index 11a58782..d4e159e0 100644 --- a/src/server/options.js +++ b/src/server/options.js @@ -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} } diff --git a/src/server/server.js b/src/server/server.js index 8277626c..c9fac068 100644 --- a/src/server/server.js +++ b/src/server/server.js @@ -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 ?') } diff --git a/src/server/settings.js b/src/server/settings.js index 99a67cbe..20124673 100644 --- a/src/server/settings.js +++ b/src/server/settings.js @@ -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) }