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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::{
    collections::{HashMap, HashSet},
    fmt::Display,
    iter,
    path::{Path, PathBuf},
};

use globwalk::{FileType, GlobWalkerBuilder};
use log::{debug, trace};
use rayon::prelude::*;
use serde::Deserialize;
use typescript_tools::{configuration_file::ConfigurationFile, monorepo_manifest};

use crate::{
    io::read_json_from_file,
    path::{self, *},
    typescript_package::{
        FromTypescriptConfigFileError, PackageInMonorepoRootError, PackageManifest,
        PackageManifestFile, TypescriptConfigFile, TypescriptPackage,
    },
};

#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CompilerOptions {
    #[serde(default)]
    allow_js: bool,
    #[serde(default)]
    resolve_json_module: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct TypescriptConfig {
    #[serde(default)]
    compiler_options: CompilerOptions,
    // DISCUSS: how should we behave if `include` is not present?
    include: Vec<String>,
}

impl TypescriptConfig {
    /// LIMITATION: The TypeScript compiler docs state:
    ///
    /// > If a glob pattern doesn’t include a file extension, then only files
    /// > with supported extensions are included (e.g. .ts, .tsx, and .d.ts by
    /// > default, with .js and .jsx if allowJs is set to true).
    ///
    /// This implementation does not examine if globs contain extensions.
    fn whitelisted_file_extensions(&self) -> HashSet<String> {
        let mut whitelist: Vec<String> = vec![
            String::from(".ts"),
            String::from(".tsx"),
            String::from(".d.ts"),
        ];
        if self.compiler_options.allow_js {
            whitelist.append(&mut vec![String::from(".js"), String::from(".jsx")]);
        }

        // add extensions from any glob that specifies one
        let mut glob_extensions: Vec<String> = self
            .include
            .iter()
            .filter(|pattern| is_glob(pattern))
            .filter_map(|glob| glob_file_extension(glob))
            .collect();

        // FIXME: glob extensions apply to a specific glob, not every glob
        whitelist.append(&mut glob_extensions);
        whitelist
            .into_iter()
            .filter(|extension| {
                if !extension.ends_with(".json") {
                    return true;
                }
                // For JSON modules, the presence of a "src/**/*.json" include glob
                // is not enough, JSON imports are still gated by this compiler option.
                self.compiler_options.resolve_json_module
            })
            .collect()
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub struct BuildWalkerError {
    kind: BuildWalkerErrorKind,
}

impl Display for BuildWalkerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            BuildWalkerErrorKind::PackageInMonorepoRoot(path) => {
                write!(f, "unexpected package in monorepo root: {:?}", path)
            }
            BuildWalkerErrorKind::IO(_) => write!(f, "unable to estimate tsconfig includes"),
        }
    }
}

impl std::error::Error for BuildWalkerError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            BuildWalkerErrorKind::IO(err) => Some(err),
            BuildWalkerErrorKind::PackageInMonorepoRoot(_) => None,
        }
    }
}

#[derive(Debug)]
pub enum BuildWalkerErrorKind {
    #[non_exhaustive]
    IO(crate::io::FromFileError),
    #[non_exhaustive]
    PackageInMonorepoRoot(PathBuf),
}

impl From<crate::io::FromFileError> for BuildWalkerErrorKind {
    fn from(err: crate::io::FromFileError) -> Self {
        Self::IO(err)
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub struct WalkError {
    kind: WalkErrorKind,
}

impl Display for WalkError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            WalkErrorKind::WalkError(_) => write!(f, "unable to walk directory tree"),
            // DISCUSS: is this something we can fully test for at compile time?
            // If so, we can use `expect` instead of exposing this possibility to the user.
            WalkErrorKind::Path(_) => write!(f, "unable to strip path prefix"),
        }
    }
}

impl std::error::Error for WalkError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            WalkErrorKind::Path(err) => Some(err),
            WalkErrorKind::WalkError(err) => Some(err),
        }
    }
}

impl From<globwalk::WalkError> for WalkError {
    fn from(err: globwalk::WalkError) -> Self {
        Self {
            kind: WalkErrorKind::WalkError(err),
        }
    }
}

#[derive(Debug)]
pub enum WalkErrorKind {
    #[non_exhaustive]
    Path(path::StripPrefixError),
    #[non_exhaustive]
    WalkError(globwalk::WalkError),
}

/// Use the `tsconfig_file`'s `include` configuration to enumerate the list of files
/// matching include globs.
fn tsconfig_includes_estimate<'a, 'b>(
    monorepo_root: &'a Path,
    tsconfig_file: &'b TypescriptConfigFile,
) -> Result<impl Iterator<Item = Result<PathBuf, WalkError>>, BuildWalkerError> {
    let monorepo_root = monorepo_root.to_owned();
    let package_directory = tsconfig_file
        .package_directory(&monorepo_root)
        .map_err(|kind| BuildWalkerError {
            kind: BuildWalkerErrorKind::PackageInMonorepoRoot(kind.0),
        })?;
    let tsconfig: TypescriptConfig =
        read_json_from_file(monorepo_root.join(tsconfig_file.as_path())).map_err(|err| {
            BuildWalkerError {
                kind: BuildWalkerErrorKind::IO(err),
            }
        })?;

    let whitelisted_file_extensions = tsconfig.whitelisted_file_extensions();

    let is_whitelisted_file_extension = move |path: &Path| -> bool {
        // Can't use path::extension here because some globs specify more than
        // just a single extension (like .d.ts).
        whitelisted_file_extensions.iter().any(|extension| {
            path.to_str()
                .expect("Path should contain only valid UTF-8")
                .ends_with(extension)
        })
    };

    let monorepo_root_two = monorepo_root.clone();
    let included_files = GlobWalkerBuilder::from_patterns(package_directory, &tsconfig.include)
        .file_type(FileType::FILE)
        .min_depth(0)
        .build()
        .expect("should be able to create glob walker")
        .filter(move |maybe_dir_entry| match maybe_dir_entry {
            Ok(dir_entry) => {
                is_monorepo_file(&monorepo_root_two, dir_entry.path())
                    && is_whitelisted_file_extension(dir_entry.path())
            }
            Err(_) => true,
        })
        .map(move |maybe_dir_entry| -> Result<PathBuf, WalkError> {
            let dir_entry = maybe_dir_entry?;
            let path = dir_entry
                .path()
                .strip_prefix(&monorepo_root)
                .map(ToOwned::to_owned)
                .expect(&format!(
                    "Should be able to strip monorepo-root prefix from path in monorepo: {:?}",
                    dir_entry.path()
                ));
            Ok(path)
        });

    Ok(included_files)
}

#[derive(Debug)]
#[non_exhaustive]
pub struct Error {
    kind: ErrorKind,
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            ErrorKind::PackageInMonorepoRoot(path) => {
                write!(f, "unexpected package in monorepo root: {:?}", path)
            }
            _ => write!(f, "unable to estimate tsconfig includes"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            ErrorKind::MonorepoManifest(err) => Some(err),
            ErrorKind::EnumeratePackageManifestsError(err) => Some(err),
            ErrorKind::PackageInMonorepoRoot(_) => None,
            ErrorKind::FromFile(err) => Some(err),
            ErrorKind::BuildWalker(err) => Some(err),
            ErrorKind::Walk(err) => Some(err),
        }
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Self {
        Self { kind }
    }
}

impl From<typescript_tools::io::FromFileError> for Error {
    fn from(err: typescript_tools::io::FromFileError) -> Self {
        Self {
            kind: ErrorKind::MonorepoManifest(err),
        }
    }
}

impl From<typescript_tools::monorepo_manifest::EnumeratePackageManifestsError> for Error {
    fn from(err: typescript_tools::monorepo_manifest::EnumeratePackageManifestsError) -> Self {
        Self {
            kind: ErrorKind::EnumeratePackageManifestsError(err),
        }
    }
}

impl From<crate::io::FromFileError> for Error {
    fn from(err: crate::io::FromFileError) -> Self {
        Self {
            kind: ErrorKind::FromFile(err),
        }
    }
}

impl From<BuildWalkerError> for Error {
    fn from(err: BuildWalkerError) -> Self {
        match err.kind {
            // avoid nesting this error to present a cleaner backtrace
            BuildWalkerErrorKind::PackageInMonorepoRoot(path) => Self {
                kind: ErrorKind::PackageInMonorepoRoot(path),
            },
            _ => Self {
                kind: ErrorKind::BuildWalker(err),
            },
        }
    }
}

impl From<WalkError> for Error {
    fn from(err: WalkError) -> Self {
        Self {
            kind: ErrorKind::Walk(err),
        }
    }
}

impl From<FromTypescriptConfigFileError> for Error {
    fn from(err: FromTypescriptConfigFileError) -> Self {
        let kind = match err {
            FromTypescriptConfigFileError::PackageInMonorepoRoot(path) => {
                ErrorKind::PackageInMonorepoRoot(path)
            }
            FromTypescriptConfigFileError::FromFile(err) => ErrorKind::FromFile(err),
        };
        Self { kind }
    }
}

impl From<PackageInMonorepoRootError> for Error {
    fn from(err: PackageInMonorepoRootError) -> Self {
        Self {
            kind: ErrorKind::PackageInMonorepoRoot(err.0),
        }
    }
}

#[derive(Debug)]
pub enum ErrorKind {
    #[non_exhaustive]
    MonorepoManifest(typescript_tools::io::FromFileError),
    #[non_exhaustive]
    EnumeratePackageManifestsError(
        typescript_tools::monorepo_manifest::EnumeratePackageManifestsError,
    ),
    #[non_exhaustive]
    PackageInMonorepoRoot(PathBuf),
    #[non_exhaustive]
    FromFile(crate::io::FromFileError),
    #[non_exhaustive]
    BuildWalker(BuildWalkerError),
    #[non_exhaustive]
    Walk(WalkError),
}

/// Enumerate source code files used by the TypeScript compiler during
/// compilation. The return value is a list of alphabetically-sorted relative
/// paths from the monorepo root, grouped by scoped package name.
///
/// - `monorepo_root` may be an absolute path
/// - `tsconfig_files` should be relative paths from the monorepo root
pub fn tsconfig_includes_by_package_name<P, T>(
    monorepo_root: P,
    tsconfig_files: T,
) -> Result<HashMap<String, Vec<PathBuf>>, Error>
where
    P: AsRef<Path> + Sync,
    T: IntoIterator,
    T::Item: AsRef<Path>,
{
    // REFACTOR: avoid duplicated code in estimate.rs and exact.rs
    let lerna_manifest =
        monorepo_manifest::MonorepoManifest::from_directory(monorepo_root.as_ref())?;
    let package_manifests_by_package_name = lerna_manifest.package_manifests_by_package_name()?;
    trace!("{:?}", lerna_manifest);

    // As relative path from monorepo root
    let transitive_internal_dependency_tsconfigs_inclusive_to_enumerate: HashSet<
        TypescriptPackage,
    > = tsconfig_files
        .into_iter()
        .map(|tsconfig_file| -> Result<Vec<TypescriptPackage>, Error> {
            let tsconfig_file: TypescriptConfigFile =
                monorepo_root.as_ref().join(tsconfig_file.as_ref()).into();
            let package_manifest: PackageManifest = (&tsconfig_file).try_into()?;
            let package_manifest = package_manifests_by_package_name
                .get(&package_manifest.name)
                .expect(&format!(
                    "tsconfig {:?} should belong to a package in the lerna monorepo",
                    tsconfig_file
                ));

            // RESUME: replace this comment with something sensible
            // DISCUSS: what's the deal with transitive deps if enumerate is point and shoot?
            let transitive_internal_dependencies_inclusive = {
                // Enumerate internal dependencies (exclusive)
                package_manifest
                    .transitive_internal_dependency_package_names_exclusive(
                        &package_manifests_by_package_name,
                    )
                    // Make this list inclusive of the target package
                    .chain(iter::once(package_manifest))
            };

            Ok(transitive_internal_dependencies_inclusive
                .map(
                    |package_manifest| -> Result<_, PackageInMonorepoRootError> {
                        let package_manifest_file =
                            PackageManifestFile::from(package_manifest.path());
                        let tsconfig_file: TypescriptConfigFile =
                            package_manifest_file.try_into()?;
                        let typescript_package = TypescriptPackage {
                            scoped_package_name: package_manifest.contents.name.clone(),
                            tsconfig_file,
                        };
                        Ok(typescript_package)
                    },
                )
                .collect::<Result<_, _>>()?)
        })
        // REFACTOR: avoid intermediate allocations
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .flatten()
        .collect();

    debug!(
        "transitive_internal_dependency_tsconfigs_inclusive_to_enumerate: {:?}",
        transitive_internal_dependency_tsconfigs_inclusive_to_enumerate
    );

    let included_files: HashMap<String, Vec<PathBuf>> =
        transitive_internal_dependency_tsconfigs_inclusive_to_enumerate
            .into_par_iter()
            .map(|typescript_package| -> Result<(_, _), Error> {
                // This relies on the assumption that tsconfig.json is always the name of the tsconfig file
                let tsconfig_file = &typescript_package.tsconfig_file;
                let mut included_files: Vec<_> =
                    tsconfig_includes_estimate(monorepo_root.as_ref(), tsconfig_file)?
                        .collect::<Result<_, _>>()?;
                included_files.sort_unstable();
                Ok((typescript_package.scoped_package_name, included_files))
            })
            .collect::<Result<HashMap<_, _>, _>>()?;

    debug!("tsconfig_includes: {:?}", included_files);
    Ok(included_files)
}