-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.ts
140 lines (127 loc) · 3.61 KB
/
lib.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import child_process = require("child_process");
import { launchBackgroundProcess } from "./background-worker";
import * as inspector from "inspector";
export interface EasyAttachArgs {
/**
* Sets a label for the debug target.
* Defaults to `undefined`..
*/
label?: string;
/**
* If enabled, it does not break after attaching the debugger.
* Defaults to `false`.
*/
continue?: boolean;
/**
* Specifies the port to use for the debug port.
* Use `preconfigured` when the debugger was already launched.
* Defaults to `random`;
*/
debugPort?: DebugPortConfig;
/**
* Specifies the port to use for the debug proxy.
* This is usefull if you want to forward this port.
* Defaults to `random`;
*/
debugProxyPort?: PortConfig;
/**
* Use this option when the debug proxy does not recognize connection attempts and does not close automatically. Defaults to `false`.
*/
eagerExitDebugProxy?: boolean;
/**
* Print logs from background worker. Defaults to `false`.
*/
logBackgroundWorker?: boolean;
/**
* Use this option to control whether the UI is shown.
* If only the VS Code Extension is used, disabling the UI speeds up the auto attach feature.
* Defaults to `true`.
*/
showUI?: boolean;
}
export type PortConfig = "random" | number | number[];
export type DebugPortConfig = PortConfig | "preconfigured";
let first = true;
export function debugProcessAndWait(args?: EasyAttachArgs): boolean {
if (!first) {
return false;
}
first = false;
let label = undefined;
let debugPortConfig: DebugPortConfig = "random";
let debugProxyPortConfig: PortConfig = "random";
let eagerExitDebugProxy = false;
let log = false;
let showUi = true;
let shouldContinue = false;
if (args) {
label = args.label;
debugPortConfig = args.debugPort || "random";
if (args.eagerExitDebugProxy !== undefined) {
eagerExitDebugProxy = args.eagerExitDebugProxy;
}
if (args.logBackgroundWorker !== undefined) {
log = args.logBackgroundWorker;
}
if (args.debugProxyPort !== undefined) {
debugProxyPortConfig = args.debugProxyPort;
}
if (args.showUI !== undefined) {
showUi = args.showUI;
}
if (args.continue !== undefined) {
shouldContinue = args.continue;
}
}
const { debugPort } = initializeDebugPort(debugPortConfig);
launchBackgroundProcess({
debugPort,
label,
log,
eagerExitDebugProxy,
debugProxyPortConfig,
showUi,
shouldContinue,
});
// Wait for debugger to connect.
inspector.open(undefined, undefined, true);
return true;
}
let debugPort: number | undefined = undefined;
function initializeDebugPort(
portConfig: DebugPortConfig
): { debugPort: number } {
// use a random port for debugPort to prevent port clashes.
if (!debugPort) {
if (portConfig === "preconfigured") {
debugPort = process.debugPort;
} else {
if (portConfig === "random") {
debugPort = getRandomPortSync();
} else if (typeof portConfig === "number") {
debugPort = portConfig;
} else {
debugPort = getRandomPortSync(portConfig);
}
process.debugPort = debugPort;
}
}
return { debugPort };
}
function getRandomPortSync(allowedPorts?: number[]): number {
let options = "";
if (allowedPorts !== undefined) {
options = `{ port: [${allowedPorts
.map(p => (+p).toString())
.join(",")}] }`;
}
// we must use execSync as get-port is async.
const portStr = child_process.execSync(
`node -e "require('get-port')(${options}).then(p => console.log(p))"`,
// Set cwd so that node_modules can be found.
{ cwd: __dirname, encoding: "utf8" }
);
const port = parseInt(portStr);
// It could be that `port` is not from `allowedPorts` as they are only preferences.
return port;
}