-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
437 lines (392 loc) · 12.9 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
let argv = require('yargs')
.usage('Usage: $0 [flag]')
.example('$0 --sink', 'configure a sink location to store the memos')
.example(
'$0 --write',
'write a memo by providing a title, description, source (an external (URL) reference), and code/segment if exists'
)
.example('$0 --zync', 'sync the memos to find the memos using the keyword')
.example('$0 --find <keyword>', 'find a memo using the keyword')
.example('$0 --update <keyword>', 'update/rewrite an existing memo')
.example('$0 --remove <keyword>', 'remove a memo from zinc')
.example('$0 --list', 'list all memos')
// sink memo
.option('s', {
alias: 'sink',
describe: 'configure a location to sink the memos',
type: 'boolean',
nargs: 0,
})
// write zinc memo
.option('w', {
alias: 'write',
describe: 'write a memo',
type: 'boolean',
nargs: 0,
})
// zi(sy)nc alias to sync the markdowns
.option('z', {
alias: 'zync',
describe: 'zi(sy)nc the memos',
type: 'boolean',
nargs: 0,
})
// search keyword
.option('f', {
alias: 'find',
describe: 'keyword to search through',
type: 'string',
nargs: 1,
})
// update memo
.option('u', {
alias: 'update',
describe: 'update/rewrite an existing memo',
type: 'string',
nargs: 1,
})
// remove memo
.option('r', {
alias: 'remove',
describe: 'remove a memo',
type: 'string',
nargs: 1,
})
// list memo
.option('l', {
alias: 'list',
describe: 'list all memos',
type: 'boolean',
nargs: 0,
})
// help
.help('help').argv;
const ora = require('ora');
const inquirer = require('inquirer');
const _ = require('lodash');
const { JSONPath } = require('jsonpath-plus');
const { parse } = require('./lib/parser');
const { searchJSONObject } = require('./lib/searcher');
const {
appendZincMemo,
configureZinc,
constructInquirerObject,
isZincConfigured,
populateMemoMD,
readMetaJSON,
removeZincMemo,
writeToTerminal,
writeListToTerminal,
} = require('./lib/util');
var sinkPath = false;
var parsedContent, results, parentKey;
if (argv.z) {
/**
* code block handling the `zync` flag
* * checks whether the zinc is configured with a sink location
* * reads the md content and parse to the meta JSON
*/
isSinkConfigured();
const spinner = ora('zi(sy)ncing markdown memos').start();
try {
parse(sinkPath).then(() => {
if (spinner.isSpinning) spinner.succeed('zinced');
});
} catch (err) {
if (spinner.isSpinning) spinner.fail();
}
}
if (argv.f) {
/**
* code block handling the `find` flag
* * checks whether the zinc is configured with a sink location
* * reads the meta JSON content
* * searches through the JSON with the defined keyword
* * search result is printed in the terminal as a structured output
*/
isSinkConfigured();
const spinner = ora("searching for '" + argv.f + "'").start();
parsedContent = readMetaJSON(sinkPath);
results = searchJSONObject(parsedContent, 'keywords', argv.f);
if (results['resultObjects'].length == 0) {
if (spinner.isSpinning) spinner.fail("no matching results found for '" + argv.f + "'");
process.exit(0);
}
if (spinner.isSpinning) spinner.succeed("search results for '" + argv.f + "'");
console.log(writeToTerminal(results['resultObjects']));
}
if (argv.w) {
/**
* code block handling the `write` flag
* * checks whether the zinc is configured with a sink location
* * prompts for input using the inquirer lib
* * once the inputs are recorded, the object is parsed to populate a markdown content
* * the content is added to the zinc.md
* * the meta JSON is updated
*/
isSinkConfigured();
const promptSchema = [
{
name: 'title',
message: 'Title of the memo',
},
{
name: 'desc',
message: 'Description of the memo',
},
{
name: 'source',
message: 'Source',
},
{
name: 'keys',
message: 'Keywords (comma, separated)',
validate: function (keywords) {
return !_.isEmpty(keywords);
},
},
{
name: 'isCodeAvailable',
message: 'Code input available',
type: 'confirm',
default: true,
},
{
name: 'lang',
message: 'Language',
when: function (answers) {
return answers.isCodeAvailable;
},
},
{
name: 'code',
type: 'editor',
message: 'Code segment',
when: function (answers) {
return answers.isCodeAvailable;
},
},
];
inquirer.prompt(promptSchema).then((answers) => {
appendZincMemo(populateMemoMD(answers), sinkPath);
parse(sinkPath);
});
}
if (argv.s) {
/**
* code block handling the `sink` flag
* * prompts the user to select a sink location with choices
* * once the input is recorded, the object is passed to configure the Zinc
* * a .zincrc is created and the configurations are recorded
*/
const sinkChoices = ['Here', 'Provide a custom location', 'Use default location'];
const promptSchema = [
{
name: 'sink',
message: 'Where do you want to sink the memos?',
type: 'list',
choices: sinkChoices,
},
{
name: 'location',
message: 'Write me the location (absolute path)',
when: function (answers) {
return answers.sink === sinkChoices[1];
},
},
];
inquirer.prompt(promptSchema).then((answers) => {
if (answers.sink === sinkChoices[0]) answers.location = process.cwd();
if (answers.sink === sinkChoices[2]) answers.location = __dirname;
answers.dirname = __dirname;
configureZinc(answers);
});
}
if (argv.r) {
/**
* code block handling the `remove` flag
* * checks whether the zinc is configured with a sink location
* * reads the meta JSON content
* * searches through the JSON with the defined keyword
* * search result is printed in the terminal as a structured output
* * along with the output, question is asked to whether remove or not
* * once the input is recorded by the inquirer lib removes the content from the zinc.md
* * updates the meta JSON
*/
isSinkConfigured();
const spinner = ora("searching for '" + argv.r + "'").start();
parsedContent = readMetaJSON(sinkPath);
results = searchJSONObject(parsedContent, 'keywords', argv.r);
if (results['resultObjects'].length == 0) {
if (spinner.isSpinning) spinner.fail("no matching results found for '" + argv.r + "'");
process.exit(0);
}
if (spinner.isSpinning) spinner.succeed("search results for '" + argv.r + "'");
parentKey = results['resultKeys'][0];
console.log(writeToTerminal(results['resultObjects']));
const promptSchema = [
{
name: 'remove',
message: 'Remove this memo',
type: 'confirm',
default: true,
},
];
inquirer.prompt(promptSchema).then((answers) => {
if (answers.remove) {
removeZincMemo(parentKey, sinkPath);
parse(sinkPath);
ora().succeed('removed the memo from zinc successfully!');
}
});
}
if (argv.u) {
/**
* code block handling the `update` flag
* * checks whether the zinc is configured with a sink location
* * reads the meta JSON content
* * searches through the JSON with the defined keyword
* * search result is printed in the terminal as a structured output
* * along with the output, question is asked to whether update or not
* * if yes, the inquirer prompts the set of questions of with the previously set values
* * once the input is recorded by the inquirer lib, the final object is constructed
* * the existing content is removed from the zinc.md
* * the updated content is written to the zinc.md
* * the meta JSON is updated
*/
isSinkConfigured();
const spinner = ora("searching for '" + argv.u + "'").start();
parsedContent = readMetaJSON(sinkPath);
results = searchJSONObject(parsedContent, 'keywords', argv.u);
if (results['resultObjects'].length == 0) {
if (spinner.isSpinning) spinner.fail("no matching results found for '" + argv.u + "'");
process.exit(0);
}
if (spinner.isSpinning) spinner.succeed("search results for '" + argv.u + "'");
parentKey = results['resultKeys'][0];
console.log(writeToTerminal(results['resultObjects']));
var zincObject = constructInquirerObject(results['resultObjects']);
const promptSchema = [
{
name: 'update',
message: 'Do you want to update/rewrite this memo?',
type: 'confirm',
default: true,
},
{
name: 'title',
message: 'Title of the memo',
default: zincObject['title'],
when: function (answers) {
return answers.update;
},
},
{
name: 'desc',
message: 'Description of the memo',
default: zincObject['desc'],
when: function (answers) {
return answers.update;
},
},
{
name: 'source',
message: 'Source',
default: zincObject['source'],
when: function (answers) {
return answers.update;
},
},
{
name: 'keys',
message: 'Keywords (comma, separated)',
default: zincObject['keys'],
when: function (answers) {
return answers.update;
},
validate: function (keywords) {
return !_.isEmpty(keywords);
},
},
{
name: 'isCodeAvailable',
message: 'Do you want to input a code?',
type: 'confirm',
default: true,
when: function (answers) {
return answers.update && _.isEmpty(zincObject.code);
},
},
{
name: 'updateExistingCode',
message: 'Do you want to update the existing code?',
type: 'confirm',
default: true,
when: function (answers) {
return answers.update && !_.isEmpty(zincObject.code);
},
},
{
name: 'lang',
message: 'Language',
default: zincObject['lang'],
when: function (answers) {
return (answers.isCodeAvailable || answers.updateExistingCode) && answers.update;
},
},
{
name: 'code',
type: 'editor',
message: 'Code segment',
default: zincObject['code'],
when: function (answers) {
return (answers.isCodeAvailable || answers.updateExistingCode) && answers.update;
},
},
];
inquirer.prompt(promptSchema).then((answers) => {
if (!answers.update) {
ora().succeed('no updates were done to the memo');
process.exit(0);
}
// if not updating the existing code segment then, passing
// the existing code element from the zincObject
if (!answers.updateExistingCode) {
answers.isCodeAvailable = true;
answers.lang = zincObject.lang;
answers.code = zincObject.code;
}
removeZincMemo(parentKey, sinkPath);
appendZincMemo(populateMemoMD(answers), sinkPath);
parse(sinkPath);
ora().succeed(`updated the memo successfully!`);
process.exit(0);
});
}
if (argv.l) {
/**
* code block handling the `list` flag
* * checks whether the zinc is configured with a sink location
* * reads the meta JSON content
* * prints the list in the terminal as a structured output
*/
isSinkConfigured();
const spinner = ora('preparing the list').start();
parsedContent = readMetaJSON(sinkPath);
results = JSONPath({ path: '$..[?(@.title)]', json: parsedContent });
if (results.length == 0) {
if (spinner.isSpinning) spinner.fail('no memos found');
process.exit(0);
}
if (spinner.isSpinning) spinner.succeed('list prepared');
console.log(writeListToTerminal(results));
}
//#region helpers
function isSinkConfigured() {
sinkPath = isZincConfigured(__dirname);
if (!sinkPath) {
ora().fail('zinc is not configured properly. please execute `zinc -s` to configure');
process.exit(0);
}
}
//#endregion