diff --git a/CHANGELOG.md b/CHANGELOG.md index c63bc78..6a79419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.3.0 + +- Support browser window options from the manifest +- Support custom main for electron from the manifest + ## 1.2.2 - Fix relative requires in console (bug introduced by v1.2.0) diff --git a/README.md b/README.md index e27d561..d95bb42 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,21 @@ denode `which browserify` To reload the app, simply press CTRL + R on Linux/Windows and CMD + R on Mac OSX. +## Advanced Usage + +If you do `denode .` or `denode ./` or `denode .\`, it tries to load options from the local `package.json` file. Here are the magic fields it understands of a schema + +```json +{ + "name": "my-cool-app", + "electronMain": "./electron.js", + "electronOptions": { + "hide": false + } +} +``` +The `electronOptions` field accepts all of [`BrowserWindow`][3] options, if `electronMain` is present, it is used instead of `main`. + ## Screenshot DeNode @@ -31,3 +46,4 @@ DeNode is licensed under the terms of MIT License, see the LICENSE file for more [1]:http://electron.atom.io/ [2]:http://nodejs.org/ +[3]:http://electron.atom.io/docs/latest/api/browser-window diff --git a/denode/index.js b/denode/index.js index a246054..6d783c6 100644 --- a/denode/index.js +++ b/denode/index.js @@ -3,16 +3,30 @@ const Path = require('path') const Electron = require('electron') const STDIN = [] -let win +let window +let request = process.argv[2] Electron.app.on('ready', function() { - let name, opts - try { - let pinfo = require(Path.join(process.cwd(), 'package.json')) - name = pinfo.name - opts = pinfo.electronOpts - } catch (_) { } - name = name || 'DeNode' + let name = 'DeNode' + let options = { + hide: true, + width: 800, + height: 800 + } + if (request === './' || request === '.' || request === '.\\') { + try { + let manifestInfo = require(Path.join(process.cwd(), 'package.json')) + if (typeof manifestInfo.name === 'string' && manifestInfo.name) { + name = manifestInfo.name + } + if (typeof manifestInfo.electronOptions === 'object' && manifestInfo.electronOptions) { + Object.assign(options, manifestInfo.electronOptions) + } + if (typeof manifestInfo.electronMain === 'string' && manifestInfo.electronMain) { + request = manifestInfo.electronMain + } + } catch (_) { } + } Electron.protocol.interceptFileProtocol('file', function(request, callback) { if (request.url === `file:///app-${name}`) { @@ -22,32 +36,34 @@ Electron.app.on('ready', function() { } }) - win = new Electron.BrowserWindow(opts || { width: 800, height: 600, title: 'DeNode' }); - win.loadURL(`file:///app-${name}`) - win.on('closed', function() { + window = new Electron.BrowserWindow(options) + window.loadURL(`file:///app-${name}`) + window.on('closed', function() { Electron.app.quit() }) - win.webContents.openDevTools({ detach: true }) - win.webContents.once('devtools-opened', function() { - setImmediate(function() { - win.hide() - win.webContents.once('devtools-closed', function() { - setImmediate(function() { - win.close() - }) + window.webContents.openDevTools({ detach: options.hide }) + if (options.hide) { + window.webContents.once('devtools-opened', function() { + setImmediate(function() { + window.hide() + window.webContents.once('devtools-closed', function() { + setImmediate(function() { + window.close() + }) + }) }) }) - }) + } - win.webContents.on('dom-ready', function() { - win.webContents.send('setup', JSON.stringify({ + window.webContents.on('dom-ready', function() { + window.webContents.send('setup', JSON.stringify({ + request: request, stdoutIsTTY: process.stdout.isTTY, - stderrIsTTY: process.stderr.isTTY, - request: process.argv[2] || '' + stderrIsTTY: process.stderr.isTTY })) for (let i = 0, length = STDIN.length; i < length; ++i) { - win.webContents.send('stdin', STDIN[i]) + window.webContents.send('stdin', STDIN[i]) } }) }) @@ -60,8 +76,8 @@ Electron.ipcMain.on('stderr', function(_, data) { }) process.stdin.on('data', function(chunk) { const stringish = chunk.toString() - if (win) { - win.webContents.send('stdin', stringish) + if (window) { + window.webContents.send('stdin', stringish) } STDIN.push(stringish) })