-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·234 lines (203 loc) · 6.26 KB
/
index.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const json2csv = require('json2csv').parse;
const TurndownService = require('turndown');
const program = require('commander');
let srcUsersPath;
let srcTasksPath;
let srcFoldersPath;
let outputPath;
let excludeTags = [];
let listNames = [];
program.version(require('./package.json').version, '-v, --version');
program
.option('-f, --folders <srcFoldersPath>', 'Path to your folders.json file')
.option('-t, --tasks <srcTasksPath>', 'Path to your tasks.json file')
.option('-u, --users <srcUsersPath>', 'Path to your users.json file')
.option('-o, --output <outputPath>', 'Where to save the output file')
.option('-e, --excludeTags [excludeTags]', "Don't convert these folder names to tags", '')
.option('-l, --listNames [listNames]', 'Save these folders as lists and try to keep their tasks', '')
.description("Convert all tasks in a Wrike account to Clickup's csv import format")
.action((cmd) => {
const {
users,
tasks,
folders,
output,
} = cmd;
if (!users || !tasks || !folders || !output) {
program.help();
}
srcUsersPath = path.join(process.cwd(), users);
srcTasksPath = path.join(process.cwd(), tasks);
srcFoldersPath = path.join(process.cwd(), folders);
outputPath = path.join(process.cwd(), output);
excludeTags = cmd.excludeTags.split(',');
listNames = cmd.listNames.split(',');
});
program.parse(process.argv);
const srcUsers = JSON.parse(fs.readFileSync(srcUsersPath, 'utf8'));
const srcTasks = JSON.parse(fs.readFileSync(srcTasksPath, 'utf8'));
const srcFolders = JSON.parse(fs.readFileSync(srcFoldersPath, 'utf8'));
const ts = new TurndownService();
const toMarkdown = (str) => str && ts.turndown(str);
const exists = (x) => !!x;
// Remove wrike bot users from the team
const users = srcUsers.filter(u => u.firstName !== 'Wrike');
// Given a user uid, return that user's email
function getUserEmailById(uid) {
const user = users.find(u => u.uid === uid);
return user && user.email;
}
/**
* Return the full name of the task's status
* Prefer the custom name when it is set, otherwise use the default
*
* @param {*} t
* @returns
*/
function getStatusName(t) {
if (t.customStatus) return t.customStatus.title;
if (t.status === 0) return 'New';
if (t.status === 1) return 'Finished';
if (t.status === 2) return 'Waiting';
if (t.status === 3) return 'Canceled';
}
/**
* Traverse all subtasks of a task and flatten to a single array
*
* @param {*} t
* @returns
*/
function flattenTask(t) {
if (!t) return [];
if (t.successors) {
t.successor_ids = t.successors.map(s => s.id).join(',') || undefined;
const subtasks = t.successors.reduce(
(acc, sub) => {
sub.parent_id = t.id;
acc.push(...flattenTask(sub));
return acc;
},
[],
);
delete t.successors;
return [t, ...subtasks];
}
return [t];
}
/**
* Prepare a task in a format that's actually usable by Clickup
*
* @param {srcTask} t
* @returns {task}
*/
function transformTask(t) {
if (!t) return;
return _(t)
.omit(['comments', 'googleDocs', 'attachments', 'timelog', 'duration'])
.assign(
{
id: t.id,
title: t.title,
description: toMarkdown(t.description),
customStatus: getStatusName(t),
shared: t.shared.map(getUserEmailById).filter(exists).join(',') || undefined,
assigned: t.assigned.map(getUserEmailById).filter(exists).join(',') || undefined,
successors: t.successors && t.successors.map(getTaskById).filter(exists),
author: getUserEmailById(t.author),
},
)
.value();
}
/**
* Return a formatted task with its id
*
* @param {integer} id
* @returns {task}
*/
function getTaskById(id) {
const task = srcTasks.find(f => f.id === id);
return transformTask(task);
}
/**
* Prepare a folder in a format that's actually usable by Clickup (similar to a `List`)
*
* @param {*} f
* @returns
*/
function transformFolder(f) {
if (!f) return;
return _(f)
.omit(['comments', 'attachments', 'googleDocs', 'isProject', 'dateCreated', 'projectCreatedDate'])
.assign({
id: f.id,
title: f.title,
description: toMarkdown(f.description),
shared: f.shared.map(getUserEmailById).filter(exists).join(',') || undefined,
owners: f.owners.map(getUserEmailById).filter(exists).join(',') || undefined,
author: getUserEmailById(f.author),
tasks: f.children.map(getTaskById).filter(exists).reduce((acc, t) => [...acc, ...flattenTask(t)], []),
})
.value();
}
// Prepare folders and tasks
const folders = srcFolders.map(transformFolder).filter(exists);
/*
* Clickup (sadly) does not have a concept of linking the same task to multiple folders
* Let's define what folders will be our future `lists`.
* By default all folder names will be tags.
*/
const keepFolders = folders.filter(f => listNames.includes(f.title));
const tagFolders = folders.filter(f => !excludeTags.includes(f.title));
// return a list of comma-separated tags for the task from its parent folder
function getTagNames(t) {
return tagFolders.filter(f => f.tasks.find(child => child.id === t.id)).map(f => f.title).join(',');
}
// return the main folder title
function getListTitle(t) {
const match = keepFolders.find(f => f.tasks.find(child => child.id === t.id));
return match && match.title;
}
const tasks = _
.chain(srcTasks)
// create the formatted object
.map(transformTask)
.filter(exists)
// flatten the subtasks
.reduce((acc, t) => [...acc, ...flattenTask(t)], [])
// remove duplicates
.uniqBy('id')
// add each task's main folder name as list title
// and all folder names the task is a member of as tags
// this will get folders, subfolders, projects... except discarded folders
.map(t => Object.assign(
t,
{
tags: getTagNames(t),
list: getListTitle(t),
},
))
.value();
// Let's convert this bad boy to CSV and we should be about done.
const fields = [
'id',
'title',
'description',
'status',
'customStatus',
'tags',
'list',
'importance',
'author',
'assigned',
'shared',
'dateCreated',
'dateStart',
'dateDue',
];
const csv = json2csv(tasks, { fields });
// Write the output to file
fs.writeFileSync(outputPath, csv);