@teambit/ts-server 0.0.45 → 0.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,18 +66,20 @@ export declare class ProcessBasedTsServer {
66
66
  private logger;
67
67
  private cancellationPipeName;
68
68
  constructor(options: TspClientOptions, tsServerArgs?: TsServerArgs);
69
- start(): void;
70
- notify(command: CommandTypes.Open, args: protocol.OpenRequestArgs): void;
71
- notify(command: CommandTypes.Close, args: protocol.FileRequestArgs): void;
72
- notify(command: CommandTypes.Saveto, args: protocol.SavetoRequestArgs): void;
73
- notify(command: CommandTypes.Change, args: protocol.ChangeRequestArgs): void;
69
+ restart(): Promise<void>;
70
+ start(): Promise<void>;
71
+ notify(command: CommandTypes.Open, args: protocol.OpenRequestArgs): Promise<void>;
72
+ notify(command: CommandTypes.Close, args: protocol.FileRequestArgs): Promise<void>;
73
+ notify(command: CommandTypes.Saveto, args: protocol.SavetoRequestArgs): Promise<void>;
74
+ notify(command: CommandTypes.Change, args: protocol.ChangeRequestArgs): Promise<void>;
74
75
  request<K extends keyof TypeScriptRequestTypes>(command: K, args: TypeScriptRequestTypes[K][0], token?: CancellationToken): Promise<TypeScriptRequestTypes[K][1]>;
75
76
  kill(): void;
76
77
  private log;
77
78
  protected sendMessage(command: string, notification: boolean, args?: any): void;
78
- protected processMessage(untrimmedMessageString: string): void;
79
+ protected processMessage(untrimmedMessageString: string, resolve?: () => void, reject?: (err: any) => void): void;
79
80
  private resolveResponse;
80
81
  private isEvent;
81
82
  private isResponse;
82
83
  private isRequestCompletedEvent;
84
+ private ensureServerIsRunning;
83
85
  }
@@ -25,6 +25,15 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  __setModuleDefault(result, mod);
26
26
  return result;
27
27
  };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
28
37
  var __importDefault = (this && this.__importDefault) || function (mod) {
29
38
  return (mod && mod.__esModule) ? mod : { "default": mod };
30
39
  };
@@ -51,78 +60,108 @@ class ProcessBasedTsServer {
51
60
  this.deferreds = {};
52
61
  this.logger = options.logger;
53
62
  }
63
+ restart() {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ this.kill();
66
+ yield this.start();
67
+ });
68
+ }
54
69
  start() {
55
- var _a;
56
- if (this.readlineInterface) {
57
- return;
58
- }
59
- const { tsserverPath } = this.options;
60
- const { logFile, logVerbosity, maxTsServerMemory, globalPlugins, pluginProbeLocations } = this.tsServerArgs;
61
- const args = [];
62
- if (logFile) {
63
- args.push('--logFile', logFile);
64
- }
65
- if (logVerbosity) {
66
- args.push('--logVerbosity', logVerbosity);
67
- }
68
- if (globalPlugins && globalPlugins.length) {
69
- args.push('--globalPlugins', globalPlugins.join(','));
70
- }
71
- if (pluginProbeLocations && pluginProbeLocations.length) {
72
- args.push('--pluginProbeLocations', pluginProbeLocations.join(','));
73
- }
74
- this.cancellationPipeName = tempy_1.default.file({ name: 'tscancellation' });
75
- args.push('--cancellationPipeName', `${this.cancellationPipeName}*`);
76
- this.logger.info(`Starting tsserver : '${tsserverPath} ${args.join(' ')}'`);
77
- const tsserverPathIsModule = path.extname(tsserverPath) === '.js';
78
- const options = {
79
- silent: true,
80
- execArgv: [...(maxTsServerMemory ? [`--max-old-space-size=${maxTsServerMemory}`] : [])],
81
- };
82
- this.tsServerProcess = tsserverPathIsModule ? cp.fork(tsserverPath, args, options) : cp.spawn(tsserverPath, args);
83
- this.readlineInterface = readline.createInterface(this.tsServerProcess.stdout, this.tsServerProcess.stdin, undefined);
84
- process.on('exit', () => {
70
+ return new Promise((resolve, reject) => {
85
71
  var _a;
86
- this.readlineInterface.close();
87
- (_a = this.tsServerProcess.stdin) === null || _a === void 0 ? void 0 : _a.destroy();
88
- this.tsServerProcess.kill();
89
- });
90
- this.readlineInterface.on('line', (line) => this.processMessage(line));
91
- const dec = new decoder.StringDecoder('utf-8');
92
- (_a = this.tsServerProcess.stderr) === null || _a === void 0 ? void 0 : _a.addListener('data', (data) => {
93
- const stringMsg = typeof data === 'string' ? data : dec.write(data);
94
- this.logger.error(stringMsg);
72
+ if (this.tsServerProcess) {
73
+ reject(new Error('server already started'));
74
+ return;
75
+ }
76
+ const { tsserverPath } = this.options;
77
+ const { logFile, logVerbosity, maxTsServerMemory, globalPlugins, pluginProbeLocations } = this.tsServerArgs;
78
+ const args = [];
79
+ if (logFile) {
80
+ args.push('--logFile', logFile);
81
+ }
82
+ if (logVerbosity) {
83
+ args.push('--logVerbosity', logVerbosity);
84
+ }
85
+ if (globalPlugins && globalPlugins.length) {
86
+ args.push('--globalPlugins', globalPlugins.join(','));
87
+ }
88
+ if (pluginProbeLocations && pluginProbeLocations.length) {
89
+ args.push('--pluginProbeLocations', pluginProbeLocations.join(','));
90
+ }
91
+ this.cancellationPipeName = tempy_1.default.file({ name: 'tscancellation' });
92
+ args.push('--cancellationPipeName', `${this.cancellationPipeName}*`);
93
+ this.logger.info(`Starting tsserver : '${tsserverPath} ${args.join(' ')}'`);
94
+ const tsserverPathIsModule = path.extname(tsserverPath) === '.js';
95
+ const options = {
96
+ silent: true,
97
+ execArgv: [...(maxTsServerMemory ? [`--max-old-space-size=${maxTsServerMemory}`] : [])],
98
+ };
99
+ this.tsServerProcess = tsserverPathIsModule ? cp.fork(tsserverPath, args, options) : cp.spawn(tsserverPath, args);
100
+ this.readlineInterface = readline.createInterface(this.tsServerProcess.stdout, this.tsServerProcess.stdin, undefined);
101
+ process.on('exit', () => {
102
+ this.kill();
103
+ reject(new Error('TSServer was killed'));
104
+ });
105
+ this.readlineInterface.on('line', (line) => {
106
+ this.processMessage(line, resolve, reject);
107
+ });
108
+ const dec = new decoder.StringDecoder('utf-8');
109
+ (_a = this.tsServerProcess.stderr) === null || _a === void 0 ? void 0 : _a.addListener('data', (data) => {
110
+ const stringMsg = typeof data === 'string' ? data : dec.write(data);
111
+ this.logger.error(stringMsg);
112
+ reject(new Error(stringMsg));
113
+ });
95
114
  });
96
115
  }
97
116
  notify(command, args) {
98
- this.sendMessage(command, true, args);
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ yield this.ensureServerIsRunning();
119
+ this.sendMessage(command, true, args);
120
+ });
99
121
  }
100
122
  request(command, args, token) {
101
- this.sendMessage(command, false, args);
102
- const seq = this.seq;
103
- const deferred = new utils_1.Deferred();
104
- this.deferreds[seq] = deferred;
105
- const request = deferred.promise;
106
- if (token) {
107
- const onCancelled = token.onCancellationRequested(() => {
108
- onCancelled.dispose();
109
- if (this.cancellationPipeName) {
110
- const requestCancellationPipeName = `${this.cancellationPipeName}${seq}`;
111
- fs.writeFile(requestCancellationPipeName, '', (err) => {
112
- if (!err) {
113
- // eslint-disable-next-line
114
- request.then(() => fs.unlink(requestCancellationPipeName, () => {
115
- /* no-op */
116
- }));
117
- }
118
- });
119
- }
120
- });
121
- }
122
- return request;
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ yield this.ensureServerIsRunning();
125
+ this.sendMessage(command, false, args);
126
+ const seq = this.seq;
127
+ const deferred = new utils_1.Deferred();
128
+ this.deferreds[seq] = deferred;
129
+ const request = deferred.promise;
130
+ let onCancelled;
131
+ if (token) {
132
+ onCancelled = token.onCancellationRequested(() => {
133
+ onCancelled.dispose();
134
+ if (this.cancellationPipeName) {
135
+ const requestCancellationPipeName = `${this.cancellationPipeName}${seq}`;
136
+ fs.writeFile(requestCancellationPipeName, '', (err) => {
137
+ if (!err) {
138
+ // eslint-disable-next-line
139
+ request.then(() => fs.unlink(requestCancellationPipeName, () => {
140
+ /* no-op */
141
+ }));
142
+ }
143
+ });
144
+ }
145
+ });
146
+ }
147
+ try {
148
+ const result = yield request;
149
+ onCancelled === null || onCancelled === void 0 ? void 0 : onCancelled.dispose();
150
+ return result;
151
+ }
152
+ catch (error) {
153
+ this.logger.error(`Error in request: ${error}`);
154
+ throw error;
155
+ }
156
+ });
123
157
  }
124
158
  kill() {
125
- this.tsServerProcess.kill();
159
+ var _a, _b, _c, _d;
160
+ (_a = this.tsServerProcess) === null || _a === void 0 ? void 0 : _a.kill();
161
+ (_c = (_b = this.tsServerProcess) === null || _b === void 0 ? void 0 : _b.stdin) === null || _c === void 0 ? void 0 : _c.destroy();
162
+ (_d = this.readlineInterface) === null || _d === void 0 ? void 0 : _d.close();
163
+ this.tsServerProcess = null;
164
+ this.readlineInterface = null;
126
165
  }
127
166
  log(msg, obj = {}) {
128
167
  msg = `[tsserver] ${msg}`;
@@ -134,7 +173,7 @@ class ProcessBasedTsServer {
134
173
  }
135
174
  }
136
175
  sendMessage(command, notification, args) {
137
- var _a;
176
+ var _a, _b;
138
177
  this.seq += 1;
139
178
  const request = {
140
179
  command,
@@ -145,16 +184,27 @@ class ProcessBasedTsServer {
145
184
  request.arguments = args;
146
185
  }
147
186
  const serializedRequest = `${JSON.stringify(request)}\n`;
148
- (_a = this.tsServerProcess.stdin) === null || _a === void 0 ? void 0 : _a.write(serializedRequest);
187
+ (_b = (_a = this.tsServerProcess) === null || _a === void 0 ? void 0 : _a.stdin) === null || _b === void 0 ? void 0 : _b.write(serializedRequest);
149
188
  this.log(notification ? 'notify' : 'request', request);
150
189
  }
151
- processMessage(untrimmedMessageString) {
190
+ processMessage(untrimmedMessageString, resolve, reject) {
152
191
  const messageString = untrimmedMessageString.trim();
153
192
  if (!messageString || messageString.startsWith('Content-Length:')) {
154
193
  return;
155
194
  }
156
- const message = JSON.parse(messageString);
195
+ let message;
196
+ try {
197
+ message = JSON.parse(messageString);
198
+ }
199
+ catch (error) {
200
+ // If the message isn't valid JSON, it's not a valid tsserver message. Reject the promise.
201
+ reject === null || reject === void 0 ? void 0 : reject(new Error(`Received invalid message from TSServer: ${untrimmedMessageString}`));
202
+ return;
203
+ }
157
204
  this.log('processMessage', message);
205
+ if (this.isEvent(message)) {
206
+ resolve === null || resolve === void 0 ? void 0 : resolve();
207
+ }
158
208
  if (this.isResponse(message)) {
159
209
  this.resolveResponse(message, message.request_seq, message.success);
160
210
  }
@@ -189,6 +239,13 @@ class ProcessBasedTsServer {
189
239
  isRequestCompletedEvent(message) {
190
240
  return this.isEvent(message) && message.event === 'requestCompleted';
191
241
  }
242
+ ensureServerIsRunning() {
243
+ return __awaiter(this, void 0, void 0, function* () {
244
+ if (!this.tsServerProcess) {
245
+ yield this.restart();
246
+ }
247
+ });
248
+ }
192
249
  }
193
250
  exports.ProcessBasedTsServer = ProcessBasedTsServer;
194
251
  //# sourceMappingURL=process-based-tsserver.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"process-based-tsserver.js","sourceRoot":"","sources":["../process-based-tsserver.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH;;;;;GAKG;AAEH,uCAAyB;AACzB,2CAA6B;AAC7B,kDAAoC;AACpC,mDAAqC;AAGrC,wDAA0C;AAG1C,kDAA0B;AAG1B,mCAAmC;AAwDnC,MAAa,oBAAoB;IAY/B,YAAoB,OAAyB,EAAU,eAA6B,EAAE;QAAlE,YAAO,GAAP,OAAO,CAAkB;QAAU,iBAAY,GAAZ,YAAY,CAAmB;QAT9E,QAAG,GAAG,CAAC,CAAC;QAEC,cAAS,GAEtB,EAAE,CAAC;QAML,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC/B,CAAC;IAED,KAAK;;QACH,IAAI,IAAI,CAAC,iBAAiB,EAAE;YAC1B,OAAO;SACR;QACD,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QACtC,MAAM,EAAE,OAAO,EAAE,YAAY,EA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,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;SACpC;IACH,CAAC;IAEO,OAAO,CAAC,OAAyB;QACvC,OAAO,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC;IAClC,CAAC;IAEO,UAAU,CAAC,OAAyB;QAC1C,OAAO,OAAO,CAAC,IAAI,KAAK,UAAU,CAAC;IACrC,CAAC;IAEO,uBAAuB,CAAC,OAAyB;QACvD,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,KAAK,KAAK,kBAAkB,CAAC;IACvE,CAAC;IAEa,qBAAqB;;YACjC,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE;gBACzB,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;aACtB;QACH,CAAC;KAAA;CACF;AA7ND,oDA6NC"}
@@ -22,6 +22,7 @@ export declare class TsserverClient {
22
22
  private files;
23
23
  private tsServer;
24
24
  lastDiagnostics: protocol.DiagnosticEventBody[];
25
+ private serverRunning;
25
26
  constructor(
26
27
  /**
27
28
  * absolute root path of the project.
@@ -37,7 +38,7 @@ export declare class TsserverClient {
37
38
  * this methods returns pretty fast. if checkTypes is enabled, it runs the process in the background and
38
39
  * doesn't wait for it.
39
40
  */
40
- init(): void;
41
+ init(): Promise<void>;
41
42
  private checkTypesIfNeeded;
42
43
  private shouldCheckTypes;
43
44
  /**
@@ -45,6 +46,7 @@ export declare class TsserverClient {
45
46
  */
46
47
  onFileChange(file: string): Promise<void>;
47
48
  killTsServer(): void;
49
+ isServerRunning(): boolean;
48
50
  /**
49
51
  * get diagnostic of all files opened in the project.
50
52
  * there is little to no value of getting diagnostic for a specific file, as
@@ -82,9 +84,9 @@ export declare class TsserverClient {
82
84
  * ask tsserver to open a file if it was not opened before.
83
85
  * @param file absolute path of the file
84
86
  */
85
- openIfNeeded(file: string): void;
87
+ openIfNeeded(file: string): Promise<void>;
86
88
  private open;
87
- close(file: string): void;
89
+ close(file: string): Promise<void>;
88
90
  /**
89
91
  * since Bit is not an IDE, it doesn't have the information such as the exact line/offset of the changes.
90
92
  * as a workaround, to tell tsserver what was changed, we pretend that the entire file was cleared and new text was
@@ -38,6 +38,7 @@ class TsserverClient {
38
38
  this.options = options;
39
39
  this.files = files;
40
40
  this.lastDiagnostics = [];
41
+ this.serverRunning = false;
41
42
  }
42
43
  /**
43
44
  * start the ts-server and keep its process alive.
@@ -45,18 +46,40 @@ class TsserverClient {
45
46
  * doesn't wait for it.
46
47
  */
47
48
  init() {
48
- this.tsServer = new process_based_tsserver_1.ProcessBasedTsServer({
49
- logger: this.logger,
50
- tsserverPath: this.findTsserverPath(),
51
- logToConsole: this.options.verbose,
52
- onEvent: this.onTsserverEvent.bind(this),
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ try {
51
+ this.tsServer = new process_based_tsserver_1.ProcessBasedTsServer({
52
+ logger: this.logger,
53
+ tsserverPath: this.findTsserverPath(),
54
+ logToConsole: this.options.verbose,
55
+ onEvent: this.onTsserverEvent.bind(this),
56
+ });
57
+ this.tsServer
58
+ .start()
59
+ .then(() => {
60
+ this.serverRunning = true;
61
+ })
62
+ .catch((err) => {
63
+ this.logger.error('TsserverClient.init failed', err);
64
+ });
65
+ if (this.files.length) {
66
+ const openPromises = this.files.map((file) => this.open(file));
67
+ yield Promise.all(openPromises.map((promise) => promise.catch((error) => error)));
68
+ const failedFiles = openPromises.filter((promise) => promise instanceof Error);
69
+ if (failedFiles.length > 0) {
70
+ this.logger.error('TsserverClient.init failed to open files:', failedFiles);
71
+ }
72
+ if (failedFiles.length > 0) {
73
+ this.logger.error('TsserverClient.init failed to open files:', failedFiles);
74
+ }
75
+ this.checkTypesIfNeeded();
76
+ }
77
+ this.logger.debug('TsserverClient.init completed');
78
+ }
79
+ catch (err) {
80
+ this.logger.error('TsserverClient.init failed', err);
81
+ }
53
82
  });
54
- this.tsServer.start();
55
- if (this.files.length) {
56
- this.files.forEach((file) => this.open(file));
57
- }
58
- this.checkTypesIfNeeded();
59
- this.logger.debug('TsserverClient.init completed');
60
83
  }
61
84
  checkTypesIfNeeded(files = this.files) {
62
85
  if (!this.shouldCheckTypes()) {
@@ -95,7 +118,14 @@ class TsserverClient {
95
118
  });
96
119
  }
97
120
  killTsServer() {
98
- this.tsServer.kill();
121
+ if (this.tsServer && this.serverRunning) {
122
+ this.tsServer.kill();
123
+ this.tsServer = null;
124
+ this.serverRunning = false;
125
+ }
126
+ }
127
+ isServerRunning() {
128
+ return this.serverRunning;
99
129
  }
100
130
  /**
101
131
  * get diagnostic of all files opened in the project.
@@ -108,27 +138,30 @@ class TsserverClient {
108
138
  * were found or not.
109
139
  */
110
140
  getDiagnostic(files = this.files) {
141
+ var _a;
111
142
  return __awaiter(this, void 0, void 0, function* () {
112
143
  this.lastDiagnostics = [];
113
- return this.tsServer.request(tsp_command_types_1.CommandTypes.Geterr, { delay: 0, files });
144
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.Geterr, { delay: 0, files });
114
145
  });
115
146
  }
116
147
  /**
117
148
  * avoid using this method, it takes longer than `getDiagnostic()` and shows errors from paths outside the project
118
149
  */
119
150
  getDiagnosticAllProject(requestedByFile) {
151
+ var _a;
120
152
  return __awaiter(this, void 0, void 0, function* () {
121
- return this.tsServer.request(tsp_command_types_1.CommandTypes.GeterrForProject, { file: requestedByFile, delay: 0 });
153
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.GeterrForProject, { file: requestedByFile, delay: 0 });
122
154
  });
123
155
  }
124
156
  /**
125
157
  * @param file can be absolute or relative to this.projectRoot.
126
158
  */
127
159
  getQuickInfo(file, position) {
160
+ var _a;
128
161
  return __awaiter(this, void 0, void 0, function* () {
129
162
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
130
- this.openIfNeeded(absFile);
131
- return this.tsServer.request(tsp_command_types_1.CommandTypes.Quickinfo, {
163
+ yield this.openIfNeeded(absFile);
164
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.Quickinfo, {
132
165
  file: absFile,
133
166
  line: position.line,
134
167
  offset: position.character,
@@ -139,10 +172,11 @@ class TsserverClient {
139
172
  * @param file can be absolute or relative to this.projectRoot.
140
173
  */
141
174
  getTypeDefinition(file, position) {
175
+ var _a;
142
176
  return __awaiter(this, void 0, void 0, function* () {
143
177
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
144
- this.openIfNeeded(absFile);
145
- return this.tsServer.request(tsp_command_types_1.CommandTypes.TypeDefinition, {
178
+ yield this.openIfNeeded(absFile);
179
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.TypeDefinition, {
146
180
  file: absFile,
147
181
  line: position.line,
148
182
  offset: position.character,
@@ -150,15 +184,16 @@ class TsserverClient {
150
184
  });
151
185
  }
152
186
  getDefinition(file, position) {
187
+ var _a;
153
188
  return __awaiter(this, void 0, void 0, function* () {
154
189
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
155
- this.openIfNeeded(absFile);
156
- const response = yield this.tsServer.request(tsp_command_types_1.CommandTypes.Definition, {
190
+ yield this.openIfNeeded(absFile);
191
+ const response = yield ((_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.Definition, {
157
192
  file: absFile,
158
193
  line: position.line,
159
194
  offset: position.character,
160
- });
161
- if (!response.success) {
195
+ }));
196
+ if (!(response === null || response === void 0 ? void 0 : response.success)) {
162
197
  // TODO: we need a function to handle responses properly here for all.
163
198
  return response;
164
199
  }
@@ -169,10 +204,11 @@ class TsserverClient {
169
204
  * @param file can be absolute or relative to this.projectRoot.
170
205
  */
171
206
  getReferences(file, position) {
207
+ var _a;
172
208
  return __awaiter(this, void 0, void 0, function* () {
173
209
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
174
- this.openIfNeeded(absFile);
175
- return this.tsServer.request(tsp_command_types_1.CommandTypes.References, {
210
+ yield this.openIfNeeded(absFile);
211
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.References, {
176
212
  file: absFile,
177
213
  line: position.line,
178
214
  offset: position.character,
@@ -183,10 +219,11 @@ class TsserverClient {
183
219
  * @param file can be absolute or relative to this.projectRoot.
184
220
  */
185
221
  getSignatureHelp(file, position) {
222
+ var _a;
186
223
  return __awaiter(this, void 0, void 0, function* () {
187
224
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
188
- this.openIfNeeded(absFile);
189
- return this.tsServer.request(tsp_command_types_1.CommandTypes.SignatureHelp, {
225
+ yield this.openIfNeeded(absFile);
226
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.SignatureHelp, {
190
227
  file: absFile,
191
228
  line: position.line,
192
229
  offset: position.character,
@@ -194,8 +231,9 @@ class TsserverClient {
194
231
  });
195
232
  }
196
233
  configure(configureArgs = {}) {
234
+ var _a;
197
235
  return __awaiter(this, void 0, void 0, function* () {
198
- return this.tsServer.request(tsp_command_types_1.CommandTypes.Configure, configureArgs);
236
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.request(tsp_command_types_1.CommandTypes.Configure, configureArgs);
199
237
  });
200
238
  }
201
239
  /**
@@ -203,23 +241,31 @@ class TsserverClient {
203
241
  * @param file absolute path of the file
204
242
  */
205
243
  openIfNeeded(file) {
206
- if (this.files.includes(file)) {
207
- return;
208
- }
209
- this.open(file);
210
- this.files.push(file);
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ if (this.files.includes(file)) {
246
+ return;
247
+ }
248
+ yield this.open(file);
249
+ this.files.push(file);
250
+ });
211
251
  }
212
252
  open(file) {
213
- this.tsServer.notify(tsp_command_types_1.CommandTypes.Open, {
214
- file,
215
- projectRootPath: this.projectPath,
253
+ var _a;
254
+ return __awaiter(this, void 0, void 0, function* () {
255
+ return (_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.notify(tsp_command_types_1.CommandTypes.Open, {
256
+ file,
257
+ projectRootPath: this.projectPath,
258
+ });
216
259
  });
217
260
  }
218
261
  close(file) {
219
- this.tsServer.notify(tsp_command_types_1.CommandTypes.Close, {
220
- file,
262
+ var _a;
263
+ return __awaiter(this, void 0, void 0, function* () {
264
+ yield ((_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.notify(tsp_command_types_1.CommandTypes.Close, {
265
+ file,
266
+ }));
267
+ this.files = this.files.filter((openFile) => openFile !== file);
221
268
  });
222
- this.files = this.files.filter((openFile) => openFile !== file);
223
269
  }
224
270
  /**
225
271
  * since Bit is not an IDE, it doesn't have the information such as the exact line/offset of the changes.
@@ -228,26 +274,27 @@ class TsserverClient {
228
274
  * the file content remained the same. (closing/re-opening the file doesn't help).
229
275
  */
230
276
  changed(file) {
277
+ var _a, _b;
231
278
  return __awaiter(this, void 0, void 0, function* () {
232
279
  // tell tsserver that all content was removed
233
- this.tsServer.notify(tsp_command_types_1.CommandTypes.Change, {
280
+ yield ((_a = this.tsServer) === null || _a === void 0 ? void 0 : _a.notify(tsp_command_types_1.CommandTypes.Change, {
234
281
  file,
235
282
  line: 1,
236
283
  offset: 1,
237
284
  endLine: 99999,
238
285
  endOffset: 1,
239
286
  insertString: '',
240
- });
287
+ }));
241
288
  const content = yield fs_extra_1.default.readFile(file, 'utf-8');
242
289
  // tell tsserver that all file content was added
243
- this.tsServer.notify(tsp_command_types_1.CommandTypes.Change, {
290
+ yield ((_b = this.tsServer) === null || _b === void 0 ? void 0 : _b.notify(tsp_command_types_1.CommandTypes.Change, {
244
291
  file,
245
292
  line: 1,
246
293
  offset: 1,
247
294
  endLine: 1,
248
295
  endOffset: 1,
249
296
  insertString: content,
250
- });
297
+ }));
251
298
  });
252
299
  }
253
300
  onTsserverEvent(event) {
@@ -1 +1 @@
1
- {"version":3,"file":"ts-server-client.js","sourceRoot":"","sources":["../ts-server-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,wDAA0B;AAE1B,gD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Aa,CAAC,IAAI,CAAC,IAAA,6BAAqB,GAAE,CAAC,EAAE;YAC/C,OAAO,IAAA,6BAAqB,GAAE,CAAC;SAChC;QAED,MAAM,IAAI,KAAK,CAAC,kBAAkB,IAAA,6BAAqB,GAAE,sCAAsC,CAAC,CAAC;IACnG,CAAC;CACF;AA3TD,wCA2TC"}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/ts-server",
3
- "version": "0.0.45",
3
+ "version": "0.0.47",
4
4
  "homepage": "https://bit.cloud/teambit/typescript/ts-server",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.typescript",
8
8
  "name": "ts-server",
9
- "version": "0.0.45"
9
+ "version": "0.0.47"
10
10
  },
11
11
  "dependencies": {
12
12
  "typescript": "4.7.4",
@@ -78,8 +78,8 @@ export interface TypeScriptRequestTypes {
78
78
  }
79
79
 
80
80
  export class ProcessBasedTsServer {
81
- private readlineInterface: readline.ReadLine;
82
- private tsServerProcess: cp.ChildProcess;
81
+ private readlineInterface: readline.ReadLine | null;
82
+ private tsServerProcess: cp.ChildProcess | null;
83
83
  private seq = 0;
84
84
 
85
85
  private readonly deferreds: {
@@ -93,73 +93,90 @@ export class ProcessBasedTsServer {
93
93
  this.logger = options.logger;
94
94
  }
95
95
 
96
- start(): void {
97
- if (this.readlineInterface) {
98
- return;
99
- }
100
- const { tsserverPath } = this.options;
101
- const { logFile, logVerbosity, maxTsServerMemory, globalPlugins, pluginProbeLocations } = this.tsServerArgs;
102
- const args: string[] = [];
103
- if (logFile) {
104
- args.push('--logFile', logFile);
105
- }
106
- if (logVerbosity) {
107
- args.push('--logVerbosity', logVerbosity);
108
- }
109
- if (globalPlugins && globalPlugins.length) {
110
- args.push('--globalPlugins', globalPlugins.join(','));
111
- }
112
- if (pluginProbeLocations && pluginProbeLocations.length) {
113
- args.push('--pluginProbeLocations', pluginProbeLocations.join(','));
114
- }
115
- this.cancellationPipeName = tempy.file({ name: 'tscancellation' });
116
- args.push('--cancellationPipeName', `${this.cancellationPipeName}*`);
117
- this.logger.info(`Starting tsserver : '${tsserverPath} ${args.join(' ')}'`);
118
- const tsserverPathIsModule = path.extname(tsserverPath) === '.js';
119
- const options = {
120
- silent: true,
121
- execArgv: [...(maxTsServerMemory ? [`--max-old-space-size=${maxTsServerMemory}`] : [])],
122
- };
123
- this.tsServerProcess = tsserverPathIsModule ? cp.fork(tsserverPath, args, options) : cp.spawn(tsserverPath, args);
124
- this.readlineInterface = readline.createInterface(
125
- this.tsServerProcess.stdout as Readable,
126
- this.tsServerProcess.stdin as Writable,
127
- undefined
128
- );
129
- process.on('exit', () => {
130
- this.readlineInterface.close();
131
- this.tsServerProcess.stdin?.destroy();
132
- this.tsServerProcess.kill();
133
- });
134
- this.readlineInterface.on('line', (line) => this.processMessage(line));
96
+ async restart() {
97
+ this.kill();
98
+ await this.start();
99
+ }
100
+
101
+ start() {
102
+ return new Promise<void>((resolve, reject) => {
103
+ if (this.tsServerProcess) {
104
+ reject(new Error('server already started'));
105
+ return;
106
+ }
107
+
108
+ const { tsserverPath } = this.options;
109
+ const { logFile, logVerbosity, maxTsServerMemory, globalPlugins, pluginProbeLocations } = this.tsServerArgs;
110
+ const args: string[] = [];
111
+ if (logFile) {
112
+ args.push('--logFile', logFile);
113
+ }
114
+ if (logVerbosity) {
115
+ args.push('--logVerbosity', logVerbosity);
116
+ }
117
+ if (globalPlugins && globalPlugins.length) {
118
+ args.push('--globalPlugins', globalPlugins.join(','));
119
+ }
120
+ if (pluginProbeLocations && pluginProbeLocations.length) {
121
+ args.push('--pluginProbeLocations', pluginProbeLocations.join(','));
122
+ }
123
+ this.cancellationPipeName = tempy.file({ name: 'tscancellation' });
124
+ args.push('--cancellationPipeName', `${this.cancellationPipeName}*`);
125
+ this.logger.info(`Starting tsserver : '${tsserverPath} ${args.join(' ')}'`);
126
+ const tsserverPathIsModule = path.extname(tsserverPath) === '.js';
127
+ const options = {
128
+ silent: true,
129
+ execArgv: [...(maxTsServerMemory ? [`--max-old-space-size=${maxTsServerMemory}`] : [])],
130
+ };
131
+ this.tsServerProcess = tsserverPathIsModule ? cp.fork(tsserverPath, args, options) : cp.spawn(tsserverPath, args);
132
+
133
+ this.readlineInterface = readline.createInterface(
134
+ this.tsServerProcess.stdout as Readable,
135
+ this.tsServerProcess.stdin as Writable,
136
+ undefined
137
+ );
138
+ process.on('exit', () => {
139
+ this.kill();
140
+ reject(new Error('TSServer was killed'));
141
+ });
142
+
143
+ this.readlineInterface.on('line', (line) => {
144
+ this.processMessage(line, resolve, reject);
145
+ });
135
146
 
136
- const dec = new decoder.StringDecoder('utf-8');
137
- this.tsServerProcess.stderr?.addListener('data', (data) => {
138
- const stringMsg = typeof data === 'string' ? data : dec.write(data);
139
- this.logger.error(stringMsg);
147
+ const dec = new decoder.StringDecoder('utf-8');
148
+ this.tsServerProcess.stderr?.addListener('data', (data) => {
149
+ const stringMsg = typeof data === 'string' ? data : dec.write(data);
150
+ this.logger.error(stringMsg);
151
+ reject(new Error(stringMsg));
152
+ });
140
153
  });
141
154
  }
142
155
 
143
- notify(command: CommandTypes.Open, args: protocol.OpenRequestArgs): void;
144
- notify(command: CommandTypes.Close, args: protocol.FileRequestArgs): void;
145
- notify(command: CommandTypes.Saveto, args: protocol.SavetoRequestArgs): void;
146
- notify(command: CommandTypes.Change, args: protocol.ChangeRequestArgs): void;
147
- notify(command: string, args: any): void {
156
+ async notify(command: CommandTypes.Open, args: protocol.OpenRequestArgs): Promise<void>;
157
+ async notify(command: CommandTypes.Close, args: protocol.FileRequestArgs): Promise<void>;
158
+ async notify(command: CommandTypes.Saveto, args: protocol.SavetoRequestArgs): Promise<void>;
159
+ async notify(command: CommandTypes.Change, args: protocol.ChangeRequestArgs): Promise<void>;
160
+ async notify(command: string, args: any): Promise<void> {
161
+ await this.ensureServerIsRunning();
148
162
  this.sendMessage(command, true, args);
149
163
  }
150
164
 
151
- request<K extends keyof TypeScriptRequestTypes>(
165
+ async request<K extends keyof TypeScriptRequestTypes>(
152
166
  command: K,
153
167
  args: TypeScriptRequestTypes[K][0],
154
168
  token?: CancellationToken
155
169
  ): Promise<TypeScriptRequestTypes[K][1]> {
170
+ await this.ensureServerIsRunning();
156
171
  this.sendMessage(command, false, args);
157
172
  const seq = this.seq;
158
173
  const deferred = new Deferred<TypeScriptRequestTypes[K][1]>();
159
174
  this.deferreds[seq] = deferred;
160
175
  const request = deferred.promise;
176
+
177
+ let onCancelled;
161
178
  if (token) {
162
- const onCancelled = token.onCancellationRequested(() => {
179
+ onCancelled = token.onCancellationRequested(() => {
163
180
  onCancelled.dispose();
164
181
  if (this.cancellationPipeName) {
165
182
  const requestCancellationPipeName = `${this.cancellationPipeName}${seq}`;
@@ -176,11 +193,23 @@ export class ProcessBasedTsServer {
176
193
  }
177
194
  });
178
195
  }
179
- return request;
196
+
197
+ try {
198
+ const result = await request;
199
+ onCancelled?.dispose();
200
+ return result;
201
+ } catch (error) {
202
+ this.logger.error(`Error in request: ${error}`);
203
+ throw error;
204
+ }
180
205
  }
181
206
 
182
207
  kill() {
183
- this.tsServerProcess.kill();
208
+ this.tsServerProcess?.kill();
209
+ this.tsServerProcess?.stdin?.destroy();
210
+ this.readlineInterface?.close();
211
+ this.tsServerProcess = null;
212
+ this.readlineInterface = null;
184
213
  }
185
214
 
186
215
  private log(msg: string, obj: Record<string, any> = {}) {
@@ -203,17 +232,31 @@ export class ProcessBasedTsServer {
203
232
  request.arguments = args;
204
233
  }
205
234
  const serializedRequest = `${JSON.stringify(request)}\n`;
206
- this.tsServerProcess.stdin?.write(serializedRequest);
235
+ this.tsServerProcess?.stdin?.write(serializedRequest);
207
236
  this.log(notification ? 'notify' : 'request', request);
208
237
  }
209
238
 
210
- protected processMessage(untrimmedMessageString: string): void {
239
+ protected processMessage(untrimmedMessageString: string, resolve?: () => void, reject?: (err) => void): void {
211
240
  const messageString = untrimmedMessageString.trim();
212
241
  if (!messageString || messageString.startsWith('Content-Length:')) {
213
242
  return;
214
243
  }
215
- const message: protocol.Message = JSON.parse(messageString);
244
+ let message: protocol.Message;
245
+
246
+ try {
247
+ message = JSON.parse(messageString);
248
+ } catch (error) {
249
+ // If the message isn't valid JSON, it's not a valid tsserver message. Reject the promise.
250
+ reject?.(new Error(`Received invalid message from TSServer: ${untrimmedMessageString}`));
251
+ return;
252
+ }
253
+
216
254
  this.log('processMessage', message);
255
+
256
+ if (this.isEvent(message)) {
257
+ resolve?.();
258
+ }
259
+
217
260
  if (this.isResponse(message)) {
218
261
  this.resolveResponse(message, message.request_seq, message.success);
219
262
  } else if (this.isEvent(message)) {
@@ -249,4 +292,10 @@ export class ProcessBasedTsServer {
249
292
  private isRequestCompletedEvent(message: protocol.Message): message is protocol.RequestCompletedEvent {
250
293
  return this.isEvent(message) && message.event === 'requestCompleted';
251
294
  }
295
+
296
+ private async ensureServerIsRunning() {
297
+ if (!this.tsServerProcess) {
298
+ await this.restart();
299
+ }
300
+ }
252
301
  }
@@ -20,8 +20,10 @@ export type TsserverClientOpts = {
20
20
  };
21
21
 
22
22
  export class TsserverClient {
23
- private tsServer: ProcessBasedTsServer;
23
+ private tsServer: ProcessBasedTsServer | null;
24
24
  public lastDiagnostics: protocol.DiagnosticEventBody[] = [];
25
+ private serverRunning = false;
26
+
25
27
  constructor(
26
28
  /**
27
29
  * absolute root path of the project.
@@ -41,19 +43,40 @@ export class TsserverClient {
41
43
  * this methods returns pretty fast. if checkTypes is enabled, it runs the process in the background and
42
44
  * doesn't wait for it.
43
45
  */
44
- init() {
45
- this.tsServer = new ProcessBasedTsServer({
46
- logger: this.logger,
47
- tsserverPath: this.findTsserverPath(),
48
- logToConsole: this.options.verbose,
49
- onEvent: this.onTsserverEvent.bind(this),
50
- });
51
- this.tsServer.start();
52
- if (this.files.length) {
53
- this.files.forEach((file) => this.open(file));
46
+ async init(): Promise<void> {
47
+ try {
48
+ this.tsServer = new ProcessBasedTsServer({
49
+ logger: this.logger,
50
+ tsserverPath: this.findTsserverPath(),
51
+ logToConsole: this.options.verbose,
52
+ onEvent: this.onTsserverEvent.bind(this),
53
+ });
54
+
55
+ this.tsServer
56
+ .start()
57
+ .then(() => {
58
+ this.serverRunning = true;
59
+ })
60
+ .catch((err) => {
61
+ this.logger.error('TsserverClient.init failed', err);
62
+ });
63
+
64
+ if (this.files.length) {
65
+ const openPromises = this.files.map((file) => this.open(file));
66
+ await Promise.all(openPromises.map((promise) => promise.catch((error) => error)));
67
+ const failedFiles = openPromises.filter((promise) => promise instanceof Error);
68
+ if (failedFiles.length > 0) {
69
+ this.logger.error('TsserverClient.init failed to open files:', failedFiles);
70
+ }
71
+ if (failedFiles.length > 0) {
72
+ this.logger.error('TsserverClient.init failed to open files:', failedFiles);
73
+ }
74
+ this.checkTypesIfNeeded();
75
+ }
76
+ this.logger.debug('TsserverClient.init completed');
77
+ } catch (err) {
78
+ this.logger.error('TsserverClient.init failed', err);
54
79
  }
55
- this.checkTypesIfNeeded();
56
- this.logger.debug('TsserverClient.init completed');
57
80
  }
58
81
 
59
82
  private checkTypesIfNeeded(files = this.files) {
@@ -93,7 +116,15 @@ export class TsserverClient {
93
116
  }
94
117
 
95
118
  killTsServer() {
96
- this.tsServer.kill();
119
+ if (this.tsServer && this.serverRunning) {
120
+ this.tsServer.kill();
121
+ this.tsServer = null;
122
+ this.serverRunning = false;
123
+ }
124
+ }
125
+
126
+ isServerRunning() {
127
+ return this.serverRunning;
97
128
  }
98
129
 
99
130
  /**
@@ -108,14 +139,14 @@ export class TsserverClient {
108
139
  */
109
140
  async getDiagnostic(files = this.files): Promise<any> {
110
141
  this.lastDiagnostics = [];
111
- return this.tsServer.request(CommandTypes.Geterr, { delay: 0, files });
142
+ return this.tsServer?.request(CommandTypes.Geterr, { delay: 0, files });
112
143
  }
113
144
 
114
145
  /**
115
146
  * avoid using this method, it takes longer than `getDiagnostic()` and shows errors from paths outside the project
116
147
  */
117
148
  async getDiagnosticAllProject(requestedByFile: string): Promise<any> {
118
- return this.tsServer.request(CommandTypes.GeterrForProject, { file: requestedByFile, delay: 0 });
149
+ return this.tsServer?.request(CommandTypes.GeterrForProject, { file: requestedByFile, delay: 0 });
119
150
  }
120
151
 
121
152
  /**
@@ -123,8 +154,8 @@ export class TsserverClient {
123
154
  */
124
155
  async getQuickInfo(file: string, position: Position): Promise<protocol.QuickInfoResponse | undefined> {
125
156
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
126
- this.openIfNeeded(absFile);
127
- return this.tsServer.request(CommandTypes.Quickinfo, {
157
+ await this.openIfNeeded(absFile);
158
+ return this.tsServer?.request(CommandTypes.Quickinfo, {
128
159
  file: absFile,
129
160
  line: position.line,
130
161
  offset: position.character,
@@ -136,8 +167,8 @@ export class TsserverClient {
136
167
  */
137
168
  async getTypeDefinition(file: string, position: Position): Promise<protocol.TypeDefinitionResponse | undefined> {
138
169
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
139
- this.openIfNeeded(absFile);
140
- return this.tsServer.request(CommandTypes.TypeDefinition, {
170
+ await this.openIfNeeded(absFile);
171
+ return this.tsServer?.request(CommandTypes.TypeDefinition, {
141
172
  file: absFile,
142
173
  line: position.line,
143
174
  offset: position.character,
@@ -146,14 +177,14 @@ export class TsserverClient {
146
177
 
147
178
  async getDefinition(file: string, position: Position) {
148
179
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
149
- this.openIfNeeded(absFile);
150
- const response = await this.tsServer.request(CommandTypes.Definition, {
180
+ await this.openIfNeeded(absFile);
181
+ const response = await this.tsServer?.request(CommandTypes.Definition, {
151
182
  file: absFile,
152
183
  line: position.line,
153
184
  offset: position.character,
154
185
  });
155
186
 
156
- if (!response.success) {
187
+ if (!response?.success) {
157
188
  // TODO: we need a function to handle responses properly here for all.
158
189
  return response;
159
190
  }
@@ -166,8 +197,8 @@ export class TsserverClient {
166
197
  */
167
198
  async getReferences(file: string, position: Position): Promise<protocol.ReferencesResponse | undefined> {
168
199
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
169
- this.openIfNeeded(absFile);
170
- return this.tsServer.request(CommandTypes.References, {
200
+ await this.openIfNeeded(absFile);
201
+ return this.tsServer?.request(CommandTypes.References, {
171
202
  file: absFile,
172
203
  line: position.line,
173
204
  offset: position.character,
@@ -179,9 +210,9 @@ export class TsserverClient {
179
210
  */
180
211
  async getSignatureHelp(file: string, position: Position): Promise<protocol.SignatureHelpResponse | undefined> {
181
212
  const absFile = this.convertFileToAbsoluteIfNeeded(file);
182
- this.openIfNeeded(absFile);
213
+ await this.openIfNeeded(absFile);
183
214
 
184
- return this.tsServer.request(CommandTypes.SignatureHelp, {
215
+ return this.tsServer?.request(CommandTypes.SignatureHelp, {
185
216
  file: absFile,
186
217
  line: position.line,
187
218
  offset: position.character,
@@ -191,30 +222,30 @@ export class TsserverClient {
191
222
  private async configure(
192
223
  configureArgs: protocol.ConfigureRequestArguments = {}
193
224
  ): Promise<protocol.ConfigureResponse | undefined> {
194
- return this.tsServer.request(CommandTypes.Configure, configureArgs);
225
+ return this.tsServer?.request(CommandTypes.Configure, configureArgs);
195
226
  }
196
227
 
197
228
  /**
198
229
  * ask tsserver to open a file if it was not opened before.
199
230
  * @param file absolute path of the file
200
231
  */
201
- openIfNeeded(file: string) {
232
+ async openIfNeeded(file: string) {
202
233
  if (this.files.includes(file)) {
203
234
  return;
204
235
  }
205
- this.open(file);
236
+ await this.open(file);
206
237
  this.files.push(file);
207
238
  }
208
239
 
209
- private open(file: string) {
210
- this.tsServer.notify(CommandTypes.Open, {
240
+ private async open(file: string) {
241
+ return this.tsServer?.notify(CommandTypes.Open, {
211
242
  file,
212
243
  projectRootPath: this.projectPath,
213
244
  });
214
245
  }
215
246
 
216
- close(file: string) {
217
- this.tsServer.notify(CommandTypes.Close, {
247
+ async close(file: string) {
248
+ await this.tsServer?.notify(CommandTypes.Close, {
218
249
  file,
219
250
  });
220
251
  this.files = this.files.filter((openFile) => openFile !== file);
@@ -228,7 +259,7 @@ export class TsserverClient {
228
259
  */
229
260
  async changed(file: string) {
230
261
  // tell tsserver that all content was removed
231
- this.tsServer.notify(CommandTypes.Change, {
262
+ await this.tsServer?.notify(CommandTypes.Change, {
232
263
  file,
233
264
  line: 1,
234
265
  offset: 1,
@@ -240,7 +271,7 @@ export class TsserverClient {
240
271
  const content = await fs.readFile(file, 'utf-8');
241
272
 
242
273
  // tell tsserver that all file content was added
243
- this.tsServer.notify(CommandTypes.Change, {
274
+ await this.tsServer?.notify(CommandTypes.Change, {
244
275
  file,
245
276
  line: 1,
246
277
  offset: 1,