deno.land / x / lume@v2.1.4 / cli / build.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { log } from "../core/utils/log.ts";import { localIp } from "../core/utils/net.ts";import { setEnv } from "../core/utils/env.ts";import Server from "../core/server.ts";import { SiteWatcher } from "../core/watcher.ts";import logger from "../middlewares/logger.ts";import noCache from "../middlewares/no_cache.ts";import notFound from "../middlewares/not_found.ts";import reload from "../middlewares/reload.ts";import { createSite } from "./run.ts";
/** Build the website and optionally watch changes and serve the site */export async function build( config: string | undefined, serve?: boolean, watch?: boolean,) { const site = await createSite(config);
performance.mark("start"); await site.build(); performance.mark("end");
log.info(`🍾 Site built into <gray>${site.options.dest}</gray>`); const duration = performance.measure("duration", "start", "end").duration / 1000; const total = site.pages.length + site.files.length; log.info( ` <gray>${total} files generated in ${duration.toFixed(2)} seconds</gray>`, );
if (!serve && !watch) { // Prevent possible timers to keep the process alive forever (wait preventively 10 seconds) const id = setTimeout(() => { log.warn( "After waiting 10 seconds, there are some timers that avoid ending the process.", ); log.warn("They have been forcibly closed."); Deno.exit(0); }, 10000);
Deno.unrefTimer(id); return; }
// Set the live reload environment variable to add hash to the URLs in the module loader setEnv("LUME_LIVE_RELOAD", "true");
// Start the watcher const watcher = site.getWatcher();
watcher.addEventListener("change", (event) => { const files = event.files!;
log.info("Changes detected:"); files.forEach((file) => log.info(`- <gray>${file}</gray>`)); return site.update(files); });
watcher.addEventListener("error", (event) => { console.error(Deno.inspect(event.error, { colors: true })); });
watcher.start();
if (!serve) { return; }
// Start the local server const { port, open, page404, middlewares } = site.options.server; const root = site.options.server.root || site.dest(); const server = new Server({ root, port });
server.addEventListener("start", () => { const ipAddr = localIp();
log.info(" Server started at:"); log.info(` <green>http://localhost:${port}/</green> (local)`);
if (ipAddr) { log.info(` <green>http://${ipAddr}:${port}/</green> (network)`); }
if (open) { const commands: Record<typeof Deno.build.os, string> = { darwin: "open", linux: "xdg-open", freebsd: "xdg-open", netbsd: "xdg-open", aix: "xdg-open", solaris: "xdg-open", illumos: "xdg-open", windows: "explorer", };
new Deno.Command(commands[Deno.build.os], { args: [`http://localhost:${port}/`], stdout: "inherit", stderr: "inherit", }).output(); }
site.dispatchEvent({ type: "afterStartServer" }); });
if (log.level === 0) { server.use(logger()); }
server.use( reload({ watcher: new SiteWatcher(site) }), noCache(), notFound({ root, page404, directoryIndex: true, }), );
if (middlewares) { server.use(...middlewares); }
server.start();}
lume

Version Info

Tagged at
7 months ago