Skip to content

Commit

Permalink
add support for requiring node modules
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Mar 4, 2017
1 parent 5625f45 commit 099da66
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ Options:
--browser, -b Browser to use. Always available: electron. Available if installed: chrome, firefox, ie, phantom, safari [default: "electron"]
--port Starts listening on that port and waits for you to open a browser
--static Serve static assets from this directory
--mock Path to code to handle requests for mocking a dynamic back-end
--mock Path to code to handle requests for mocking a dynamic back-end
--input Input type. Defaults to 'javascript', can be set to 'html'.
--node-integration Enable nodejs apis in electron
--help Print help

--node-integration Enable nodejs apis in electron
--basedir Set this if you need to require node modules in node mode
--help Print help
```
## Custom html file
Expand Down Expand Up @@ -98,6 +98,7 @@ Returns a duplex stream and starts a webserver.
* `mock`: Path to code to handle requests for mocking a dynamic back-end
* `input`: Input type. Defaults to `javascript`, can be set to `html`.
* `nodeIntegration`: Enable nodejs integration in electron
* `basedir`: Set this if you need to require node modules in `node` mode
If only an empty string is written to it, an error will be thrown as there is nothing to execute.
Expand Down
3 changes: 3 additions & 0 deletions bin/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var argv = optimist
.describe('node-integration', 'Enable nodejs apis in electron')
.alias('n', 'node-integration')

.describe('basedir', 'Set this if you need to require node modules in node mode')
.alias('b', 'basedir')

.describe('help', 'Print help')
.alias('h', 'help')

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ function runner (opts) {

launch(extend(opts, {
loc: 'http://localhost:' + port,
name: opts.browser
name: opts.browser,
bundle: bundle
}), function(err, _browser){
if (err) return dpl.emit('error', err);
browser = _browser;
Expand Down
2 changes: 1 addition & 1 deletion lib/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function(opts, cb){
process.nextTick(cb.bind(null, null, browser));
} else if (/^electron/.test(opts.name)) {
var browser = Electron(opts);
browser.end('location = ' + JSON.stringify(opts.loc));
opts.bundle.createReadStream().pipe(browser);
process.nextTick(cb.bind(null, null, browser));
} else {
launcher(function(err, launch){
Expand Down
22 changes: 21 additions & 1 deletion test/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('electron', function (t) {
});

browser.pipe(concat(function(data){
t.ok(/electron\.asar/.test(data.toString()));
t.ok(/browser-run/.test(data.toString()));
}));

browser.on('exit', function (code) {
Expand All @@ -40,5 +40,25 @@ test('electron', function (t) {
browser.write('window.close();');
browser.end();
});
t.test('basedir option', function (t) {
t.plan(2);
var browser = run({
browser: 'electron',
nodeIntegration: true,
basedir: __dirname + '/../'
});

browser.pipe(concat(function(data){
t.ok(/^true\n$/.test(data.toString()));
}));

browser.on('exit', function (code) {
t.equal(code, 0, 'exit');
});

browser.write('console.log(!!require.resolve("tap"));');
browser.write('window.close();');
browser.end();
});
t.end();
});

0 comments on commit 099da66

Please sign in to comment.