UNPKG

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