Skip to content

Commit dfd2cb9

Browse files
authored
Merge pull request #18 from reaganmcf/node-mode-fallback
✨ cli::build_node_config() resolves exec_commands using package.json
2 parents 6b61682 + e0e0387 commit dfd2cb9

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

Cargo.lock

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ walkdir = "2"
2525
log = "0.4.0"
2626
env_logger = "0.8.3"
2727
termcolor = "1.1.2"
28+
serde_json = "1.0"

src/cli.rs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
extern crate serde_json;
12
use clap::{App, ArgMatches};
23
use env_logger::Builder;
34
use log::LevelFilter;
5+
use serde_json::Value;
6+
use std::fs::File;
7+
use std::io::BufReader;
48
use std::path::Path;
59

610
#[derive(Debug)]
@@ -67,12 +71,96 @@ impl Cli {
6771
config.unwrap()
6872
}
6973

74+
/// Build the `nodejs` configuration.
75+
///
76+
/// ### Watch Patterns
77+
/// [`.jsx`, `.js`, `.html`, `.css`]
78+
///
79+
/// ### Exec Commands
80+
/// If there is a `package.json` in the root directory, lightmon attempts to resolve the exec command
81+
/// in the following order:
82+
/// 1. The value at `scripts.start`
83+
/// 2. The value at `scripts.run`
84+
/// 3. `node main` where `main` is the value of the `main` key in `package.json` (the entry point of the project).
85+
///
86+
/// **NOTE:** exec command will fallback to `node index.js` if all of the above fail.
87+
///
88+
/// For example, the following `package.json` will result in the exec command resolving to
89+
/// `react-scripts start`:
90+
/// ```json
91+
/// {
92+
/// "name": "calculator",
93+
/// "main": "index.js",
94+
/// "scripts": {
95+
/// "start": "react-scripts start"
96+
/// "build": "react-scripts build"
97+
/// }
98+
/// }
99+
/// ```
100+
///
101+
/// In this example, the exec command will resolve to `node my_entry_point.js`:
102+
/// ```json
103+
/// {
104+
/// "name": "bar",
105+
/// "main": "my_entry_point.js"
106+
/// }
107+
/// ```
70108
pub fn build_node_config() -> Self {
71109
debug!("Configuring for node mode...");
110+
let watch_patterns: Vec<String> = vec![
111+
".jsx".to_string(),
112+
".js".to_string(),
113+
".html".to_string(),
114+
".css".to_string(),
115+
];
116+
let mut exec_commands: Vec<String> = Vec::new();
117+
118+
if Path::new("package.json").exists() {
119+
let file = File::open("package.json").unwrap();
120+
let reader = BufReader::new(file);
121+
let values: Value = serde_json::from_reader(reader).unwrap();
122+
123+
if values.is_object() {
124+
if let Some(scripts) = values.get("scripts") {
125+
debug!("scripts found! Value is = {}", scripts);
126+
if let Some(scripts_start) = scripts.get("start") {
127+
debug!(
128+
"scripts.start found! Resolving exec_commands as '{}'",
129+
scripts_start
130+
);
131+
exec_commands.push(scripts_start.to_string());
132+
} else if let Some(scripts_run) = scripts.get("run") {
133+
debug!(
134+
"scripts.run found! Resolvig exec_commands as '{}'",
135+
scripts_run
136+
);
137+
exec_commands.push(scripts_run.to_string());
138+
}
139+
}
140+
141+
// If scripts resolution failed, try getting main entry point
142+
if exec_commands.is_empty() {
143+
if let Some(main_entry_point) = values.get("main") {
144+
debug!(
145+
"main found! Resolving exec_commands as '{}'",
146+
main_entry_point
147+
);
148+
exec_commands.push(format!("node {}", main_entry_point))
149+
}
150+
}
151+
}
152+
}
153+
154+
// exec commands resolution fallback
155+
if exec_commands.is_empty() {
156+
debug!("Failed to resolve exec command using package.json, falling back to 'node index.js'");
157+
exec_commands.push("node index.js".to_string());
158+
}
159+
72160
Cli {
73-
watch_patterns: vec!["*.jsx".to_string(), ".js".to_string()],
161+
watch_patterns,
74162
project_language: SupportedLanguage::Node,
75-
exec_commands: vec!["npm start".to_string()],
163+
exec_commands,
76164
}
77165
}
78166

0 commit comments

Comments
 (0)