-
Notifications
You must be signed in to change notification settings - Fork 1
/
tp.js
executable file
·200 lines (170 loc) · 5.29 KB
/
tp.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
#!/usr/bin/env node
var path = require('path')
, util = require('util')
var pouch = require('pouchdb')
, naturaltime = require('naturaltime')
, timepouch = require('./timepouch')
, formats = require('./formats')
var options = require('optimist')
.alias('d', 'display')
.alias('s', 'sheet')
.alias('r', 'remove')
.alias('l', 'list')
.alias('e', 'edit')
.alias('i', 'in')
.alias('o', 'out')
.alias('a', 'at')
.alias('v', 'verbose')
.alias('h', 'help')
.boolean(['i', 'o'])
options.describe('d', 'Display checkins for the current or specified sheet');
options.describe('s', 'Select or create a sheet');
options.describe('r', 'Delete the current or specified sheet (add --entries to remove sheet and its entries)');
options.describe('l', 'Display timesheets');
options.describe('e', 'Edit an entry (specify an id with --id)');
options.describe('i', 'Check in to the current timesheet');
options.describe('o', 'Check out of the current timesheet');
options.describe('v', 'Verbose display including ids');
options.describe('format', 'Display format: text, csv, or tsv (default: text)');
options.describe('sync [url]', 'Sync changes with the CouchDB server at url');
var argv = options.argv;
var dir = path.join(process.env.HOME, '.timepouch');
var out = process.stdout;
timepouch(dir, run);
function run(err, tpouch) {
if (argv.query) {
tpouch.query(argv, console.log);
}
// check in to current sheet
else if (argv.in || argv.edit) {
if (argv.edit && !argv.id) {
return console.log('> specify the id of the entry to edit (use --verbose when displaying to see ids)');
}
// start date: at, or start, defaults to now or null if --edit was specified
var start = argv.at
? getDate(argv.at)
: argv.start
? getDate(argv.start)
: argv.edit ? null : new Date();
// check in and out in one fell swoop
var end = argv.end ? getDate(argv.end) : null;
tpouch.in({
start: start,
end: end,
note: argv._.join(' '),
sheet: argv.sheet || null,
id: argv.id || null
}, inout);
}
// check out of current sheet/task
else if (argv.out) {
// end: specify with --at or --end. defaults to now
var end = argv.at
? getDate(argv.at)
: argv.end
? getDate(argv.end)
: new Date();
tpouch.out({
end: end,
id: argv.id || null,
sheet: argv.sheet || null
}, inout);
}
// display current sheet
else if (argv.display) {
tpouch.sheets(function(err, sheets, cur, active) {
if (err) return console.log(err);
var options = argv;
if (options.display !== true) {
options.sheet = options.display;
}
options.sheet = options.sheet || cur;
if (!options.sheet) {
return console.log("> no sheet specified or selected");
}
tpouch.query(options, function(err, results) {
var formatter = formats.text;
if (options.format) {
formatter = formats[options.format];
}
if (!formatter) {
return console.error('> Unknown formatter "%s"', options.format);
}
options.out = out;
formatter(options, results.rows);
});
});
}
// create/select a sheet
else if (argv.sheet) {
if (argv.sheet === true) {
return console.log("> specify a sheet to create/select");
}
tpouch.sheet(argv.sheet, function(err, changed) {
if (err) console.error(err);
else if (changed)
console.log("> selected sheet '%s'", argv.sheet);
else
console.log("> sheet '%s' already selected", argv.sheet);
});
}
// delete a sheet
else if (argv.remove) {
tpouch.rmsheet(argv.remove, argv.entries !== undefined, function(err, response) {
if (err) return console.error('> error:', err.reason);
if (response.ok) {
console.log("> deleted sheet '%s'%s", response.sheet, argv.entries ? ' and entries' : '');
}
else {
console.log("> failed to delete '%s'%s", argv.remove, argv.entries ? ' and entries' : '');
}
});
}
// list sheets
else if (argv.list) {
tpouch.sheets(function(err, sheets, cur, active) {
if (err) return log(err);
sheets.forEach(function(sheet) {
if (sheet == cur) {
sheet = '*' + sheet;
}
out.write(" > " + sheet + "\n");
});
if (sheets.length == 0)
console.log('> no sheets found');
});
}
else if (argv.sync) {
tpouch.sync(argv.sync, function(err, up, down) {
if (err) {
return console.error(err);
}
console.log('> synced %d to remote, %d from remote',
up.docs_written,
down.docs_written);
});
}
else {
options.showHelp();
process.exit();
}
}
/*
* inout: output messages for checkin/checkout
*/
function inout(err, results) {
if (err) console.error("Error:", err.reason);
else if (results.start && !results.end) {
console.log("> %s: starting task '%s' at %s", results.sheet, results.note || '(no note)', results.start);
}
else if (results.start && results.end) {
console.log("> %s: checked out of '%s' at %s", results.sheet, results.note || '(no note)', results.end);
}
}
function getDate(string) {
var d = new Date(string);
if (d == 'Invalid Date') {
d = naturaltime(string);
}
return d;
}