-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
78 lines (65 loc) · 2.06 KB
/
main.ts
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
import { readFile } from 'fs/promises';
import util from 'util';
// import jsonToTs from 'json-to-ts';
interface RootObject {
kind: string;
items: TaskList[];
}
interface TaskList {
id: string;
kind: string;
title: string;
updated: string;
items?: Task[];
selfLink: string;
}
interface Task {
id: string;
notes?: string;
due?: string;
kind: string;
created: string;
title: string;
task_type: string;
updated: string;
selfLink: string;
status: string;
completed?: string;
}
async function readAndOutputJsonFile() {
try {
const filePath = './Tasks.json';
const fileContent = await readFile(filePath, 'utf8');
const parsedJson = JSON.parse(fileContent) as RootObject;
console.log(util.inspect(parsedJson, { showHidden: false, depth: null }));
return {parsedJson, fileContent};
} catch (error) {
throw new Error('Error reading or parsing the JSON file:', { cause: error });
}
}
const { parsedJson } = await readAndOutputJsonFile();
// console.log(jsonToTs(parsedJson).join('\n'));
const { taskListsWithTasks, emptyTaskLists } = Object.groupBy(
parsedJson.items,
({ items }) => !!items?.length
? 'taskListsWithTasks'
: 'emptyTaskLists'
) as {taskListsWithTasks: (TaskList & { items: Task[] })[], emptyTaskLists: Omit<TaskList, 'items'>[]}; // need https://github.com/microsoft/TypeScript/pull/56805 to be merged to remove type assertion
console.log('Task lists with tasks:', taskListsWithTasks.map(({ title }) => title).join(', '));
console.log('Empty task lists:', emptyTaskLists.map(({ title }) => title).join(', '));
const markdownLines = taskListsWithTasks.flatMap(
({ items: tasks, ...taskList }, taskListIndex) => [
(taskListIndex + 1) + '. ' + taskList.title,
...tasks
.filter(({ status }) => status !== 'completed')
.map(
({ title: taskTitle, notes }, localTaskIndex) =>
' '
+ (localTaskIndex + 1)
+ '. '
+ taskTitle
+ (notes ? ` (${notes.replaceAll('\n', ' ')})` : '')
),
]
);
console.log(markdownLines.join('\n'));