@@ -5,6 +5,8 @@ import { getProjectDirname } from './getDirname.js';
55import { Logger } from './logger.js' ;
66import { getRuntimeExecutableForIde } from './utils/getRuntimeExecutableForIde.js' ;
77
8+ const configName = 'Lambda Live Debugger' ;
9+
810const workspaceXmlPath = path . join (
911 getProjectDirname ( ) ,
1012 '.idea' ,
@@ -69,58 +71,81 @@ async function writeWorkspaceXml(filePath: string, json: any) {
6971}
7072
7173async function isConfigured ( ) {
72- const { json } = await readWorkspaceXml ( workspaceXmlPath ) ;
73- if ( ! json ) return false ;
74-
75- const components = Array . isArray ( json . project ?. component )
76- ? json . project . component
77- : [ json . project ?. component ] ;
78- const runManager = components . find ( ( c : any ) => c [ '@_name' ] === 'RunManager' ) ;
79- if ( ! runManager ) return false ;
80-
81- const configurations = runManager . configuration || [ ] ;
82- return configurations . some (
83- ( c : any ) => c [ '@_name' ] === 'Lambda Live Debugger' ,
84- ) ;
74+ try {
75+ const { json } = await readWorkspaceXml ( workspaceXmlPath ) ;
76+ if ( ! json ) return false ;
77+
78+ const { lldConfigExists } = extractWorkspaceData ( json ) ;
79+ return lldConfigExists ;
80+ } catch ( err ) {
81+ Logger . error ( 'Error checking JetBrains IDE configuration' , err ) ;
82+ return true ;
83+ }
8584}
8685
8786async function addConfiguration ( ) {
88- Logger . verbose ( 'Adding JetBrains IDE run/debug configuration' ) ;
89- const { json } = await readWorkspaceXml ( workspaceXmlPath ) ;
90- const config = await getJetBrainsLaunchConfig ( ) ;
91-
92- if ( ! config ) {
93- Logger . error (
94- 'Failed to configure JetBrains IDE. Cannot find a locally installed Lambda Live Debugger. The JetBrains IDE debugger cannot use a globally installed version.' ,
95- ) ;
96- return ;
97- }
87+ try {
88+ Logger . verbose ( 'Adding JetBrains IDE run/debug configuration' ) ;
89+ const { json } = await readWorkspaceXml ( workspaceXmlPath ) ;
90+ const config = await getJetBrainsLaunchConfig ( ) ;
91+
92+ if ( ! config ) {
93+ Logger . error (
94+ 'Failed to configure JetBrains IDE. Cannot find a locally installed Lambda Live Debugger. The JetBrains IDE debugger cannot use a globally installed version.' ,
95+ ) ;
96+ return ;
97+ }
9898
99- if ( ! json ) {
100- // Create new workspace.xml if it does not exist
101- const newJson = {
102- '?xml' : { '@_version' : '1.0' , '@_encoding' : 'UTF-8' } ,
103- project : {
104- '@_version' : '4' ,
105- component : {
106- '@_name' : 'RunManager' ,
107- configuration : [ config . configuration ] ,
99+ if ( ! json ) {
100+ // Create new workspace.xml if it does not exist
101+ const newJson = {
102+ '?xml' : { '@_version' : '1.0' , '@_encoding' : 'UTF-8' } ,
103+ project : {
104+ '@_version' : '4' ,
105+ component : {
106+ '@_name' : 'RunManager' ,
107+ configuration : [ config . configuration ] ,
108+ } ,
108109 } ,
109- } ,
110- } ;
111- await fs . mkdir ( path . dirname ( workspaceXmlPath ) , { recursive : true } ) ;
112- await writeWorkspaceXml ( workspaceXmlPath , newJson ) ;
113- return ;
110+ } ;
111+ await fs . mkdir ( path . dirname ( workspaceXmlPath ) , { recursive : true } ) ;
112+ await writeWorkspaceXml ( workspaceXmlPath , newJson ) ;
113+ return ;
114+ }
115+
116+ const { lldConfigExists, components, runManager, configurations } =
117+ extractWorkspaceData ( json ) ;
118+ json . project . component = components ;
119+
120+ if ( ! lldConfigExists ) {
121+ Logger . verbose ( 'Adding new configuration to workspace.xml' ) ;
122+ runManager . configuration = [ ...configurations , config . configuration ] ;
123+ await writeWorkspaceXml ( workspaceXmlPath , json ) ;
124+ } else {
125+ Logger . verbose ( 'Configuration already exists in workspace.xml' ) ;
126+ }
127+ } catch ( err ) {
128+ Logger . error ( 'Error adding JetBrains IDE configuration' , err ) ;
129+ }
130+ }
131+
132+ /**
133+ * Extract workspace data from JetBrains IDE configuration
134+ * @param json
135+ * @returns
136+ */
137+ function extractWorkspaceData ( json : any ) {
138+ let components = json . project . component ;
139+ if ( ! Array . isArray ( components ) ) {
140+ components = [ components ] ;
114141 }
115142
116- let runManager = json . project . component . find (
117- ( c : any ) => c [ '@_name' ] === 'RunManager' ,
118- ) ;
143+ let runManager = components . find ( ( c : any ) => c [ '@_name' ] === 'RunManager' ) ;
119144
120145 if ( ! runManager ) {
121146 Logger . verbose ( 'RunManager not found, creating new RunManager component' ) ;
122147 runManager = { '@_name' : 'RunManager' , configuration : [ ] } ;
123- json . project . component . push ( runManager ) ;
148+ components . push ( runManager ) ;
124149 }
125150
126151 let configurations ;
@@ -132,16 +157,10 @@ async function addConfiguration() {
132157 configurations = runManager . configuration ;
133158 }
134159
135- const exists = configurations . some (
136- ( c : any ) => c [ '@_name' ] === config . configuration [ '@_name' ] ,
160+ const lldConfigExists = configurations . some (
161+ ( c : any ) => c [ '@_name' ] === configName ,
137162 ) ;
138- if ( ! exists ) {
139- Logger . verbose ( 'Adding new configuration to workspace.xml' ) ;
140- runManager . configuration = [ ...configurations , config . configuration ] ;
141- await writeWorkspaceXml ( workspaceXmlPath , json ) ;
142- } else {
143- Logger . verbose ( 'Configuration already exists in workspace.xml' ) ;
144- }
163+ return { lldConfigExists, runManager, configurations, components } ;
145164}
146165
147166export const JetBrains = {
0 commit comments