-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
mod.ts
105 lines (85 loc) 路 2.42 KB
/
mod.ts
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
import { parse } from "./deps/flags.ts";
import { resolve } from "./deps/path.ts";
import Site from "./core/site.ts";
import url from "./plugins/url.ts";
import json from "./plugins/json.ts";
import markdown, { Options as MarkdownOptions } from "./plugins/markdown.ts";
import modules from "./plugins/modules.ts";
import nunjucks, { Options as NunjucksOptions } from "./plugins/nunjucks.ts";
import search from "./plugins/search.ts";
import yaml, { Options as YamlOptions } from "./plugins/yaml.ts";
import { merge } from "./core/utils.ts";
import { ServerOptions, SiteOptions } from "./core.ts";
interface PluginOptions {
markdown?: MarkdownOptions;
nunjucks?: NunjucksOptions;
yaml?: YamlOptions;
}
interface Options extends Omit<Partial<SiteOptions>, "server"> {
server?: Partial<ServerOptions>;
}
export default function (
options: Options = {},
pluginOptions: PluginOptions = {},
) {
options = merge(options, getOptionsFromCli());
const site = new Site(options as Partial<SiteOptions>);
return site
.ignore("node_modules")
.use(url())
.use(json())
.use(markdown(pluginOptions.markdown))
.use(modules())
.use(nunjucks(pluginOptions.nunjucks))
.use(search())
.use(yaml(pluginOptions.yaml));
}
function getOptionsFromCli(): Partial<Options> {
const options = parse(Deno.args, {
string: [
"root",
"src",
"dest",
"location",
"metrics",
"port",
],
boolean: ["quiet", "dev", "serve", "open"],
alias: { dev: "d", serve: "s", port: "p", open: "o" },
["--"]: true,
});
const overrides: Partial<Options> = {};
if (options.root) {
overrides.cwd = resolve(Deno.cwd(), options.root);
}
if (options.src) {
overrides.src = options.src;
}
if (options.dest) {
overrides.dest = options.dest;
}
if (options.location) {
overrides.location = new URL(options.location);
} else if (options.serve) {
overrides.location = new URL(`http://localhost:${options.port || 3000}/`);
}
if ("metrics" in options) {
overrides.metrics = options.metrics || true;
}
if (options.quiet) {
overrides.quiet = options.quiet;
}
if (options.dev) {
overrides.dev = options.dev;
}
if (options.port) {
(overrides.server ||= {}).port = parseInt(options.port);
}
if (options.open) {
(overrides.server ||= {}).open = options.open;
}
if (options["--"]) {
overrides.flags = options["--"];
}
return overrides;
}