Skip to content

Commit 0520e65

Browse files
authored
Merge pull request #22 from arduino/atl-972--remove-theia-folder-dependency
ATL-972: Can customize the `launch.json` location.
2 parents 537e98e + b97444c commit 0520e65

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/extension.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as fs from 'fs';
21
import * as path from 'path';
3-
import { promisify } from 'util';
2+
import { promises as fs } from 'fs';
43
import { spawnSync } from 'child_process';
54
import deepEqual from 'deep-equal';
65
import WebRequest from 'web-request';
@@ -35,6 +34,11 @@ interface DebugConfig {
3534
readonly name?: string;
3635
}
3736
readonly sketchPath: string;
37+
/**
38+
* Location where the `launch.config` will be created on the fly before starting every debug session.
39+
* If not defined, it falls back to `sketchPath/.vscode/launch.json`.
40+
*/
41+
readonly configPath?: string;
3842
}
3943

4044
interface DebugInfo {
@@ -140,13 +144,10 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
140144

141145
let customDebugConfig = {};
142146
try {
143-
const raw = await promisify(fs.readFile)(path.join(config.sketchPath, 'debug_custom.json'), { encoding: 'utf8' });
147+
const raw = await fs.readFile(path.join(config.sketchPath, 'debug_custom.json'), { encoding: 'utf8' });
144148
customDebugConfig = JSON.parse(raw);
145149
} catch { }
146150
const mergedDebugConfig = deepmerge(defaultDebugConfig, customDebugConfig);
147-
148-
// Create the `launch.json` if it does not exist. Otherwise, update the existing.
149-
const configuration = vscode.workspace.getConfiguration();
150151
const launchConfig = {
151152
version: '0.2.0',
152153
'configurations': [
@@ -155,7 +156,7 @@ async function startDebug(_: ExtensionContext, config: DebugConfig): Promise<boo
155156
}
156157
]
157158
};
158-
await configuration.update('launch', launchConfig, false);
159+
await updateLaunchConfig(config, launchConfig);
159160
return vscode.debug.startDebugging(undefined, mergedDebugConfig);
160161
}
161162

@@ -199,7 +200,7 @@ async function buildLanguageClient(config: LanguageServerConfig): Promise<Langua
199200
let logPath: string | undefined = undefined;
200201
if (typeof log === 'string') {
201202
try {
202-
const stats = await promisify(fs.stat)(log);
203+
const stats = await fs.stat(log);
203204
if (stats.isDirectory()) {
204205
logPath = log;
205206
}
@@ -248,3 +249,16 @@ async function buildLanguageClient(config: LanguageServerConfig): Promise<Langua
248249
}
249250
);
250251
}
252+
253+
/**
254+
* Instead of writing the `launch.json` to the workspace, the file is written to the temporary binary output location.
255+
*/
256+
async function updateLaunchConfig(debugConfig: DebugConfig, launchConfig: object): Promise<void> {
257+
if (debugConfig.configPath) {
258+
await fs.mkdir(debugConfig.configPath, { recursive: true });
259+
await fs.writeFile(path.join(debugConfig.configPath, 'launch.json'), JSON.stringify(launchConfig, null, 2));
260+
} else {
261+
const configuration = vscode.workspace.getConfiguration();
262+
await configuration.update('launch', launchConfig, false);
263+
}
264+
}

0 commit comments

Comments
 (0)