11import { join } from 'path' ;
2+ import { promises as fs } from 'fs' ;
23import { inject , injectable , named } from '@theia/core/shared/inversify' ;
34import { spawn , ChildProcess } from 'child_process' ;
45import { FileUri } from '@theia/core/lib/node/file-uri' ;
@@ -142,9 +143,12 @@ export class ArduinoDaemonImpl
142143 }
143144
144145 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+ ] ) ;
146150 const cliConfigPath = join ( FileUri . fsPath ( configDirUri ) , CLI_CONFIG ) ;
147- return [
151+ const args = [
148152 'daemon' ,
149153 '--format' ,
150154 'jsonmini' ,
@@ -156,6 +160,41 @@ export class ArduinoDaemonImpl
156160 '--log-format' ,
157161 'json' ,
158162 ] ;
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+ }
159198 }
160199
161200 protected async spawnDaemonProcess ( ) : Promise < {
0 commit comments