]>
git.r.bdr.sh - rbdr/blog/blob - bin/blog.js
4 const Config
= require('../config/config');
5 const Blog
= require('..');
6 const Minimist
= require('minimist');
9 blog: new Blog(Config
),
10 expectedKeys: ['add', 'update', 'publish'],
12 // Application entry point. Reads arguments and calls the
13 // corresponding method from the blog lib
18 const parsedArguments
= this._parseArguments();
20 for (const argument
in parsedArguments
) {
21 if (parsedArguments
.hasOwnProperty(argument
)) {
23 const value
= parsedArguments
[argument
];
25 if (argument
=== 'add') {
26 await internals
.blog
.add(value
);
30 if (argument
=== 'update') {
31 await internals
.blog
.update(value
);
35 if (argument
=== 'publish') {
36 await internals
.blog
.publish(value
);
41 console
.log('Not yet implemented');
44 console
.error(err
.message
|| err
);
50 // Parses arguments and returns them if valid. otherwise Throws
54 const parsedArguments
= Minimist(process
.argv
.slice(2));
56 if (!this._areArgumentsValid(parsedArguments
)) {
57 throw new Error(internals
.strings
.invalidArguments
);
60 return parsedArguments
;
63 // Checks if the arguments are valid, returns a boolean value.
65 _areArgumentsValid(parsedArguments
) {
67 const argumentKeys
= Object
.keys(parsedArguments
);
69 return argumentKeys
.some((key
) => internals
.expectedKeys
.indexOf(key
) >= 0);
72 // Prints the usage to stderr
76 console
.error('\nUsage:\n');
77 console
.error('blog --add path/to/blog_post\t\t(creates new blog post)');
78 console
.error('blog --update path/to/blog_post\t(updates latest blog post)');
79 console
.error('blog --publish \t\t\t(publishes the blog)');
83 // Add the strings, added after declaration so they can consume the
87 invalidArguments: `Invalid Arguments, expecting one of: ${internals.expectedKeys.join(', ')}`