-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
63 lines (55 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { join } = require('path');
const createHandler = require('./lib/handler');
function requiredParam(param, errorMessage) {
if (!param) {
throw new Error(`electron-protocol-serve: ${errorMessage}`);
}
}
/**
* Registers a file protocol with a single endpoint which handles
* the proper discovery of local files inside of Electron
* without modifiying the Ember app.
*
* @param {String} options.cwd the path to the dist folder of your Ember app
* @param {Object} options.app electron.app
* @param {Object} options.protocol electron.protocol
* @param {String} options.name name of your protocol, defaults to `serve`
* @param {String} options.endpoint endpoint of your protocol, defaults to `dist`
* @param {String} options.indexPath defaults to 'index.html' in cwd
*
* @return {String} name of your protocol
*/
module.exports = function protocolServe({
cwd = undefined,
app,
protocol,
name = 'serve',
endpoint = 'dist',
indexPath = undefined,
}) {
requiredParam(cwd, 'cwd must be specified, should be a valid path');
requiredParam(protocol, 'protocol must be specified, should be electron.protocol');
requiredParam(app, 'app must be specified, should be electron.app');
indexPath = indexPath || join(cwd, 'index.html');
const options = { cwd, name, endpoint, indexPath };
const handler = createHandler(options);
let isProtocolSynchronous;
try {
isProtocolSynchronous = parseFloat(process.versions.electron) >= 7;
} catch (e) {
// Not running in electron
isProtocolSynchronous = false;
}
app.on('ready', () => {
const callback = isProtocolSynchronous ? undefined : error => {
if (error) {
console.error('Failed to register protocol');
}
};
protocol.registerFileProtocol(name, handler, callback);
});
// Set our ELECTRON_PROTOCOL_SERVE_INDEX environment variable so the renderer
// process can find index.html to set it up as its main module path
handler({ url: `${name}://${endpoint}` }, ({ path }) => process.env.ELECTRON_PROTOCOL_SERVE_INDEX = path);
return name;
};