forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
120 lines (110 loc) · 3.98 KB
/
server.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import * as fs from 'fs';
import * as net from 'net';
import type * as ts from 'typescript';
import { collectExtractProps } from './requests/collectExtractProps';
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos';
import { containsFile } from './requests/containsFile';
import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition';
import { NamedPipeServer, connect, pipeTable } from './utils';
export interface Request {
type: 'containsFile'
| 'collectExtractProps'
| 'getPropertiesAtLocation'
| 'getQuickInfoAtPosition'
// Component Infos
| 'getComponentProps'
| 'getComponentEvents'
| 'getTemplateContextProps'
| 'getComponentNames'
| 'getElementAttrs';
args: any;
}
let started = false;
export function startNamedPipeServer(serverKind: ts.server.ProjectKind, currentDirectory: string) {
if (started) return;
started = true;
const pipeFile = process.platform === 'win32'
? `\\\\.\\pipe\\vue-tsp-${process.pid}`
: `/tmp/vue-tsp-${process.pid}`;
const server = net.createServer(connection => {
connection.on('data', data => {
const text = data.toString();
const request: Request = JSON.parse(text);
if (request.type === 'containsFile') {
const result = containsFile.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'collectExtractProps') {
const result = collectExtractProps.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getPropertiesAtLocation') {
const result = getPropertiesAtLocation.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getQuickInfoAtPosition') {
const result = getQuickInfoAtPosition.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
// Component Infos
else if (request.type === 'getComponentProps') {
const result = getComponentProps.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getComponentEvents') {
const result = getComponentEvents.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getTemplateContextProps') {
const result = getTemplateContextProps.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getComponentNames') {
const result = getComponentNames.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getElementAttrs') {
const result = getElementAttrs.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else {
console.warn('[Vue Named Pipe Server] Unknown request type:', request.type);
connection.write(JSON.stringify(null));
}
});
connection.on('error', err => console.error('[Vue Named Pipe Server]', err.message));
});
cleanupPipeTable();
if (!fs.existsSync(pipeTable)) {
fs.writeFileSync(pipeTable, JSON.stringify([] satisfies NamedPipeServer[]));
}
const table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
table.push({
path: pipeFile,
serverKind,
currentDirectory,
});
fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2));
try {
fs.unlinkSync(pipeFile);
} catch { }
server.listen(pipeFile);
}
function cleanupPipeTable() {
if (!fs.existsSync(pipeTable)) {
return;
}
for (const server of JSON.parse(fs.readFileSync(pipeTable, 'utf8'))) {
connect(server.path).then(client => {
if (client) {
client.end();
}
else {
let table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
table = table.filter(item => item.path !== server.path);
fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2));
}
});
}
}