|
1 | 1 | const { join } = require('path');
|
2 | 2 | const fs = require('fs');
|
3 | 3 |
|
| 4 | +function requiredParam(param, errorMessage) { |
| 5 | + if (!param) { |
| 6 | + throw new Error(`electron-protocol-serve: ${errorMessage}`); |
| 7 | + } |
| 8 | +} |
| 9 | + |
4 | 10 | /**
|
5 | 11 | * Registers a file protocol with a single endpoint which handles
|
6 | 12 | * the proper discovery of local files inside of Electron
|
7 | 13 | * without modifiying the Ember app.
|
8 | 14 | *
|
9 |
| - * @param {String} cwd the path to the dist folder of your Ember app |
| 15 | + * @param {String} options.cwd the path to the dist folder of your Ember app |
10 | 16 | * @param {Object} options.app electron.app
|
11 | 17 | * @param {Object} options.protocol electron.protocol
|
12 | 18 | * @param {String} options.name name of your protocol, defaults to `serve`
|
13 | 19 | * @param {String} options.endpoint endpoint of your protocol, defaults to `dist`
|
14 | 20 | * @param {String} options.directoryIndexFile directory index. usally the default, `index.html`
|
| 21 | + * @param {String} options.indexPath defaults to cwd + directoryIndexFile |
| 22 | + * |
15 | 23 | * @return {String} name of your protocol
|
16 | 24 | */
|
17 |
| -module.exports = (cwd, { |
| 25 | +module.exports = function protocolServe({ |
| 26 | + cwd = undefined, |
18 | 27 | app,
|
19 | 28 | protocol,
|
20 | 29 | name = 'serve',
|
21 | 30 | endpoint = 'dist',
|
22 | 31 | directoryIndexFile = 'index.html',
|
23 |
| -}) => { |
24 |
| - const indexPath = join(cwd, directoryIndexFile); |
| 32 | + indexPath = undefined, |
| 33 | +}) { |
| 34 | + requiredParam(cwd, 'cwd must be specified, should be a valid path'); |
| 35 | + requiredParam(protocol, 'protocol must be specified, should be electron.protocol'); |
| 36 | + requiredParam(app, 'app must be specified, should be electron.app'); |
| 37 | + |
| 38 | + indexPath = indexPath || join(cwd, directoryIndexFile); |
| 39 | + |
25 | 40 | const cache = {};
|
26 | 41 | const prefixLength = name.length + 3 + endpoint.length;
|
27 | 42 |
|
|
0 commit comments