1
1
use std:: collections:: HashMap ;
2
2
use std:: path:: PathBuf ;
3
3
4
- use anyhow:: { anyhow , bail, Result } ;
4
+ use anyhow:: { bail, Result } ;
5
5
6
6
use pathdiff:: diff_paths;
7
7
8
8
use crate :: configuration_file:: ConfigurationFile ;
9
- use crate :: io:: {
10
- write_project_references, TypescriptParentProjectReference , TypescriptProjectReference ,
11
- } ;
12
9
use crate :: monorepo_manifest:: MonorepoManifest ;
13
10
use crate :: opts;
14
11
use crate :: package_manifest:: PackageManifest ;
15
- use crate :: typescript_config:: TypescriptConfig ;
12
+ use crate :: typescript_config:: {
13
+ TypescriptConfig , TypescriptParentProjectReference , TypescriptProjectReference ,
14
+ } ;
16
15
17
16
fn key_children_by_parent (
18
17
mut accumulator : HashMap < PathBuf , Vec < String > > ,
@@ -35,17 +34,14 @@ fn key_children_by_parent(
35
34
accumulator
36
35
}
37
36
38
- fn create_project_references ( mut children : Vec < String > ) -> TypescriptParentProjectReference {
37
+ fn create_project_references ( mut children : Vec < String > ) -> Vec < TypescriptProjectReference > {
39
38
// Sort the TypeScript project references for deterministic file contents.
40
39
// This minimizes diffs since the tsconfig.json files are stored in version control.
41
40
children. sort_unstable ( ) ;
42
- TypescriptParentProjectReference {
43
- files : Vec :: new ( ) ,
44
- references : children
45
- . into_iter ( )
46
- . map ( |path| TypescriptProjectReference { path } )
47
- . collect ( ) ,
48
- }
41
+ children
42
+ . into_iter ( )
43
+ . map ( |path| TypescriptProjectReference { path } )
44
+ . collect ( )
49
45
}
50
46
51
47
fn vecs_match < T : PartialEq > ( a : & [ T ] , b : & [ T ] ) -> bool {
@@ -54,7 +50,7 @@ fn vecs_match<T: PartialEq>(a: &[T], b: &[T]) -> bool {
54
50
}
55
51
56
52
// Create a tsconfig.json file in each parent directory to an internal package.
57
- // This permits us to build the monorepo from the top down.
53
+ // This permits us to compile the monorepo from the top down.
58
54
fn link_children_packages ( opts : & opts:: Link , lerna_manifest : & MonorepoManifest ) -> Result < bool > {
59
55
let mut is_exit_success = true ;
60
56
@@ -66,19 +62,10 @@ fn link_children_packages(opts: &opts::Link, lerna_manifest: &MonorepoManifest)
66
62
. try_for_each ( |( directory, children) | -> Result < ( ) > {
67
63
let desired_project_references = create_project_references ( children) ;
68
64
let tsconfig_filename = opts. root . join ( & directory) . join ( "tsconfig.json" ) ;
69
- let tsconfig = TypescriptConfig :: from_directory ( & opts. root , & directory) ?;
70
- let current_project_references = tsconfig
71
- . contents
72
- . get ( "references" )
73
- . map ( |value| {
74
- serde_json:: from_value :: < Vec < TypescriptProjectReference > > ( value. clone ( ) )
75
- . expect ( "Value starting as JSON should be serializable as JSON" )
76
- } )
77
- . unwrap_or_default ( ) ;
78
- let needs_update = !vecs_match (
79
- & current_project_references,
80
- & desired_project_references. references ,
81
- ) ;
65
+ let mut tsconfig =
66
+ TypescriptParentProjectReference :: from_directory ( & opts. root , & directory) ?;
67
+ let current_project_references = tsconfig. contents . references ;
68
+ let needs_update = !current_project_references. eq ( & desired_project_references) ;
82
69
if !needs_update {
83
70
return Ok ( ( ) ) ;
84
71
}
@@ -92,7 +79,8 @@ fn link_children_packages(opts: &opts::Link, lerna_manifest: &MonorepoManifest)
92
79
println ! ( "{}" , serialized) ;
93
80
Ok ( ( ) )
94
81
} else {
95
- write_project_references ( tsconfig_filename, & desired_project_references)
82
+ tsconfig. contents . references = desired_project_references;
83
+ tsconfig. write ( )
96
84
}
97
85
} ) ?;
98
86
@@ -150,14 +138,10 @@ fn link_package_dependencies(opts: &opts::Link, lerna_manifest: &MonorepoManifes
150
138
}
151
139
152
140
// Update the current tsconfig with the desired references
153
- tsconfig
154
- . contents
155
- . as_object_mut ( )
156
- . ok_or_else ( || anyhow ! ( "Expected tsconfig.json to contain an Object" ) ) ?
157
- . insert (
158
- String :: from ( "references" ) ,
159
- serde_json:: to_value ( desired_project_references) ?,
160
- ) ;
141
+ tsconfig. contents . insert (
142
+ String :: from ( "references" ) ,
143
+ serde_json:: to_value ( desired_project_references) ?,
144
+ ) ;
161
145
162
146
Ok ( Some ( tsconfig) )
163
147
} ,
0 commit comments