-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·137 lines (121 loc) · 5.09 KB
/
cli.js
File metadata and controls
executable file
·137 lines (121 loc) · 5.09 KB
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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const chalk = require('chalk');
// const godo = require('./indexx');
const fs = require('fs');
const fileName = './godolist.json'
const file = require(fileName);
const cli = meow(`
Usage
$ ${chalk.magenta('godo')} ${chalk.yellow('add')} "item" ${chalk.gray('# add a godo')}
$ ${chalk.magenta('godo')} ${chalk.yellow('add')} "item" group group2 ${chalk.gray('# add a godo to one or more groups')}
$ ${chalk.magenta('godo')} ${chalk.gray('# list all godos')}
$ ${chalk.magenta('godo')} ${chalk.yellow('group')} ${chalk.gray('# list all godos in a group')}
$ ${chalk.magenta('godo')} ${chalk.yellow('done')} 0 ${chalk.gray('# mark godo at index as done')}
$ ${chalk.magenta('godo')} ${chalk.yellow('done')} group ${chalk.gray('# mark all godos in a group as done')}
$ ${chalk.magenta('godo')} ${chalk.yellow('undone')} 0 ${chalk.gray('# undo mark godo at index as done')}
$ ${chalk.magenta('godo')} ${chalk.yellow('undone')} group ${chalk.gray('# undo mark godos in a group as done')}
$ ${chalk.magenta('godo')} ${chalk.yellow('clear')} ${chalk.gray('# delete all godos')}
$ ${chalk.magenta('godo')} ${chalk.yellow('delete')} 0 ${chalk.gray('# delete godo at index')}
$ ${chalk.magenta('godo')} ${chalk.yellow('delete')} group ${chalk.gray('# delete all godos in a group')}
$ ${chalk.magenta('godo')} ${chalk.yellow('clean')} ${chalk.gray('# delete all godos marked as done')}
$ ${chalk.magenta('godo')} ${chalk.yellow('clean')} group ${chalk.gray('# delete all godos marked as done in a group')}
`);
var command = cli['input'][0];
var data = cli['input'].slice(1);
if (!command && data.length == 0) {
for (var i=0; i < file['godos'].length; i++) {
var item = file['godos'][i];
var groups = '';
if (item['groups']) {
for (var j=0; j < item['groups'].length; j++) {
groups += ' #' + item['groups'][j];
}
}
var output = chalk.magenta(item['content']) + ' ' + chalk.cyan(groups)
if (item['done']) {
console.log(chalk.gray(chalk.stripColor(output)));
} else {
console.log(output);
}
// console.log(chalk.gray(item['content'] + ' ' + groups));
}
} else if (command == 'add') {
var content = data[0] || '';
var groups = data.slice(1) || '';
var numGodos = file['godos'].length;
var id = 0;
if (numGodos) {
id = file['godos'][numGodos - 1]['id'] + 1;
}
var item = {id: id, content: data[0], groups: groups, done: false};
file['godos'].push(item);
var fileString = JSON.stringify(file, null, 4);
fs.writeFile(fileName, fileString, function(err) {
if (err) {
console.log('unable to add godo 😮');
} else {
console.log('added godo ' + chalk.magenta(data[0]) + ' ✨');
}
});
} else if (command == 'done' || command =='undone') {
var done = command == 'done';
if (Number.isInteger(data[0])) {
file['godos'][data[0]]['done'] = done;
} else {
for (var i=0; i < file['godos'].length; i++) {
var item = file['godos'][i];
for (var j=0; j < data.length; j++ ) {
var group = data[j];
if (item['groups'].includes(group)) {
item['done'] = done;
}
}
}
}
var fileString = JSON.stringify(file, null, 4);
fs.writeFile(fileName, fileString, function(err) {
if (err) {
console.log('unable to mark godo(s) as' + command + '😮');
} else {
console.log('godo(s) marked as ' + command + '! 🎉');
}
});
} else if (command == 'delete') {
if (Number.isInteger(data[0])) {
var index = file['godos'].findIndex(function(item) {
return item['id'] == data[0];
});
file['godos'].splice(index, 1);
} else {
for (var i=0; i < file['godos'].length; i++) {
var item = file['godos'][i];
for (var j=0; j < data.length; j++ ) {
var group = data[j];
if (item['groups'].includes(group)) {
file['godos'].splice(i, 1);
i--;
}
}
}
}
var fileString = JSON.stringify(file, null, 4);
fs.writeFile(fileName, fileString, function(err) {
if (err) {
console.log('unable to remove godo(s) 😮');
} else {
console.log('godo(s) removed 🔥');
}
});
} else if (command == 'clear') {
file['godos'] = [];
var fileString = JSON.stringify(file, null, 4);
fs.writeFile(fileName, fileString, function(err) {
if (err) {
console.log('unable to clear godo(s) 😮');
} else {
console.log('all godos have been removed! 👏🏽');
}
});
}