Skip to content

Commit

Permalink
🆕 Support advance options from package manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
steelbrain committed Apr 25, 2016
1 parent 3025564 commit ce22388
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<img alt="DeNode" src="https://cloud.githubusercontent.com/assets/4278113/14579761/20e2a9a2-036b-11e6-8bc7-fba7cda9a026.png">
Expand All @@ -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
70 changes: 43 additions & 27 deletions denode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`) {
Expand All @@ -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])
}
})
})
Expand All @@ -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)
})
Expand Down

0 comments on commit ce22388

Please sign in to comment.