1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use super::js_package_manager_main as js;
use crate::{
file::path_to_content,
model::{command, runner_type},
};
use anyhow::{Result, anyhow};
use std::{path::PathBuf, process};
const YARN_LOCKFILE_NAME: &str = "yarn.lock";
#[derive(Clone, Debug, PartialEq)]
pub struct Yarn {
pub path: PathBuf,
commands: Vec<command::CommandWithPreview>,
}
enum YarnVersion {
V1,
V2OrLater,
}
impl Yarn {
pub fn command_to_run(&self, command: &command::CommandForExec) -> Result<String> {
Ok(format!("yarn {}", command.args))
}
pub fn execute(&self, command: &command::CommandForExec) -> Result<()> {
let child = process::Command::new("yarn")
.stdin(process::Stdio::inherit())
.args(command.args.split_whitespace().collect::<Vec<&str>>())
.spawn();
match child {
Ok(mut child) => match child.wait() {
Ok(_) => Ok(()),
Err(e) => Err(anyhow!("failed to run: {}", e)),
},
Err(e) => Err(anyhow!("failed to spawn: {}", e)),
}
}
pub fn new(current_dir: PathBuf, cwd_file_names: Vec<String>) -> Option<Yarn> {
Iterator::find(&mut cwd_file_names.iter(), |&f| f == js::METADATA_FILE_NAME)?;
if Iterator::find(&mut cwd_file_names.iter(), |&f| f == YARN_LOCKFILE_NAME).is_some() {
// package.json and yarn.lock exist
match Yarn::collect_workspace_scripts(current_dir.clone()) {
Some(commands) => {
return Some(Yarn {
path: current_dir,
commands,
});
}
None => return None,
}
}
// package.json exists, but yarn.lock does not exist
// executed in child packages of yarn workspaces || not in yarn workspace (including using an other package manager)
match Self::get_yarn_version() {
Some(yarn_version) => {
let workspace_output = match yarn_version {
YarnVersion::V1 => process::Command::new("yarn")
.arg("workspaces")
.arg("info")
.arg("--json")
.output(),
YarnVersion::V2OrLater => process::Command::new("yarn")
.arg("workspaces")
.arg("list")
.arg("--json")
.output(),
};
let workspace_output = match workspace_output {
Ok(output) => output,
Err(_) => return None, // failed to run above command
};
// If `yarn workspaces (info|list) --json` returns non-zero status code, it means that the current directory is not a yarn workspace.
if !workspace_output.status.success() {
return None;
}
// not in yarn workspace, but has a package.json
Self::collect_scripts_in_package_json(current_dir.clone()).map(|commands| Yarn {
path: current_dir,
commands,
})
}
None => None, // yarn is not installed
}
}
pub fn to_commands(&self) -> Vec<command::CommandWithPreview> {
self.commands.clone()
}
// scripts_to_commands collects all scripts by following steps:
// 1. Collect scripts defined in package.json in the current directory(which fzf-make is launched)
// 2. Collect the paths of all `package.json` in the workspace.
// 3. Collect all scripts defined in given `package.json` paths.
fn collect_workspace_scripts(current_dir: PathBuf) -> Option<Vec<command::CommandWithPreview>> {
// Collect scripts defined in package.json in the current directory(which fzf-make is launched)
let mut result = Self::collect_scripts_in_package_json(current_dir.clone())?;
// Collect the paths of all `package.json` in the workspace.
let package_json_in_workspace = match Self::get_yarn_version() {
Some(YarnVersion::V1) => Self::get_workspace_packages_for_v1(),
Some(YarnVersion::V2OrLater) => Self::get_workspace_packages_for_v2_or_later(),
None => return None,
};
// Collect all scripts defined in given `package.json` paths.
if let Ok(workspace_package_json_paths) = package_json_in_workspace {
for path in workspace_package_json_paths {
if let Ok(c) = path_to_content::path_to_content(&path)
&& let Some((name, parsing_result)) = js::JsPackageManager::parse_package_json(&c)
{
for (key, _, line_number) in parsing_result {
result.push(command::CommandWithPreview::new(
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn),
// yarn executes workspace script following format: `yarn workspace {package_name} {script_name}`
// e.g. `yarn workspace app4 build`
format!("workspace {} {}", name.clone(), key.as_str()),
path.clone(),
line_number,
));
}
};
}
};
Some(result)
}
fn collect_scripts_in_package_json(current_dir: PathBuf) -> Option<Vec<command::CommandWithPreview>> {
let parsed_scripts_part_of_package_json =
match path_to_content::path_to_content(¤t_dir.join(js::METADATA_FILE_NAME)) {
Ok(c) => match js::JsPackageManager::parse_package_json(&c) {
Some(result) => result.1,
None => return None,
},
Err(_) => return None,
};
Some(
parsed_scripts_part_of_package_json
.iter()
.map(|(key, _value, line_number)| {
command::CommandWithPreview::new(
runner_type::RunnerType::JsPackageManager(runner_type::JsPackageManager::Yarn),
key.to_string(),
current_dir.clone().join(js::METADATA_FILE_NAME),
*line_number,
)
})
.collect(),
)
}
/// Determines the installed Yarn version, if available.
/// yarn v1 support `yarn workspaces info --json` instead of `yarn workspaces list --json`.
/// We need to handle them separately, because their output format is different.
///
/// # Returns
/// - `Some(YarnVersion::V1)` if Yarn v1 is detected.
/// - `Some(YarnVersion::V2OrLater)` if Yarn v2 or later is detected.
/// - `None` if Yarn is not installed or cannot be executed.
fn get_yarn_version() -> Option<YarnVersion> {
let output = process::Command::new("yarn").arg("--version").output();
match output {
Ok(output) => {
if !output.status.success() {
return None;
}
let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
/* Example output:
"1.22.22\n"
*/
if version.starts_with("1.") {
Some(YarnVersion::V1)
} else {
Some(YarnVersion::V2OrLater)
}
}
Err(_) => None,
}
}
// get_workspaces_list parses the result of `yarn workspaces info --json` and return path of `package.json` of each package.
fn get_workspace_packages_for_v1() -> Result<Vec<PathBuf>> {
let output = process::Command::new("yarn")
.arg("workspaces")
.arg("info")
.arg("--json")
.output()?;
/* Example output:
yarn workspaces v1.22.22
{
"app1": {
"location": "packages/app",
"workspaceDependencies": [],
"mismatchedWorkspaceDependencies": []
}
}
✨ Done in 0.02s.
*/
#[derive(serde::Deserialize, Debug)]
struct Workspace {
// Relation path to the package
location: String,
}
let workspaces_json = {
let output = String::from_utf8(output.stdout)?;
/* Example output:
"yarn workspaces v1.22.22\n{\n \"app1\": {\n \"location\": \"packages/app\",\n \"workspaceDependencies\": [],\n \"mismatchedWorkspaceDependencies\": []\n }\n}\nDone in 0.01s.\n"
*/
// split by newline to remove unnecessary lines.
let lines = output.split('\n').collect::<Vec<&str>>();
// remove the first and last line and the second line from the end.
match lines.get(1..(lines.len() - 2)) {
Some(lines) => lines.join(""),
None => return Err(anyhow!("unexpected output")),
}
};
// parse json
let mut workspaces: Vec<Workspace> = vec![];
if let Ok(serde_json::Value::Object(map)) =
serde_json::from_slice::<serde_json::Value>(workspaces_json.as_bytes())
{
for (_, value) in map {
if let Ok(workspace) = serde_json::from_value(value) {
workspaces.push(workspace)
}
}
}
Ok(workspaces
.iter()
.map(|w| PathBuf::from(w.location.clone()).join(js::METADATA_FILE_NAME))
.collect())
}
// get_workspaces_list parses the result of `yarn workspaces list --json` and return path of `package.json` of each package.
fn get_workspace_packages_for_v2_or_later() -> Result<Vec<PathBuf>> {
let output = process::Command::new("yarn")
.arg("workspaces")
.arg("list")
.arg("--json")
.output()?;
// The format is the same as v1 by chance, so we do not unify intentionally.
#[derive(serde::Deserialize, Debug)]
struct Workspace {
// Relation path to the package
location: String,
}
let mut workspaces: Vec<Workspace> = vec![];
/* output is like:
"{\"location\":\".\",\"name\":\"project\"}\n{\"location\":\"packages/app1\",\"name\":\"app1\"}\n"
*/
let output = String::from_utf8(output.stdout)?;
for line in output.lines() {
// To parse json like above, use `serde_json::from_slice(line.as_bytes())`.
// see: https://stackoverflow.com/a/69001942.
if let Ok(workspace) = serde_json::from_slice(line.as_bytes()) {
workspaces.push(workspace)
}
}
Ok(workspaces
.iter()
.filter(|workspace| workspace.location != ".") // Ignore package.json in the current directory.
.map(|w| PathBuf::from(w.location.clone()).join(js::METADATA_FILE_NAME))
.collect())
}
}