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
//! Enumerate source code files used by the TypeScript compiler during
//! compilation. The return value is a list of relative paths from the monorepo
//! root, sorted in alphabetical order.
//!
//! There are two methods of calculating this list of files: the exact
//! way, and using an estimation.
//!
//! The **exact** method uses the TypeScript compiler's [listFilesOnly] flag as the
//! source of truth. We do not try to reimplement this algorithm independently
//! because this list requires following `import` statements in JavaScript and
//! TypeScript code. From the [tsconfig exclude] documentation:
//!
//! > Important: `exclude` *only* changes which files are included as a result
//! > of the `include` setting. A file specified by exclude can still become
//! > part of your codebase due to an import statement in your code, a types
//! > inclusion, a `/// <reference` directive, or being specified in the
//! > `files` list.
//!
//! The TypeScript compiler is a project where the implementation is the spec,
//! so this method of enumeration trades the runtime penalty of invoking the
//! TypeScript compiler for accuracy of output as defined by the "spec".
//!
//! The **estimation** method uses the list of globs from the `include`
//! property in a package's tsconfig.json file to calculate the list of source
//! files.
//!
//! This estimation is currently imprecise (and likely to stay that way) --
//! it makes a best attempt to follow the `exclude` or file-type based rules:
//!
//! > 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).
//!
//! without any guarantee of exhaustive compatibility.
//!
//! Additionally, this method performs no source-code analysis to follow
//! imported files.
//!
//! You might want to use the estimation method if speed is a concern, because it
//! is several orders of magnitude faster than the exact method.
//!
//! [listfilesonly]: https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options
//! [tsconfig exclude]: https://www.typescriptlang.org/tsconfig#exclude

#![forbid(unsafe_code)]
#![deny(warnings, missing_docs)]

use std::{
    collections::{HashMap, HashSet},
    fs::File,
    io::Read,
    path::{Path, PathBuf},
    process::Command,
};

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

mod error;

use crate::error::Error;

/// Method to use to enumerate inputs to the TypeScript compiler.
#[derive(Copy, Clone, Debug)]
pub enum Calculation {
    /// Estimate the true list of inputs to the TypeScript compiler by listing
    /// the files matching the tsconfig's `include` globs.
    Estimate,
    /// Calculate the exact list of inputs to the TypeScript compiler by
    /// invoking `tsc --listFilesOnly`.
    Exact,
}

#[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>,
}

// This gives us a way to look up the typescript-tools PackageManifest
// from the path to a package's tsconfig.json file, but it does incur
// a runtime penalty of reading this information from disk again.
//
// It's a definite hack, but it unblocks today.
#[derive(Debug, Deserialize)]
struct PackageManifest {
    name: String,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct TypescriptPackage {
    scoped_package_name: String,
    tsconfig_file: PathBuf,
}

fn is_glob(string: &str) -> bool {
    string.contains('*')
}

fn glob_file_extension(glob: &str) -> Option<String> {
    if glob.ends_with('*') {
        return None;
    }
    Some(
        glob.rsplit('*')
            .next()
            .expect("Expected glob to contain an asterisk")
            .to_owned(),
    )
}

fn is_monorepo_file(monorepo_root: &Path, file: &Path) -> bool {
    for ancestor in file.ancestors() {
        if ancestor.ends_with(monorepo_root) {
            return true;
        }
    }
    false
}

fn read_json_from_file<T>(filename: &Path) -> Result<T, Error>
where
    for<'de> T: Deserialize<'de>,
{
    // Reading a file into a string before invoking Serde is faster than
    // invoking Serde from a BufReader, see
    // https://github.com/serde-rs/json/issues/160
    let mut string = String::new();
    File::open(filename)?.read_to_string(&mut string)?;
    Ok(
        serde_json::from_str(&string).map_err(|err| Error::TypescriptConfigParseError {
            source: err,
            filename: filename.to_owned(),
        })?,
    )
}

fn remove_relative_path_prefix_from_absolute_path(
    prefix: &Path,
    absolute_path: &Path,
) -> Result<PathBuf, Error> {
    for ancestor in absolute_path.ancestors() {
        if ancestor.ends_with(prefix) {
            return Ok(absolute_path
                .strip_prefix(ancestor)
                .map(|path| path.to_owned())
                .map_err(|err| Error::RelativePathStripError {
                    source: err,
                    absolute_path: absolute_path.to_path_buf(),
                })?);
        }
    }

    eprintln!(
        "Absolute path {:?} did not contain relative-path prefix {:?}",
        absolute_path, prefix,
    );
    panic!();
}

/// Invoke the TypeScript compiler with the [listFilesOnly] flag to enumerate
/// the files included in the compilation process.
fn tsconfig_includes_exact(monorepo_root: &Path, tsconfig: &Path) -> Result<Vec<PathBuf>, Error> {
    let child = Command::new("tsc")
        .arg("--listFilesOnly")
        .arg("--project")
        .arg(tsconfig)
        .output()?;
    if child.status.code() != Some(0) {
        return Err(Error::TypescriptCompilerError {
            command: format!("tsc --listFilesOnly --project {:?}", tsconfig),
            error: String::from_utf8(child.stderr)?,
        });
    }
    let stdout = String::from_utf8(child.stdout)?;

    let included_files: Vec<PathBuf> = stdout
        .lines()
        // Drop the empty newline at the end of stdout
        .filter(|s| !s.is_empty())
        .map(|s| PathBuf::from(s))
        .filter(|path| is_monorepo_file(monorepo_root, path))
        .map(|source_file| {
            remove_relative_path_prefix_from_absolute_path(monorepo_root, &source_file)
        })
        .collect::<Result<_, _>>()?;

    Ok(included_files)
}

/// Use the `tsconfig_file`'s `include` configuration to enumerate the list of files
/// matching include globs.
fn tsconfig_includes_estimate(
    monorepo_root: &Path,
    tsconfig_file: &Path,
) -> Result<Vec<PathBuf>, Error> {
    let package_directory = tsconfig_file
        .parent()
        .expect("No package should exist in the monorepo root");
    let tsconfig: TypescriptConfig = read_json_from_file(tsconfig_file)?;

    // 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.

    let whitelisted_file_extensions: HashSet<String> = {
        let mut whitelist = vec![".ts", ".tsx", ".d.ts"];
        if tsconfig.compiler_options.allow_js {
            whitelist.append(&mut vec![".js", ".jsx"]);
        }
        let mut whitelist: Vec<String> = whitelist.into_iter().map(|s| s.to_owned()).collect();

        // add extensions from any glob that specifies one
        let mut glob_extensions: Vec<String> = tsconfig
            .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.
                tsconfig.compiler_options.resolve_json_module
            })
            .collect()
    };

    let is_whitelisted_file_extension = |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 included_files: Vec<PathBuf> =
        GlobWalkerBuilder::from_patterns(package_directory, &tsconfig.include)
            .file_type(FileType::FILE)
            .min_depth(0)
            .build()
            .expect("Should be able to create glob walker")
            .into_iter()
            .filter(|maybe_dir_entry| {
                if let Ok(dir_entry) = maybe_dir_entry {
                    is_monorepo_file(monorepo_root, dir_entry.path())
                        && is_whitelisted_file_extension(dir_entry.path())
                } else {
                    true
                }
            })
            .filter_map(|maybe_dir_entry| match maybe_dir_entry {
                Ok(dir_entry) => Some(Ok(dir_entry
                    .path()
                    .strip_prefix(monorepo_root)
                    .map(|path| path.to_owned())
                    .expect(&format!(
                        "Should be able to strip monorepo-root prefix from path in monorepo: {:?}",
                        dir_entry.path()
                    )))),
                Err(err) => Some(Err(err.into())),
            })
            .collect::<Result<Vec<PathBuf>, Error>>()?;

    Ok(included_files)
}

/// 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, Q, C>(
    monorepo_root: P,
    tsconfig_files: &[Q],
    calculation_type: C,
) -> Result<HashMap<String, Vec<PathBuf>>, Error>
where
    P: AsRef<Path> + Sync,
    Q: AsRef<Path>,
    C: Into<Calculation> + Copy + Sync + Send,
{
    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
        .iter()
        .map(|tsconfig_file| -> Result<Vec<TypescriptPackage>, Error> {
            let package_manifest_file = tsconfig_file
                .as_ref()
                .parent()
                .expect("No package should exist in the monorepo root")
                .join("package.json");
            let PackageManifest {
                name: package_manifest_name,
            } = read_json_from_file(&monorepo_root.as_ref().join(package_manifest_file))?;
            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.as_ref()
                ));

            let transitive_internal_dependencies_inclusive = {
                // Enumerate internal dependencies (exclusive)
                let mut packages = package_manifest
                    .transitive_internal_dependency_package_names_exclusive(
                        &package_manifests_by_package_name,
                    );
                // Make this list inclusive of the target package
                packages.push(&package_manifest);
                packages
            };

            Ok(transitive_internal_dependencies_inclusive
                .iter()
                .map(|package_manifest| TypescriptPackage {
                    scoped_package_name: package_manifest.contents.name.clone(),
                    tsconfig_file: package_manifest
                        .path()
                        .parent()
                        .expect("No package should exist in the monorepo root")
                        .join("tsconfig.json"),
                })
                .collect())
        })
        .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(|package| -> Result<(_, _), Error> {
                // This relies on the assumption that tsconfig.json is always the name of the tsconfig file
                let tsconfig = &monorepo_root.as_ref().join(package.tsconfig_file);
                let mut included_files = match calculation_type.into() {
                    Calculation::Estimate => {
                        tsconfig_includes_estimate(monorepo_root.as_ref(), tsconfig)
                    }
                    Calculation::Exact => tsconfig_includes_exact(monorepo_root.as_ref(), tsconfig),
                }?;
                included_files.sort_unstable();
                Ok((package.scoped_package_name, included_files))
            })
            .collect::<Result<HashMap<_, _>, _>>()?;

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