on master help sign in
htmlang/src/cli.rs
92 lines 3.2 kB history raw
1use std::env;
2use std::process;
4/// Subcommands understood by the CLI.
5pub const COMMANDS: &[&str] = &["build", "serve", "watch", "check", "lint", "fmt", "lsp"];
7pub fn print_help() {
8 eprintln!(
9 "\
10htmlang {} - a minimalist layout language that compiles to static HTML
12Usage: htmlang [options] <file.hl | directory>
13 htmlang <command> [args]
15Commands:
16 build <dir> [-o <out>] [--minify] [--strict]
17 Compile every .hl file under a directory, except
18 libraries (files that hold only @let definitions)
19 serve [dir|file] [-p N] [--open]
20 Dev server with live reload
21 watch [dir|file] [-o <out>]
22 Recompile on change, without a server
23 check <file.hl | dir> [--format json]
24 Report diagnostics without writing output
25 lint <file.hl | dir> [--format json]
26 Stricter checks (accessibility, nesting)
27 fmt <file.hl>... Format files in place
28 lsp Start the language server (stdio)
30Options:
31 -o, --output <path> Output file or directory
32 -w, --watch Recompile on change
33 -s, --serve Dev server with live reload (implies --watch)
34 -p, --port <N> Dev server port (default: 3000)
35 --open Open the browser (with --serve)
36 -d, --dev Development mode (readable output, source maps)
37 -c, --check Check for errors without writing output
38 --strict Treat warnings as errors
39 --partial Output an HTML fragment without the document wrapper
40 --format json Print diagnostics as JSON
41 -h, --help Show this help
42 -V, --version Show version
44Examples:
45 htmlang page.hl Compile page.hl to page.html
46 htmlang site/ Compile every .hl file in a directory
47 htmlang -s --open site/ Serve a site and open the browser
48 htmlang build src/ -o dist/ Compile src/ into dist/
49 htmlang check src/ Check every file in a directory",
50 env!("CARGO_PKG_VERSION")
51 );
54pub fn run_lsp() {
55 let self_exe = env::current_exe().ok();
56 let lsp_name = if cfg!(windows) {
57 "htmlang-lsp.exe"
58 } else {
59 "htmlang-lsp"
60 };
62 let lsp_path = self_exe
63 .as_ref()
64 .and_then(|exe| exe.parent())
65 .map(|dir| dir.join(lsp_name))
66 .filter(|p| p.exists());
68 let lsp_cmd = lsp_path
69 .map(|p| p.to_string_lossy().to_string())
70 .unwrap_or_else(|| lsp_name.to_string());
72 let status = std::process::Command::new(&lsp_cmd)
73 .stdin(std::process::Stdio::inherit())
74 .stdout(std::process::Stdio::inherit())
75 .stderr(std::process::Stdio::inherit())
76 .status();
78 match status {
79 Ok(s) => {
80 if !s.success() {
81 process::exit(s.code().unwrap_or(1));
82 }
83 }
84 Err(_) => {
85 eprintln!("error: could not find htmlang-lsp binary");
86 eprintln!(
87 "hint: ensure htmlang-lsp is in the same directory as htmlang, or in your PATH"
88 );
89 process::exit(1);
90 }
91 }