@@ -36,6 +36,7 @@ import {
36
36
normalizeFilePath ,
37
37
parcelSeverityToLspSeverity ,
38
38
} from './utils' ;
39
+ import type { FSWatcher } from 'fs' ;
39
40
40
41
const lookupPid : Query => Program [ ] = promisify ( ps . lookup ) ;
41
42
@@ -67,58 +68,103 @@ let bundleGraphDeferrable =
67
68
let bundleGraph : Promise < ?BundleGraph < PackagedBundle >> =
68
69
bundleGraphDeferrable . promise ;
69
70
70
- export default ( new Reporter ( {
71
- async report ( { event, options} ) {
72
- switch ( event . type ) {
73
- case 'watchStart' : {
74
- await fs . promises . mkdir ( BASEDIR , { recursive : true } ) ;
75
-
76
- // For each existing file, check if the pid matches a running process.
77
- // If no process matches, delete the file, assuming it was orphaned
78
- // by a process that quit unexpectedly.
79
- for ( let filename of fs . readdirSync ( BASEDIR ) ) {
80
- if ( filename . endsWith ( '.json' ) ) continue ;
81
- let pid = parseInt ( filename . slice ( 'parcel-' . length ) , 10 ) ;
82
- let resultList = await lookupPid ( { pid} ) ;
83
- if ( resultList . length > 0 ) continue ;
84
- fs . unlinkSync ( path . join ( BASEDIR , filename ) ) ;
85
- ignoreFail ( ( ) =>
86
- fs . unlinkSync ( path . join ( BASEDIR , filename + '.json' ) ) ,
87
- ) ;
88
- }
71
+ let watchStarted = false ;
72
+ let lspStarted = false ;
73
+ let watchStartPromise ;
89
74
90
- server = await createServer ( SOCKET_FILE , connection => {
91
- // console.log('got connection');
92
- connections . push ( connection ) ;
93
- connection . onClose ( ( ) => {
94
- connections = connections . filter ( c => c !== connection ) ;
95
- } ) ;
75
+ const LSP_SENTINEL_FILENAME = 'lsp-server' ;
76
+ const LSP_SENTINEL_FILE = path . join ( BASEDIR , LSP_SENTINEL_FILENAME ) ;
96
77
97
- connection . onRequest ( RequestDocumentDiagnostics , async uri => {
98
- let graph = await bundleGraph ;
99
- if ( ! graph ) return ;
78
+ async function watchLspActive ( ) : Promise < FSWatcher > {
79
+ // Check for lsp-server when reporter is first started
80
+ try {
81
+ await fs . promises . access ( LSP_SENTINEL_FILE , fs . constants . F_OK ) ;
82
+ lspStarted = true ;
83
+ } catch {
84
+ //
85
+ }
100
86
101
- return getDiagnosticsUnusedExports ( graph , uri ) ;
87
+ return fs . watch ( BASEDIR , ( eventType : string , filename : string ) => {
88
+ switch ( eventType ) {
89
+ case 'rename' :
90
+ if ( filename === LSP_SENTINEL_FILENAME ) {
91
+ fs . access ( LSP_SENTINEL_FILE , fs . constants . F_OK , err => {
92
+ if ( err ) {
93
+ lspStarted = false ;
94
+ } else {
95
+ lspStarted = true ;
96
+ }
102
97
} ) ;
98
+ }
99
+ }
100
+ } ) ;
101
+ }
103
102
104
- connection . onRequest ( RequestImporters , async params => {
105
- let graph = await bundleGraph ;
106
- if ( ! graph ) return null ;
103
+ async function doWatchStart ( ) {
104
+ await fs . promises . mkdir ( BASEDIR , { recursive : true } ) ;
105
+
106
+ // For each existing file, check if the pid matches a running process.
107
+ // If no process matches, delete the file, assuming it was orphaned
108
+ // by a process that quit unexpectedly.
109
+ for ( let filename of fs . readdirSync ( BASEDIR ) ) {
110
+ if ( filename . endsWith ( '.json' ) ) continue ;
111
+ let pid = parseInt ( filename . slice ( 'parcel-' . length ) , 10 ) ;
112
+ let resultList = await lookupPid ( { pid} ) ;
113
+ if ( resultList . length > 0 ) continue ;
114
+ fs . unlinkSync ( path . join ( BASEDIR , filename ) ) ;
115
+ ignoreFail ( ( ) => fs . unlinkSync ( path . join ( BASEDIR , filename + '.json' ) ) ) ;
116
+ }
107
117
108
- return getImporters ( graph , params ) ;
109
- } ) ;
118
+ server = await createServer ( SOCKET_FILE , connection => {
119
+ // console.log('got connection');
120
+ connections . push ( connection ) ;
121
+ connection . onClose ( ( ) => {
122
+ connections = connections . filter ( c => c !== connection ) ;
123
+ } ) ;
110
124
111
- sendDiagnostics ( ) ;
112
- } ) ;
113
- await fs . promises . writeFile (
114
- META_FILE ,
115
- JSON . stringify ( {
116
- projectRoot : options . projectRoot ,
117
- pid : process . pid ,
118
- argv : process . argv ,
119
- } ) ,
120
- ) ;
125
+ connection . onRequest ( RequestDocumentDiagnostics , async uri => {
126
+ let graph = await bundleGraph ;
127
+ if ( ! graph ) return ;
128
+
129
+ return getDiagnosticsUnusedExports ( graph , uri ) ;
130
+ } ) ;
131
+
132
+ connection . onRequest ( RequestImporters , async params => {
133
+ let graph = await bundleGraph ;
134
+ if ( ! graph ) return null ;
135
+
136
+ return getImporters ( graph , params ) ;
137
+ } ) ;
138
+
139
+ sendDiagnostics ( ) ;
140
+ } ) ;
141
+ await fs . promises . writeFile (
142
+ META_FILE ,
143
+ JSON . stringify ( {
144
+ projectRoot : process . cwd ( ) ,
145
+ pid : process . pid ,
146
+ argv : process . argv ,
147
+ } ) ,
148
+ ) ;
149
+ }
150
+
151
+ watchLspActive ( ) ;
121
152
153
+ export default ( new Reporter ( {
154
+ async report ( { event, options} ) {
155
+ if ( event . type === 'watchStart' ) {
156
+ watchStarted = true ;
157
+ }
158
+
159
+ if ( watchStarted && lspStarted ) {
160
+ if ( ! watchStartPromise ) {
161
+ watchStartPromise = doWatchStart ( ) ;
162
+ }
163
+ await watchStartPromise ;
164
+ }
165
+
166
+ switch ( event . type ) {
167
+ case 'watchStart' : {
122
168
break ;
123
169
}
124
170
0 commit comments