-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (52 loc) · 1.56 KB
/
app.js
File metadata and controls
62 lines (52 loc) · 1.56 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
const chalk = require('chalk');
const yargs = require('yargs');
const fs = require('fs');
const notes = require('./notes.js');
//Customize yargs version
yargs.version(chalk.blue('Notes version: 1.1.0'))
//Command handlers
const addHandler = (argv) => notes.addNote(argv.title, argv.body);
const removeNote = (argv) => notes.removeNote(argv.title);
const listNotes = () => notes.listNotes();
const readNote = (argv) => notes.readNote(argv.title);
//Command builders
const noBuilder = {}
//add
const addBuilder = {
title: {
describe: 'Note title',
demandOption: true,
type: 'string'
},
body: {
describe: 'Note body',
demandOption: true,
type: 'string'
}
};
//remove
const removeBuilder = {
title: {
describe: 'Note title',
demandOption: true,
type: 'string'
}
};
//read
const readBuilder = {
title: {
describe: 'Note title',
demandOption: true,
type: 'string'
}
};
//Commands list
const commands = [
{ action: 'add', description: 'Add a new note', builder: addBuilder, handler: addHandler },
{ action: 'remove', description: 'Remove a note', builder: removeBuilder, handler: removeNote },
{ action: 'list', description: 'List all notes', builder: noBuilder, handler: listNotes },
{ action: 'read', description: 'Read a note', builder: readBuilder, handler: readNote }
];
//Generate all commands from the list
commands.forEach(command => yargs.command(command.action, chalk.blue(command.description), command.builder, command.handler))
yargs.parse()