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
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use globwalk::{FileType, GlobWalkerBuilder};
use indoc::formatdoc;
use pariter::IteratorExt;
use serde::Deserialize;
use crate::configuration_file::ConfigurationFile;
use crate::io::read_json_from_file;
use crate::package_manifest::PackageManifest;
#[derive(Debug, Deserialize)]
struct PackageManifestGlob(String);
#[derive(Debug, Deserialize)]
struct LernaManifestFile {
packages: Vec<PackageManifestGlob>,
}
#[derive(Debug, Deserialize)]
struct PackageManifestFile {
workspaces: Vec<PackageManifestGlob>,
}
#[derive(Debug)]
pub struct MonorepoManifest {
root: PathBuf,
globs: Vec<PackageManifestGlob>,
}
fn get_internal_package_manifests(
monorepo_root: &Path,
package_globs: &[PackageManifestGlob],
) -> Result<Vec<PackageManifest>> {
let mut package_manifests: Vec<String> = package_globs
.iter()
.map(|package_manifest_glob| {
Path::new(&package_manifest_glob.0)
.join("package.json")
.to_str()
.expect("Path not valid UTF-8")
.to_string()
})
.collect();
package_manifests.push(String::from("!node_modules/"));
let monorepo_root = monorepo_root.to_owned();
GlobWalkerBuilder::from_patterns(&monorepo_root, &package_manifests)
.file_type(FileType::FILE)
.min_depth(1)
.build()
.expect("Unable to create glob")
.into_iter()
.filter_map(Result::ok)
.parallel_map_custom(
|options| options.threads(32),
move |dir_entry| {
PackageManifest::from_directory(
&monorepo_root,
dir_entry
.path()
.parent()
.expect("Unexpected package in monorepo root")
.strip_prefix(&monorepo_root)
.expect("Unexpected package in monorepo root"),
)
},
)
.collect()
}
impl MonorepoManifest {
const LERNA_MANIFEST_FILENAME: &'static str = "lerna.json";
const PACKAGE_MANIFEST_FILENAME: &'static str = "package.json";
fn from_lerna_manifest(root: &Path) -> Result<MonorepoManifest> {
let filename = root.join(Self::LERNA_MANIFEST_FILENAME);
let lerna_manifest: LernaManifestFile =
read_json_from_file(&filename).with_context(|| {
formatdoc!(
"
Unexpected contents in {:?}
I'm trying to parse the following properties and values out
of this lerna.json file:
- packages: string[]
",
filename
)
})?;
Ok(MonorepoManifest {
root: root.to_owned(),
globs: lerna_manifest.packages,
})
}
fn from_package_manifest(root: &Path) -> Result<MonorepoManifest> {
let filename = root.join(Self::PACKAGE_MANIFEST_FILENAME);
let package_manifest: PackageManifestFile =
read_json_from_file(&filename).with_context(|| {
formatdoc!(
"
Unexpected contents in {:?}
I'm trying to parse the following properties and values out
of this package.json file:
- workspaces: string[]
",
filename
)
})?;
Ok(MonorepoManifest {
root: root.to_owned(),
globs: package_manifest.workspaces,
})
}
pub fn from_directory(root: &Path) -> Result<MonorepoManifest> {
MonorepoManifest::from_lerna_manifest(root)
.or_else(|_| MonorepoManifest::from_package_manifest(root))
}
pub fn package_manifests_by_package_name(&self) -> Result<HashMap<String, PackageManifest>> {
Ok(get_internal_package_manifests(&self.root, &self.globs)?
.into_iter()
.map(|manifest| (manifest.contents.name.to_owned(), manifest))
.collect())
}
pub fn internal_package_manifests(&self) -> Result<Vec<PackageManifest>> {
get_internal_package_manifests(&self.root, &self.globs)
}
}