Skip to content

Commit c307a57

Browse files
Support loading an HTML file with a custom name (#36)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 2968431 commit c307a57

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ declare namespace electronServe {
2222
*/
2323
hostname?: string;
2424

25+
/**
26+
Custom HTML filename. This gets appended with `'.html'`.
27+
28+
@default 'index'
29+
*/
30+
file?: string;
31+
2532
/**
2633
Whether [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) should be enabled.
2734
Useful for testing purposes.

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const stat = promisify(fs.stat);
99
// See https://cs.chromium.org/chromium/src/net/base/net_error_list.h
1010
const FILE_NOT_FOUND = -6;
1111

12-
const getPath = async path_ => {
12+
const getPath = async (path_, file) => {
1313
try {
1414
const result = await stat(path_);
1515

@@ -18,7 +18,7 @@ const getPath = async path_ => {
1818
}
1919

2020
if (result.isDirectory()) {
21-
return getPath(path.join(path_, 'index.html'));
21+
return getPath(path.join(path_, `${file}.html`));
2222
}
2323
} catch (_) {}
2424
};
@@ -27,7 +27,8 @@ module.exports = options => {
2727
options = Object.assign({
2828
isCorsEnabled: true,
2929
scheme: 'app',
30-
hostname: '-'
30+
hostname: '-',
31+
file: 'index'
3132
}, options);
3233

3334
if (!options.directory) {
@@ -37,9 +38,9 @@ module.exports = options => {
3738
options.directory = path.resolve(electron.app.getAppPath(), options.directory);
3839

3940
const handler = async (request, callback) => {
40-
const indexPath = path.join(options.directory, 'index.html');
41+
const indexPath = path.join(options.directory, `${options.file}.html`);
4142
const filePath = path.join(options.directory, decodeURIComponent(new URL(request.url).pathname));
42-
const resolvedPath = await getPath(filePath);
43+
const resolvedPath = await getPath(filePath, options.file);
4344
const fileExtension = path.extname(filePath);
4445

4546
if (resolvedPath || !fileExtension || fileExtension === '.html' || fileExtension === '.asar') {

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ Default: `'-'`
6464

6565
Custom hostname.
6666

67+
##### file
68+
69+
Type: `string`\
70+
Default: `'index'`
71+
72+
Custom HTML filename. This gets appended with `'.html'`.
73+
6774
##### isCorsEnabled
6875

6976
Type: `boolean`\

test/fixture-custom-file.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
const {app, BrowserWindow} = require('electron');
3+
const serve = require('..');
4+
5+
const loadUrl = serve({directory: __dirname, file: 'owl'});
6+
7+
let mainWindow;
8+
9+
(async () => {
10+
await app.whenReady();
11+
12+
mainWindow = new BrowserWindow();
13+
loadUrl(mainWindow);
14+
})();

test/owl.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
</head>
6+
<body>
7+
<h1>🦉</h1>
8+
</body>
9+
</html>

test/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ test('throws error if unresolved path has an extension other than .html', async
6565
await t.throwsAsync(client.waitUntilTextExists('h1', '🦄', 5000));
6666
t.pass();
6767
});
68+
69+
test('serves directory custom file', async t => {
70+
t.context.spectron = new Application({
71+
path: electron,
72+
args: ['fixture-custom-file.js']
73+
});
74+
await t.context.spectron.start();
75+
const {client} = t.context.spectron;
76+
await client.waitUntilWindowLoaded();
77+
await client.waitUntilTextExists('h1', '🦉', 5000);
78+
t.pass();
79+
});

0 commit comments

Comments
 (0)