UNPKG

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