@@ -5,6 +5,8 @@ import { getProjectDirname } from './getDirname.js';
5
5
import { Logger } from './logger.js' ;
6
6
import { getRuntimeExecutableForIde } from './utils/getRuntimeExecutableForIde.js' ;
7
7
8
+ const configName = 'Lambda Live Debugger' ;
9
+
8
10
const workspaceXmlPath = path . join (
9
11
getProjectDirname ( ) ,
10
12
'.idea' ,
@@ -69,58 +71,81 @@ async function writeWorkspaceXml(filePath: string, json: any) {
69
71
}
70
72
71
73
async 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
+ }
85
84
}
86
85
87
86
async 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
+ }
98
98
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
+ } ,
108
109
} ,
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 ] ;
114
141
}
115
142
116
- let runManager = json . project . component . find (
117
- ( c : any ) => c [ '@_name' ] === 'RunManager' ,
118
- ) ;
143
+ let runManager = components . find ( ( c : any ) => c [ '@_name' ] === 'RunManager' ) ;
119
144
120
145
if ( ! runManager ) {
121
146
Logger . verbose ( 'RunManager not found, creating new RunManager component' ) ;
122
147
runManager = { '@_name' : 'RunManager' , configuration : [ ] } ;
123
- json . project . component . push ( runManager ) ;
148
+ components . push ( runManager ) ;
124
149
}
125
150
126
151
let configurations ;
@@ -132,16 +157,10 @@ async function addConfiguration() {
132
157
configurations = runManager . configuration ;
133
158
}
134
159
135
- const exists = configurations . some (
136
- ( c : any ) => c [ '@_name' ] === config . configuration [ '@_name' ] ,
160
+ const lldConfigExists = configurations . some (
161
+ ( c : any ) => c [ '@_name' ] === configName ,
137
162
) ;
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 } ;
145
164
}
146
165
147
166
export const JetBrains = {
0 commit comments