Skip to content

Commit

Permalink
chore: improve cli style (umijs#1265)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Jun 11, 2024
1 parent 5dee7e5 commit d24fa70
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 29 deletions.
79 changes: 60 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/mako/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cached = { workspace = true }
dashmap = "4.0.1"
delegate = "0.12.0"
fixedbitset = "0.4.2"
get_if_addrs = "0.5.3"
glob-match = "0.2.1"
heck = "0.4.1"
mako_core = { path = "../core" }
Expand Down
48 changes: 42 additions & 6 deletions crates/mako/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::path::PathBuf;
use std::sync::{mpsc, Arc};
use std::time::{Duration, Instant, UNIX_EPOCH};

use get_if_addrs::get_if_addrs;
use mako_core::anyhow::{self, Result};
use mako_core::colored::Colorize;
use mako_core::futures::{SinkExt, StreamExt};
Expand Down Expand Up @@ -84,6 +85,25 @@ impl DevServer {
});
let server = Server::bind(&addr).serve(make_svc);
// TODO: print when mako is run standalone
if std::env::var("MAKO_CLI").is_ok() {
println!();
println!(
"Local: {}",
format!("http://localhost:{}/", port).to_string().cyan()
);
let ips = Self::get_ips();
let ips = ips
.iter()
.filter(|ip| !ip.starts_with("127."))
.collect::<Vec<_>>();
for ip in ips {
println!(
"Network: {}",
format!("http://{}:{}/", ip, port).to_string().cyan()
);
}
println!();
}
debug!("Listening on http://{:?}", addr);
if let Err(e) = server.await {
eprintln!("Error starting server: {:?}", e);
Expand Down Expand Up @@ -167,6 +187,22 @@ impl DevServer {
}
}

fn get_ips() -> Vec<String> {
let mut ips = vec![];
match get_if_addrs() {
Ok(if_addrs) => {
for if_addr in if_addrs {
if let get_if_addrs::IfAddr::V4(addr) = if_addr.addr {
let ip = addr.ip.to_string();
ips.push(ip);
}
}
}
Err(_e) => {}
}
ips
}

// TODO: refact socket message data structure
async fn handle_websocket(
websocket: hyper_tungstenite::HyperWebsocket,
Expand Down Expand Up @@ -285,12 +321,12 @@ impl DevServer {
"hot update chunks generated, next_full_hash: {:?}",
next_hash
);
if !has_missing_deps {
println!(
"Hot rebuilt in {}",
format!("{}ms", t_compiler.elapsed().as_millis()).bold()
);
}
// if !has_missing_deps {
// println!(
// "Hot rebuilt in {}",
// format!("{}ms", t_compiler.elapsed().as_millis()).bold()
// );
// }
if let Err(e) = next_hash {
eprintln!("Error in watch: {:?}", e);
return Err(e);
Expand Down
4 changes: 4 additions & 0 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

/* auto-generated by NAPI-RS */

export interface TransformOutput {
code: string;
map?: string;
}
export interface JsHooks {
name?: string;
load?: (
Expand Down
3 changes: 2 additions & 1 deletion packages/mako/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"license": "MIT",
"dependencies": {
"@swc/helpers": "0.5.1",
"chalk": "^4.1.2",
"less": "^4.2.0",
"less-plugin-resolve": "^1.0.2",
"lodash": "^4.17.21",
Expand Down Expand Up @@ -74,4 +75,4 @@
"@umijs/mako-linux-x64-musl": "0.5.4"
},
"repository": "git@github.com:umijs/mako.git"
}
}
8 changes: 8 additions & 0 deletions packages/mako/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import chalk from 'chalk';
import yParser from 'yargs-parser';

(async () => {
Expand All @@ -9,6 +10,13 @@ import yParser from 'yargs-parser';
process.exit(1);
}

// use MAKO_CLI to identify if it's running in mako cli standalone
// so that we can print extra information
process.env.MAKO_CLI = '1';
console.log();
console.log(chalk.bold(`Mako v${require('../package.json').version}`));
console.log();

let argv = yParser(process.argv.slice(2));
let command = argv._[0];
switch (command) {
Expand Down
Loading

0 comments on commit d24fa70

Please sign in to comment.