forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceMapSupport.ts
More file actions
85 lines (80 loc) · 3.1 KB
/
sourceMapSupport.ts
File metadata and controls
85 lines (80 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as path from 'path';
import { WorkspaceConfiguration } from 'vscode';
import './common/extensions';
import { traceError } from './common/logger';
import { FileSystem } from './common/platform/fileSystem';
import { EXTENSION_ROOT_DIR } from './constants';
type VSCode = typeof import('vscode');
const setting = 'sourceMapsEnabled';
export class SourceMapSupport {
private readonly config: WorkspaceConfiguration;
constructor(private readonly vscode: VSCode) {
this.config = this.vscode.workspace.getConfiguration('python.diagnostics', null);
}
public async initialize(): Promise<void> {
if (!this.enabled) {
return;
}
await this.enableSourceMaps(true);
require('source-map-support').install();
const localize = require('./common/utils/localize') as typeof import('./common/utils/localize');
const disable = localize.Diagnostics.disableSourceMaps();
this.vscode.window.showWarningMessage(localize.Diagnostics.warnSourceMaps(), disable).then((selection) => {
if (selection === disable) {
this.disable().ignoreErrors();
}
});
}
public get enabled(): boolean {
return this.config.get<boolean>(setting, false);
}
public async disable(): Promise<void> {
if (this.enabled) {
await this.config.update(setting, false, this.vscode.ConfigurationTarget.Global);
}
await this.enableSourceMaps(false);
}
protected async enableSourceMaps(enable: boolean) {
const extensionSourceFile = path.join(EXTENSION_ROOT_DIR, 'out', 'client', 'extension.js');
const debuggerSourceFile = path.join(
EXTENSION_ROOT_DIR,
'out',
'client',
'debugger',
'debugAdapter',
'main.js',
);
await Promise.all([
this.enableSourceMap(enable, extensionSourceFile),
this.enableSourceMap(enable, debuggerSourceFile),
]);
}
protected async enableSourceMap(enable: boolean, sourceFile: string) {
const sourceMapFile = `${sourceFile}.map`;
const disabledSourceMapFile = `${sourceFile}.map.disabled`;
if (enable) {
await this.rename(disabledSourceMapFile, sourceMapFile);
} else {
await this.rename(sourceMapFile, disabledSourceMapFile);
}
}
protected async rename(sourceFile: string, targetFile: string) {
const fs = new FileSystem();
if (await fs.fileExists(targetFile)) {
return;
}
await fs.move(sourceFile, targetFile);
}
}
export function initialize(vscode: VSCode = require('vscode')) {
if (!vscode.workspace.getConfiguration('python.diagnostics', null).get('sourceMapsEnabled', false)) {
new SourceMapSupport(vscode).disable().ignoreErrors();
return;
}
new SourceMapSupport(vscode).initialize().catch((_ex) => {
traceError('Failed to initialize source map support in extension');
});
}