Skip to content

Commit b2d68af

Browse files
authored
Cleanup codebase and refactors (#37)
1 parent 776efc4 commit b2d68af

File tree

12 files changed

+84
-214
lines changed

12 files changed

+84
-214
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
[package]
22
name = "lightmon"
3-
version = "0.2.0-alpha.0"
3+
version = "0.2.0-alpha.2"
44
authors = ["Reagan McFarland <me@reaganmcf.com>", "Alay Shah"]
55
description = "A lightweight, cross-platform, language-agnostic 'run code on file change' tool, inspired by Nodemon"
66
homepage = "https://github.com/reaganmcf/lightmon"
7-
documentation = "https://docs.rs/lightmon"
87
license = "GPL-3.0"
98
categories = ["command-line-utilities"]
109
keywords = ["dev-tools", "nodemon", "productivity", "file-monitoring"]

src/cli.rs

Lines changed: 35 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//! Responsible for parsing command line arguments and returning information needed by the
2-
//! watcher and exec threads.
3-
4-
extern crate serde_json;
51
use clap::{App, AppSettings, ArgMatches};
62
use env_logger::Builder;
73
use log::LevelFilter;
@@ -10,9 +6,9 @@ use std::fs::File;
106
use std::io::BufReader;
117
use std::path::Path;
128

13-
/// Enum definitions of all supported languages
9+
// Enum definitions of all supported languages
1410
#[derive(Debug)]
15-
pub enum SupportedLanguage {
11+
pub(crate) enum SupportedLanguage {
1612
Rust,
1713
Node,
1814
Shell,
@@ -28,18 +24,18 @@ impl std::fmt::Display for SupportedLanguage {
2824
}
2925
}
3026

31-
/// Struct that contains all parsed data necessary for `exec` and `watcher`
27+
// Struct that contains all parsed data necessary for `exec` and `watcher`
3228
#[derive(Debug)]
33-
pub struct Cli {
29+
pub(crate) struct Cli {
3430
pub watch_patterns: Vec<String>, // file patterns to watch
3531
pub project_language: SupportedLanguage,
3632
pub exec_commands: Vec<String>, // list of commands to run
3733
}
3834

3935
impl Cli {
40-
/// Entry point for generating a new Cli struct. Uses clap, as well as automatic language
41-
/// project detection to determine which config to build.
42-
pub fn new() -> Self {
36+
// Entry point for generating a new Cli struct. Uses clap, as well as automatic language
37+
// project detection to determine which config to build.
38+
pub(crate) fn new() -> Self {
4339
let yaml = load_yaml!("cli.yaml");
4440
let matches: ArgMatches = App::from_yaml(yaml)
4541
.global_setting(AppSettings::AllowExternalSubcommands)
@@ -90,39 +86,16 @@ impl Cli {
9086
config.unwrap()
9187
}
9288

93-
/// Build the `nodejs` configuration.
94-
///
95-
/// ### Watch Patterns
96-
/// [`.jsx`, `.js`, `.html`, `.css`]
97-
///
98-
/// ### Exec Commands
99-
/// If there is a `package.json` in the root directory, lightmon attempts to resolve the exec command
100-
/// in the following order:
101-
/// 1. The value at `scripts.start`
102-
/// 2. `node main` where `main` is the value of the `main` key in `package.json` (the entry point of the project).
103-
///
104-
/// **NOTE:** exec command will fallback to `node index.js` if all of the above fail.
105-
///
106-
/// For example, the following `package.json` will result in the exec command resolving to
107-
/// `react-scripts start`:
108-
/// ```json
109-
/// {
110-
/// "name": "calculator",
111-
/// "main": "index.js",
112-
/// "scripts": {
113-
/// "start": "react-scripts start"
114-
/// "build": "react-scripts build"
115-
/// }
116-
/// }
117-
/// ```
118-
///
119-
/// In this example, the exec command will resolve to `node my_entry_point.js`:
120-
/// ```json
121-
/// {
122-
/// "name": "bar",
123-
/// "main": "my_entry_point.js"
124-
/// }
125-
/// ```
89+
// Build the `nodejs` configuration.
90+
//
91+
// ### Watch Patterns
92+
// [`.jsx`, `.js`, `.html`, `.css`]
93+
//
94+
// ### Exec Commands
95+
// If there is a `package.json` in the root directory, lightmon attempts to resolve the exec command
96+
// in the following order:
97+
// 1. The value at `scripts.start`
98+
// 2. `node main` where `main` is the value of the `main` key in `package.json` (the entry point of the project).
12699
fn build_node_config() -> Self {
127100
debug!("Configuring for node mode...");
128101
let watch_patterns: Vec<String> = vec![
@@ -177,22 +150,22 @@ impl Cli {
177150
}
178151
}
179152

180-
/// Build the `rust` configuration.
181-
///
182-
/// ### Watch Patterns
183-
/// [`Cargo.toml`, `.rs`]
184-
///
185-
/// ### Exec Commands
186-
/// If no subcommand is passed in, the exec command resolves with the following rules:
187-
/// 1. `cargo run` if `src/main.rs` exists
188-
/// 2. `cargo test` if `src/lib.rs` exists
189-
///
190-
/// However, you can also specify any subcommand and custom arguments explicitly and they will
191-
/// be carried over to the exec command. For example
192-
/// ```
193-
/// lightmon rust build --bin my_bin --all-targets
194-
/// ```
195-
/// Will resolve the exec command to `cargo build --bin my_bin --all-targets`
153+
// Build the `rust` configuration.
154+
//
155+
// ### Watch Patterns
156+
// [`Cargo.toml`, `.rs`]
157+
//
158+
// ### Exec Commands
159+
// If no subcommand is passed in, the exec command resolves with the following rules:
160+
// 1. `cargo run` if `src/main.rs` exists
161+
// 2. `cargo test` if `src/lib.rs` exists
162+
//
163+
// However, you can also specify any subcommand and custom arguments explicitly and they will
164+
// be carried over to the exec command. For example
165+
// ```
166+
// lightmon rust build --bin my_bin --all-targets
167+
// ```
168+
// Will resolve the exec command to `cargo build --bin my_bin --all-targets`
196169
fn build_rust_config(sub_matcher: Option<&ArgMatches>) -> Self {
197170
let mut exec_commands: Vec<String> = Vec::new();
198171
debug!("Configuring for rust mode...");
@@ -230,8 +203,8 @@ impl Cli {
230203
}
231204
}
232205

233-
/// Build the `shell` configuration.
234-
/// The watch patterns and exec commands are determined by the arguments passed in.
206+
// Build the `shell` configuration.
207+
// The watch patterns and exec commands are determined by the arguments passed in.
235208
fn build_shell_config(sub_matcher: &ArgMatches) -> Self {
236209
let mut watch_patterns: Vec<String> = Vec::new();
237210
let mut exec_commands: Vec<String> = Vec::new();

src/cli.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: lightmon
2-
version: "0.0.1"
2+
version: "v0.2.0-alpha.2"
33
author: Reagan McFarland <me@reaganmcf.com>, Alay Shah
44
about: A light-weight, cross-platform, language agnostic \"run code on file change\" tool, inspired by Nodemon
55
args:

src/exec.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
//! Contains the method for starting a thread that will run the exec commands in parallel.
1+
// Contains the method for starting a thread that will run the exec commands in parallel.
22

33
use std::process::Command;
44
use std::sync::mpsc::Sender;
55
use std::sync::Arc;
66
use std::thread;
7-
use std::{
8-
io::{self},
9-
process::Child,
10-
};
7+
use std::{io, process::Child};
118

12-
pub use crate::cli::Cli;
13-
pub use crate::LightmonEvent;
9+
use crate::cli::Cli;
10+
use crate::LightmonEvent;
1411

15-
/// Start an exec thread that will run the exec commands
16-
///
17-
/// Returns a handler to the thread
18-
pub fn start(
12+
// Start an exec thread that will run the exec commands
13+
//
14+
// Returns a handler to the thread
15+
pub(crate) fn start(
1916
cli_args: Arc<Cli>,
2017
lightmon_event_sender: Sender<LightmonEvent>,
2118
exec_child_process_sender: Sender<Child>,

src/main.rs

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,8 @@
1-
//! <p align="center">
2-
//! <img height="250px" src="https://raw.githubusercontent.com/reaganmcf/lightmon/master/assets/logo.png"/>
3-
//! </p>
4-
//!
5-
//! # lightmon
6-
//! A lightweight, cross-platform, language-agnostic "run code on file change" tool, inspired by Nodemon
7-
//! <p align="left">
8-
//! <img src="https://img.shields.io/static/v1?label=status&message=In%20Development&color=critical"/>
9-
//! <img src="https://img.shields.io/crates/v/lightmon"/>
10-
//! <img src="https://github.com/reaganmcf/lightmon/actions/workflows/ci.yml/badge.svg"/>
11-
//! <img src="https://shields.io/github/license/reaganmcf/lightmon"/>
12-
//! </p>
13-
//!
14-
//! ### Why lightmon over nodemon?
15-
//! There are many reasons to use lightmon over nodemon: __it's faster, lighter, and can be used for all types of projects__. Not only this, but lightmon is a **drag and drop replacement** for projects that use `nodemon` because lightman can parse existing `nodemon.json` config files.
16-
//! - Note: [Parse nodemon.json config is still WIP](https://github.com/reaganmcf/lightmon/issues/3)
17-
//!
18-
//! ## Usage
19-
//! ```
20-
//! lightmon
21-
//! ```
22-
//! By default, `lightmon` will automatically determine what kind of files it should watch based upon your project structure. For example, if a `node_modules` folder is present in the directory, `lightmon` will run in the `node` configuration, parsing your `package.json` to infer the correct command to run.
23-
//!
24-
//! ## Supported languages
25-
//!
26-
//! Watch patterns are the file patterns that lightmon will watch for file changes, and Exec commands are the list of commands that are executed when those events happen.
27-
//!
28-
//! ### Rust
29-
//! ```
30-
//! lightmon rust [cargo_subcommand]?
31-
//! ```
32-
//!
33-
//! ##### Watch Patterns
34-
//! [`Cargo.toml`, `.rs`]
35-
//!
36-
//! ##### Exec Commands
37-
//! By default, the `rust` configuration will set the Exec command to `cargo run` if it's a binary project, and `cargo test` if it's a library.
38-
//!
39-
//! However, you can override this behavior by specifying the subcommand manually. For example, you want to do `cargo test` on a binary project instead of `cargo run` on file change events, you should do the following:
40-
//! ```
41-
//! lightmon rust test
42-
//! ```
43-
//!
44-
//! Refer to `lightmon help rust` for more information.
45-
//! ### Node.js
46-
//! **Note: This configuration also works for React, React-Native, TypeScript, etc. Anything with a package.json!**
47-
//!
48-
//! ```
49-
//! lightmon node
50-
//! ```
51-
//!
52-
//! ##### Watch Patterns
53-
//!
54-
//! [`.jsx`, `.js`, `.css`, `.html`]
55-
//! ##### Exec Commands
56-
//!
57-
//! If there is a package.json in the root directory, lightmon attempts to resolve the exec command in the following order:
58-
//!
59-
//! - The value at `scripts.start`
60-
//! - `node main` where main is the value of the main key in package.json (the entry point of the project).
61-
//!
62-
//! **NOTE:** The Exec command will fallback to `node index.js` if all of the above fail.
63-
//!
64-
//! For example, the following package.json will result in the Exec command resolving to `react-scripts start`:
65-
//! ```json
66-
//! {
67-
//! "name": "calculator",
68-
//! "main": "index.js",
69-
//! "scripts": {
70-
//! "start": "react-scripts start",
71-
//! "build": "react-scripts build"
72-
//! }
73-
//! }
74-
//! ```
75-
//!
76-
//! ### C/C++
77-
//! It's very tricky to infer what the patterns and exec commands could be, so we recommend using `shell` mode with a custom script (see below).
78-
//!
79-
//! ### Shell (for unsupported languages or complicated builds)
80-
//! `lightmon shell -s <path> -w <patterns>`
81-
//! Here users can specify the path to the shell script and which file types to watch for seperated by commas.
82-
//!
83-
//! For example, let's say you have a python project with a file named `start.py` at the root of the project. Whenever you edit any `.py` files in the project, you want to
84-
//! re-run `python start.py`. To accomplish this, you could create a simple script called `run.sh` with the following contents:
85-
//! ```sh
86-
//! python start.py
87-
//! ```
88-
//!
89-
//! Now, you just run the following:
90-
//! ```
91-
//! lightmon shell -s run.sh -w .py,.ipynb
92-
//! ```
93-
941
#[macro_use]
952
extern crate log;
963
#[macro_use]
974
extern crate clap;
98-
extern crate notify;
5+
996
mod cli;
1007
mod exec;
1018
mod watcher;
@@ -107,14 +14,12 @@ use std::{
10714
sync::mpsc::{channel, Receiver, Sender},
10815
};
10916

110-
/// Type of events that lightmon handles.
111-
pub enum LightmonEvent {
112-
/// When lightmon first starts up successfully, it forces the exec thread to go off once
17+
pub(crate) enum LightmonEvent {
11318
InitExec,
11419
KillAndRestartChild,
11520
}
11621

117-
/// Entry point for the entire binary.
22+
// Entry point for the entire binary.
11823
fn main() {
11924
let cli_args = Arc::new(Cli::new());
12025

src/watcher.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
//! Contains the thread that will start the file watcher and send LightmonEvents back to the main
2-
//! thread when they happen.
3-
4-
extern crate notify;
5-
extern crate walkdir;
6-
71
use notify::poll::PollWatcher;
82
use notify::{RecursiveMode, Watcher};
93
use std::collections::HashSet;
@@ -16,13 +10,15 @@ use std::{
1610
};
1711
use walkdir::WalkDir;
1812

19-
pub use crate::cli::Cli;
20-
pub use crate::LightmonEvent;
13+
use crate::cli::Cli;
14+
use crate::LightmonEvent;
2115

22-
/// Start a new watcher thread that will send LightmonEvents back to the main thread.
23-
///
24-
/// Returns a handle to the new thread
25-
pub fn start(cli_args: Arc<Cli>, lightmon_event_sender: Sender<LightmonEvent>) -> JoinHandle<()> {
16+
// Start a new watcher thread that will send LightmonEvents back to the main thread.
17+
// Returns a handle to the new thread
18+
pub(crate) fn start(
19+
cli_args: Arc<Cli>,
20+
lightmon_event_sender: Sender<LightmonEvent>,
21+
) -> JoinHandle<()> {
2622
std::thread::spawn(move || {
2723
let (tx, rx) = channel();
2824
let mut watcher = PollWatcher::with_delay_ms(tx, 100).unwrap();
@@ -78,7 +74,7 @@ pub fn start(cli_args: Arc<Cli>, lightmon_event_sender: Sender<LightmonEvent>) -
7874
}
7975
}
8076
Err(e) => {
81-
error!("Failed to receieve event from channel {:?}", e);
77+
error!("Failed to receive event from channel {:?}", e);
8278
}
8379
}
8480
}

tests/lightmon.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
extern crate assert_cmd;
21
mod utils;
2+
33
use assert_cmd::prelude::*;
44
use std::process::Command;
55
use std::time::Duration;
@@ -8,15 +8,15 @@ use utils::*;
88
const EP_SHELL_BASIC_PATH: &str = "./tests/example_projects/shell_basic";
99

1010
#[test]
11-
fn unsupported_configuration_fails() -> Result<(), Box<dyn std::error::Error>> {
11+
fn unsupported_configuration_fails() -> TestResult {
1212
let mut cmd = Command::cargo_bin("lightmon")?;
1313
cmd.arg("java").assert().failure();
1414

1515
Ok(())
1616
}
1717

1818
#[test]
19-
fn verbose_shows_debug_statements() -> Result<(), Box<dyn std::error::Error>> {
19+
fn verbose_shows_debug_statements() -> TestResult {
2020
// Spawn child lightmon process at
2121
let output = run_example(
2222
EP_SHELL_BASIC_PATH,

0 commit comments

Comments
 (0)