Skip to content

Commit 432bf1a

Browse files
authored
Merge pull request #3 from bendemboski/set-index-path-in-env
Set environment variable containing path to index.html
2 parents b667c14 + 5d43191 commit 432bf1a

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.2.0
4+
5+
- Set environment variable allowing renderer process to set module search path correctly
6+
37
## 1.1.0
48

59
- Made indexPath accessible via options for more complex configuration

index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ module.exports = function protocolServe({
3535

3636
indexPath = indexPath || join(cwd, 'index.html');
3737

38+
const options = { cwd, name, endpoint, indexPath };
39+
const handler = createHandler(options);
40+
3841
app.on('ready', () => {
39-
const options = { cwd, name, endpoint, indexPath };
40-
protocol.registerFileProtocol(name, createHandler(options), error => {
42+
protocol.registerFileProtocol(name, handler, error => {
4143
if (error) {
4244
console.error('Failed to register protocol');
4345
}
4446
});
4547
});
4648

49+
// Set our ELECTRON_PROTOCOL_SERVE_INDEX environment variable so the renderer
50+
// process can find index.html to set it up as its main module path
51+
handler({ url: `${name}://${endpoint}` }, ({ path }) => process.env.ELECTRON_PROTOCOL_SERVE_INDEX = path);
52+
4753
return name;
4854
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"bluebird": "^3.5.0",
1616
"chai": "^3.5.0",
1717
"eslint": "^3.17.1",
18+
"lodash": "^4.17.4",
1819
"mocha": "^3.2.0",
1920
"sinon": "^1.17.7"
2021
},

tests/unit/index-test.js

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
const { assert } = require('chai');
22
const mock = require('mock-require');
3+
const sinon = require('sinon');
34
const { join } = require('path');
5+
const { clone } = require('lodash');
46

57
describe('index', () => {
8+
let env;
69
let app;
710
let protocol;
811
let onAppReady;
912
let protocolName;
1013
let handlerOptions;
14+
let handlerStub;
1115

1216
function register(options) {
1317
require('../..')(Object.assign({
@@ -17,14 +21,37 @@ describe('index', () => {
1721
}, options));
1822
}
1923

20-
before(() => {
21-
mock('../../lib/handler', options => handlerOptions = options);
24+
//
25+
// Mock our handler library
26+
//
27+
beforeEach(() => {
28+
handlerStub = sinon.stub();
29+
handlerStub.callsArgWith(1, { path: '/path/to/index.html' });
30+
31+
mock('../../lib/handler', options => {
32+
handlerOptions = options;
33+
return handlerStub;
34+
});
2235
});
2336

24-
after(() => {
37+
afterEach(() => {
2538
mock.stop('../../lib/handler');
2639
});
2740

41+
//
42+
// Sandbox any changes to the environment
43+
//
44+
beforeEach(() => {
45+
env = clone(process.env);
46+
});
47+
48+
afterEach(() => {
49+
process.env = env;
50+
});
51+
52+
//
53+
// Set up our stubbed app and protocol
54+
//
2855
beforeEach(() => {
2956
app = {
3057
on(evt, cb) {
@@ -53,6 +80,8 @@ describe('index', () => {
5380
endpoint: 'dist',
5481
indexPath: join('.', 'index.html'),
5582
});
83+
assert.ok(handlerStub.calledWith({ url: 'serve://dist' }));
84+
assert.equal(process.env.ELECTRON_PROTOCOL_SERVE_INDEX, '/path/to/index.html');
5685
});
5786

5887
it('works with a custom cwd', () => {
@@ -69,6 +98,8 @@ describe('index', () => {
6998
endpoint: 'dist',
7099
indexPath: join('foo', 'bar', 'index.html'),
71100
});
101+
assert.ok(handlerStub.calledWith({ url: 'serve://dist' }));
102+
assert.equal(process.env.ELECTRON_PROTOCOL_SERVE_INDEX, '/path/to/index.html');
72103
});
73104

74105
it('works with non-default arguments', () => {
@@ -88,6 +119,8 @@ describe('index', () => {
88119
endpoint: 'so',
89120
indexPath: join('we', 'meet', 'again.html'),
90121
});
122+
assert.ok(handlerStub.calledWith({ url: 'friend://so' }));
123+
assert.equal(process.env.ELECTRON_PROTOCOL_SERVE_INDEX, '/path/to/index.html');
91124
});
92125

93126
it('required a cwd', () => {

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ lodash.keys@^3.0.0:
651651
lodash.isarguments "^3.0.0"
652652
lodash.isarray "^3.0.0"
653653

654-
lodash@^4.0.0, lodash@^4.3.0:
654+
lodash@^4.0.0, lodash@^4.17.4, lodash@^4.3.0:
655655
version "4.17.4"
656656
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
657657

0 commit comments

Comments
 (0)