-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
cli.js
130 lines (109 loc) · 3.48 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
import { parse } from "./deps/flags.js";
import { bold, brightGreen, red } from "./deps/colors.js";
import * as upgrade from "./cli/upgrade.js";
import * as init from "./cli/init.js";
import * as build from "./cli/build.js";
import * as run from "./cli/run.js";
import { error as printError } from "./utils.js";
export const version = "v0.20.2";
const HELP = `
Docs: https://lumeland.github.io/
To build the site:
lume
To serve the site in localhost:
lume --serve
To upgrade lume to the latest version:
lume upgrade
To run a custom script:
lume run <script>
To get help with a command:
lume COMMAND --help
USAGE:
lume [COMMAND] [OPTIONS]
COMMANDS:
build Build the site (Default command)
init Create a config file
run Run a custom script
upgrade Upgrade lume to the latest version
OPTIONS:
-h, --help print usage information
-v, --version print version information
--root <dir> the root where lume should work Default: ./
--src <dir> the source directory for your site Default: ./
--dest <dir> the build destination Default: _site
--config <file> specify the lume config file Default: _config.js
--location <url> the domain for your site Default: http://localhost
-d, --dev enable dev mode (view draft pages)
-s, --serve start a live-reloading web server
-p, --port <port> the port where the server runs Default: 3000
-o, --open open the site in the browser
`;
if (import.meta.main) {
try {
await cli(Deno.args);
} catch (error) {
printError("lume", error.message, error);
}
}
export default async function cli(args, site) {
// The rest of the option parsing is handled within each command
const options = parse(args, {
boolean: ["help", "version"],
alias: { help: "h", version: "v" },
"--": true,
});
// lume --version
if (options.version) {
console.log(`🔥lume ${version}`);
return;
}
// lume --help (with no command)
if (options._.length === 0 && help(HELP)) {
return;
}
// _ contains the non-option arguments
const command = options._[0]?.toLowerCase() || "build";
/**
* print the given help message if the options asked for help
*
* @return true if help was printed, false otherwise
*/
function help(message) {
if (options.help) {
console.log(`
🔥lume ${version}
A static site generator for Deno`);
console.log(message);
return true;
} else {
return false;
}
}
/**
* run the given command, or print it's help message, if it was the one requested
*
* @param name command name to compare to the cli argument
* @param runner command code to run if this is the requested command
*/
async function maybeRun(name, runner) {
if (command === name) {
help(runner.HELP) || await runner.run(args, site);
return true;
}
return false;
}
// Check each command. If any of them ran, then return
if (
await maybeRun("build", build) ||
await maybeRun("init", init) ||
await maybeRun("run", run) ||
await maybeRun("upgrade", upgrade)
) {
return;
}
// Down here means the command was not recognized
throw new Error(`
${bold(red("error:"))} lume does not understand the command '${command}'
Run ${brightGreen("lume --help")} for usage information
`);
}