1
1
import { join } from 'path' ;
2
+ import { promises as fs } from 'fs' ;
2
3
import { inject , injectable , named } from '@theia/core/shared/inversify' ;
3
4
import { spawn , ChildProcess } from 'child_process' ;
4
5
import { FileUri } from '@theia/core/lib/node/file-uri' ;
@@ -142,9 +143,12 @@ export class ArduinoDaemonImpl
142
143
}
143
144
144
145
protected async getSpawnArgs ( ) : Promise < string [ ] > {
145
- const configDirUri = await this . envVariablesServer . getConfigDirUri ( ) ;
146
+ const [ configDirUri , debug ] = await Promise . all ( [
147
+ this . envVariablesServer . getConfigDirUri ( ) ,
148
+ this . debugDaemon ( ) ,
149
+ ] ) ;
146
150
const cliConfigPath = join ( FileUri . fsPath ( configDirUri ) , CLI_CONFIG ) ;
147
- return [
151
+ const args = [
148
152
'daemon' ,
149
153
'--format' ,
150
154
'jsonmini' ,
@@ -156,6 +160,41 @@ export class ArduinoDaemonImpl
156
160
'--log-format' ,
157
161
'json' ,
158
162
] ;
163
+ if ( debug ) {
164
+ args . push ( '--debug' ) ;
165
+ }
166
+ return args ;
167
+ }
168
+
169
+ private async debugDaemon ( ) : Promise < boolean > {
170
+ // Poor man's preferences on the backend. (https://github.com/arduino/arduino-ide/issues/1056#issuecomment-1153975064)
171
+ const configDirUri = await this . envVariablesServer . getConfigDirUri ( ) ;
172
+ const configDirPath = FileUri . fsPath ( configDirUri ) ;
173
+ try {
174
+ const raw = await fs . readFile ( join ( configDirPath , 'settings.json' ) , {
175
+ encoding : 'utf8' ,
176
+ } ) ;
177
+ const json = this . tryParse ( raw ) ;
178
+ if ( json ) {
179
+ const value = json [ 'arduino.cli.daemon.debug' ] ;
180
+ return typeof value === 'boolean' && ! ! value ;
181
+ }
182
+ return false ;
183
+ } catch ( error ) {
184
+ if ( 'code' in error && error . code === 'ENOENT' ) {
185
+ return false ;
186
+ }
187
+ throw error ;
188
+ }
189
+ }
190
+
191
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
192
+ private tryParse ( raw : string ) : any | undefined {
193
+ try {
194
+ return JSON . parse ( raw ) ;
195
+ } catch {
196
+ return undefined ;
197
+ }
159
198
}
160
199
161
200
protected async spawnDaemonProcess ( ) : Promise < {
0 commit comments