1 | 'use strict';
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | const fs = require('fs-extra');
|
11 | const path = require('path');
|
12 | const { red, yellow } = require('colors');
|
13 | const debug = require('debug')('fun:local');
|
14 | const _ = require('lodash');
|
15 | const ip = require('ip');
|
16 | const IDE_VSCODE = 'vscode';
|
17 | const IDE_PYCHARM = 'pycharm';
|
18 | const allowedDebugIdes = [IDE_VSCODE, IDE_PYCHARM];
|
19 | function getDebugIde(options = {}) {
|
20 | if (options.config) {
|
21 | const debugIde = options.config;
|
22 | const ide = _.find(allowedDebugIdes, (allowedDebugIde) => {
|
23 | if (allowedDebugIde === debugIde.toLowerCase()) {
|
24 | return allowedDebugIde;
|
25 | }
|
26 | });
|
27 | if (!ide) {
|
28 | throw new Error(red(`Error parsing debug config. Option is one of: ${JSON.stringify(allowedDebugIdes)}`));
|
29 | }
|
30 | return ide;
|
31 | }
|
32 | return null;
|
33 | }
|
34 | function getDebugPort(options) {
|
35 | let debugPort = options.debugPort;
|
36 | if (debugPort) {
|
37 | debugPort = parseInt(debugPort);
|
38 | if (Number.isNaN(debugPort)) {
|
39 | throw Error(red('debugPort must be number'));
|
40 | }
|
41 | }
|
42 | debug(`debugPort: ${debugPort}`);
|
43 | return debugPort;
|
44 | }
|
45 | function generateVscodeDebugConfig(serviceName, functionName, runtime, codePath, debugPort) {
|
46 | return __awaiter(this, void 0, void 0, function* () {
|
47 | const stats = yield fs.lstat(codePath);
|
48 | if (!stats.isDirectory()) {
|
49 | codePath = path.dirname(codePath);
|
50 | }
|
51 | switch (runtime) {
|
52 | case 'nodejs6':
|
53 | return {
|
54 | 'version': '0.2.0',
|
55 | 'configurations': [
|
56 | {
|
57 | 'name': `fc/${serviceName}/${functionName}`,
|
58 | 'type': 'node',
|
59 | 'request': 'attach',
|
60 | 'address': 'localhost',
|
61 | 'port': debugPort,
|
62 | 'localRoot': `${codePath}`,
|
63 | 'remoteRoot': '/code',
|
64 | 'protocol': 'legacy',
|
65 | 'stopOnEntry': false
|
66 | }
|
67 | ]
|
68 | };
|
69 | case 'nodejs12':
|
70 | case 'nodejs10':
|
71 | case 'nodejs8':
|
72 | return {
|
73 | 'version': '0.2.0',
|
74 | 'configurations': [
|
75 | {
|
76 | 'name': `fc/${serviceName}/${functionName}`,
|
77 | 'type': 'node',
|
78 | 'request': 'attach',
|
79 | 'address': 'localhost',
|
80 | 'port': debugPort,
|
81 | 'localRoot': `${codePath}`,
|
82 | 'remoteRoot': '/code',
|
83 | 'protocol': 'inspector',
|
84 | 'stopOnEntry': false
|
85 | }
|
86 | ]
|
87 | };
|
88 | case 'python3':
|
89 | case 'python2.7':
|
90 | return {
|
91 | 'version': '0.2.0',
|
92 | 'configurations': [
|
93 | {
|
94 | 'name': `fc/${serviceName}/${functionName}`,
|
95 | 'type': 'python',
|
96 | 'request': 'attach',
|
97 | 'host': 'localhost',
|
98 | 'port': debugPort,
|
99 | 'pathMappings': [
|
100 | {
|
101 | 'localRoot': `${codePath}`,
|
102 | 'remoteRoot': '/code'
|
103 | }
|
104 | ]
|
105 | }
|
106 | ]
|
107 | };
|
108 | case 'java8':
|
109 | return {
|
110 | 'version': '0.2.0',
|
111 | 'configurations': [
|
112 | {
|
113 | 'name': `fc/${serviceName}/${functionName}`,
|
114 | 'type': 'java',
|
115 | 'request': 'attach',
|
116 | 'hostName': 'localhost',
|
117 | 'port': debugPort
|
118 | }
|
119 | ]
|
120 | };
|
121 | case 'php7.2':
|
122 | return {
|
123 | 'version': '0.2.0',
|
124 | 'configurations': [
|
125 | {
|
126 | 'name': `fc/${serviceName}/${functionName}`,
|
127 | 'type': 'php',
|
128 | 'request': 'launch',
|
129 | 'port': debugPort,
|
130 | 'stopOnEntry': false,
|
131 | 'pathMappings': {
|
132 | '/code': `${codePath}`
|
133 | },
|
134 | 'ignore': [
|
135 | '/var/fc/runtime/**'
|
136 | ]
|
137 | }
|
138 | ]
|
139 | };
|
140 | case 'dotnetcore2.1':
|
141 | return {
|
142 | 'version': '0.2.0',
|
143 | 'configurations': [
|
144 | {
|
145 | 'name': `fc/${serviceName}/${functionName}`,
|
146 | 'type': 'coreclr',
|
147 | 'request': 'attach',
|
148 | 'processName': 'dotnet',
|
149 | 'pipeTransport': {
|
150 | 'pipeProgram': 'sh',
|
151 | 'pipeArgs': [
|
152 | '-c',
|
153 | `docker exec -i $(docker ps -q -f publish=${debugPort}) \${debuggerCommand}`
|
154 | ],
|
155 | 'debuggerPath': '/vsdbg/vsdbg',
|
156 | 'pipeCwd': '${workspaceFolder}'
|
157 | },
|
158 | 'windows': {
|
159 | 'pipeTransport': {
|
160 | 'pipeProgram': 'powershell',
|
161 | 'pipeArgs': [
|
162 | '-c',
|
163 | `docker exec -i $(docker ps -q -f publish=${debugPort}) \${debuggerCommand}`
|
164 | ],
|
165 | 'debuggerPath': '/vsdbg/vsdbg',
|
166 | 'pipeCwd': '${workspaceFolder}'
|
167 | }
|
168 | },
|
169 | 'sourceFileMap': {
|
170 | '/code': codePath
|
171 | }
|
172 | }
|
173 | ]
|
174 | };
|
175 | default:
|
176 | break;
|
177 | }
|
178 | debug('CodePath: ' + codePath);
|
179 | });
|
180 | }
|
181 | function generateDebugEnv(runtime, debugPort, debugIde) {
|
182 | const remoteIp = ip.address();
|
183 | switch (runtime) {
|
184 | case 'nodejs12':
|
185 | case 'nodejs10':
|
186 | case 'nodejs8':
|
187 | return { 'DEBUG_OPTIONS': `--inspect-brk=0.0.0.0:${debugPort}` };
|
188 | case 'nodejs6':
|
189 | return { 'DEBUG_OPTIONS': `--debug-brk=${debugPort}` };
|
190 | case 'python2.7':
|
191 | case 'python3':
|
192 | if (debugIde === IDE_PYCHARM) {
|
193 | return {};
|
194 | }
|
195 | return { 'DEBUG_OPTIONS': `-m ptvsd --host 0.0.0.0 --port ${debugPort} --wait` };
|
196 | case 'java8':
|
197 | return { 'DEBUG_OPTIONS': `-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,address=${debugPort}` };
|
198 | case 'php7.2':
|
199 | console.log(`using remote_ip ${remoteIp}`);
|
200 | return { 'XDEBUG_CONFIG': `remote_enable=1 remote_autostart=1 remote_port=${debugPort} remote_host=${remoteIp}` };
|
201 | case 'dotnetcore2.1':
|
202 | return { 'DEBUG_OPTIONS': 'true' };
|
203 | default:
|
204 | throw new Error(red('could not found runtime.'));
|
205 | }
|
206 | }
|
207 | function generateDockerDebugOpts(runtime, debugPort, debugIde) {
|
208 | const exposedPort = `${debugPort}/tcp`;
|
209 | if (debugIde === IDE_PYCHARM) {
|
210 | if (runtime !== 'python2.7' && runtime !== 'python3') {
|
211 | throw new Error(`${yellow(IDE_PYCHARM)} debug config only support for runtime [python2.7, python3]`);
|
212 | }
|
213 | else {
|
214 | return {};
|
215 | }
|
216 | }
|
217 | else if (runtime === 'php7.2') {
|
218 | return {};
|
219 | }
|
220 | else {
|
221 | return {
|
222 | ExposedPorts: {
|
223 | [exposedPort]: {}
|
224 | },
|
225 | HostConfig: {
|
226 | PortBindings: {
|
227 | [exposedPort]: [
|
228 | {
|
229 | 'HostIp': '',
|
230 | 'HostPort': `${debugPort}`
|
231 | }
|
232 | ]
|
233 | }
|
234 | }
|
235 | };
|
236 | }
|
237 | }
|
238 | module.exports = {
|
239 | generateVscodeDebugConfig, generateDebugEnv, generateDockerDebugOpts,
|
240 | getDebugIde, getDebugPort
|
241 | };
|