|
1 | | -#![forbid(unsafe_code)] |
2 | | -#![feature(absolute_path)] |
3 | | -#![deny(warnings, missing_docs)] |
4 | | - |
5 | 1 | //! Enumerate source code files used by the TypeScript compiler during |
6 | 2 | //! compilation. The return value is a list of relative paths from the monorepo |
7 | 3 | //! root, sorted in alphabetical order. |
|
44 | 40 | //! [listfilesonly]: https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options |
45 | 41 | //! [tsconfig exclude]: https://www.typescriptlang.org/tsconfig#exclude |
46 | 42 |
|
| 43 | +#![forbid(unsafe_code)] |
| 44 | +#![feature(absolute_path)] |
| 45 | +#![deny(warnings, missing_docs)] |
| 46 | + |
47 | 47 | use std::{ |
| 48 | + collections::HashMap, |
48 | 49 | fs::File, |
49 | 50 | io::Read, |
50 | 51 | path::{self, Path, PathBuf}, |
@@ -246,3 +247,81 @@ pub fn tsconfig_includes( |
246 | 247 | debug!("tsconfig_includes: {:?}", included_files); |
247 | 248 | Ok(included_files) |
248 | 249 | } |
| 250 | + |
| 251 | +/// Enumerate source code files used by the TypeScript compiler during |
| 252 | +/// compilation. The return value is a list of relative paths from the monorepo |
| 253 | +/// root, grouped by scoped package name. |
| 254 | +pub fn tsconfig_includes_by_package_name( |
| 255 | + tsconfig: &Path, |
| 256 | + calculation_type: Calculation, |
| 257 | +) -> Result<HashMap<String, Vec<PathBuf>>, Error> { |
| 258 | + let tsconfig = path::absolute(tsconfig).expect(&format!( |
| 259 | + "Should be able to convert parameter `tsconfig` ({:?}) into an absolute path", |
| 260 | + tsconfig, |
| 261 | + )); |
| 262 | + debug!("tsconfig absolute path is {:?}", tsconfig); |
| 263 | + |
| 264 | + let monorepo_root = find_up::find_file(&tsconfig, "lerna.json").ok_or_else(|| { |
| 265 | + Error::TypescriptProjectNotInMonorepo { |
| 266 | + filename: tsconfig.to_string_lossy().into_owned(), |
| 267 | + } |
| 268 | + })?; |
| 269 | + debug!("monorepo_root: {:?}", monorepo_root); |
| 270 | + |
| 271 | + // This relies on an assumption that the package's package.json and tsconfig.json |
| 272 | + // live in the same directory (the package root). |
| 273 | + let target_package_manifest = tsconfig.parent().unwrap().join("package.json"); |
| 274 | + debug!("target package manifest: {:?}", target_package_manifest); |
| 275 | + |
| 276 | + let lerna_manifest = monorepo_manifest::MonorepoManifest::from_directory(&monorepo_root)?; |
| 277 | + let package_manifests_by_package_name = lerna_manifest.package_manifests_by_package_name()?; |
| 278 | + trace!("{:?}", lerna_manifest); |
| 279 | + |
| 280 | + let package_manifest = lerna_manifest |
| 281 | + .internal_package_manifests()? |
| 282 | + .into_iter() |
| 283 | + .filter(|manifest| &target_package_manifest == &monorepo_root.join(manifest.path())) |
| 284 | + .take(1) |
| 285 | + .next() |
| 286 | + .expect("Expected project to reside in monorepo"); |
| 287 | + |
| 288 | + debug!("package_manifest: {:?}", package_manifest); |
| 289 | + |
| 290 | + // Enumerate internal dependencies (exclusive) |
| 291 | + let transitive_internal_dependencies_inclusive = { |
| 292 | + let mut packages = package_manifest.transitive_internal_dependency_package_names_exclusive( |
| 293 | + &package_manifests_by_package_name, |
| 294 | + ); |
| 295 | + // Make this list inclusive of the target package |
| 296 | + packages.push(&package_manifest); |
| 297 | + packages |
| 298 | + }; |
| 299 | + |
| 300 | + debug!( |
| 301 | + "transitive_internal_dependencies_inclusive: {:?}", |
| 302 | + transitive_internal_dependencies_inclusive |
| 303 | + .iter() |
| 304 | + .map(|manifest| manifest.contents.name.clone()) |
| 305 | + .collect::<Vec<_>>() |
| 306 | + ); |
| 307 | + |
| 308 | + let included_files: HashMap<String, Vec<PathBuf>> = transitive_internal_dependencies_inclusive |
| 309 | + .into_par_iter() |
| 310 | + .map(|manifest| -> Result<(_, _), Error> { |
| 311 | + // This relies on the assumption that tsconfig.json is always the name of the tsconfig file |
| 312 | + let tsconfig = &monorepo_root |
| 313 | + .join(manifest.path()) |
| 314 | + .parent() |
| 315 | + .unwrap() |
| 316 | + .join("tsconfig.json"); |
| 317 | + let included = match calculation_type { |
| 318 | + Calculation::Estimate => tsconfig_includes_estimate(&monorepo_root, tsconfig), |
| 319 | + Calculation::Exact => tsconfig_includes_exact(&monorepo_root, tsconfig), |
| 320 | + }?; |
| 321 | + Ok((manifest.contents.name.clone(), included)) |
| 322 | + }) |
| 323 | + .collect::<Result<HashMap<_, _>, _>>()?; |
| 324 | + |
| 325 | + debug!("tsconfig_includes: {:?}", included_files); |
| 326 | + Ok(included_files) |
| 327 | +} |
0 commit comments