-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheed-server.js
More file actions
executable file
·94 lines (82 loc) · 2.36 KB
/
heed-server.js
File metadata and controls
executable file
·94 lines (82 loc) · 2.36 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
import { readFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import { Command, InvalidOptionArgumentError } from 'commander';
import { preparePresentation } from './server/prepare.js';
import { startServer } from './server/server.js';
/**
* Locate the root of the heedjs package
*/
const heedRoot = path.dirname(fileURLToPath(import.meta.url));
/**
* Load the Heed package.json
*/
const pkg = JSON.parse(await readFile(path.join(heedRoot, 'package.json')));
/**
* Parse and validate the port number program option
*/
const parsePort = (value) => {
const port = Number(value);
if (!Number.isInteger(port) || port < 1 || port > 65535) {
throw new InvalidOptionArgumentError(
'Port must be an integer between 1 and 65535',
);
}
return port;
};
/**
* Root Commander-command
*/
const program = new Command();
/**
* Commander-command for starting the Heed Server.
*/
program
.name('heed')
.description('Serve a Heed presentation over HTTP.')
.version(pkg.version)
.argument('[path]', 'Path to presentation directory or archive', '.')
.option(
'-p, --port <number>',
'HTTP port to serve presentation on',
parsePort,
4000,
)
.option('-n, --no-watch', 'Disable file watching/auto-reload.', true)
.option('-w, --show-watches', 'Display components under watch.', false)
.option('-s, --silent-ws', 'Disable websocket logging', false)
.action(async (pathArg, options) => {
try {
const { presentationRoot, presentationName, archiveFile } =
await preparePresentation(pathArg, heedRoot);
const serverOpts = {
port: options.port,
watch: options.watch,
presentationName,
presentationRoot,
archiveFile,
heedRoot,
pkg,
silentWs: options.silentWs,
showWatches: options.showWatches,
};
await startServer(serverOpts);
} catch (e) {
console.error(`Failed to start Heed: ${e.message}`);
if (e.suggestion) {
console.error(e.suggestion);
}
console.log('');
process.exit(1);
}
});
/**
* Display Heed version and start the server.
*/
const bannerString = ` Heed v${pkg.version} - Slides as code `;
console.log('-'.repeat(bannerString.length));
console.log(bannerString);
console.log('-'.repeat(bannerString.length));
console.log('');
program.parse();