From 1fd47f2517682d26d39bf0d105a96ef4ce6d7f35 Mon Sep 17 00:00:00 2001 From: Valera Rozuvan Date: Fri, 28 Sep 2018 18:08:11 +0300 Subject: [PATCH 1/2] Fix GH#18217 issue for FileLog. --- src/typingsInstaller/nodeTypingsInstaller.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 00ee8b4fa1923..5b68a20765e49 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -1,5 +1,3 @@ -// tslint:disable no-unnecessary-type-assertion (TODO: tslint can't find node types) - namespace ts.server.typingsInstaller { const fs: { appendFileSync(file: string, content: string): void @@ -13,15 +11,25 @@ namespace ts.server.typingsInstaller { class FileLog implements Log { private logEnabled = true; - constructor(private readonly logFile?: string) { + private readonly logFile: string = ""; + + constructor(logFile: string | undefined) { + if (typeof logFile !== "string") { + this.logEnabled = false; + } + else { + this.logFile = logFile; + } } isEnabled = () => { - return this.logEnabled && this.logFile !== undefined; + return this.logEnabled; } writeLine = (text: string) => { + if (!this.logEnabled) return; + try { - fs.appendFileSync(this.logFile!, `[${nowString()}] ${text}${sys.newLine}`); // TODO: GH#18217 + fs.appendFileSync(this.logFile, `[${nowString()}] ${text}${sys.newLine}`); } catch (e) { this.logEnabled = false; From 397364ce5a9f2f719d9a804a0ee9eb2e9c0c99ab Mon Sep 17 00:00:00 2001 From: Valera Rozuvan Date: Tue, 2 Oct 2018 12:11:03 +0300 Subject: [PATCH 2/2] Refactor FileLog class to not use isEnabled property. --- src/typingsInstaller/nodeTypingsInstaller.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 5b68a20765e49..62bdcfce2603b 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -10,29 +10,20 @@ namespace ts.server.typingsInstaller { } = require("path"); class FileLog implements Log { - private logEnabled = true; - private readonly logFile: string = ""; - - constructor(logFile: string | undefined) { - if (typeof logFile !== "string") { - this.logEnabled = false; - } - else { - this.logFile = logFile; - } + constructor(private logFile: string | undefined) { } isEnabled = () => { - return this.logEnabled; + return typeof this.logFile === "string"; } writeLine = (text: string) => { - if (!this.logEnabled) return; + if (typeof this.logFile !== "string") return; try { fs.appendFileSync(this.logFile, `[${nowString()}] ${text}${sys.newLine}`); } catch (e) { - this.logEnabled = false; + this.logFile = undefined; } } }