-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathtsservers.mts
More file actions
82 lines (64 loc) · 1.88 KB
/
tsservers.mts
File metadata and controls
82 lines (64 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { spawn } from 'child_process';
import { TsServer } from './tsserver.mjs';
import Path from 'node:path';
/**
* This object is responsible for managing multiple tsserver instances.
*/
export class TsServers {
private servers: Record<string, TsServer> = {};
get(id: string) {
const server = this.servers[id];
if (!server) {
throw new Error(`tsserver for ${id} does not exist.`);
}
return server;
}
set(id: string, server: TsServer) {
if (this.servers[id]) {
throw new Error(`tsserver for ${id} already exists.`);
}
this.servers[id] = server;
}
has(id: string) {
return this.servers[id] !== undefined;
}
del(id: string) {
delete this.servers[id];
}
create(id: string, options: { cwd: string }) {
if (this.has(id)) {
throw new Error(`tsserver for ${id} already exists.`);
}
// This is using the TypeScript dependency in the user's Srcbook.
//
// Note: If a user creates a typescript Srcbook, when it is first
// created, the dependencies are not installed and thus this will
// shut down immediately. Make sure that we handle this case after
// package.json has finished installing its deps.
const tsserverPath = Path.resolve(
options.cwd,
'node_modules',
'bin',
process.platform === 'win32' ? 'tsserver.cmd' : 'tsserver',
);
const child = spawn(tsserverPath, [], {
cwd: options.cwd,
shell: true,
});
const server = new TsServer(child);
this.set(id, server);
child.on('exit', () => {
this.del(id);
});
return server;
}
shutdown(id: string) {
if (!this.has(id)) {
console.warn(`tsserver for ${id} does not exist. Skipping shutdown.`);
return;
}
// The server is removed from this.servers in the
// process exit handler which covers all exit cases.
return this.get(id).shutdown();
}
}