Skip to content

Commit bf4ec1d

Browse files
Fix timestamp writing, npm install, and cache behavior
1 parent b0321dc commit bf4ec1d

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/server/typingsInstaller/nodeTypingsInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ namespace ts.server.typingsInstaller {
185185
if (this.log.isEnabled()) {
186186
this.log.writeLine(`#${requestId} with arguments'${JSON.stringify(packageNames)}'.`);
187187
}
188-
const command = `${this.npmPath} install@latest --ignore-scripts ${packageNames.join(" ")} --save-dev --force --user-agent="typesInstaller/${version}"`;
188+
const command = `${this.npmPath} install --ignore-scripts ${packageNames.join(" ")} --save-dev --user-agent="typesInstaller/${version}"`;
189189
const start = Date.now();
190190
const hasError = this.execSyncAndLog(command, { cwd });
191191
if (this.log.isEnabled()) {

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace ts.server.typingsInstaller {
1919
writeLine: noop
2020
};
2121

22+
const timestampsFile = "timestamps.json";
23+
2224
function typingToFileName(cachePath: string, packageName: string, installTypingHost: InstallTypingHost, log: Log): string {
2325
try {
2426
const result = resolveModuleName(packageName, combinePaths(cachePath, "index.d.ts"), { moduleResolution: ModuleResolutionKind.NodeJs }, installTypingHost);
@@ -223,7 +225,7 @@ namespace ts.server.typingsInstaller {
223225
}
224226
return;
225227
}
226-
const timestampJson = combinePaths(cacheLocation, "timestamps.json");
228+
const timestampJson = combinePaths(cacheLocation, timestampsFile);
227229
this.typeDeclarationTimestamps = loadTypeDeclarationTimestampFile(timestampJson, this.installTypingHost, this.log);
228230
const packageJson = combinePaths(cacheLocation, "package.json");
229231
if (this.log.isEnabled()) {
@@ -264,6 +266,9 @@ namespace ts.server.typingsInstaller {
264266
// defaults to old behavior of never updating if we ever use a host without getModifiedTime in the future
265267
const timestamp = this.installTypingHost.getModifiedTime === undefined ? Date.now() : this.installTypingHost.getModifiedTime(typingFile).getTime();
266268
this.typeDeclarationTimestamps[key] = timestamp;
269+
if (this.log.isEnabled()) {
270+
this.log.writeLine(`Adding entry into timestamp cache: '${key}' => '${timestamp}'`);
271+
}
267272
}
268273
// timestamp guaranteed to not be undefined by above check
269274
const newTyping: JsTyping.CachedTyping = { typingLocation: typingFile, timestamp: getProperty(this.typeDeclarationTimestamps, key) };
@@ -360,6 +365,7 @@ namespace ts.server.typingsInstaller {
360365
this.log.writeLine(`Installed typings ${JSON.stringify(scopedTypings)}`);
361366
}
362367
const installedTypingFiles: string[] = [];
368+
const typesPackageName = (packageName: string) => `@types/${packageName}`;
363369
for (const packageName of filteredTypings) {
364370
const typingFile = typingToFileName(cachePath, packageName, this.installTypingHost, this.log);
365371
if (!typingFile) {
@@ -370,15 +376,16 @@ namespace ts.server.typingsInstaller {
370376
const newTimestamp = Date.now();
371377
const newTyping: JsTyping.CachedTyping = { typingLocation: typingFile, timestamp: newTimestamp };
372378
this.packageNameToTypingLocation.set(packageName, newTyping);
373-
this.typeDeclarationTimestamps[packageName] = newTimestamp;
379+
this.typeDeclarationTimestamps[typesPackageName(packageName)] = newTimestamp;
374380
installedTypingFiles.push(typingFile);
375381
}
376382
if (this.log.isEnabled()) {
377383
this.log.writeLine(`Installed typing files ${JSON.stringify(installedTypingFiles)}`);
378384
}
379385

380386
const newFileContents: TypeDeclarationTimestampFile = { entries: this.typeDeclarationTimestamps };
381-
writeTypeDeclarationTimestampFile(cachePath, newFileContents, this.installTypingHost, this.log); // WRONG PATH
387+
const timestampJson = combinePaths(cachePath, timestampsFile);
388+
writeTypeDeclarationTimestampFile(timestampJson, newFileContents, this.installTypingHost, this.log);
382389

383390
this.sendResponse(this.createSetTypings(req, currentlyCachedTypings.concat(installedTypingFiles)));
384391
}

src/services/jsTyping.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ namespace ts.JsTyping {
3131
timestamp: number;
3232
}
3333

34-
const typingLifetime = new Date(0, 1);
35-
3634
export function isTypingExpired(typing: JsTyping.CachedTyping | undefined) {
37-
return typing && Date.now() - typingLifetime.getTime() < typing.timestamp;
35+
const comparisonDate = new Date();
36+
const currentMonth = comparisonDate.getMonth();
37+
if (currentMonth) {
38+
comparisonDate.setMonth(11);
39+
comparisonDate.setFullYear(comparisonDate.getFullYear() - 1);
40+
} else {
41+
comparisonDate.setMonth(currentMonth - 1);
42+
}
43+
return !typing || typing.timestamp < comparisonDate.getTime();
3844
}
3945

4046
/* @internal */

0 commit comments

Comments
 (0)