11import * as fs from 'fs' ;
22import * as net from 'net' ;
3+ import type * as ts from 'typescript' ;
34import { collectExtractProps } from './requests/collectExtractProps' ;
45import { getComponentEvents , getComponentNames , getComponentProps , getElementAttrs , getTemplateContextProps } from './requests/componentInfos' ;
6+ import { containsFile } from './requests/containsFile' ;
57import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation' ;
68import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition' ;
7- import { pipeFile } from './utils' ;
9+ import { PipeTable , pipeTable } from './utils' ;
810
911export interface Request {
10- type : 'collectExtractProps'
12+ type : 'containsFile'
13+ | 'collectExtractProps'
1114 | 'getPropertiesAtLocation'
1215 | 'getQuickInfoAtPosition'
1316 // Component Infos
@@ -21,13 +24,23 @@ export interface Request {
2124
2225let started = false ;
2326
24- export function startNamedPipeServer ( ) {
27+ export function startNamedPipeServer ( serverKind : ts . server . ProjectKind ) {
28+
2529 if ( started ) return ;
2630 started = true ;
31+
32+ const pipeFile = process . platform === 'win32'
33+ ? `\\\\.\\pipe\\vue-tsp-${ process . pid } `
34+ : `/tmp/vue-tsp-${ process . pid } ` ;
2735 const server = net . createServer ( connection => {
2836 connection . on ( 'data' , data => {
29- const request : Request = JSON . parse ( data . toString ( ) ) ;
30- if ( request . type === 'collectExtractProps' ) {
37+ const text = data . toString ( ) ;
38+ const request : Request = JSON . parse ( text ) ;
39+ if ( request . type === 'containsFile' ) {
40+ const result = containsFile . apply ( null , request . args ) ;
41+ connection . write ( JSON . stringify ( result ?? null ) ) ;
42+ }
43+ else if ( request . type === 'collectExtractProps' ) {
3144 const result = collectExtractProps . apply ( null , request . args ) ;
3245 connection . write ( JSON . stringify ( result ?? null ) ) ;
3346 }
@@ -68,9 +81,42 @@ export function startNamedPipeServer() {
6881 connection . on ( 'error' , err => console . error ( '[Vue Named Pipe Server]' , err . message ) ) ;
6982 } ) ;
7083
84+ clearupPipeTable ( ) ;
85+
86+ if ( ! fs . existsSync ( pipeTable ) ) {
87+ fs . writeFileSync ( pipeTable , JSON . stringify ( { } ) ) ;
88+ }
89+ const table : PipeTable = JSON . parse ( fs . readFileSync ( pipeTable , 'utf8' ) ) ;
90+ table [ process . pid ] = {
91+ pid : process . pid ,
92+ pipeFile,
93+ serverKind,
94+ } ;
95+ fs . writeFileSync ( pipeTable , JSON . stringify ( table , undefined , 2 ) ) ;
96+
7197 try {
7298 fs . unlinkSync ( pipeFile ) ;
7399 } catch { }
74100
75101 server . listen ( pipeFile ) ;
76102}
103+
104+ function clearupPipeTable ( ) {
105+ if ( fs . existsSync ( pipeTable ) ) {
106+ const table : PipeTable = JSON . parse ( fs . readFileSync ( pipeTable , 'utf8' ) ) ;
107+ for ( const pid in table ) {
108+ const { pipeFile } = table [ pid ] ;
109+ try {
110+ const client = net . connect ( pipeFile ) ;
111+ client . on ( 'connect' , ( ) => {
112+ client . end ( ) ;
113+ } ) ;
114+ client . on ( 'error' , ( ) => {
115+ const table = JSON . parse ( fs . readFileSync ( pipeTable , 'utf8' ) ) ;
116+ delete table [ pid ] ;
117+ fs . writeFileSync ( pipeTable , JSON . stringify ( table , undefined , 2 ) ) ;
118+ } ) ;
119+ } catch { }
120+ }
121+ }
122+ }
0 commit comments