-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (69 loc) · 2.3 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
"use strict";
const _ = require("lodash"),
jsonFile = require("jsonfile"),
fs = require("fs");
const resolveConfigDir = () => {
const configDir = process.env.CONFIG_DIR || "./";
return configDir.endsWith("/") ? configDir : configDir + "/";
};
const configDir = resolveConfigDir();
const logger = require("./logger")(configDir);
const configFile = configDir + "config.json";
let config;
if (fs.existsSync(configFile)) {
config = require(configFile);
} else {
// apply configuration from environment vars
const habiticaApiUser = process.env.HABITICA_API_USER;
const habiticaApiKey = process.env.HABITICA_API_KEY;
const todoistApiToken = process.env.TODOIST_API_TOKEN;
if (!!habiticaApiUser && !!habiticaApiKey && !!todoistApiToken) {
logger.info("Applying configuration from environment variables");
config = {
habitica: {
apiUser: habiticaApiUser,
apiKey: habiticaApiKey,
},
todoist: {
token: todoistApiToken,
},
ignoreProjects: [],
};
} else {
logger.error("No config found!");
}
}
const Habitica = require("./habitica");
const Todoist = require("./todoist");
const Sync = require("./sync");
config.todoist.unmatchedDailyTask =
config.todoist.unmatchedDailyTask || process.env.TODOIST_UNMATCHED_DAILY_TASK;
const todoist = Todoist.from(config.todoist.token, logger);
const habitica = Habitica.from(
config.habitica.apiUser,
config.habitica.apiKey,
logger,
);
const initialSyncToken = process.env.INITIAL_SYNC_TOKEN;
const lastRun =
jsonFile.readFileSync(configDir + "lastRun.json", { throws: false }) || {};
logger.info("Last run: " + JSON.stringify(lastRun));
if (!lastRun.syncToken && !!initialSyncToken) {
logger.info(`Using initial sync token: ${initialSyncToken}`);
lastRun.syncToken = initialSyncToken;
} else {
logger.info(`Using sync token ${lastRun.syncToken}`);
}
logger.info("Last run (2): " + JSON.stringify(lastRun));
logger.info("Sync started");
new Sync(todoist, habitica, logger, config)
.sync(lastRun)
.then((config) => {
const sync = config.sync;
lastRun.syncToken = sync.sync_token;
logger.info(
`Sync finished, writing lastRun.json with new syncToken: ${lastRun.syncToken}`,
);
jsonFile.writeFileSync(configDir + "lastRun.json", lastRun);
})
.then(() => logger.info("done"));