11import { memoize } from "lodash" ;
22import path from "node:path" ;
33import { HashProvider } from "./hash-provider" ;
4- import { getSelectivityHashesPath , readHashFileContents , shallowSortObject } from "./utils" ;
4+ import { getSelectivityHashesPath } from "./utils" ;
55import { writeJsonWithCompression } from "./json-utils" ;
6- import type { NormalizedDependencies , SelectivityCompressionType } from "./types" ;
6+ import type { HashFileContents , NormalizedDependencies , SelectivityCompressionType } from "./types" ;
77
88export class HashWriter {
99 private readonly _hashProvider = new HashProvider ( ) ;
10- // "null" - successfully writed, " Promise<string>" - file/module hash, "Promise<Error>" - calculating hash error
11- private readonly _stagedFileHashes = new Map < string , null | Promise < string | Error > > ( ) ;
12- private readonly _stagedModuleHashes = new Map < string , null | Promise < string | Error > > ( ) ;
13- private readonly _stagedPatternHashes = new Map < string , null | Promise < string | Error > > ( ) ;
10+ // "Promise<string>" - file/module hash, "Promise<Error>" - calculating hash error
11+ private readonly _stagedFileHashes = new Map < string , Promise < string | Error > > ( ) ;
12+ private readonly _stagedModuleHashes = new Map < string , Promise < string | Error > > ( ) ;
13+ private readonly _stagedPatternHashes = new Map < string , Promise < string | Error > > ( ) ;
1414 private readonly _selectivityHashesPath : string ;
1515 private readonly _compresion : SelectivityCompressionType ;
1616
@@ -50,8 +50,8 @@ export class HashWriter {
5050 this . _stagedModuleHashes . set ( modulePath , value ) ;
5151 }
5252
53- addPatternDependencyHash ( dependencyPatterns : string ) : void {
54- return this . _addPatternDependency ( dependencyPatterns ) ;
53+ addPatternDependencyHash ( dependencyPattern : string ) : void {
54+ return this . _addPatternDependency ( dependencyPattern ) ;
5555 }
5656
5757 addTestDependencyHashes ( dependencies : NormalizedDependencies ) : void {
@@ -60,7 +60,7 @@ export class HashWriter {
6060 dependencies . modules . forEach ( dependency => this . _addModuleDependency ( dependency ) ) ;
6161 }
6262
63- async commit ( ) : Promise < void > {
63+ async save ( ) : Promise < void > {
6464 const hasStaged = Boolean (
6565 this . _stagedFileHashes . size || this . _stagedModuleHashes . size || this . _stagedPatternHashes . size ,
6666 ) ;
@@ -69,96 +69,34 @@ export class HashWriter {
6969 return ;
7070 }
7171
72- const stagedModuleNames = Array . from ( this . _stagedModuleHashes . keys ( ) ) ;
73- const stagedFileNames = Array . from ( this . _stagedFileHashes . keys ( ) ) ;
74- const stagedPatternNames = Array . from ( this . _stagedPatternHashes . keys ( ) ) ;
75-
76- const filterMatchingHashes = async (
77- keys : string [ ] ,
78- src : Map < string , null | Promise < string | Error > > ,
79- dest : Record < string , string > ,
80- ) : Promise < string [ ] > => {
81- const remainingKeys : string [ ] = [ ] ;
82-
83- for ( const key of keys ) {
84- const oldValue = dest [ key ] ;
85- const newValue = await src . get ( key ) ;
86-
87- if ( newValue === null ) {
88- continue ;
89- }
90-
91- if ( newValue === oldValue ) {
92- src . set ( key , null ) ;
93- } else {
94- remainingKeys . push ( key ) ;
95- }
96- }
97-
98- return remainingKeys ;
99- } ;
100-
10172 const writeTo = async (
102- keys : string [ ] ,
103- src : Map < string , null | Promise < string | Error > > ,
73+ src : Map < string , Promise < string | Error > > ,
10474 dest : Record < string , string > ,
10575 ) : Promise < void > => {
106- let needsReSort = false ;
76+ const keys = Array . from ( src . keys ( ) ) ;
10777
10878 for ( const key of keys ) {
10979 const hash = await src . get ( key ) ;
11080
111- if ( ! hash ) {
112- continue ;
113- }
114-
11581 if ( hash instanceof Error ) {
11682 throw hash ;
11783 }
11884
119- needsReSort = needsReSort || ! Object . hasOwn ( dest , key ) ;
120-
121- dest [ key ] = hash ;
122- }
123-
124- if ( needsReSort ) {
125- shallowSortObject ( dest ) ;
85+ dest [ key ] = hash as string ;
12686 }
12787 } ;
12888
129- const markAsCommited = ( keys : string [ ] , src : Map < string , null | Promise < string | Error > > ) : void => {
130- keys . forEach ( key => src . set ( key , null ) ) ;
89+ const fileContents : HashFileContents = {
90+ files : { } ,
91+ modules : { } ,
92+ patterns : { } ,
13193 } ;
13294
133- // Waiting for hashes to be calculated before locking file to reduce lock time
134- await Promise . all ( [
135- ...Object . values ( this . _stagedFileHashes ) ,
136- ...Object . values ( this . _stagedModuleHashes ) ,
137- ...Object . values ( this . _stagedPatternHashes ) ,
138- ] ) ;
139-
140- const existingHashesContent = await readHashFileContents ( this . _selectivityHashesPath , this . _compresion ) ;
141-
142- const [ updatedModules , updatedFiles , updatedPatterns ] = await Promise . all ( [
143- filterMatchingHashes ( stagedModuleNames , this . _stagedModuleHashes , existingHashesContent . modules ) ,
144- filterMatchingHashes ( stagedFileNames , this . _stagedFileHashes , existingHashesContent . files ) ,
145- filterMatchingHashes ( stagedPatternNames , this . _stagedPatternHashes , existingHashesContent . patterns ) ,
146- ] ) ;
147-
148- if ( ! updatedFiles . length && ! updatedModules . length && ! updatedPatterns . length ) {
149- return ;
150- }
151-
152- await Promise . all ( [
153- writeTo ( updatedModules , this . _stagedModuleHashes , existingHashesContent . modules ) ,
154- writeTo ( updatedFiles , this . _stagedFileHashes , existingHashesContent . files ) ,
155- writeTo ( updatedPatterns , this . _stagedPatternHashes , existingHashesContent . patterns ) ,
156- ] ) ;
157-
158- await writeJsonWithCompression ( this . _selectivityHashesPath , existingHashesContent , this . _compresion ) ;
95+ await writeTo ( this . _stagedFileHashes , fileContents . files ) ;
96+ await writeTo ( this . _stagedModuleHashes , fileContents . modules ) ;
97+ await writeTo ( this . _stagedPatternHashes , fileContents . patterns ) ;
15998
160- markAsCommited ( updatedModules , this . _stagedModuleHashes ) ;
161- markAsCommited ( updatedFiles , this . _stagedFileHashes ) ;
99+ await writeJsonWithCompression ( this . _selectivityHashesPath , fileContents , this . _compresion ) ;
162100 }
163101}
164102
0 commit comments