UNPKG

9.37 kBJavaScriptView Raw
1'use strict';
2var __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};
10const fs = require('fs-extra');
11const path = require('path');
12const { red, yellow } = require('colors');
13const debug = require('debug')('fun:local');
14const _ = require('lodash');
15const ip = require('ip');
16const IDE_VSCODE = 'vscode';
17const IDE_PYCHARM = 'pycharm';
18const allowedDebugIdes = [IDE_VSCODE, IDE_PYCHARM];
19function 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}
34function 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}
45function 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 'nodejs10':
70 case 'nodejs8':
71 return {
72 'version': '0.2.0',
73 'configurations': [
74 {
75 'name': `fc/${serviceName}/${functionName}`,
76 'type': 'node',
77 'request': 'attach',
78 'address': 'localhost',
79 'port': debugPort,
80 'localRoot': `${codePath}`,
81 'remoteRoot': '/code',
82 'protocol': 'inspector',
83 'stopOnEntry': false
84 }
85 ]
86 };
87 case 'python3':
88 case 'python2.7':
89 return {
90 'version': '0.2.0',
91 'configurations': [
92 {
93 'name': `fc/${serviceName}/${functionName}`,
94 'type': 'python',
95 'request': 'attach',
96 'host': 'localhost',
97 'port': debugPort,
98 'pathMappings': [
99 {
100 'localRoot': `${codePath}`,
101 'remoteRoot': '/code'
102 }
103 ]
104 }
105 ]
106 };
107 case 'java8':
108 return {
109 'version': '0.2.0',
110 'configurations': [
111 {
112 'name': `fc/${serviceName}/${functionName}`,
113 'type': 'java',
114 'request': 'attach',
115 'hostName': 'localhost',
116 'port': debugPort
117 }
118 ]
119 };
120 case 'php7.2':
121 return {
122 'version': '0.2.0',
123 'configurations': [
124 {
125 'name': `fc/${serviceName}/${functionName}`,
126 'type': 'php',
127 'request': 'launch',
128 'port': debugPort,
129 'stopOnEntry': false,
130 'pathMappings': {
131 '/code': `${codePath}`
132 },
133 'ignore': [
134 '/var/fc/runtime/**'
135 ]
136 }
137 ]
138 };
139 case 'dotnetcore2.1':
140 return {
141 'version': '0.2.0',
142 'configurations': [
143 {
144 'name': `fc/${serviceName}/${functionName}`,
145 'type': 'coreclr',
146 'request': 'attach',
147 'processName': 'dotnet',
148 'pipeTransport': {
149 'pipeProgram': 'sh',
150 'pipeArgs': [
151 '-c',
152 `docker exec -i $(docker ps -q -f publish=${debugPort}) \${debuggerCommand}`
153 ],
154 'debuggerPath': '/vsdbg/vsdbg',
155 'pipeCwd': '${workspaceFolder}'
156 },
157 'windows': {
158 'pipeTransport': {
159 'pipeProgram': 'powershell',
160 'pipeArgs': [
161 '-c',
162 `docker exec -i $(docker ps -q -f publish=${debugPort}) \${debuggerCommand}`
163 ],
164 'debuggerPath': '/vsdbg/vsdbg',
165 'pipeCwd': '${workspaceFolder}'
166 }
167 },
168 'sourceFileMap': {
169 '/code': codePath
170 }
171 }
172 ]
173 };
174 default:
175 break;
176 }
177 debug('CodePath: ' + codePath);
178 });
179}
180function generateDebugEnv(runtime, debugPort, debugIde) {
181 const remoteIp = ip.address();
182 switch (runtime) {
183 case 'nodejs10':
184 case 'nodejs8':
185 return { 'DEBUG_OPTIONS': `--inspect-brk=0.0.0.0:${debugPort}` };
186 case 'nodejs6':
187 return { 'DEBUG_OPTIONS': `--debug-brk=${debugPort}` };
188 case 'python2.7':
189 case 'python3':
190 if (debugIde === IDE_PYCHARM) {
191 return {};
192 }
193 return { 'DEBUG_OPTIONS': `-m ptvsd --host 0.0.0.0 --port ${debugPort} --wait` };
194 case 'java8':
195 return { 'DEBUG_OPTIONS': `-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,quiet=y,address=${debugPort}` };
196 case 'php7.2':
197 console.log(`using remote_ip ${remoteIp}`);
198 return { 'XDEBUG_CONFIG': `remote_enable=1 remote_autostart=1 remote_port=${debugPort} remote_host=${remoteIp}` };
199 case 'dotnetcore2.1':
200 return { 'DEBUG_OPTIONS': 'true' };
201 default:
202 throw new Error(red('could not found runtime.'));
203 }
204}
205function generateDockerDebugOpts(runtime, debugPort, debugIde) {
206 const exposedPort = `${debugPort}/tcp`;
207 if (debugIde === IDE_PYCHARM) {
208 if (runtime !== 'python2.7' && runtime !== 'python3') {
209 throw new Error(`${yellow(IDE_PYCHARM)} debug config only support for runtime [python2.7, python3]`);
210 }
211 else {
212 return {};
213 }
214 }
215 else if (runtime === 'php7.2') {
216 return {};
217 }
218 else {
219 return {
220 ExposedPorts: {
221 [exposedPort]: {}
222 },
223 HostConfig: {
224 PortBindings: {
225 [exposedPort]: [
226 {
227 'HostIp': '',
228 'HostPort': `${debugPort}`
229 }
230 ]
231 }
232 }
233 };
234 }
235}
236module.exports = {
237 generateVscodeDebugConfig, generateDebugEnv, generateDockerDebugOpts,
238 getDebugIde, getDebugPort
239};