Skip to content

Commit c6d8b18

Browse files
Fixes
1 parent 1ee1cd0 commit c6d8b18

File tree

4 files changed

+153
-137
lines changed

4 files changed

+153
-137
lines changed

src/gitignore.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function doesExistInGitIgnore() {
1414
'utf-8',
1515
);
1616
// split by new line
17-
const lines = gitignoreContent.split('\n');
17+
const lines = gitignoreContent.split('\n').map((line) => line.trim());
1818
// check if ".lldebugger" exists
1919
const exists = lines.includes(outputFolder);
2020
return exists;
@@ -37,20 +37,24 @@ function getGitIgnoreFileLocation() {
3737
*/
3838
async function addToGitIgnore() {
3939
Logger.log(`Adding ${outputFolder} to .gitignore.`);
40-
const exists = await doesExistInGitIgnore();
41-
if (!exists) {
42-
// does file exist?
43-
try {
44-
await fs.access(getGitIgnoreFileLocation());
45-
} catch {
46-
await fs.writeFile(getGitIgnoreFileLocation(), `${outputFolder}\n`);
47-
return;
48-
}
40+
try {
41+
const exists = await doesExistInGitIgnore();
42+
if (!exists) {
43+
// does file exist?
44+
try {
45+
await fs.access(getGitIgnoreFileLocation());
46+
} catch {
47+
await fs.writeFile(getGitIgnoreFileLocation(), `${outputFolder}\n`);
48+
return;
49+
}
4950

50-
// append to existing file
51-
await fs.appendFile(getGitIgnoreFileLocation(), `\n${outputFolder}\n`);
52-
} else {
53-
Logger.log(`${outputFolder} already exists in .gitignore`);
51+
// append to existing file
52+
await fs.appendFile(getGitIgnoreFileLocation(), `\n${outputFolder}\n`);
53+
} else {
54+
Logger.log(`${outputFolder} already exists in .gitignore`);
55+
}
56+
} catch (err) {
57+
Logger.error('Error adding to .gitignore', err);
5458
}
5559
}
5660

src/jetBrains.ts

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { getProjectDirname } from './getDirname.js';
55
import { Logger } from './logger.js';
66
import { getRuntimeExecutableForIde } from './utils/getRuntimeExecutableForIde.js';
77

8+
const configName = 'Lambda Live Debugger';
9+
810
const workspaceXmlPath = path.join(
911
getProjectDirname(),
1012
'.idea',
@@ -69,58 +71,81 @@ async function writeWorkspaceXml(filePath: string, json: any) {
6971
}
7072

7173
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+
}
8584
}
8685

8786
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+
}
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

147166
export const JetBrains = {

src/utils/getRuntimeExecutableForIde.ts

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
import fs from 'fs/promises';
22
import path from 'path';
3-
import { getModuleDirname, getProjectDirname } from '../getDirname.js';
3+
import { getProjectDirname } from '../getDirname.js';
44
import { Logger } from '../logger.js';
5+
import { exec } from 'child_process';
6+
import { promisify } from 'util';
7+
import os from 'os';
8+
9+
const execAsync = promisify(exec);
10+
11+
async function findGlobalPackagePath(packageName: string) {
12+
try {
13+
const command =
14+
os.platform() === 'win32'
15+
? `where ${packageName}`
16+
: `which ${packageName}`;
17+
const { stdout } = await execAsync(command);
18+
19+
const path = stdout?.trim().split('\n')[0];
20+
if (path) {
21+
console.log(
22+
`Global installation path for ${packageName}: ${path.trim()}`,
23+
);
24+
}
25+
return undefined;
26+
} catch (error) {
27+
Logger.verbose(`Error finding package ${packageName}`, error);
28+
return undefined;
29+
}
30+
}
531

632
/**
733
* Get the runtime executable for the IDE, like WebStorm or VSCode
@@ -11,7 +37,6 @@ export async function getRuntimeExecutableForIde(allowGlobal = true) {
1137
let runtimeExecutable: string | undefined;
1238
const localRuntimeExecutable = '${workspaceFolder}/node_modules/.bin/lld';
1339

14-
const moduleDirname = getModuleDirname();
1540
const projectDirname = getProjectDirname();
1641

1742
const localFolder = path.resolve(
@@ -35,53 +60,9 @@ export async function getRuntimeExecutableForIde(allowGlobal = true) {
3560
Logger.verbose('Lambda Live Debugger is installed globally');
3661

3762
if (allowGlobal) {
38-
Logger.verbose(
39-
`Setting absolute path for runtimeExecutable setting for VsCode configuration`,
40-
);
41-
const localFolderSubfolder = path.resolve('node_modules/.bin/lld');
42-
const globalModule1 = path.join(moduleDirname, '..', '..', '.bin/lld');
43-
const globalModule2 = path.join(moduleDirname, '..', '..', 'bin/lld');
44-
const globalModule3 = path.join(
45-
moduleDirname,
46-
'..',
47-
'..',
48-
'..',
49-
'..',
50-
'bin/lld',
51-
);
52-
const possibleFolders = {
53-
[localFolder]: '${workspaceFolder}/node_modules/.bin/lld',
54-
[localFolderSubfolder]: localFolderSubfolder,
55-
[globalModule1]: globalModule1,
56-
[globalModule2]: globalModule2,
57-
[globalModule3]: globalModule3,
58-
};
59-
60-
Logger.verbose(
61-
`Checking the following possible folders for lld executable:`,
62-
JSON.stringify(possibleFolders, null, 2),
63-
);
64-
65-
// check each possible folder and set the runtimeExecutable
66-
for (const folder in possibleFolders) {
67-
try {
68-
//Logger.log("Checking folder", folder);
69-
await fs.access(folder, fs.constants.F_OK);
70-
runtimeExecutable = possibleFolders[folder];
71-
Logger.verbose(`Found folder with lld executable: ${folder}`);
72-
break;
73-
} catch {
74-
// Not found
75-
}
76-
}
77-
78-
if (!runtimeExecutable) {
79-
Logger.error(
80-
`Could not find lld executable. Please check your IDE debugger settings.`,
81-
);
82-
}
63+
runtimeExecutable = await findGlobalPackagePath('lld');
8364
} else {
84-
return null;
65+
return undefined;
8566
}
8667
}
8768

src/vsCode.ts

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -150,40 +150,52 @@ async function getCurrentState(): Promise<
150150
}
151151

152152
async function isConfigured() {
153-
const state = await getCurrentState();
153+
try {
154+
const state = await getCurrentState();
155+
156+
if (state.state === 'FILE_EXISTS_CONFIGURATION_EXISTS') {
157+
return true;
158+
}
154159

155-
if (state.state === 'FILE_EXISTS_CONFIGURATION_EXISTS') {
160+
return false;
161+
} catch (err) {
162+
Logger.error(
163+
'Error checking if configuration exists in .vscode/launch.json',
164+
err,
165+
);
156166
return true;
157167
}
158-
159-
return false;
160168
}
161169

162170
async function addConfiguration() {
163-
Logger.log('Adding configuration to .vscode/launch.json');
164-
const state = await getCurrentState();
171+
try {
172+
Logger.log('Adding configuration to .vscode/launch.json');
173+
const state = await getCurrentState();
165174

166-
const config = await getVsCodeLaunchConfig();
175+
const config = await getVsCodeLaunchConfig();
167176

168-
if (state.state === 'FILE_EXISTS_CONFIGURATION_DOES_NOT_EXIST') {
169-
const { jsonString, filePath, configurationsLength } = state;
177+
if (state.state === 'FILE_EXISTS_CONFIGURATION_DOES_NOT_EXIST') {
178+
const { jsonString, filePath, configurationsLength } = state;
170179

171-
await writeConfiguration(
172-
filePath,
173-
jsonString,
174-
config.configurations![0],
175-
configurationsLength,
176-
);
177-
} else if (state.state === 'FILE_DOES_NOT_EXIST') {
178-
// crete folder of filePath recursive if not exists
179-
await fs.mkdir(path.dirname(state.filePath), { recursive: true });
180-
181-
Logger.verbose(`Creating VsCode configuration file: ${state.filePath}`);
182-
await fs.writeFile(
183-
state.filePath,
184-
JSON.stringify(config, null, 2),
185-
'utf-8',
186-
);
180+
await writeConfiguration(
181+
filePath,
182+
jsonString,
183+
config.configurations![0],
184+
configurationsLength,
185+
);
186+
} else if (state.state === 'FILE_DOES_NOT_EXIST') {
187+
// crete folder of filePath recursive if not exists
188+
await fs.mkdir(path.dirname(state.filePath), { recursive: true });
189+
190+
Logger.verbose(`Creating VsCode configuration file: ${state.filePath}`);
191+
await fs.writeFile(
192+
state.filePath,
193+
JSON.stringify(config, null, 2),
194+
'utf-8',
195+
);
196+
}
197+
} catch (err) {
198+
Logger.error('Error adding configuration to .vscode/launch.json', err);
187199
}
188200
}
189201

0 commit comments

Comments
 (0)