Skip to content

Commit 3f23d5d

Browse files
Respond to CR
1 parent 4c32ac0 commit 3f23d5d

3 files changed

Lines changed: 21 additions & 53 deletions

File tree

src/harness/unittests/typingsInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path="../harness.ts" />
22
/// <reference path="./tsserverProjectSystem.ts" />
3-
/// <reference path="../../server/typingsInstaller/typingsInstaller.ts" />import { deepEqual } from "assert";import { deepEqual } from "assert";
3+
/// <reference path="../../server/typingsInstaller/typingsInstaller.ts" />
44

55

66

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/services/jsTyping.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,8 @@ namespace ts.JsTyping {
3232
}
3333

3434
export function isTypingExpired(typing: JsTyping.CachedTyping | undefined) {
35-
const comparisonDate = new Date();
36-
const currentMonth = comparisonDate.getMonth();
37-
if (currentMonth) {
38-
comparisonDate.setMonth(11);
39-
comparisonDate.setFullYear(comparisonDate.getFullYear() - 1);
40-
}
41-
else {
42-
comparisonDate.setMonth(currentMonth - 1);
43-
}
44-
return !typing || typing.timestamp < comparisonDate.getTime();
35+
const msPerMonth = 1000 * 60 * 60 * 24 * 30; // ms/second * second/minute * minutes/hour * hours/day * days/month
36+
return !typing || typing.timestamp < Date.now() - msPerMonth;
4537
}
4638

4739
/* @internal */

0 commit comments

Comments
 (0)