@@ -19,7 +19,7 @@ namespace ts.server.typingsInstaller {
1919 writeLine : noop
2020 } ;
2121
22- const timestampsFile = "timestamps.json" ;
22+ const timestampsFileName = "timestamps.json" ;
2323
2424 function typingToFileName ( cachePath : string , packageName : string , installTypingHost : InstallTypingHost , log : Log ) : string {
2525 try {
@@ -44,60 +44,37 @@ namespace ts.server.typingsInstaller {
4444 }
4545
4646 interface TypeDeclarationTimestampFile {
47+ // entries maps from package names (e.g. "@types/node") to timestamp values (as produced by Date#getTime)
4748 entries : MapLike < number > ;
4849 }
4950
5051 function loadTypeDeclarationTimestampFile ( typeDeclarationTimestampFilePath : string , host : InstallTypingHost , log : Log ) : MapLike < number > {
51- const fileExists = host . fileExists ( typeDeclarationTimestampFilePath ) ;
52- if ( ! fileExists ) {
53- if ( log . isEnabled ( ) ) {
54- log . writeLine ( `Type declaration timestamp file '${ typeDeclarationTimestampFilePath } ' does not exist` ) ;
55- }
56- }
5752 try {
58- if ( fileExists ) {
59- const content = < TypeDeclarationTimestampFile > JSON . parse ( host . readFile ( typeDeclarationTimestampFilePath ) ) ;
60- return content . entries || { } ;
61- }
62- else {
63- host . writeFile ( typeDeclarationTimestampFilePath , "{}" ) ;
64- if ( log . isEnabled ( ) ) {
65- log . writeLine ( "Type declaration timestamp file was created." ) ;
66- }
67- return { } ;
53+ if ( log . isEnabled ( ) ) {
54+ log . writeLine ( "Loading type declaration timestamp file." ) ;
6855 }
56+ const content = < TypeDeclarationTimestampFile > JSON . parse ( host . readFile ( typeDeclarationTimestampFilePath ) ) ;
57+ return content . entries || { } ;
6958 }
7059 catch ( e ) {
7160 if ( log . isEnabled ( ) ) {
7261 log . writeLine ( `Error when loading type declaration timestamp file '${ typeDeclarationTimestampFilePath } ': ${ ( < Error > e ) . message } , ${ ( < Error > e ) . stack } ` ) ;
7362 }
63+ // If file cannot be read, we update all requested type declarations.
7464 return { } ;
7565 }
7666 }
7767
7868 function writeTypeDeclarationTimestampFile ( typeDeclarationTimestampFilePath : string , newContents : TypeDeclarationTimestampFile , host : InstallTypingHost , log : Log ) : void {
79- const fileExists = host . fileExists ( typeDeclarationTimestampFilePath ) ;
80- if ( ! fileExists ) {
81- if ( log . isEnabled ( ) ) {
82- log . writeLine ( `Type declaration timestamp file '${ typeDeclarationTimestampFilePath } ' does not exist` ) ;
83- }
84- }
8569 try {
86- if ( fileExists ) {
87- host . writeFile ( typeDeclarationTimestampFilePath , JSON . stringify ( newContents ) ) ;
88- return ;
89- }
90- else {
91- host . writeFile ( typeDeclarationTimestampFilePath , JSON . stringify ( newContents ) ) ;
92- if ( log . isEnabled ( ) ) {
93- log . writeLine ( "Type declaration time stamp file was created." ) ;
94- }
95- return ;
70+ if ( log . isEnabled ( ) ) {
71+ log . writeLine ( "Writing type declaration timestamp file." ) ;
9672 }
73+ host . writeFile ( typeDeclarationTimestampFilePath , JSON . stringify ( newContents ) ) ;
9774 }
9875 catch ( e ) {
9976 if ( log . isEnabled ( ) ) {
100- log . writeLine ( `Error when writing new type declaration timestamp file '${ typeDeclarationTimestampFilePath } ': ${ ( < Error > e ) . message } , ${ ( < Error > e ) . stack } ` ) ;
77+ log . writeLine ( `Error when writing type declaration timestamp file '${ typeDeclarationTimestampFilePath } ': ${ ( < Error > e ) . message } , ${ ( < Error > e ) . stack } ` ) ;
10178 }
10279 return ;
10380 }
@@ -162,11 +139,12 @@ namespace ts.server.typingsInstaller {
162139 }
163140
164141 // load existing typing information from the cache
142+ const timestampsFilePath = combinePaths ( req . cachePath || this . globalCachePath , timestampsFileName ) ;
165143 if ( req . cachePath ) {
166144 if ( this . log . isEnabled ( ) ) {
167145 this . log . writeLine ( `Request specifies cache path '${ req . cachePath } ', loading cached information...` ) ;
168146 }
169- this . processCacheLocation ( req . cachePath ) ;
147+ this . processCacheLocation ( req . cachePath , timestampsFilePath ) ;
170148 }
171149
172150 if ( this . safeList === undefined ) {
@@ -191,7 +169,7 @@ namespace ts.server.typingsInstaller {
191169
192170 // install typings
193171 if ( discoverTypingsResult . newTypingNames . length ) {
194- this . installTypings ( req , req . cachePath || this . globalCachePath , discoverTypingsResult . cachedTypingPaths , discoverTypingsResult . newTypingNames ) ;
172+ this . installTypings ( req , req . cachePath || this . globalCachePath , discoverTypingsResult . cachedTypingPaths , discoverTypingsResult . newTypingNames , timestampsFilePath ) ;
195173 }
196174 else {
197175 this . sendResponse ( this . createSetTypings ( req , discoverTypingsResult . cachedTypingPaths ) ) ;
@@ -215,7 +193,7 @@ namespace ts.server.typingsInstaller {
215193 this . safeList = JsTyping . loadSafeList ( this . installTypingHost , this . safeListPath ) ;
216194 }
217195
218- private processCacheLocation ( cacheLocation : string ) {
196+ private processCacheLocation ( cacheLocation : string , timestampsFilePath ?: string ) {
219197 if ( this . log . isEnabled ( ) ) {
220198 this . log . writeLine ( `Processing cache location '${ cacheLocation } '` ) ;
221199 }
@@ -225,8 +203,7 @@ namespace ts.server.typingsInstaller {
225203 }
226204 return ;
227205 }
228- const timestampJson = combinePaths ( cacheLocation , timestampsFile ) ;
229- this . typeDeclarationTimestamps = loadTypeDeclarationTimestampFile ( timestampJson , this . installTypingHost , this . log ) ;
206+ this . typeDeclarationTimestamps = loadTypeDeclarationTimestampFile ( timestampsFilePath || combinePaths ( cacheLocation , timestampsFileName ) , this . installTypingHost , this . log ) ;
230207 const packageJson = combinePaths ( cacheLocation , "package.json" ) ;
231208 if ( this . log . isEnabled ( ) ) {
232209 this . log . writeLine ( `Trying to find '${ packageJson } '...` ) ;
@@ -321,7 +298,7 @@ namespace ts.server.typingsInstaller {
321298 }
322299 }
323300
324- private installTypings ( req : DiscoverTypings , cachePath : string , currentlyCachedTypings : string [ ] , typingsToInstall : string [ ] ) {
301+ private installTypings ( req : DiscoverTypings , cachePath : string , currentlyCachedTypings : string [ ] , typingsToInstall : string [ ] , timestampsFilePath : string ) {
325302 if ( this . log . isEnabled ( ) ) {
326303 this . log . writeLine ( `Installing typings ${ JSON . stringify ( typingsToInstall ) } ` ) ;
327304 }
@@ -384,8 +361,7 @@ namespace ts.server.typingsInstaller {
384361 }
385362
386363 const newFileContents : TypeDeclarationTimestampFile = { entries : this . typeDeclarationTimestamps } ;
387- const timestampJson = combinePaths ( cachePath , timestampsFile ) ;
388- writeTypeDeclarationTimestampFile ( timestampJson , newFileContents , this . installTypingHost , this . log ) ;
364+ writeTypeDeclarationTimestampFile ( timestampsFilePath , newFileContents , this . installTypingHost , this . log ) ;
389365
390366 this . sendResponse ( this . createSetTypings ( req , currentlyCachedTypings . concat ( installedTypingFiles ) ) ) ;
391367 }
0 commit comments