forked from Coernel82/MMM-CalDAV-Tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-debug.js
More file actions
executable file
·505 lines (430 loc) · 16.8 KB
/
cli-debug.js
File metadata and controls
executable file
·505 lines (430 loc) · 16.8 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
* CLI Debug Tool for MMM-CalDAV-Tasks
*
* Tests the node_helper functionality from command line
* Usage: node cli-debug.js [command] [options]
*
* Commands:
* test-config - Validate configuration
* fetch - Fetch tasks from CalDAV server
* toggle <uid> - Toggle task completion status
*
* Options:
* --config <path> - Path to config file (default: ./config/config.js)
*/
/* eslint-disable n/no-process-exit */
// CLI tool requires process.exit() for proper exit codes
const fs = require("fs");
const path = require("path");
// Mock the MagicMirror NodeHelper module
class MockNodeHelper {
constructor() {
this.name = "MMM-CalDAV-Tasks";
}
sendSocketNotification() {
// Mock method - not used in CLI tool
}
socketNotificationReceived() {
// Will be overridden by actual helper
}
create() {
return this;
}
}
// Mock the node_helper module for require
const mockNodeHelperModule = {
exports: MockNodeHelper
};
// Inject mock before requiring node_helper
require.cache["node_helper"] = mockNodeHelperModule;
// Load node_helper functions directly
const { transformData, sortList, appendUrlIndex } = require("./transformer");
const {
parseList,
mapEmptyPriorityTo,
mapEmptySortIndexTo,
fetchCalendarData,
initDAVClient
} = require("./webDavHelper");
const VTodoCompleter = require("./vtodo-completer.js");
const { validateConfig } = require("./config-validator");
// Configuration
let config = null;
let configPath = path.join(__dirname, "config", "config.js");
// Parse command line arguments
const args = process.argv.slice(2);
const command = args[0] || "help";
// Parse flags
const flags = {
showFile: args.includes("--show-file"),
showUid: args.includes("--show-uid"),
showCompleted: args.includes("--show-completed"),
verbose: args.includes("--verbose") || args.includes("-v")
};
// Check for --config flag
const configIndex = args.indexOf("--config");
if (configIndex !== -1 && args[configIndex + 1]) {
configPath = path.resolve(args[configIndex + 1]);
}
/**
* Load configuration from file
*/
function loadConfig() {
try {
if (!fs.existsSync(configPath)) {
console.error(`❌ Config file not found: ${configPath}`);
console.log(
"\n💡 Tip: Create config/config.js from config/config.template.js"
);
process.exit(1);
}
const configContent = fs.readFileSync(configPath, "utf8");
// Check if it's a MagicMirror config file (contains 'modules:')
if (configContent.includes("modules:")) {
// Extract MMM-CalDAV-Tasks module config
const moduleMatch = configContent.match(
/module:\s*['"]MMM-CalDAV-Tasks['"]\s*,[\s\S]*?config:\s*(\{[\s\S]*?\})\s*,?\s*\}/
);
if (!moduleMatch) {
console.error("❌ MMM-CalDAV-Tasks module not found in config file");
console.log(
"\n💡 Tip: Make sure the module is configured in config.js"
);
process.exit(1);
}
// Parse the config object
// Clean up JavaScript code for eval
let configStr = moduleMatch[1];
// Use safer function constructor
const parseConfig = new Function(`return ${configStr}`);
config = parseConfig();
} else {
// Standalone config file - extract the object
const configMatch = configContent.match(/\{[\s\S]*\}/);
if (!configMatch) {
console.error("❌ Could not parse config file");
process.exit(1);
}
config = eval(`(${configMatch[0]})`);
}
console.log("✅ Configuration loaded successfully");
console.log(" Server:", config.webDavAuth?.url || "Not configured");
console.log(" User:", config.webDavAuth?.username || "Not configured");
return config;
} catch (error) {
console.error("❌ Error loading config:", error.message);
if (process.env.DEBUG) {
console.error("Stack trace:", error.stack);
}
process.exit(1);
}
}
/**
* Validate configuration using config-validator
*/
async function testConfig() {
console.log("\n🔍 Testing Configuration...\n");
const { valid, config: normalizedConfig, errors } = validateConfig(config);
if (valid) {
console.log("✅ Configuration is valid!\n");
console.log("📋 Normalized Config:");
console.log(JSON.stringify(normalizedConfig, null, 2));
} else {
console.log("❌ Configuration has errors:\n");
errors.forEach((error, index) => {
console.log(` ${index + 1}. [${error.type}] ${error.message}`);
});
const criticalErrors = errors.filter((e) => e.type !== "deprecation");
if (criticalErrors.length > 0) {
console.log("\n❌ Critical errors found. Please fix your configuration.");
process.exit(1);
}
}
}
/**
* Fetch tasks from CalDAV server (implements getData from node_helper)
*/
async function fetchTasks() {
console.log("\n📥 Fetching tasks from CalDAV server...\n");
try {
// Validate and normalize configuration
const { valid, config: normalizedConfig, errors } = validateConfig(config);
if (!valid) {
const criticalErrors = errors.filter((e) => e.type !== "deprecation");
if (criticalErrors.length > 0) {
const errorMsg = criticalErrors.map((e) => e.message).join("; ");
throw new Error(`Configuration error: ${errorMsg}`);
}
// Log deprecation warnings
errors
.filter((e) => e.type === "deprecation")
.forEach((e) => console.warn(`[CLI] ${e.message}`));
}
// Use normalized config with defaults
const effectiveConfig = { ...config, ...normalizedConfig };
let allTasks = [];
const calendarData = await fetchCalendarData(effectiveConfig);
// Iterate over all calendars
for (let i = 0; i < calendarData.length; i++) {
const icsList = calendarData[i].icsStrings;
const rawList = parseList(icsList, effectiveConfig.dateFormat);
const priorityList = mapEmptyPriorityTo(
rawList,
effectiveConfig.mapEmptyPriorityTo
);
const sortIndexList = mapEmptySortIndexTo(
priorityList,
effectiveConfig.mapEmptySortIndexTo
);
const indexedList = appendUrlIndex(sortIndexList, i);
const sortedList = sortList(indexedList, effectiveConfig.sortMethod);
const sortedAppleList = sortList(sortedList, "apple");
const nestedList = transformData(sortedAppleList);
allTasks = allTasks.concat(nestedList);
calendarData[i].tasks = nestedList;
}
// Display results
const completedCount = allTasks.filter(
(t) => t.status === "COMPLETED"
).length;
const activeCount = allTasks.length - completedCount;
console.log(
`✅ Successfully fetched ${allTasks.length} tasks from ${calendarData.length} calendar(s)`
);
console.log(
` Active: ${activeCount} | Completed: ${completedCount}${!flags.showCompleted ? " (hidden)" : ""}\n`
);
calendarData.forEach((calendar, index) => {
console.log(`📅 Calendar ${index + 1}: ${calendar.summary || "Unnamed"}`);
console.log(` URL: ${calendar.url}`);
console.log(` Color: ${calendar.calendarColor || "default"}`);
console.log(` Tasks: ${calendar.tasks?.length || 0}`);
if (calendar.tasks && calendar.tasks.length > 0) {
// Filter tasks based on flags
let displayTasks = calendar.tasks;
if (!flags.showCompleted) {
displayTasks = displayTasks.filter(
(task) => task.status !== "COMPLETED"
);
}
if (displayTasks.length === 0) {
console.log(
` ℹ️ No active tasks (use --show-completed to see all)`
);
} else {
console.log(`\n 📝 Tasks (${displayTasks.length}):\n`);
displayTasks.forEach((task, index) => {
const status = task.status === "COMPLETED" ? "✅" : "⬜";
const priority = task.priority || "-";
const summary = task.summary || "Unnamed task";
// Main task line
console.log(
` ${index + 1}. ${status} ${summary} ${priority !== "-" ? `[P${priority}]` : ""}`
);
// Due date
if (task.due) {
const dueDate = task.dueFormatted || task.due;
const isOverdue =
new Date(task.due) < new Date() && task.status !== "COMPLETED";
console.log(
` 📅 Due: ${dueDate}${isOverdue ? " ⚠️ OVERDUE" : ""}`
);
}
// Start date
if (task.dtstart && flags.verbose) {
console.log(
` 🏁 Started: ${task.dtstartFormatted || task.dtstart}`
);
}
// Recurrence rule
if (task.rrule) {
let rruleText = "";
if (typeof task.rrule === "string") {
rruleText = task.rrule;
} else if (task.rrule.options) {
const opts = task.rrule.options;
// freq is a number constant from rrule library, map to string
const freqMap = {
0: "yearly",
1: "monthly",
2: "weekly",
3: "daily",
4: "hourly",
5: "minutely",
6: "secondly"
};
const freq = freqMap[opts.freq] || "unknown";
const interval = opts.interval || 1;
const until = opts.until
? ` until ${new Date(opts.until).toLocaleDateString()}`
: "";
const count = opts.count ? ` (${opts.count} times)` : "";
rruleText = `Every ${interval > 1 ? interval + " " : ""}${freq}${until}${count}`;
}
console.log(` 🔁 Repeats: ${rruleText}`);
}
// Description (first line only)
if (task.description && flags.verbose) {
const firstLine = task.description.split("\n")[0];
if (firstLine.length > 60) {
console.log(` 💬 ${firstLine.substring(0, 57)}...`);
} else {
console.log(` 💬 ${firstLine}`);
}
}
// UID (only with flag)
if (flags.showUid && task.uid) {
console.log(` 🔑 UID: ${task.uid}`);
}
// Filename (only with flag)
if (flags.showFile && task.filename) {
console.log(` 📄 File: ${task.filename}`);
}
console.log(""); // Empty line between tasks
});
}
}
});
return calendarData;
} catch (error) {
console.error("❌ Error fetching tasks:", error.message);
if (process.env.DEBUG) {
console.error("Stack trace:", error.stack);
}
throw error;
}
}
/**
* Toggle task completion status (implements toggleStatusViaWebDav from node_helper)
*/
async function toggleTask(uid) {
if (!uid) {
console.error("❌ Error: UID is required");
console.log("Usage: node cli-debug.js toggle <uid>");
process.exit(1);
}
console.log(`\n🔄 Toggling task: ${uid}...\n`);
// First fetch to find the task
const calendars = await fetchTasks();
let foundTask = null;
let filename = null;
// Find the task
for (const calendar of calendars) {
if (calendar.tasks) {
for (const task of calendar.tasks) {
if (task.uid === uid || task.filename?.includes(uid)) {
foundTask = task;
filename = task.filename;
break;
}
}
}
if (foundTask) break;
}
if (!foundTask) {
console.error(`❌ Task with UID ${uid} not found`);
console.log(
'\n💡 Tip: Run "node --run debug:fetch" to see all available UIDs'
);
process.exit(1);
}
console.log(`\n📝 Found task: ${foundTask.summary}`);
console.log(` Status: ${foundTask.status}`);
console.log(` Filename: ${filename}`);
try {
const client = initDAVClient(config);
const completer = new VTodoCompleter(client);
await completer.completeVTodo(config, filename);
console.log("\n✅ Task toggled successfully!");
console.log(
" New status:",
foundTask.status === "COMPLETED" ? "IN-PROGRESS" : "COMPLETED"
);
console.log("\n🔄 Fetching updated tasks...\n");
await fetchTasks();
} catch (error) {
console.error("\n❌ Error toggling task:", error.message);
if (process.env.DEBUG) {
console.error("Stack trace:", error.stack);
}
throw error;
}
}
/**
* Show help
*/
function showHelp() {
console.log(`
📘 MMM-CalDAV-Tasks CLI Debug Tool
Usage: node cli-debug.js <command> [options]
Commands:
help Show this help message
test-config Validate configuration file
fetch Fetch and display all tasks
toggle <uid> Toggle task completion status by UID
Options:
--config <path> Path to config file (default: ./config/config.js)
--show-completed Show completed tasks (default: hidden)
--show-file Show task filenames
--show-uid Show task UIDs
--verbose, -v Show additional details (start dates, descriptions)
Examples:
node cli-debug.js test-config
node cli-debug.js fetch
node cli-debug.js fetch --show-completed --verbose
node cli-debug.js fetch --show-uid --show-file
node cli-debug.js toggle abc123-def456-ghi789
node cli-debug.js fetch --config /path/to/custom-config.js
Configuration:
The tool reads configuration from config/config.js
Create this file from config/config.template.js
For more information, see: https://github.com/Coernel82/MMM-CalDAV-Tasks
`);
}
/**
* Main execution
*/
async function main() {
console.log("🚀 MMM-CalDAV-Tasks CLI Debug Tool\n");
try {
switch (command) {
case "help":
case "--help":
case "-h":
showHelp();
break;
case "test-config":
loadConfig();
await testConfig();
break;
case "fetch":
loadConfig();
await fetchTasks();
break;
case "toggle": {
loadConfig();
const uid = args[1];
await toggleTask(uid);
break;
}
default:
console.error(`❌ Unknown command: ${command}`);
console.log('Run "node cli-debug.js help" for usage information\n');
process.exit(1);
}
console.log("\n✨ Done!\n");
process.exit(0);
} catch (error) {
console.error("\n💥 Error:", error.message);
if (process.env.DEBUG) {
console.error("\nStack trace:");
console.error(error.stack);
}
process.exit(1);
}
}
// Run main
if (require.main === module) {
main();
}
module.exports = { loadConfig, testConfig, fetchTasks, toggleTask };