-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcli.js
More file actions
136 lines (109 loc) · 3.41 KB
/
Copy pathcli.js
File metadata and controls
136 lines (109 loc) · 3.41 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { createRequire } from "node:module";
import { parseArgs } from "node:util";
import DevServer from "./server.js";
const require = createRequire(import.meta.url);
const pkg = require("./package.json");
export const Logger = {
info(...args) {
console.log( "[11ty/eleventy-dev-server]", ...args );
},
error(...args) {
console.error( "[11ty/eleventy-dev-server]", ...args );
},
fatal(...args) {
Logger.error(...args);
process.exitCode = 1;
},
log(...args) {
return Logger.info(...args);
}
};
export class Cli {
static getVersion() {
return pkg.version;
}
static getHelp() {
return `Usage:
eleventy-dev-server [dir] [options]
Examples:
eleventy-dev-server
eleventy-dev-server _site
eleventy-dev-server --dir=_site --port=3000
eleventy-dev-server --host=0.0.0.0
Options:
--dir=.
Directory to serve (default: \`.\`), or pass it as the first argument.
--input (alias for --dir)
--port=8080
Run the web server on this port (default: \`8080\`)
Will autoincrement if already in use.
--host=127.0.0.1
Address to listen on (default: \`127.0.0.1\`)
Use \`0.0.0.0\` to allow access from other devices on your network.
--domdiff (enabled, default)
--no-domdiff (disabled)
Apply HTML changes without a full page reload.
--version
--help`;
}
static parseArgs(args = []) {
let defaults = Cli.getDefaultOptions();
let { values, positionals } = parseArgs({
args,
allowNegative: true,
allowPositionals: true,
options: {
dir: { type: "string" },
input: { type: "string" },
port: { type: "string", default: defaults.port },
host: { type: "string" },
domdiff: { type: "boolean", default: defaults.domDiff },
help: { type: "boolean", default: false },
version: { type: "boolean", default: false },
},
});
let usageError = (message) => {
// Printed without a stack trace, like Node's own argument errors
return Object.assign(new Error(message), { code: "ERR_PARSE_ARGS_DIRECTORY" });
};
if(positionals.length > 1) {
throw usageError(`Expected one directory, received ${positionals.length}: ${positionals.join(", ")}.`);
}
let [dir] = positionals;
if(dir !== undefined && (values.dir !== undefined || values.input !== undefined)) {
throw usageError("Pass the directory either as an argument or with --dir, not both.");
}
return {
...values,
dir: dir ?? values.dir ?? values.input ?? defaults.input,
};
}
static getDefaultOptions() {
return {
port: "8080",
input: ".",
domDiff: true,
}
}
async serve(options = {}) {
this.options = Object.assign(Cli.getDefaultOptions(), options);
this.server = DevServer.getServer("eleventy-dev-server-cli", this.options.input, {
// TODO allow server configuration extensions
showVersion: true,
logger: Logger,
domDiff: this.options.domDiff,
host: this.options.host,
// CLI watches all files in the folder by default
// this is different from Eleventy usage!
watch: [ this.options.input ],
});
this.server.serve(this.options.port);
// TODO? send any errors here to the server too
// with server.sendError({ error });
}
close() {
if(this.server) {
return this.server.close();
}
}
}