|
| 1 | +extern crate serde_json; |
1 | 2 | use clap::{App, ArgMatches};
|
2 | 3 | use env_logger::Builder;
|
3 | 4 | use log::LevelFilter;
|
| 5 | +use serde_json::Value; |
| 6 | +use std::fs::File; |
| 7 | +use std::io::BufReader; |
4 | 8 | use std::path::Path;
|
5 | 9 |
|
6 | 10 | #[derive(Debug)]
|
@@ -67,12 +71,96 @@ impl Cli {
|
67 | 71 | config.unwrap()
|
68 | 72 | }
|
69 | 73 |
|
| 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 | + /// ``` |
70 | 108 | pub fn build_node_config() -> Self {
|
71 | 109 | 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 | + |
72 | 160 | Cli {
|
73 |
| - watch_patterns: vec!["*.jsx".to_string(), ".js".to_string()], |
| 161 | + watch_patterns, |
74 | 162 | project_language: SupportedLanguage::Node,
|
75 |
| - exec_commands: vec!["npm start".to_string()], |
| 163 | + exec_commands, |
76 | 164 | }
|
77 | 165 | }
|
78 | 166 |
|
|
0 commit comments