-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
server.ts
154 lines (143 loc) · 4.96 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 { getPropertiesAtLocation } from './requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition';
import { NamedPipeServer, connect, pipeTable } from './utils';
import type { FileRegistry, VueCompilerOptions } from '@vue/language-core';
export interface Request {
type: 'containsFile'
| 'collectExtractProps'
| 'getPropertiesAtLocation'
| 'getQuickInfoAtPosition'
// Component Infos
| 'getComponentProps'
| 'getComponentEvents'
| 'getTemplateContextProps'
| 'getComponentNames'
| 'getElementAttrs';
args: [fileName: string, ...rest: any];
}
let started = false;
export function startNamedPipeServer(
ts: typeof import('typescript'),
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);
const fileName = request.args[0];
const project = getProject(fileName);
if (project) {
const requestContext = {
typescript: ts,
languageService: project.info.languageService,
files: project.files,
vueOptions: project.vueOptions,
isTsPlugin: true,
getFileId: (fileName: string) => fileName,
};
if (request.type === 'containsFile') {
const result = !!getProject(fileName);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'collectExtractProps') {
const result = collectExtractProps.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getPropertiesAtLocation') {
const result = getPropertiesAtLocation.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getQuickInfoAtPosition') {
const result = getQuickInfoAtPosition.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
// Component Infos
else if (request.type === 'getComponentProps') {
const result = getComponentProps.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getComponentEvents') {
const result = getComponentEvents.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getTemplateContextProps') {
const result = getTemplateContextProps.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getComponentNames') {
const result = getComponentNames.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getElementAttrs') {
const result = getElementAttrs.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else {
console.warn('[Vue Named Pipe Server] Unknown request type:', request.type);
}
}
else {
console.warn('[Vue Named Pipe Server] No project found for:', fileName);
}
connection.end();
});
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));
}
});
}
}
export const projects = new Map<ts.server.Project, {
info: ts.server.PluginCreateInfo;
files: FileRegistry;
vueOptions: VueCompilerOptions;
}>();
function getProject(fileName: string) {
for (const [project, data] of projects) {
if (project.containsFile(fileName as ts.server.NormalizedPath)) {
return data;
}
}
}