typescript_tools/
typescript_config.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::configuration_file::ConfigurationFile;
6use crate::io::{read_json_from_file, FromFileError};
7use crate::types::Directory;
8
9#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
10pub struct TypescriptProjectReference {
11    pub path: String,
12}
13
14#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
15pub struct TypescriptParentProjectReferenceFile {
16    /// This list is expected to be empty, but must be present to satisfy the
17    /// TypeScript compiler.
18    #[serde(default)]
19    pub files: Vec<String>,
20    #[serde(default)]
21    pub references: Vec<TypescriptProjectReference>,
22}
23
24#[derive(Debug)]
25pub struct TypescriptParentProjectReference {
26    directory: Directory,
27    pub contents: TypescriptParentProjectReferenceFile,
28}
29
30impl ConfigurationFile for TypescriptParentProjectReference {
31    type Contents = TypescriptParentProjectReferenceFile;
32
33    const FILENAME: &'static str = "tsconfig.json";
34
35    fn from_directory(
36        monorepo_root: &Directory,
37        directory: Directory,
38    ) -> Result<Self, FromFileError> {
39        let filename = monorepo_root.join(&directory).join(Self::FILENAME);
40        let manifest_contents: TypescriptParentProjectReferenceFile =
41            read_json_from_file(&filename)?;
42        Ok(TypescriptParentProjectReference {
43            directory,
44            contents: manifest_contents,
45        })
46    }
47
48    fn directory(&self) -> &Directory {
49        &self.directory
50    }
51
52    fn path(&self) -> PathBuf {
53        self.directory.join(Self::FILENAME)
54    }
55
56    fn contents(&self) -> &Self::Contents {
57        &self.contents
58    }
59}
60
61#[derive(Debug)]
62pub struct TypescriptConfig {
63    directory: Directory,
64    pub contents: serde_json::Map<String, serde_json::Value>,
65}
66
67impl ConfigurationFile for TypescriptConfig {
68    type Contents = serde_json::Map<String, serde_json::Value>;
69
70    const FILENAME: &'static str = "tsconfig.json";
71
72    fn from_directory(
73        monorepo_root: &Directory,
74        directory: Directory,
75    ) -> Result<TypescriptConfig, FromFileError> {
76        let filename = monorepo_root.join(&directory).join(Self::FILENAME);
77        Ok(TypescriptConfig {
78            directory,
79            contents: read_json_from_file(&filename)?,
80        })
81    }
82
83    fn directory(&self) -> &Directory {
84        &self.directory
85    }
86
87    fn path(&self) -> PathBuf {
88        self.directory.join(Self::FILENAME)
89    }
90
91    fn contents(&self) -> &Self::Contents {
92        &self.contents
93    }
94}