-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·305 lines (273 loc) · 9.15 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
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
#!/usr/bin/env node
"use strict";
var moment = require("moment");
var _ = require("lodash");
var fs = require("fs");
var os = require("os");
var rl = require("readline");
var argv = require("minimist")(process.argv.slice(2));
var columnify = require("columnify");
// Constants
var FL_DIRECTORY = os.homedir() + "/.friendlog/";
var FRIENDS_PATH = FL_DIRECTORY + "friends.json";
var EVENTS_PATH = FL_DIRECTORY + "events.json";
var CONFIG_PATH = FL_DIRECTORY + "config.json";
var DATE_STORAGE_FORMAT = "YYYY-MM-DD";
var DATE_PARSE_FORMAT = "YYYY-MM-DD";
var DATE_DISPLAY_FORMAT = "YYYY-MM-DD";
var TODAY = moment().startOf("day");
var DEFAULT_NUM_DAYS = 10;
// Config
var config = loadConfigData();
// Setup
try {
loadFriendsData();
loadEventsData();
} catch (err) {
console.log("Setting up friendlog! :-)");
fs.existsSync(FL_DIRECTORY) || fs.mkdirSync(FL_DIRECTORY);
fs.existsSync(CONFIG_PATH) || ((config = JSON.stringify({"prettyPrintJson": true})) && writeConfigData());
fs.existsSync(FRIENDS_PATH) || writeFriendsData([]);
fs.existsSync(EVENTS_PATH) || writeEventsData([]);
}
// Data transfer
function loadFriendsData() {
var rawFriends = fs.readFileSync(FRIENDS_PATH);
return JSON.parse(rawFriends);
}
function loadEventsData() {
var rawEvents = fs.readFileSync(EVENTS_PATH);
return JSON.parse(rawEvents);
}
function writeFriendsData(friends) {
var rawNewFriends = stringify(friends);
fs.writeFileSync(FRIENDS_PATH, rawNewFriends);
}
function writeEventsData(events) {
var rawNewEvents = stringify(events);
fs.writeFileSync(EVENTS_PATH, rawNewEvents);
}
function writeConfigData() {
fs.writeFileSync(CONFIG_PATH, stringify(config));
}
function stringify(jsonObject) {
if (config.prettyPrintJson) {
return JSON.stringify(jsonObject, null, 2);
}
return JSON.stringify(jsonObject);
}
function loadConfigData() {
try {
var rawConfig = fs.readFileSync(CONFIG_PATH);
return JSON.parse(rawConfig);
} catch (err) {
return {};
}
}
// Helper functions
function ask(question, callback) {
var r = rl.createInterface({
input: process.stdin,
output: process.stdout});
r.question(question + "\n", function(answer) {
r.close();
callback(answer);
});
}
function ifYesAddFriend(answer, name, callback) {
if (-1 !== ["y", "yes", "Y", "YES"].indexOf(answer)) {
addFriend(name);
return callback();
}
fail("Friend " + name + " does not exist");
}
function getFriendByName(friends, name) {
return _.filter(friends, friend => friend.name === name);
}
function fail(msg) {
throw msg;
}
// Date-related helpers
function convertDaysToNumber(days) {
var numDays = +days;
if (!numDays || numDays <= 0) {
fail("Please use a positive number of days (specified '" + days + "')");
}
return days;
}
function parseDate(input) {
var date;
if (input === "today") {
date = moment();
} else if (input === "yesterday" || input === "yday") {
date = moment().subtract(1, "days");
} else if (parseWeekday(input)) {
date = parseWeekday(input);
} else {
date = moment(input, DATE_PARSE_FORMAT, true);
if (!date.isValid()) {
fail("Invalid date (specified '" + input + "')");
}
}
return date.startOf("day");
}
function parseWeekday(s) {
const weekdays = [
["sunday", "sun"],
["monday", "mon"],
["tuesday", "tue"],
["wednesday", "wed"],
["thursday", "thu"],
["friday", "fri"],
["saturday", "sat"],
];
for (let i = 1; i <= 7; i++) {
const d = moment(TODAY).subtract(i, "days");
if (weekdays[d.day()].indexOf(s) !== -1) {
return d;
}
}
return false;
}
// Public API
function listFriends() {
var NEW_INDICATOR = "new ";
var events = loadEventsData();
var friends = loadFriendsData();
var eventsByFriend = _.groupBy(events, "name");
var nextEventByFriend = _.map(friends, friend => {
var lastEvent = _.sortBy(eventsByFriend[friend.name], "date").reverse()[0];
return {
name: friend.name,
date: lastEvent ? moment(lastEvent.date).add(friend.interval, "days").format(DATE_DISPLAY_FORMAT) : NEW_INDICATOR
};
});
var newFriends = _.filter(nextEventByFriend, x => x.date === NEW_INDICATOR);
var nonNewFriends = _.filter(nextEventByFriend, x => x.date !== NEW_INDICATOR);
_.sortBy(nonNewFriends, "date").forEach(displayFriend);
_.sortBy(newFriends, "name").forEach(displayFriend);
}
function displayFriend(friend) {
const today = TODAY.format(DATE_DISPLAY_FORMAT); // TODO - what if date display format is not Year, Month, Day?
const dateColumn = friend.date + (friend.date < today ? "*" : " ");
console.log(dateColumn + " " + friend.name);
}
function addFriend(name, days) {
var friends = loadFriendsData();
var myFriend = getFriendByName(friends, name)[0];
if (myFriend) {
fail("Friend " + name + " already exists");
}
var numDays = days !== undefined ? convertDaysToNumber(days) : DEFAULT_NUM_DAYS;
friends.push({"name": name, "interval": numDays});
writeFriendsData(friends);
console.info("Added friend " + name);
}
function editFriend(name, days) {
var friends = loadFriendsData();
var myFriend = getFriendByName(friends, name)[0];
if (!myFriend) {
return ask(
"New friend " + name + "! Add them to friendlog? [yN]",
(answer) => ifYesAddFriend(answer, name, () => editFriend(name, days))
);
}
var numDays = convertDaysToNumber(days);
var oldNumDays = myFriend.interval;
myFriend.interval = numDays;
writeFriendsData(friends);
console.info("Friend " + name + " now at " + numDays + " days (was " + oldNumDays + ")");
}
function getFriendInfo(name) {
var friends = _.sortBy(loadFriendsData(), "name");
if (!name) {
return (argv.n || argv.names)
? friends.map(f => console.log(f.name))
: friends.map(f => console.log(f));
}
var myFriend = getFriendByName(friends, name)[0];
if (!myFriend) {
return ask(
"New friend " + name + "! Add them to friendlog? [yN]",
(answer) => ifYesAddFriend(answer, name, () => getFriendInfo(name))
);
}
console.log(myFriend);
}
function addEvent(friendName, date, memo) {
var friends = loadFriendsData();
var myFriend = getFriendByName(friends, friendName)[0];
if (!myFriend) {
return ask(
"New friend " + friendName + "! Add them to friendlog? [yN]",
(answer) => ifYesAddFriend(answer, friendName, () => addEvent(friendName, date, memo))
);
}
var events = loadEventsData();
var isoDate = parseDate(date).format(DATE_STORAGE_FORMAT);
events.push({"name": friendName, "date": isoDate, "memo": memo});
writeEventsData(events);
console.info("Added event '" + memo + "' on " + isoDate + " with " + friendName);
}
function showHelp() {
console.log("welcome to friendlog :-)");
console.log(" add [friend] [interval=10] " + "Adds [friend]. Events expected every [interval] days");
console.log(" list " + "Lists each friend and their next expected event");
console.log(" info [-n] " + "View info or just [-names] of each friend");
console.log(" info [friend] " + "View info about [friend]");
console.log(" edit [friend] [interval] " + "Edits [friend]'s expected [interval]");
console.log(" hangout [f] [d] [m] " + "Records event with [friend] on [date] with [memo]");
console.log(" history [-c] " + "See history, grouped by friend or [-chronological]");
console.log(" history [friend] " + "See history with specific friend");
console.log(" shortcuts " + "Print tab-completion and flh='fl hangout' shortcut");
}
function showHistory(friendName) {
var events = loadEventsData();
if (friendName) {
events = _.filter(events, e => e.name === friendName);
}
if (argv.c || argv.chronological) {
events = _.orderBy(events, ["date", "memo", "name"], ["desc", "asc", "asc"]);
} else {
events = _.orderBy(events, ["name", "date", "memo"], ["asc", "desc", "asc"]);
}
return console.log(columnify(events));
}
function printShortcuts() {
console.log("## start friendlog shortcuts");
console.log("alias flh=\"friendlog hangout\"");
console.log("source fl-completion-setup");
console.log("## end friendlog shortcuts");
}
// Default behavior
var callDefault = listFriends;
// figure out which command you're running
function main() {
var args = argv._;
if (0 === args.length) {
callDefault();
} else if (2 === args.length && "add" === args[0]) {
addFriend(args[1]);
} else if (3 === args.length && "add" === args[0]) {
addFriend(args[1], args[2]);
} else if (1 === args.length && "list" === args[0]) {
listFriends();
} else if (1 === args.length && "info" === args[0]) {
getFriendInfo();
} else if (2 === args.length && "info" === args[0]) {
getFriendInfo(args[1]);
} else if (3 === args.length && "edit" === args[0]) {
editFriend(args[1], args[2]);
} else if (1 === args.length && "history" === args[0]) {
showHistory();
} else if (2 === args.length && "history" === args[0]) {
showHistory(args[1]);
} else if (4 === args.length && "hangout" === args[0]) {
addEvent(args[1], args[2], args[3]);
} else if (1 === args.length && "shortcuts" === args[0]) {
printShortcuts();
} else {
showHelp();
}
}
main();