-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.js
297 lines (268 loc) · 8.81 KB
/
debug.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* eslint-disable import/no-dynamic-require */
const repl = require('repl');
const chalk = require('chalk');
const path = require('path');
const argv = require('yargs').argv;
const fs = require('fs');
const util = require(path.join(__dirname, 'helpers', 'util'));
const dwConfigPath = argv.dwconfig || path.join('config', 'dw.json');
const configPath = argv.config || path.join('config', 'config.js');
const sfccOptions = require(path.join(__dirname, dwConfigPath));
const config = require(path.join(__dirname, configPath));
const debuggerStateFile = path.join(__dirname, 'tmp', 'state.json');
const debugMode = config.debug || false;
const DebuggerApi = require(path.join(__dirname, 'sfcc', 'debugger'));
const debuggerClient = new DebuggerApi(debugMode, sfccOptions);
const allFilesOfWorkspaces = util.getAllFilesFromWorkspaces(config);
if (allFilesOfWorkspaces && allFilesOfWorkspaces.length > 0) {
console.log(`Total files indexed ${allFilesOfWorkspaces.length}`);
}
async function evalOnSFCCServer(cmd, context, filename, callback) {
const commandWithoutLineBreaks = cmd.replace(/(\r\n|\n|\r)/gm, '');
if (commandWithoutLineBreaks.length > 0) {
const expressionValue = await debuggerClient.getValueByEval(cmd);
if (expressionValue) {
callback(null, expressionValue);
} else {
callback('Unable to evaluate expression', null);
}
} else {
callback();
}
}
const replServer = repl.start({
prompt: 'sfcc-cli-debug > ',
eval: evalOnSFCCServer,
useColors: true
});
replServer.defineCommand('start', {
help: 'Attach a Debugger Client',
async action() {
this.clearBufferedCommand();
await debuggerClient.create();
this.displayPrompt();
}
});
replServer.defineCommand('stop', {
help: 'Detach a Debugger Client',
async action() {
this.clearBufferedCommand();
await debuggerClient.delete();
this.displayPrompt();
}
});
replServer.defineCommand('b', {
help: 'Add a breakpoint',
async action(data) {
this.clearBufferedCommand();
await util.setBreakPoint(data, debuggerClient);
this.displayPrompt();
}
});
replServer.defineCommand('break', {
help: 'Alias : Add a breakpoint',
async action(data) {
this.clearBufferedCommand();
await util.setBreakPoint(data, debuggerClient);
this.displayPrompt();
}
});
replServer.defineCommand('bi', {
help: 'Add a breakpoint interactively',
async action() {
this.clearBufferedCommand();
await util.setBreakPointInteractive(debuggerClient, config, configPath);
this.displayPrompt();
}
});
replServer.defineCommand('sbr', {
help: 'Add a breakpoint and resume/continue',
async action(lineNumber) {
this.clearBufferedCommand();
await util.setBreakPoint(lineNumber, debuggerClient);
// debugger will automatically be moved to new breakpoint location
const success = await debuggerClient.resume();
if (success) {
await util.printLines(null, debuggerClient, allFilesOfWorkspaces);
}
this.displayPrompt();
}
});
replServer.defineCommand('tbr', {
help: 'Add a breakpoint temporarily and resume/continue',
async action(lineNumber) {
this.clearBufferedCommand();
const resp = await util.setBreakPoint(lineNumber, debuggerClient);
// debugger will automatically be moved to new breakpoint location
const success = await debuggerClient.resume();
if (success) {
await util.printLines(null, debuggerClient, allFilesOfWorkspaces);
// since this is a temporary breakpoint delete it now but in an async way
if (resp && resp.length > 0) {
const brkpID = resp[0].id;
debuggerClient.deleteBreakpoints(brkpID, true);
}
}
this.displayPrompt();
}
});
replServer.defineCommand('gb', {
help: 'Display all breakpoints',
async action() {
this.clearBufferedCommand();
await debuggerClient.getBreakpoints();
this.displayPrompt();
}
});
replServer.defineCommand('rb', {
help: 'remove breakpoint(s)',
async action(id) {
this.clearBufferedCommand();
await debuggerClient.deleteBreakpoints(id);
this.displayPrompt();
}
});
replServer.defineCommand('ct', {
help: 'Get current thread',
async action() {
this.clearBufferedCommand();
const response = await debuggerClient.getCurrentThreadObject();
if (response) {
console.table(response);
}
this.displayPrompt();
}
});
replServer.defineCommand('v', {
help: 'Get Variables in scope',
async action() {
this.clearBufferedCommand();
const variables = await debuggerClient.getVariables();
if (variables) {
console.table(variables);
}
this.displayPrompt();
}
});
replServer.defineCommand('m', {
help: 'Get members of variables',
async action(data) {
this.clearBufferedCommand();
const dataParts = data.split(',');
const variableName = dataParts[0];
const maxCount = dataParts[1];
const members = await debuggerClient.getMembersOfVariable(variableName, maxCount);
if (members) {
console.table(members);
}
this.displayPrompt();
}
});
replServer.defineCommand('p', {
help: 'Evaluate On Server and print expression value',
async action(expression) {
this.clearBufferedCommand();
const expressionValue = await debuggerClient.getValueByEval(expression);
if (expressionValue) {
console.log(expressionValue.result);
}
this.displayPrompt();
}
});
replServer.defineCommand('sn', {
help: 'Step Over/Next to next line',
async action() {
this.clearBufferedCommand();
const success = await debuggerClient.stepOver();
if (success) {
await util.printLines(null, debuggerClient, allFilesOfWorkspaces);
}
this.displayPrompt();
}
});
replServer.defineCommand('si', {
help: 'Step Into',
async action() {
this.clearBufferedCommand();
const success = await debuggerClient.stepInto();
if (success) {
await util.printLines(null, debuggerClient, allFilesOfWorkspaces);
}
this.displayPrompt();
}
});
replServer.defineCommand('so', {
help: 'Step Out',
async action() {
this.clearBufferedCommand();
const success = await debuggerClient.stepOut();
if (success) {
await util.printLines(null, debuggerClient, allFilesOfWorkspaces);
}
this.displayPrompt();
}
});
replServer.defineCommand('r', {
help: 'Resume and halt at next breakpoint',
async action() {
this.clearBufferedCommand();
const success = await debuggerClient.resume();
if (success) {
await util.printLines(null, debuggerClient, allFilesOfWorkspaces);
}
this.displayPrompt();
}
});
replServer.defineCommand('l', {
help: 'print source code',
async action(offset) {
this.clearBufferedCommand();
await util.printLines(offset, debuggerClient, allFilesOfWorkspaces);
this.displayPrompt();
}
});
replServer.defineCommand('save', {
help: 'Save debugger state',
async action() {
this.clearBufferedCommand();
if (debuggerClient.connected) {
const breakpoints = await debuggerClient.getBreakpoints();
if (breakpoints && breakpoints.length > 0) {
const breakpointObj = breakpoints.map((brk) => ({
script_path: brk.script,
line_number: brk.line
}));
if (fs.existsSync(debuggerStateFile)) {
fs.unlinkSync(debuggerStateFile);
}
fs.writeFileSync(debuggerStateFile, JSON.stringify(breakpointObj));
console.log(chalk.green('Debugger current state successfully saved'));
}
} else {
console.log(chalk.red('Debugger not connected'));
}
this.displayPrompt();
}
});
replServer.defineCommand('restore', {
help: 'Restore debugger state',
async action() {
this.clearBufferedCommand();
if (debuggerClient.connected) {
const breakpoints = util.getJSONFile(debuggerStateFile);
if (breakpoints) {
await debuggerClient.setBreakpoint(breakpoints);
} else {
console.log(chalk.red('No saved state found'));
}
} else {
console.log(chalk.red('Debugger not connected'));
}
this.displayPrompt();
}
});
replServer.on('exit', async () => {
await debuggerClient.delete();
// util.cleanup();
process.exit();
});