-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·225 lines (188 loc) · 7.02 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
#! /usr/bin/env node
const ASANA_ACCESS_TOKEN = "";
const DEBUG = false;
const FORCE_CREATE_LOCAL = false;
const os = require('os');
var fs = require('fs-extra');
var asana = require('asana');
var applescript = require('applescript');
var moment = require("moment");
// Determine if OmniFocus is running
applescript.execString('tell application "System Events" to (name of processes) contains "OmniFocus"', function (error, data, body) {
if (data != "true") {
if (DEBUG) console.log("OmniFocus not running");
process.exit();
}
});
// Copy Applescripts
try {
if (!fs.existsSync(os.homedir() + "/Library/Script Libraries/omnifocus.scpt")) {
fs.copySync(__dirname + "/node_modules/OmniFocus/OmniFocus Library/omnifocus.scpt", os.homedir() + "/Library/Script Libraries/omnifocus.scpt");
}
if (!fs.existsSync(os.homedir() + "/Library/Script Libraries/json.scpt")) {
fs.copySync(__dirname + "/node_modules/applescript-json/json.scpt", os.homedir() + "/Library/Script Libraries/json.scpt");
}
} catch (error) {
console.log("Could not copy file: ", error);
}
var client = asana.Client.create().useAccessToken(ASANA_ACCESS_TOKEN);
function createLocalTask(task) {
if (DEBUG) console.log("Creating local task for: " + task.name);
if (!FORCE_CREATE_LOCAL && task.external && task.external.id) {
if (DEBUG) console.log("Task " + task.id + " already linked");
return false;
} else if (task.completed) {
if (DEBUG) console.log("Task " + task.id + " already completed, skipping");
return true;
}
applescript.execFile(__dirname + "/applescripts/createTask.scpt", [
//transportText
task.name,
(task.projects && task.projects[0] ? task.projects[0].name : '-'),
(task.projects && task.projects[0] && task.projects[0].section ? task.projects[0].section.name : '-'),
(task.due_on ? moment(task.due_on + (task.due_at ? ' ' + task.due_at : '')).format("YYYY-MM-DD" + (task.due_at ? " HH:mm:ss" : '')) : '-'),
(task.notes ? task.notes : '-')
], function (error, data, body) {
if (typeof data == 'string') {
var taskId = data;
if (DEBUG == "verbose") console.log("Local task created: " + taskId);
linkAsanaTask(task.id, taskId);
} else {
console.log("Local task not created.", error, body);
}
});
}
function updateLocalTask(task) {
if (DEBUG) console.log("Updating local task: " + task.name);
if (!task.external || !task.external.id) return false;
var localId = task.external.id;
var parameters = [
localId,
task.name ? task.name : '-',
task.completed ? "true" : "false",
task.due_on ? moment(task.due_on + (task.due_at ? ' ' + task.due_at : '')).format("YYYY-MM-DD" + (task.due_at ? " HH:mm:ss" : '')) : "-",
task.notes ? task.notes : '-'
];
applescript.execFile(__dirname + "/applescripts/updateTask.scpt", parameters, function (error, data, body) {
if (!error) {
if (DEBUG == "verbose") console.log("Local task updated.");
} else {
console.log("Local task not updated.", error);
}
});
}
function getLocalTaskData(id, callback, secondTry) {
if (DEBUG == "verbose") console.log("Getting local task data: " + id);
applescript.execFile(__dirname + "/applescripts/getTaskData.scpt", [id], function (error, data, body) {
try {
var parsedData = JSON.parse(data);
} catch (error) {
if (!secondTry) {
if (DEBUG == "verbose") console.log("Invalid JSON Data for task ID " + id + ", trying again...");
getLocalTaskData(id, callback, true);
} else {
console.log("Invalid JSON Data for task ID " + id, data, body);
}
return false;
}
if (secondTry) {
if (DEBUG == "verbose") console.log("Second try success for task ID " + id);
}
var df = "dddd, MMMM D, YYYY [at] h:mm:ss A";
parsedData.due_on = (parsedData.due_on.length > 0 && parsedData.due_on != "missing value" ? moment(parsedData.due_on, df) : null);
parsedData.created = (parsedData.created.length > 0 && parsedData.created != "missing value" ? moment(parsedData.created, df) : null);
parsedData.modified = (parsedData.modified.length > 0 && parsedData.modified != "missing value" ? moment(parsedData.modified, df) : null);
callback(parsedData);
});
}
function updateRemoteTask(task, localData) {
if (DEBUG) console.log("Updating remote task for: " + localData.name);
if (!task.external || !task.external.id || !localData || !localData.name) return false;
var parameters = {
name: localData.name,
completed: localData.completed
};
if (localData.due_on) {
parameters.due_on = localData.due_on.format("YYYY-MM-DD");
}
if (localData.notes && localData.notes.length > 0) {
parameters.notes = localData.notes;
}
client.tasks.update(task.id, parameters).then(function (response) {
}).catch(function (error) {
console.log("Could not update remote task " + task.id);
});
}
function linkAsanaTask(asanaId, omnifocusId) {
if (DEBUG == "verbose") console.log("Linking asana ID " + asanaId + " with OF ID " + omnifocusId);
client.tasks.update(asanaId, {
external: {id:omnifocusId}
}).then(function (response) {
}).catch(function (error) {
console.log("Could not link Asana task " + asanaId);
});
}
function processLinkedTask(task) {
if (DEBUG == "verbose") console.log("Processing linked task: " + task.name);
if (!task.external || !task.external.id) return false;
var modifiedOnAsana = moment(task.modified_at);
getLocalTaskData(task.external.id, function (localData) {
if (
task.name == localData.name
&& task.completed == localData.completed
&& (
(task.due_on && localData.due_on)
|| (!task.due_on && !localData.due_on)
)
&& (
!task.due_on || moment(task.due_on).startOf('day').isSame(localData.due_on.startOf('day'))
)
&& task.notes == localData.notes
) {
if (DEBUG == "verbose") console.log("Data is the same, skipping");
return true;
}
var modifiedLocally = localData.modified;
if (modifiedOnAsana.isBefore(modifiedLocally)) {
// Trust local
updateRemoteTask(task, localData);
} else {
// Trust remote
updateLocalTask(task);
}
});
}
var created = 0;
var updated = 0;
function processCollection(collection) {
collection.data.forEach((task) => processTask(task));
collection.nextPage().then((collection) => processCollection(collection)).catch(function (error) {
if (DEBUG == "verbose") console.log("Paging complete");
});
}
function processTask(task) {
if (!FORCE_CREATE_LOCAL && task.external && task.external.id) {
processLinkedTask(task);
updated++;
} else {
createLocalTask(task);
created++;
}
}
client.users.me().then(function(user) {
var userId = user.id;
var workspaceId = user.workspaces[0].id;
return client.tasks.findAll({
assignee: userId,
workspace: workspaceId,
completed_since: moment().subtract(1, 'month').format(),
opt_fields: "id,name,assignee_status,completed,due_on,due_at,external,modified_at,notes,projects,projects.name,projects.section,projects.section.name,parent"
});
}).then(processCollection).catch(function(error) {
console.log("Error: ", error);
});
process.on('exit', function () {
if (created + updated > 0) {
console.log("Processed " + (created + updated) + " remote tasks (" + created + " new; " + updated + " existing)");
}
});