forked from lumeland/lume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
256 lines (213 loc) · 5.55 KB
/
cli.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { existsSync } from "./deps/fs.js";
import { parse } from "./deps/flags.js";
import { brightGreen, gray } from "./deps/colors.js";
import { join, relative, resolve } from "./deps/path.js";
import lume from "./mod.js";
if (import.meta.main) {
cli(Deno.args);
}
export default async function cli(args) {
const version = "v0.10.9";
let stop = false;
const options = parse(args, {
boolean: ["serve", "init", "version", "dev", "help", "upgrade"],
string: ["run", "port", "src", "dest", "location"],
alias: {
help: "h",
version: "v",
},
["--"]: true,
unknown(option) {
if (option.startsWith("-")) {
console.log(`Unknown option: ${option}`);
stop = true;
}
},
});
if (stop) {
console.log(`Run ${brightGreen("lume --help")} for usage information`);
console.log("");
return;
}
if (options._.length > 1) {
console.log(`Too much arguments: ${options._.join(", ")}`);
console.log(`Run ${brightGreen("lume --help")} for usage information`);
console.log("");
return;
}
// lume --help
if (options.help) {
console.log(`🔥lume ${version}
A static site generator for Deno
Docs: https://oscarotero.github.io/lume/
To build the site:
lume
To serve the site in localhost
lume --serve
USAGE:
lume [OPTIONS] [<path>]
OPTIONS:
--dest Set/override the dest option
--dev Run lume in development mode
-h, --help Prints help information
--init Creates a _config.js file
--location Set/override the location option
--port Change the default port of the webserver (from 3000)
--run Run a script
--serve Starts the webserver
--src Set/override the src option
--upgrade Upgrade lume to the latest version
-v, --version Prints version information
`);
return;
}
// lume --version
if (options.version) {
console.log(`🔥lume ${version}`);
return;
}
// lume --upgrade
if (options.upgrade) {
const versions = await fetch(
"https://cdn.deno.land/lume/meta/versions.json",
).then((res) => res.json());
if (versions.latest === version) {
console.log(
`You're using the latest version of lume: ${versions.latest}!`,
);
console.log("");
return;
}
console.log(
`New version available. Updating lume to ${versions.latest}...`,
);
await Deno.run({
cmd: [
"deno",
"install",
"--unstable",
"-Afr",
`https://deno.land/x/lume@${versions.latest}/cli.js`,
],
}).status();
await Deno.run({
cmd: [
"deno",
"cache",
"--unstable",
"-r",
`https://deno.land/x/lume/mod.js`,
],
}).status();
console.log("");
console.log(
`Update successful! You're using the latest version of lume: ${
brightGreen(versions.latest)
}!`,
);
console.log("");
return;
}
let cwd, configFile;
if (options._[0]) {
const path = options._[0];
if (path.endsWith(".js") || path.endsWith(".ts")) {
configFile = resolve(path);
cwd = dirname(configFile);
} else {
cwd = resolve(path);
configFile = join(cwd, "_config.js");
if (!existsSync(cwd)) {
console.log(`The folder ${cwd} does not exists`);
console.log("");
return;
}
}
} else {
cwd = Deno.cwd();
configFile = join(cwd, "_config.js");
}
// lume --init
if (options.init) {
Deno.writeTextFileSync(
configFile,
`import lume from "https://deno.land/x/lume/mod.js";
const site = lume();
export default site;
`,
);
console.log(brightGreen("Created config file"), configFile);
return;
}
let site;
if (existsSync(configFile)) {
const mod = await import(`file://${configFile}`);
site = mod.default;
site.options.cwd = cwd;
} else {
site = lume({ cwd });
}
if (options.dev) {
site.options.dev = options.dev;
}
if (options.location) {
site.options.location = new URL(options.location);
}
if (options.src) {
site.options.src = options.src;
}
if (options.dest) {
site.options.dest = options.dest;
}
if (options["--"]) {
site.options.flags = options["--"];
}
// lume --run
if (options.run) {
const success = await site.run(options.run);
window.addEventListener("unload", () => Deno.exit(success ? 0 : 1));
return;
}
console.log("");
await site.build();
console.log("");
console.log(`🍾 ${brightGreen("Site built into")} ${gray(site.options.dest)}`);
if (!options.serve) {
return;
}
// lume --serve
const { server } = await import("./server.js");
try {
await server(site, options);
const watcher = Deno.watchFs(site.src());
const changes = new Set();
console.log("Watching for changes...");
let timer = 0;
const rebuild = async () => {
console.log("");
console.log("Changes detected. Building...");
const files = new Set(changes);
changes.clear();
try {
await site.update(files);
console.log("Done");
console.log("");
} catch (err) {
console.error(err);
}
};
for await (const event of watcher) {
if (event.paths.every((path) => path.startsWith(site.dest()))) {
continue;
}
event.paths.forEach((path) =>
changes.add(join("/", relative(site.src(), path)))
);
//Debounce
clearTimeout(timer);
timer = setTimeout(rebuild, 500);
}
} catch (err) {
console.log(err);
}
}